-
Notifications
You must be signed in to change notification settings - Fork 176
/
Copy pathinjector.go
405 lines (374 loc) · 12.7 KB
/
injector.go
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2021-Present The Zarf Authors
// Package cluster contains Zarf-specific cluster management functions.
package cluster
import (
"context"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"time"
"github.com/google/go-containerregistry/pkg/crane"
"github.com/mholt/archiver/v3"
corev1 "k8s.io/api/core/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/apimachinery/pkg/util/wait"
"github.com/defenseunicorns/pkg/helpers/v2"
"github.com/zarf-dev/zarf/src/api/v1alpha1"
"github.com/zarf-dev/zarf/src/config"
"github.com/zarf-dev/zarf/src/internal/healthchecks"
"github.com/zarf-dev/zarf/src/pkg/logger"
"github.com/zarf-dev/zarf/src/pkg/message"
"github.com/zarf-dev/zarf/src/pkg/transform"
"github.com/zarf-dev/zarf/src/pkg/utils"
v1ac "k8s.io/client-go/applyconfigurations/core/v1"
)
// StartInjection initializes a Zarf injection into the cluster.
func (c *Cluster) StartInjection(ctx context.Context, tmpDir, imagesDir string, injectorSeedSrcs []string) error {
l := logger.From(ctx)
start := time.Now()
// Stop any previous running injection before starting.
err := c.StopInjection(ctx)
if err != nil {
return err
}
spinner := message.NewProgressSpinner("Attempting to bootstrap the seed image into the cluster")
defer spinner.Stop()
l.Info("creating Zarf injector resources")
resReq := v1ac.ResourceRequirements().
WithRequests(corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse(".5"),
corev1.ResourceMemory: resource.MustParse("64Mi"),
}).
WithLimits(corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("1"),
corev1.ResourceMemory: resource.MustParse("256Mi"),
})
injectorImage, injectorNodeName, err := c.getInjectorImageAndNode(ctx, resReq)
if err != nil {
return err
}
payloadCmNames, shasum, err := c.createPayloadConfigMaps(ctx, spinner, tmpDir, imagesDir, injectorSeedSrcs)
if err != nil {
return fmt.Errorf("unable to generate the injector payload configmaps: %w", err)
}
b, err := os.ReadFile(filepath.Join(tmpDir, "zarf-injector"))
if err != nil {
return err
}
cm := v1ac.ConfigMap("rust-binary", ZarfNamespaceName).
WithBinaryData(map[string][]byte{
"zarf-injector": b,
})
_, err = c.Clientset.CoreV1().ConfigMaps(*cm.Namespace).Apply(ctx, cm, metav1.ApplyOptions{Force: true, FieldManager: FieldManagerName})
if err != nil {
return err
}
svcAc := v1ac.Service("zarf-injector", ZarfNamespaceName).
WithSpec(v1ac.ServiceSpec().
WithType(corev1.ServiceTypeNodePort).
WithPorts(
v1ac.ServicePort().WithPort(int32(5000)),
).WithSelector(map[string]string{
"app": "zarf-injector",
}))
svc, err := c.Clientset.CoreV1().Services(*svcAc.Namespace).Apply(ctx, svcAc, metav1.ApplyOptions{Force: true, FieldManager: FieldManagerName})
if err != nil {
return err
}
// TODO: Remove use of passing data through global variables.
config.ZarfSeedPort = fmt.Sprintf("%d", svc.Spec.Ports[0].NodePort)
pod := buildInjectionPod(injectorNodeName, injectorImage, payloadCmNames, shasum, resReq)
_, err = c.Clientset.CoreV1().Pods(*pod.Namespace).Apply(ctx, pod, metav1.ApplyOptions{Force: true, FieldManager: FieldManagerName})
if err != nil {
return fmt.Errorf("error creating pod in cluster: %w", err)
}
waitCtx, waitCancel := context.WithTimeout(ctx, 60*time.Second)
defer waitCancel()
podRef := v1alpha1.NamespacedObjectKindReference{
APIVersion: *pod.APIVersion,
Kind: *pod.Kind,
Namespace: *pod.Namespace,
Name: *pod.Name,
}
err = healthchecks.Run(waitCtx, c.Watcher, []v1alpha1.NamespacedObjectKindReference{podRef})
if err != nil {
return err
}
spinner.Success()
l.Debug("done with injection", "duration", time.Since(start))
return nil
}
// StopInjection handles cleanup once the seed registry is up.
func (c *Cluster) StopInjection(ctx context.Context) error {
start := time.Now()
l := logger.From(ctx)
l.Debug("deleting injector resources")
err := c.Clientset.CoreV1().Pods(ZarfNamespaceName).Delete(ctx, "injector", metav1.DeleteOptions{})
if err != nil && !kerrors.IsNotFound(err) {
return err
}
err = c.Clientset.CoreV1().Services(ZarfNamespaceName).Delete(ctx, "zarf-injector", metav1.DeleteOptions{})
if err != nil && !kerrors.IsNotFound(err) {
return err
}
err = c.Clientset.CoreV1().ConfigMaps(ZarfNamespaceName).Delete(ctx, "rust-binary", metav1.DeleteOptions{})
if err != nil && !kerrors.IsNotFound(err) {
return err
}
selector, err := metav1.LabelSelectorAsSelector(&metav1.LabelSelector{
MatchLabels: map[string]string{
"zarf-injector": "payload",
},
})
if err != nil {
return err
}
listOpts := metav1.ListOptions{
LabelSelector: selector.String(),
}
err = c.Clientset.CoreV1().ConfigMaps(ZarfNamespaceName).DeleteCollection(ctx, metav1.DeleteOptions{}, listOpts)
if err != nil {
return err
}
// This is needed because labels were not present in payload config maps previously.
// Without this injector will fail if the config maps exist from a previous Zarf version.
cmList, err := c.Clientset.CoreV1().ConfigMaps(ZarfNamespaceName).List(ctx, metav1.ListOptions{})
if err != nil {
return err
}
for _, cm := range cmList.Items {
if !strings.HasPrefix(cm.Name, "zarf-payload-") {
continue
}
err = c.Clientset.CoreV1().ConfigMaps(ZarfNamespaceName).Delete(ctx, cm.Name, metav1.DeleteOptions{})
if err != nil {
return err
}
}
// TODO: Replace with wait package in the future.
err = wait.PollUntilContextCancel(ctx, time.Second, true, func(ctx context.Context) (bool, error) {
_, err := c.Clientset.CoreV1().Pods(ZarfNamespaceName).Get(ctx, "injector", metav1.GetOptions{})
if kerrors.IsNotFound(err) {
return true, nil
}
return false, err
})
if err != nil {
return err
}
l.Debug("done deleting injector resources", "duration", time.Since(start))
return nil
}
func (c *Cluster) createPayloadConfigMaps(ctx context.Context, spinner *message.Spinner, tmpDir, imagesDir string, injectorSeedSrcs []string) ([]string, string, error) {
l := logger.From(ctx)
tarPath := filepath.Join(tmpDir, "payload.tar.gz")
seedImagesDir := filepath.Join(tmpDir, "seed-images")
if err := helpers.CreateDirectory(seedImagesDir, helpers.ReadWriteExecuteUser); err != nil {
return nil, "", fmt.Errorf("unable to create the seed images directory: %w", err)
}
localReferenceToDigest := map[string]string{}
for _, src := range injectorSeedSrcs {
ref, err := transform.ParseImageRef(src)
if err != nil {
return nil, "", fmt.Errorf("failed to create ref for image %s: %w", src, err)
}
img, err := utils.LoadOCIImage(imagesDir, ref)
if err != nil {
return nil, "", err
}
if err := crane.SaveOCI(img, seedImagesDir); err != nil {
return nil, "", err
}
imgDigest, err := img.Digest()
if err != nil {
return nil, "", err
}
localReferenceToDigest[ref.Path+ref.TagOrDigest] = imgDigest.String()
}
if err := utils.AddImageNameAnnotation(seedImagesDir, localReferenceToDigest); err != nil {
return nil, "", fmt.Errorf("unable to format OCI layout: %w", err)
}
// Chunk size has to accommodate base64 encoding & etcd 1MB limit
tarFileList, err := filepath.Glob(filepath.Join(seedImagesDir, "*"))
if err != nil {
return nil, "", err
}
if err := archiver.Archive(tarFileList, tarPath); err != nil {
return nil, "", err
}
payloadChunkSize := 1024 * 768
chunks, shasum, err := helpers.ReadFileByChunks(tarPath, payloadChunkSize)
if err != nil {
return nil, "", err
}
cmNames := []string{}
l.Info("adding archived binary configmaps of registry image to the cluster")
for i, data := range chunks {
fileName := fmt.Sprintf("zarf-payload-%03d", i)
spinner.Updatef("Adding archive binary configmap %d of %d to the cluster", i+1, len(chunks))
cm := v1ac.ConfigMap(fileName, ZarfNamespaceName).
WithLabels(map[string]string{
"zarf-injector": "payload",
}).
WithBinaryData(map[string][]byte{
fileName: data,
})
_, err = c.Clientset.CoreV1().ConfigMaps(ZarfNamespaceName).Apply(ctx, cm, metav1.ApplyOptions{Force: true, FieldManager: FieldManagerName})
if err != nil {
return nil, "", err
}
cmNames = append(cmNames, fileName)
// Give the control plane a 250ms buffer between each configmap
time.Sleep(250 * time.Millisecond)
}
return cmNames, shasum, nil
}
// getImagesAndNodesForInjection checks for images on schedulable nodes within a cluster.
func (c *Cluster) getInjectorImageAndNode(ctx context.Context, resReq *v1ac.ResourceRequirementsApplyConfiguration) (string, string, error) {
// Regex for Zarf seed image
zarfImageRegex, err := regexp.Compile(`(?m)^127\.0\.0\.1:`)
if err != nil {
return "", "", err
}
listOpts := metav1.ListOptions{
FieldSelector: fmt.Sprintf("status.phase=%s", corev1.PodRunning),
}
podList, err := c.Clientset.CoreV1().Pods(corev1.NamespaceAll).List(ctx, listOpts)
if err != nil {
return "", "", err
}
for _, pod := range podList.Items {
nodeDetails, err := c.Clientset.CoreV1().Nodes().Get(ctx, pod.Spec.NodeName, metav1.GetOptions{})
if err != nil {
return "", "", err
}
if nodeDetails.Status.Allocatable.Cpu().Cmp(*resReq.Requests.Cpu()) < 0 ||
nodeDetails.Status.Allocatable.Memory().Cmp(*resReq.Requests.Memory()) < 0 {
continue
}
if hasBlockingTaints(nodeDetails.Spec.Taints) {
continue
}
for _, container := range pod.Spec.Containers {
if zarfImageRegex.MatchString(container.Image) {
continue
}
return container.Image, pod.Spec.NodeName, nil
}
for _, container := range pod.Spec.InitContainers {
if zarfImageRegex.MatchString(container.Image) {
continue
}
return container.Image, pod.Spec.NodeName, nil
}
for _, container := range pod.Spec.EphemeralContainers {
if zarfImageRegex.MatchString(container.Image) {
continue
}
return container.Image, pod.Spec.NodeName, nil
}
}
return "", "", fmt.Errorf("no suitable injector image or node exists")
}
func hasBlockingTaints(taints []corev1.Taint) bool {
for _, taint := range taints {
if taint.Effect == corev1.TaintEffectNoSchedule || taint.Effect == corev1.TaintEffectNoExecute {
return true
}
}
return false
}
func buildInjectionPod(nodeName, image string, payloadCmNames []string, shasum string, resReq *v1ac.ResourceRequirementsApplyConfiguration) *v1ac.PodApplyConfiguration {
executeMode := int32(0777)
userID := int64(1000)
groupID := int64(2000)
fsGroupID := int64(2000)
volumes := []*v1ac.VolumeApplyConfiguration{
v1ac.Volume().
WithName("init").
WithConfigMap(
v1ac.ConfigMapVolumeSource().
WithName("rust-binary").
WithDefaultMode(executeMode),
),
v1ac.Volume().
WithName("seed").
WithEmptyDir(&v1ac.EmptyDirVolumeSourceApplyConfiguration{})}
volumeMounts := []*v1ac.VolumeMountApplyConfiguration{
v1ac.VolumeMount().
WithName("init").
WithMountPath("/zarf-init/zarf-injector").
WithSubPath("zarf-injector"),
v1ac.VolumeMount().
WithName("seed").
WithMountPath("/zarf-seed"),
}
for _, filename := range payloadCmNames {
volumes = append(volumes, v1ac.Volume().
WithName(filename).
WithConfigMap(
v1ac.ConfigMapVolumeSource().
WithName(filename),
))
volumeMounts = append(volumeMounts, v1ac.VolumeMount().
WithName(filename).
WithMountPath(fmt.Sprintf("/zarf-init/%s", filename)).
WithSubPath(filename))
}
pod := v1ac.Pod("injector", ZarfNamespaceName).
WithLabels(map[string]string{
"app": "zarf-injector",
AgentLabel: "ignore",
}).
WithSpec(
v1ac.PodSpec().
WithNodeName(nodeName).
WithRestartPolicy(corev1.RestartPolicyNever).
WithSecurityContext(
v1ac.PodSecurityContext().
WithRunAsUser(userID).
WithRunAsGroup(groupID).
WithFSGroup(fsGroupID).
WithSeccompProfile(
v1ac.SeccompProfile().
WithType(corev1.SeccompProfileTypeRuntimeDefault),
),
).
WithContainers(
v1ac.Container().
WithName("injector").
WithImage(image).
WithImagePullPolicy(corev1.PullIfNotPresent).
WithWorkingDir("/zarf-init").
WithCommand("/zarf-init/zarf-injector", shasum).
WithVolumeMounts(volumeMounts...).
WithSecurityContext(
v1ac.SecurityContext().
WithReadOnlyRootFilesystem(true).
WithAllowPrivilegeEscalation(false).
WithRunAsNonRoot(true).
WithCapabilities(v1ac.Capabilities().WithDrop(corev1.Capability("ALL"))),
).
WithReadinessProbe(
v1ac.Probe().
WithPeriodSeconds(2).
WithSuccessThreshold(1).
WithFailureThreshold(10).
WithHTTPGet(
v1ac.HTTPGetAction().
WithPath("/v2/").
WithPort(intstr.FromInt(5000)),
),
).
WithResources(resReq),
).
WithVolumes(volumes...),
)
return pod
}