-
Notifications
You must be signed in to change notification settings - Fork 33
/
pod.go
579 lines (520 loc) · 18.5 KB
/
pod.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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
// SPDX-FileCopyrightText: 2020 SAP SE or an SAP affiliate company and Gardener contributors.
//
// SPDX-License-Identifier: Apache-2.0
package container
import (
"context"
"encoding/json"
"fmt"
"path/filepath"
"strconv"
"k8s.io/utils/ptr"
"github.com/gardener/landscaper/pkg/utils/read_write_layer"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
lsv1alpha1 "github.com/gardener/landscaper/apis/core/v1alpha1"
"github.com/gardener/landscaper/apis/deployer/container"
containerv1alpha1 "github.com/gardener/landscaper/apis/deployer/container/v1alpha1"
kutil "github.com/gardener/landscaper/controller-utils/pkg/kubernetes"
"github.com/gardener/landscaper/controller-utils/pkg/logging"
)
// PodTokenPath is the path in the pod that contains the service account token.
const PodTokenPath = "/var/run/secrets/kubernetes.io/serviceaccount/token"
// InitContainerServiceAccountName generates the service account name for the init container
func InitContainerServiceAccountName(di *lsv1alpha1.DeployItem) string {
return fmt.Sprintf("%s-init", di.Name)
}
// WaitContainerServiceAccountName generates the service account name for the wait container
func WaitContainerServiceAccountName(di *lsv1alpha1.DeployItem) string {
return fmt.Sprintf("%s-wait", di.Name)
}
// ExportSecretName generates the secret name for the exported secret
func ExportSecretName(deployItemNamespace, deployItemName string) string {
return fmt.Sprintf("%s-%s-export", deployItemNamespace, deployItemName)
}
// DeployItemExportSecretName generates the secret name for the exported secret
func DeployItemExportSecretName(deployItemName string) string {
return fmt.Sprintf("%s-export", deployItemName)
}
// ConfigurationSecretName generates the secret name for the imported secret.
// todo: use container identity
func ConfigurationSecretName(deployItemNamespace, deployItemName string) string {
return fmt.Sprintf("%s-%s-config", deployItemNamespace, deployItemName)
}
// TargetSecretName generates the secret name for the imported secret.
// todo: use container identity
func TargetSecretName(deployItemNamespace, deployItemName string) string {
return fmt.Sprintf("%s-%s-target", deployItemNamespace, deployItemName)
}
// ImagePullSecretName generates the secret name for the image pull secret.
// todo: use container identity
func ImagePullSecretName(deployItemNamespace, deployItemName string) string {
return fmt.Sprintf("%s-%s-imgpullsec", deployItemNamespace, deployItemName)
}
// BluePrintPullSecretName generates the secret name for the image pull secret.
// todo: use container identity
func BluePrintPullSecretName(deployItemNamespace, deployItemName string) string {
return fmt.Sprintf("%s-%s-bppullsec", deployItemNamespace, deployItemName)
}
// ComponentDescriptorPullSecretName generates the secret name for the image pull secret.
// todo: use container identity
func ComponentDescriptorPullSecretName(deployItemNamespace, deployItemName string) string {
return fmt.Sprintf("%s-%s-cdpullsec", deployItemNamespace, deployItemName)
}
// DefaultLabels returns the default labels for a resource generated by the container deployer.
func DefaultLabels(deployerId, deployerName, diName, diNamespace string) map[string]string {
return map[string]string{
container.ContainerDeployerIDLabel: deployerId,
container.ContainerDeployerNameLabel: deployerName,
container.ContainerDeployerDeployItemNameLabel: diName,
container.ContainerDeployerDeployItemNamespaceLabel: diNamespace,
}
}
// InjectDefaultLabels injects default labels into the given object.
func InjectDefaultLabels(obj client.Object, defaultLabels map[string]string) {
labels := obj.GetLabels()
if labels == nil {
labels = make(map[string]string)
}
for k, v := range defaultLabels {
labels[k] = v
}
obj.SetLabels(labels)
}
// PodOptions contains the configuration that is needed for the scheduled pod
type PodOptions struct {
DeployerID string
ProviderConfiguration *containerv1alpha1.ProviderConfiguration
InitContainer containerv1alpha1.ContainerSpec
WaitContainer containerv1alpha1.ContainerSpec
InitContainerServiceAccountSecret types.NamespacedName
WaitContainerServiceAccountSecret types.NamespacedName
ConfigurationSecretName string
TargetSecretName string
ImagePullSecret string
BluePrintPullSecret string
ComponentDescriptorPullSecret string
UseOCM bool
Name string
Namespace string
DeployItemName string
DeployItemNamespace string
DeployItemGeneration int64
Operation container.OperationType
encBlueprintRef []byte
Debug bool
}
// Complete completes the the Blueprint provider configuration
func (o *PodOptions) Complete() error {
if o.ProviderConfiguration.Blueprint != nil {
raw, err := json.Marshal(o.ProviderConfiguration.Blueprint)
if err != nil {
return err
}
o.encBlueprintRef = raw
}
return nil
}
func generatePod(opts PodOptions) (*corev1.Pod, error) {
if err := opts.Complete(); err != nil {
return nil, err
}
sharedVolume := corev1.Volume{
Name: "shared-volume",
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{},
},
}
sharedVolumeMount := corev1.VolumeMount{
Name: sharedVolume.Name,
MountPath: container.SharedBasePath,
}
initServiceAccountVolume := corev1.Volume{
Name: "serviceaccount-init",
VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: opts.InitContainerServiceAccountSecret.Name,
},
},
}
initServiceAccountMount := corev1.VolumeMount{
Name: initServiceAccountVolume.Name,
ReadOnly: true,
MountPath: filepath.Dir(PodTokenPath),
}
waitServiceAccountMount := corev1.VolumeMount{
Name: "serviceaccount-wait",
ReadOnly: true,
MountPath: filepath.Dir(PodTokenPath),
}
waitServiceAccountVolume := corev1.Volume{
Name: "serviceaccount-wait",
VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: opts.WaitContainerServiceAccountSecret.Name,
},
},
}
configurationVolume := corev1.Volume{
Name: "configuration",
VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: opts.ConfigurationSecretName,
},
},
}
configurationVolumeMount := corev1.VolumeMount{
Name: configurationVolume.Name,
ReadOnly: true,
MountPath: filepath.Dir(container.ConfigurationPath),
}
targetVolume := corev1.Volume{
Name: "target",
VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: opts.TargetSecretName,
},
},
}
targetInitVolumeMount := corev1.VolumeMount{
Name: targetVolume.Name,
ReadOnly: true,
MountPath: container.TargetInitDir,
}
additionalInitEnvVars := []corev1.EnvVar{
{
Name: container.ConfigurationPathName,
Value: container.ConfigurationPath,
},
{
Name: container.DeployItemName,
Value: opts.DeployItemName,
},
{
Name: container.DeployItemNamespaceName,
Value: opts.DeployItemNamespace,
},
{
Name: container.RegistrySecretBasePathName,
Value: container.RegistrySecretBasePath,
},
{
Name: container.UseOCMName,
Value: fmt.Sprint(opts.UseOCM),
},
}
additionalSidecarEnvVars := []corev1.EnvVar{
{
Name: container.DeployItemName,
Value: opts.DeployItemName,
},
{
Name: container.DeployItemNamespaceName,
Value: opts.DeployItemNamespace,
},
}
additionalEnvVars := []corev1.EnvVar{
{
Name: container.OperationName,
Value: string(opts.Operation),
},
}
volumes := []corev1.Volume{
initServiceAccountVolume,
waitServiceAccountVolume,
sharedVolume,
configurationVolume,
targetVolume,
}
initMounts := []corev1.VolumeMount{configurationVolumeMount, targetInitVolumeMount, initServiceAccountMount, sharedVolumeMount}
for name, v := range map[string]string{
"blueprint-pull-secret": opts.BluePrintPullSecret,
"cd-pull-secret": opts.ComponentDescriptorPullSecret} {
if len(v) == 0 {
continue
}
volumes = append(volumes, corev1.Volume{
Name: name,
VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: v,
},
},
})
initMounts = append(initMounts, corev1.VolumeMount{
Name: name,
ReadOnly: true,
MountPath: filepath.Join(container.RegistrySecretBasePath, name),
})
}
initContainer := corev1.Container{
Name: container.InitContainerName,
Image: opts.InitContainer.Image,
Command: opts.InitContainer.Command,
Args: opts.InitContainer.Args,
Env: append(container.DefaultEnvVars, additionalInitEnvVars...),
Resources: corev1.ResourceRequirements{},
TerminationMessagePolicy: corev1.TerminationMessageFallbackToLogsOnError,
ImagePullPolicy: opts.InitContainer.ImagePullPolicy,
VolumeMounts: initMounts,
}
waitContainer := corev1.Container{
Name: container.WaitContainerName,
Image: opts.WaitContainer.Image,
Command: opts.WaitContainer.Command,
Args: opts.WaitContainer.Args,
Env: append(container.DefaultEnvVars, additionalSidecarEnvVars...),
Resources: corev1.ResourceRequirements{},
TerminationMessagePolicy: corev1.TerminationMessageFallbackToLogsOnError,
ImagePullPolicy: opts.WaitContainer.ImagePullPolicy,
VolumeMounts: []corev1.VolumeMount{
waitServiceAccountMount,
sharedVolumeMount,
},
}
mainContainer := corev1.Container{
Name: container.MainContainerName,
Image: opts.ProviderConfiguration.Image,
Command: opts.ProviderConfiguration.Command,
Args: opts.ProviderConfiguration.Args,
Env: append(container.DefaultEnvVars, additionalEnvVars...),
Resources: corev1.ResourceRequirements{},
TerminationMessagePolicy: corev1.TerminationMessageFallbackToLogsOnError,
ImagePullPolicy: corev1.PullIfNotPresent,
VolumeMounts: []corev1.VolumeMount{sharedVolumeMount},
}
if opts.Debug {
initContainer.ImagePullPolicy = corev1.PullAlways
waitContainer.ImagePullPolicy = corev1.PullAlways
}
pod := &corev1.Pod{}
pod.GenerateName = opts.Name + "-"
pod.Namespace = opts.Namespace
InjectDefaultLabels(pod, DefaultLabels(opts.DeployerID, opts.Name, opts.DeployItemName, opts.DeployItemNamespace))
pod.Labels[container.ContainerDeployerDeployItemGenerationLabel] = strconv.Itoa(int(opts.DeployItemGeneration))
pod.Finalizers = []string{container.ContainerDeployerFinalizer}
pod.Spec.AutomountServiceAccountToken = ptr.To[bool](false)
pod.Spec.RestartPolicy = corev1.RestartPolicyNever
pod.Spec.TerminationGracePeriodSeconds = ptr.To[int64](300)
pod.Spec.Volumes = volumes
pod.Spec.SecurityContext = &corev1.PodSecurityContext{
RunAsUser: ptr.To[int64](1000),
RunAsGroup: ptr.To[int64](3000),
FSGroup: ptr.To[int64](2000),
}
pod.Spec.InitContainers = []corev1.Container{initContainer}
pod.Spec.Containers = []corev1.Container{mainContainer, waitContainer}
if len(opts.ImagePullSecret) != 0 {
pod.Spec.ImagePullSecrets = []corev1.LocalObjectReference{
{
Name: opts.ImagePullSecret,
},
}
}
return pod, nil
}
// getPod returns the latest executed pod.
// Pods that have no finalizer are ignored.
func (c *Container) getPod(ctx context.Context) (*corev1.Pod, error) {
podList := &corev1.PodList{}
if err := read_write_layer.ListPods(ctx, c.hostUncachedClient, podList, read_write_layer.R000077,
client.InNamespace(c.Configuration.Namespace), client.MatchingLabels{
container.ContainerDeployerDeployItemNameLabel: c.DeployItem.Name,
container.ContainerDeployerDeployItemNamespaceLabel: c.DeployItem.Namespace,
}); err != nil {
return nil, err
}
if len(podList.Items) == 0 {
return nil, apierrors.NewNotFound(schema.GroupResource{
Group: corev1.SchemeGroupVersion.Group,
Resource: "Pod",
}, c.DeployItem.Name)
}
// only return latest pod and ignore previous runs
var latest *corev1.Pod
for _, pod := range podList.Items {
// ignore pods with no finalizer as they are already reconciled and their state was persisted.
if !controllerutil.ContainsFinalizer(&pod, container.ContainerDeployerFinalizer) {
continue
}
if latest == nil {
latest = pod.DeepCopy()
}
if pod.CreationTimestamp.After(latest.CreationTimestamp.Time) {
latest = pod.DeepCopy()
}
}
if latest == nil {
return nil, apierrors.NewNotFound(schema.GroupResource{
Group: corev1.SchemeGroupVersion.Group,
Resource: "Pod",
}, c.DeployItem.Name)
}
return latest, nil
}
// EnsureServiceAccountsResult describes the result of the ensureServiceAccounts func
type EnsureServiceAccountsResult struct {
InitContainerServiceAccountSecret types.NamespacedName
WaitContainerServiceAccountSecret types.NamespacedName
}
// EnsureServiceAccounts ensures that the service accounts for the init and wait container are created
// and have the necessary permissions.
func EnsureServiceAccounts(ctx context.Context, hostClient client.Client, deployItem *lsv1alpha1.DeployItem, hostNamespace string, labels map[string]string) (*EnsureServiceAccountsResult, error) {
var (
res = &EnsureServiceAccountsResult{}
log = logging.FromContextOrDiscard(ctx)
)
initSA := &corev1.ServiceAccount{}
initSA.Name = InitContainerServiceAccountName(deployItem)
initSA.Namespace = hostNamespace
if _, err := controllerutil.CreateOrUpdate(ctx, hostClient, initSA, func() error {
InjectDefaultLabels(initSA, labels)
return nil
}); err != nil {
return nil, err
}
// create role and rolebindings for the init service account
role := &rbacv1.Role{}
role.Name = initSA.Name
role.Namespace = initSA.Namespace
_, err := controllerutil.CreateOrUpdate(ctx, hostClient, role, func() error {
InjectDefaultLabels(role, labels)
// need to read secrets to restore the state
// deletion is needed for garbage collection
role.Rules = []rbacv1.PolicyRule{
{
APIGroups: []string{corev1.SchemeGroupVersion.Group},
Resources: []string{"secrets"},
Verbs: []string{"get", "list", "delete"},
},
}
return nil
})
if err != nil {
return nil, err
}
rolebinding := &rbacv1.RoleBinding{}
rolebinding.Name = initSA.Name
rolebinding.Namespace = initSA.Namespace
_, err = controllerutil.CreateOrUpdate(ctx, hostClient, rolebinding, func() error {
InjectDefaultLabels(rolebinding, labels)
rolebinding.RoleRef = rbacv1.RoleRef{
APIGroup: rbacv1.SchemeGroupVersion.Group,
Kind: "Role",
Name: role.Name,
}
rolebinding.Subjects = []rbacv1.Subject{
{
APIGroup: "",
Kind: "ServiceAccount",
Name: initSA.Name,
Namespace: initSA.Namespace,
},
}
return nil
})
if err != nil {
return nil, err
}
// wait for kubernetes to create the service accounts secrets
res.InitContainerServiceAccountSecret, err = WaitAndGetServiceAccountSecret(ctx, log, hostClient, initSA, labels)
if err != nil {
return nil, err
}
waitSA := &corev1.ServiceAccount{}
waitSA.Name = WaitContainerServiceAccountName(deployItem)
waitSA.Namespace = hostNamespace
if _, err := controllerutil.CreateOrUpdate(ctx, hostClient, waitSA, func() error {
InjectDefaultLabels(waitSA, labels)
return nil
}); err != nil {
return nil, err
}
// create role and rolebindings for the wait service account
role = &rbacv1.Role{}
role.Name = waitSA.Name
role.Namespace = waitSA.Namespace
_, err = controllerutil.CreateOrUpdate(ctx, hostClient, role, func() error {
InjectDefaultLabels(role, labels)
role.Rules = []rbacv1.PolicyRule{
// we need a specific create secrets role as we cannot restrict the creation of secrets to a specific name
// See https://kubernetes.io/docs/reference/access-authn-authz/rbac/
// "You cannot restrict create or deletecollection requests by resourceName. For create, this limitation is because the object name is not known at authorization time."
// the ait container needs permissions to write secrets for its state.
{
APIGroups: []string{corev1.SchemeGroupVersion.Group},
Resources: []string{"secrets"},
Verbs: []string{"create", "update", "get", "list"},
},
{
APIGroups: []string{corev1.SchemeGroupVersion.Group},
Resources: []string{"pods"},
Verbs: []string{"get"},
},
}
return nil
})
if err != nil {
return nil, err
}
rolebinding = &rbacv1.RoleBinding{}
rolebinding.Name = waitSA.Name
rolebinding.Namespace = waitSA.Namespace
_, err = controllerutil.CreateOrUpdate(ctx, hostClient, rolebinding, func() error {
InjectDefaultLabels(rolebinding, labels)
rolebinding.RoleRef = rbacv1.RoleRef{
APIGroup: rbacv1.SchemeGroupVersion.Group,
Kind: "Role",
Name: role.Name,
}
rolebinding.Subjects = []rbacv1.Subject{
{
APIGroup: "",
Kind: "ServiceAccount",
Name: waitSA.Name,
Namespace: waitSA.Namespace,
},
}
return nil
})
if err != nil {
return nil, err
}
// wait for kubernetes to create the service accounts secrets
res.WaitContainerServiceAccountSecret, err = WaitAndGetServiceAccountSecret(ctx, log, hostClient, waitSA, labels)
if err != nil {
return nil, err
}
return res, nil
}
// WaitAndGetServiceAccountSecret waits until a service accounts secret is available and returns the secrets name.
func WaitAndGetServiceAccountSecret(ctx context.Context, log logging.Logger, c client.Client, serviceAccount *corev1.ServiceAccount, labels map[string]string) (types.NamespacedName, error) {
secretKey := types.NamespacedName{}
config := &rest.Config{}
if err := kutil.AddServiceAccountToken(ctx, c, serviceAccount, config); err != nil {
return secretKey, err
}
saKey := client.ObjectKeyFromObject(serviceAccount)
secrets, err := kutil.GetSecretsForServiceAccount(ctx, c, saKey)
if err != nil {
log.Error(err, "unable to get secrets for service account", "serviceaccount", saKey.String())
return secretKey, err
}
if len(secrets) == 0 {
return secretKey, fmt.Errorf("no secret found for service account %s", saKey.String())
}
for _, secret := range secrets {
InjectDefaultLabels(secret, labels)
if err := c.Update(ctx, secret); err != nil {
return secretKey, err
}
}
secretKey = client.ObjectKeyFromObject(secrets[0])
return secretKey, nil
}