-
Notifications
You must be signed in to change notification settings - Fork 266
/
common.ts
234 lines (207 loc) · 9.61 KB
/
common.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
* Copyright (C) 2018-2023 Garden Technologies, Inc. <info@garden.io>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
import {
getUtilManifests,
skopeoManifestUnknown,
} from "../../../../../../../src/plugins/kubernetes/container/build/common"
import { expect } from "chai"
import { defaultResources, KubernetesProvider } from "../../../../../../../src/plugins/kubernetes/config"
import { DeepPartial } from "../../../../../../../src/util/util"
import { k8sUtilImageName } from "../../../../../../../src/plugins/kubernetes/constants"
describe("common build", () => {
describe("manifest error", () => {
it("should result in manifest unknown for common registry error", () => {
const errorMessage = "ERROR: manifest unknown: manifest unknown"
expect(skopeoManifestUnknown(errorMessage)).to.be.true
})
it("should result in manifest unknown for Harbor registry error", () => {
const errorMessage =
'Unable to query registry for image status: time="2021-10-13T17:50:25Z" level=fatal msg="Error parsing image name "docker://registry.domain/namespace/image-name:v-1f160eadbb": Error reading manifest v-1f160eadbb in registry.domain/namespace/image-name: unknown: artifact namespace/image-name:v-1f160eadbb not found"'
expect(skopeoManifestUnknown(errorMessage)).to.be.true
})
it("should result in manifest unknown for Harbor registry repository not found", () => {
const errorMessage =
'Unable to query registry for image status: time="2021-10-13T17:50:25Z" level=fatal msg="Error parsing image name "docker://registry.domain/namespace/image-name:v-1f160eadbb": Error reading manifest v-1f160eadbb in registry.domain/namespace/image-name: unknown: repository namespace/image-name not found"'
expect(skopeoManifestUnknown(errorMessage)).to.be.true
})
it("should result in manifest not unknown for other errors", () => {
const errorMessage =
"unauthorized: unauthorized to access repository: namespace/image-name, action: push: unauthorized to access repository: namespace/image-name, action: push"
expect(skopeoManifestUnknown(errorMessage)).to.be.false
})
})
describe("getUtilManifests", () => {
const _provider: DeepPartial<KubernetesProvider> = {
config: {
resources: {
util: defaultResources.util,
},
},
}
let provider = _provider as KubernetesProvider
beforeEach(() => {
provider = _provider as KubernetesProvider
})
it("should return the manifest", () => {
const result = getUtilManifests(provider, "test", [])
expect(result).eql({
deployment: {
apiVersion: "apps/v1",
kind: "Deployment",
metadata: { labels: { app: "garden-util" }, name: "garden-util", annotations: undefined },
spec: {
replicas: 1,
selector: { matchLabels: { app: "garden-util" } },
strategy: {
type: "Recreate",
},
template: {
metadata: { labels: { app: "garden-util" }, annotations: undefined },
spec: {
containers: [
{
name: "util",
image: k8sUtilImageName,
imagePullPolicy: "IfNotPresent",
command: ["/rsync-server.sh"],
env: [
{ name: "ALLOW", value: "0.0.0.0/0" },
{ name: "RSYNC_PORT", value: "8730" },
],
volumeMounts: [
{ name: "test", mountPath: "/home/user/.docker", readOnly: true },
{ name: "garden-sync", mountPath: "/data" },
],
ports: [{ name: "garden-rsync", protocol: "TCP", containerPort: 8730 }],
readinessProbe: {
initialDelaySeconds: 1,
periodSeconds: 1,
timeoutSeconds: 3,
successThreshold: 2,
failureThreshold: 5,
tcpSocket: { port: "garden-rsync" },
},
lifecycle: {
preStop: {
exec: {
command: [
"/bin/sh",
"-c",
"until test $(pgrep -f '^[^ ]+rsync' | wc -l) = 1; do echo waiting for rsync to finish...; sleep 1; done",
],
},
},
},
resources: { limits: { cpu: "256m", memory: "512Mi" }, requests: { cpu: "256m", memory: "512Mi" } },
securityContext: { runAsUser: 1000, runAsGroup: 1000 },
},
],
imagePullSecrets: [],
volumes: [
{
name: "test",
secret: { secretName: "test", items: [{ key: ".dockerconfigjson", path: "config.json" }] },
},
{ name: "garden-sync", emptyDir: {} },
],
tolerations: [{ key: "garden-build", operator: "Equal", value: "true", effect: "NoSchedule" }],
},
},
},
},
service: {
apiVersion: "v1",
kind: "Service",
metadata: { name: "garden-util" },
spec: {
ports: [{ name: "rsync", protocol: "TCP", port: 8730, targetPort: 8730 }],
selector: { app: "garden-util" },
type: "ClusterIP",
},
},
})
})
it("should return the manifest with kaniko config tolerations if util tolerations are not specified", () => {
const toleration = { key: "custom-kaniko-toleration", operator: "Equal", value: "true", effect: "NoSchedule" }
provider.config.kaniko = {
tolerations: [toleration],
}
const result = getUtilManifests(provider, "test", [])
const tolerations = result.deployment.spec.template.spec?.tolerations
expect(tolerations?.find((t) => t.key === toleration.key)).to.eql(toleration)
})
it("should return the manifest with util config tolerations if util tolerations are specified", () => {
const tolerationUtil = { key: "util-toleration", operator: "Equal", value: "true", effect: "NoSchedule" }
provider.config.kaniko = {
util: {
tolerations: [tolerationUtil],
},
}
const result = getUtilManifests(provider, "test", [])
const tolerations = result.deployment.spec.template.spec?.tolerations
expect(tolerations?.find((t) => t.key === tolerationUtil.key)).to.eql(tolerationUtil)
})
it("should return the manifest with util tolerations only if kaniko has separate tolerations configured", () => {
const tolerationKaniko = { key: "kaniko-toleration", operator: "Equal", value: "true", effect: "NoSchedule" }
const tolerationUtil = { key: "util-toleration", operator: "Equal", value: "true", effect: "NoSchedule" }
provider.config.kaniko = {
tolerations: [tolerationKaniko],
}
provider.config.kaniko.util = {
tolerations: [tolerationUtil],
}
const result = getUtilManifests(provider, "test", [])
const tolerations = result.deployment.spec.template.spec?.tolerations
expect(tolerations?.findIndex((t) => t.key === tolerationKaniko.key)).to.eql(-1)
})
it("should return the manifest with kaniko annotations when util annotations are missing", () => {
provider.config.kaniko = {
annotations: {
testAnnotation: "its-there",
},
}
const result = getUtilManifests(provider, "test", [])
const deploymentAnnotations = result.deployment.metadata?.annotations
expect(deploymentAnnotations).to.eql(provider.config.kaniko.annotations)
const podAnnotations = result.deployment.spec.template.metadata?.annotations
expect(podAnnotations).to.eql(provider.config.kaniko.annotations)
})
it("should return the manifest with util annotations when util annotations are specified", () => {
provider.config.kaniko = {
util: {
annotations: {
testAnnotation: "its-there",
},
},
}
const result = getUtilManifests(provider, "test", [])
const deploymentAnnotations = result.deployment.metadata?.annotations
expect(deploymentAnnotations).to.eql(provider.config.kaniko.util?.annotations)
const podAnnotations = result.deployment.spec.template.metadata?.annotations
expect(podAnnotations).to.eql(provider.config.kaniko.util?.annotations)
})
it("should return the manifest with kaniko nodeSelector when util nodeSelector is missing", () => {
provider.config.kaniko = {
nodeSelector: { "kubernetes.io/os": "linux" },
}
const result = getUtilManifests(provider, "test", [])
const podNodeSelector = result.deployment.spec.template.spec?.nodeSelector
expect(podNodeSelector).to.eql(provider.config.kaniko.nodeSelector)
})
it("should return the manifest with util nodeSelector when util nodeSelector is specified", () => {
provider.config.kaniko = {
util: {
nodeSelector: { "kubernetes.io/os": "linux" },
},
}
const result = getUtilManifests(provider, "test", [])
const podNodeSelector = result.deployment.spec.template.spec?.nodeSelector
expect(podNodeSelector).to.eql(provider.config.kaniko.util?.nodeSelector)
})
})
})