hammerkit 2.0.0-next.10 → 2.0.0-next.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getItemFilesDir = exports.isLiveService = exports.LIVE_FILES_DIR = exports.getStateFilesDir = exports.STATE_FILES_PREFIX = exports.getSandboxAffinity = exports.SANDBOX_LABEL = exports.getClaimMountLabel = exports.getClaimPodSpec = exports.getWorkspacePodSpec = exports.appendVolume = exports.getKubernetesPersistence = exports.getVolumeName = void 0;
3
+ exports.getItemFilesDir = exports.isLiveService = exports.LIVE_FILES_DIR = exports.getStateFilesDir = exports.STATE_FILES_PREFIX = exports.getSandboxAffinity = exports.SANDBOX_LABEL = exports.getClaimMountLabel = exports.getClaimPodSpec = exports.getWorkspacePodSpec = exports.appendVolume = exports.getMountedSources = exports.getKubernetesPersistence = exports.getVolumeName = void 0;
4
4
  const path_1 = require("path");
5
5
  const fs_1 = require("fs");
6
6
  const crypto_1 = require("crypto");
@@ -63,20 +63,21 @@ stateKey) {
63
63
  };
64
64
  const workspace = kubernetes.server?.workspace;
65
65
  for (const mount of work.data.mounts) {
66
+ const isFile = isLocalFile(mount.localPath, mount.isFile);
66
67
  if (workspace &&
67
- appendWorkspaceMount(workspace, persistence, mount.containerPath, mount.localPath, mount.isFile, mount.readOnly)) {
68
+ appendWorkspaceMount(workspace, persistence, mount.containerPath, mount.localPath, isFile, mount.readOnly)) {
68
69
  continue;
69
70
  }
70
- appendVolume(work, persistence, mount.containerPath, mount.localPath, mount.isFile, true, kubernetes, mounted, own);
71
+ appendVolume(work, persistence, mount.containerPath, mount.localPath, isFile, true, kubernetes, mounted, own);
71
72
  }
72
73
  // its deps' src too, uploaded into its own claim: a container task sees them as it does on
73
74
  // docker (a tsc -b of a project that references another reads the other's tsconfig)
74
- for (const src of work.data.src) {
75
- if (workspace &&
76
- appendWorkspaceMount(workspace, persistence, src.absolutePath, src.absolutePath, src.isFile, false)) {
75
+ for (const src of getMountedSources(work.data.src)) {
76
+ const isFile = isLocalFile(src.absolutePath, src.isFile);
77
+ if (workspace && appendWorkspaceMount(workspace, persistence, src.absolutePath, src.absolutePath, isFile, false)) {
77
78
  continue;
78
79
  }
79
- appendVolume(work, persistence, src.absolutePath, src.absolutePath, src.isFile, true, kubernetes, mounted, own, src.inherited ? (0, claim_layout_1.getInheritedSourceSubPath)(work, src.absolutePath) : undefined); // TODO matcher
80
+ appendVolume(work, persistence, src.absolutePath, src.absolutePath, isFile, true, kubernetes, mounted, own, src.inherited ? (0, claim_layout_1.getInheritedSourceSubPath)(work, src.absolutePath) : undefined); // TODO matcher
80
81
  }
81
82
  if (work.data.type === 'container-task') {
82
83
  for (const generate of work.data.generates) {
@@ -97,6 +98,39 @@ stateKey) {
97
98
  return persistence;
98
99
  }
99
100
  exports.getKubernetesPersistence = getKubernetesPersistence;
101
+ // Whether the path `localPath` is a file: what it is on disk, as docker binds it, and else
102
+ // `guess`, which goes by its name (a `Dockerfile` or `LICENSE` has no extension, and a
103
+ // `conf.d` folder has one)
104
+ function isLocalFile(localPath, guess) {
105
+ try {
106
+ return !(0, fs_1.statSync)(localPath).isDirectory();
107
+ }
108
+ catch {
109
+ return guess;
110
+ }
111
+ }
112
+ // The srcs a pod mounts, each path once: a src inside a folder of another one (`docs/Dockerfile`
113
+ // with `docs`) is in the upload and the mount of that folder, as on docker, and is not mounted
114
+ // apart. A mount of its own inside the folder, which is mounted read only, needs its place
115
+ // there, and two mounts of the same path are refused by Kubernetes. A task's own src is kept
116
+ // before the same path of a dep, so it stays where getClaimSubPath puts it.
117
+ function getMountedSources(sources) {
118
+ const unique = new Map();
119
+ for (const src of sources) {
120
+ const other = unique.get(src.absolutePath);
121
+ if (!other || (other.inherited && !src.inherited)) {
122
+ unique.set(src.absolutePath, src);
123
+ }
124
+ }
125
+ const all = [...unique.values()];
126
+ const folders = all.filter((src) => !isLocalFile(src.absolutePath, src.isFile));
127
+ return all.filter((src) => !folders.some((folder) => folder !== src && isInside(folder.absolutePath, src.absolutePath)));
128
+ }
129
+ exports.getMountedSources = getMountedSources;
130
+ function isInside(folder, path) {
131
+ const rel = (0, path_1.relative)(folder, path);
132
+ return rel.length > 0 && rel !== '..' && !rel.startsWith(`..${path_1.sep}`) && !(0, path_1.isAbsolute)(rel);
133
+ }
100
134
  // The places in the claim `claim` of the paths mounted inside a folder that is mounted read
101
135
  // only from it: an output of a task inside a folder of its src (pkg/coverage/lcov.info with a
102
136
  // src pkg). The container runtime makes the place of a mount where it isn't there, which it
@@ -108,12 +142,13 @@ function getNestedMountPlaces(persistence, claim) {
108
142
  const places = [];
109
143
  for (const mount of own) {
110
144
  const parent = folders
111
- .filter((folder) => folder !== mount && mount.mount.mountPath.startsWith(`${folder.mount.mountPath}/`))
145
+ .filter((folder) => folder !== mount && isInside(folder.mount.mountPath, mount.mount.mountPath))
112
146
  .sort((a, b) => b.mount.mountPath.length - a.mount.mountPath.length)[0];
113
147
  if (!parent) {
114
148
  continue;
115
149
  }
116
- const place = `${parent.mount.subPath}/${mount.mount.mountPath.slice(parent.mount.mountPath.length + 1)}`;
150
+ const inside = (0, path_1.relative)(parent.mount.mountPath, mount.mount.mountPath).split(path_1.sep).join('/');
151
+ const place = `${parent.mount.subPath}/${inside}`;
117
152
  places.push(mount.cwd === mount.mount.mountPath ? `${place}/` : place);
118
153
  }
119
154
  return places;
@@ -154,7 +189,9 @@ claimPath = (0, claim_layout_1.getClaimSubPath)(sourceWork, containerPath)) {
154
189
  if (localPath) {
155
190
  persistence.sources.push({
156
191
  localPath: localPath,
157
- matcher: () => true, // TODO
192
+ // TODO: a matcher that uploads less than the whole folder has to keep the srcs
193
+ // inside it that getMountedSources leaves out (a `docs/Dockerfile` next to `docs/**/*.md`)
194
+ matcher: () => true,
158
195
  containerPath,
159
196
  subPath: (uploadDir ? `${uploadDir}/` : '') + claimPath,
160
197
  stateKey: '', // TODO
@@ -720,13 +720,19 @@ function getPrivilegedNamespaceProblem(namespace, labels, backend) {
720
720
  `${namespace} ${ENFORCE_LABEL}=baseline ${AUDIT_LABEL}- --overwrite`);
721
721
  }
722
722
  exports.getPrivilegedNamespaceProblem = getPrivilegedNamespaceProblem;
723
- // What may mount a claim a run kept: the runs of the same repository, as they share the
724
- // cache, and on a sandbox the runs of that sandbox only, whose claim is on the same node or
725
- // zone as the kept ones. The owner is the namespace's.
723
+ // What may mount a claim a run kept: the runs of the same branch or tag of a repository,
724
+ // which share its cache, so that the runs of two branches go side by side and the runs of one find what
725
+ // the last one left; on a sandbox the runs of that sandbox only, whose claim is on the same
726
+ // node or zone as the kept ones. The owner is the namespace's.
726
727
  function getVolumeScope(run) {
727
728
  if (run.source.type === 'sandbox' && !run.source.check) {
728
729
  return `sandbox-${(0, crypto_1.createHash)('sha256').update(run.source.id).digest('hex').slice(0, 24)}`;
729
730
  }
731
+ // a ref given as a commit hash names no branch: its runs keep the repository's claims
732
+ if (run.source.type === 'ref' && !/^[0-9a-f]{40}([0-9a-f]{24})?$/i.test(run.source.ref)) {
733
+ const key = `ref\0${run.source.repo}\0${run.source.ref}`;
734
+ return `ref-${(0, crypto_1.createHash)('sha256').update(key).digest('hex').slice(0, 24)}`;
735
+ }
730
736
  return (0, executor_1.cacheNamespace)(run);
731
737
  }
732
738
  exports.getVolumeScope = getVolumeScope;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "hammerkit",
3
3
  "description": "Containerized build tool with incremental caching that runs the same build file locally, in Docker and on Kubernetes.",
4
- "version": "2.0.0-next.10",
4
+ "version": "2.0.0-next.12",
5
5
  "keywords": [
6
6
  "build",
7
7
  "build-tool",