-
Notifications
You must be signed in to change notification settings - Fork 126
/
pod_mutating_webhook.go
551 lines (453 loc) · 20.2 KB
/
pod_mutating_webhook.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
package webhooks
import (
"context"
"encoding/json"
"fmt"
"hash/fnv"
"net/http"
"reflect"
"strings"
"github.com/go-logr/logr"
klcv1alpha2 "github.com/keptn/lifecycle-toolkit/operator/api/v1alpha2"
apicommon "github.com/keptn/lifecycle-toolkit/operator/api/v1alpha2/common"
"github.com/keptn/lifecycle-toolkit/operator/api/v1alpha2/semconv"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)
// +kubebuilder:webhook:path=/mutate-v1-pod,mutating=true,failurePolicy=fail,groups="",resources=pods,verbs=create;update,versions=v1,name=mpod.keptn.sh,admissionReviewVersions=v1,sideEffects=None
//+kubebuilder:rbac:groups=core,resources=namespaces,verbs=get;list;watch
//+kubebuilder:rbac:groups=apps,resources=deployments;statefulsets;daemonsets;replicasets,verbs=get
// PodMutatingWebhook annotates Pods
type PodMutatingWebhook struct {
Client client.Client
Tracer trace.Tracer
decoder *admission.Decoder
Recorder record.EventRecorder
Log logr.Logger
}
const InvalidAnnotationMessage = "Invalid annotations"
var ErrTooLongAnnotations = fmt.Errorf("too long annotations, maximum length for app and workload is 25 characters, for version 12 characters")
// Handle inspects incoming Pods and injects the Keptn scheduler if they contain the Keptn lifecycle annotations.
func (a *PodMutatingWebhook) Handle(ctx context.Context, req admission.Request) admission.Response {
ctx, span := a.Tracer.Start(ctx, "annotate_pod", trace.WithNewRoot(), trace.WithSpanKind(trace.SpanKindServer))
defer span.End()
logger := log.FromContext(ctx).WithValues("webhook", "/mutate-v1-pod", "object", map[string]interface{}{
"name": req.Name,
"namespace": req.Namespace,
"kind": req.Kind,
})
logger.Info("webhook for pod called")
pod := &corev1.Pod{}
err := a.decoder.Decode(req, pod)
if err != nil {
return admission.Errored(http.StatusBadRequest, err)
}
// check if Lifecycle Controller is enabled for this namespace
namespace := &corev1.Namespace{}
if err = a.Client.Get(ctx, types.NamespacedName{Name: req.Namespace}, namespace); err != nil {
logger.Error(err, "could not get namespace", "namespace", req.Namespace)
return admission.Errored(http.StatusInternalServerError, err)
}
if namespace.GetAnnotations()[apicommon.NamespaceEnabledAnnotation] != "enabled" {
logger.Info("namespace is not enabled for lifecycle controller", "namespace", req.Namespace)
return admission.Allowed("namespace is not enabled for lifecycle controller")
}
logger.Info(fmt.Sprintf("Pod annotations: %v", pod.Annotations))
podIsAnnotated, err := a.isPodAnnotated(pod)
logger.Info("Checked if pod is annotated.")
if err != nil {
span.SetStatus(codes.Error, InvalidAnnotationMessage)
return admission.Errored(http.StatusBadRequest, err)
}
if !podIsAnnotated {
logger.Info("Pod is not annotated, check for parent annotations...")
podIsAnnotated, err = a.copyAnnotationsIfParentAnnotated(ctx, &req, pod)
if err != nil {
span.SetStatus(codes.Error, InvalidAnnotationMessage)
return admission.Errored(http.StatusBadRequest, err)
}
}
if podIsAnnotated {
logger.Info("Resource is annotated with Keptn annotations, using Keptn scheduler")
pod.Spec.SchedulerName = "keptn-scheduler"
logger.Info("Annotations", "annotations", pod.Annotations)
_, err := a.isAppAnnotationPresent(pod)
if err != nil {
span.SetStatus(codes.Error, InvalidAnnotationMessage)
return admission.Errored(http.StatusBadRequest, err)
}
if err := a.handleApp(ctx, logger, pod, req.Namespace); err != nil {
logger.Error(err, "Could not handle App")
span.SetStatus(codes.Error, err.Error())
return admission.Errored(http.StatusBadRequest, err)
}
semconv.AddAttributeFromAnnotations(span, pod.Annotations)
logger.Info("Attributes from annotations set")
if err := a.handleWorkload(ctx, logger, pod, req.Namespace); err != nil {
logger.Error(err, "Could not handle Workload")
span.SetStatus(codes.Error, err.Error())
return admission.Errored(http.StatusBadRequest, err)
}
}
marshaledPod, err := json.Marshal(pod)
if err != nil {
span.SetStatus(codes.Error, "Failed to marshal")
return admission.Errored(http.StatusInternalServerError, err)
}
return admission.PatchResponseFromRaw(req.Object.Raw, marshaledPod)
}
// PodMutatingWebhook implements admission.DecoderInjector.
// A decoder will be automatically injected.
// InjectDecoder injects the decoder.
func (a *PodMutatingWebhook) InjectDecoder(d *admission.Decoder) error {
a.decoder = d
return nil
}
func (a *PodMutatingWebhook) isPodAnnotated(pod *corev1.Pod) (bool, error) {
workload, gotWorkloadAnnotation := getLabelOrAnnotation(&pod.ObjectMeta, apicommon.WorkloadAnnotation, apicommon.K8sRecommendedWorkloadAnnotations)
version, gotVersionAnnotation := getLabelOrAnnotation(&pod.ObjectMeta, apicommon.VersionAnnotation, apicommon.K8sRecommendedVersionAnnotations)
if len(workload) > apicommon.MaxWorkloadNameLength || len(version) > apicommon.MaxVersionLength {
return false, ErrTooLongAnnotations
}
if gotWorkloadAnnotation {
if !gotVersionAnnotation {
if len(pod.Annotations) == 0 {
pod.Annotations = make(map[string]string)
}
pod.Annotations[apicommon.VersionAnnotation] = a.calculateVersion(pod)
}
return true, nil
}
return false, nil
}
func (a *PodMutatingWebhook) copyAnnotationsIfParentAnnotated(ctx context.Context, req *admission.Request, pod *corev1.Pod) (bool, error) {
podOwner := a.getOwnerReference(&pod.ObjectMeta)
if podOwner.UID == "" {
return false, nil
}
switch podOwner.Kind {
case "ReplicaSet":
rs := &appsv1.ReplicaSet{}
if err := a.Client.Get(ctx, types.NamespacedName{Namespace: req.Namespace, Name: podOwner.Name}, rs); err != nil {
return false, nil
}
a.Log.Info("Done fetching RS")
rsOwner := a.getOwnerReference(&rs.ObjectMeta)
if rsOwner.UID == "" {
return false, nil
}
dp := &appsv1.Deployment{}
return a.fetchParentObjectAndCopyLabels(ctx, rsOwner.Name, req.Namespace, pod, dp)
case "StatefulSet":
sts := &appsv1.StatefulSet{}
return a.fetchParentObjectAndCopyLabels(ctx, podOwner.Name, req.Namespace, pod, sts)
case "DaemonSet":
ds := &appsv1.DaemonSet{}
return a.fetchParentObjectAndCopyLabels(ctx, podOwner.Name, req.Namespace, pod, ds)
default:
return false, nil
}
}
func (a *PodMutatingWebhook) fetchParentObjectAndCopyLabels(ctx context.Context, name string, namespace string, pod *corev1.Pod, objectContainer client.Object) (bool, error) {
if err := a.Client.Get(ctx, types.NamespacedName{Namespace: namespace, Name: name}, objectContainer); err != nil {
return false, nil
}
objectContainerMetaData := metav1.ObjectMeta{
Labels: objectContainer.GetLabels(),
Annotations: objectContainer.GetAnnotations(),
}
return a.copyResourceLabelsIfPresent(&objectContainerMetaData, pod)
}
func (a *PodMutatingWebhook) copyResourceLabelsIfPresent(sourceResource *metav1.ObjectMeta, targetPod *corev1.Pod) (bool, error) {
var workloadName, appName, version, preDeploymentChecks, postDeploymentChecks, preEvaluationChecks, postEvaluationChecks string
var gotWorkloadName, gotVersion bool
workloadName, gotWorkloadName = getLabelOrAnnotation(sourceResource, apicommon.WorkloadAnnotation, apicommon.K8sRecommendedWorkloadAnnotations)
appName, _ = getLabelOrAnnotation(sourceResource, apicommon.AppAnnotation, apicommon.K8sRecommendedAppAnnotations)
version, gotVersion = getLabelOrAnnotation(sourceResource, apicommon.VersionAnnotation, apicommon.K8sRecommendedVersionAnnotations)
preDeploymentChecks, _ = getLabelOrAnnotation(sourceResource, apicommon.PreDeploymentTaskAnnotation, "")
postDeploymentChecks, _ = getLabelOrAnnotation(sourceResource, apicommon.PostDeploymentTaskAnnotation, "")
preEvaluationChecks, _ = getLabelOrAnnotation(sourceResource, apicommon.PreDeploymentEvaluationAnnotation, "")
postEvaluationChecks, _ = getLabelOrAnnotation(sourceResource, apicommon.PostDeploymentEvaluationAnnotation, "")
if len(workloadName) > apicommon.MaxWorkloadNameLength || len(version) > apicommon.MaxVersionLength {
return false, ErrTooLongAnnotations
}
if len(targetPod.Annotations) == 0 {
targetPod.Annotations = make(map[string]string)
}
if gotWorkloadName {
setMapKey(targetPod.Annotations, apicommon.WorkloadAnnotation, workloadName)
if !gotVersion {
setMapKey(targetPod.Annotations, apicommon.VersionAnnotation, a.calculateVersion(targetPod))
} else {
setMapKey(targetPod.Annotations, apicommon.VersionAnnotation, version)
}
setMapKey(targetPod.Annotations, apicommon.AppAnnotation, appName)
setMapKey(targetPod.Annotations, apicommon.PreDeploymentTaskAnnotation, preDeploymentChecks)
setMapKey(targetPod.Annotations, apicommon.PostDeploymentTaskAnnotation, postDeploymentChecks)
setMapKey(targetPod.Annotations, apicommon.PreDeploymentEvaluationAnnotation, preEvaluationChecks)
setMapKey(targetPod.Annotations, apicommon.PostDeploymentEvaluationAnnotation, postEvaluationChecks)
return true, nil
}
return false, nil
}
func (a *PodMutatingWebhook) isAppAnnotationPresent(pod *corev1.Pod) (bool, error) {
app, gotAppAnnotation := getLabelOrAnnotation(&pod.ObjectMeta, apicommon.AppAnnotation, apicommon.K8sRecommendedAppAnnotations)
if gotAppAnnotation {
if len(app) > apicommon.MaxAppNameLength {
return false, ErrTooLongAnnotations
}
return true, nil
}
if len(pod.Annotations) == 0 {
pod.Annotations = make(map[string]string)
}
pod.Annotations[apicommon.AppAnnotation], _ = getLabelOrAnnotation(&pod.ObjectMeta, apicommon.WorkloadAnnotation, apicommon.K8sRecommendedWorkloadAnnotations)
return false, nil
}
func (a *PodMutatingWebhook) calculateVersion(pod *corev1.Pod) string {
name := ""
if len(pod.Spec.Containers) == 1 {
image := strings.Split(pod.Spec.Containers[0].Image, ":")
if len(image) > 1 && image[1] != "" && image[1] != "latest" {
return image[1]
}
}
for _, item := range pod.Spec.Containers {
name = name + item.Name + item.Image
for _, e := range item.Env {
name = name + e.Name + e.Value
}
}
h := fnv.New32a()
h.Write([]byte(name))
return fmt.Sprint(h.Sum32())
}
func (a *PodMutatingWebhook) handleWorkload(ctx context.Context, logger logr.Logger, pod *corev1.Pod, namespace string) error {
ctx, span := a.Tracer.Start(ctx, "create_workload", trace.WithSpanKind(trace.SpanKindProducer))
defer span.End()
newWorkload := a.generateWorkload(ctx, pod, namespace)
newWorkload.SetSpanAttributes(span)
logger.Info("Searching for workload")
workload := &klcv1alpha2.KeptnWorkload{}
err := a.Client.Get(ctx, types.NamespacedName{Namespace: namespace, Name: newWorkload.Name}, workload)
if errors.IsNotFound(err) {
logger.Info("Creating workload", "workload", workload.Name)
workload = newWorkload
err = a.Client.Create(ctx, workload)
if err != nil {
logger.Error(err, "Could not create Workload")
a.Recorder.Event(workload, "Warning", "WorkloadNotCreated", fmt.Sprintf("Could not create KeptnWorkload / Namespace: %s, Name: %s ", workload.Namespace, workload.Name))
span.SetStatus(codes.Error, err.Error())
return err
}
a.Recorder.Event(workload, "Normal", "WorkloadCreated", fmt.Sprintf("KeptnWorkload created / Namespace: %s, Name: %s ", workload.Namespace, workload.Name))
return nil
}
if err != nil {
span.SetStatus(codes.Error, err.Error())
return fmt.Errorf("could not fetch Workload"+": %+v", err)
}
if reflect.DeepEqual(workload.Spec, newWorkload.Spec) {
logger.Info("Pod not changed, not updating anything")
return nil
}
logger.Info("Pod changed, updating workload")
workload.Spec = newWorkload.Spec
err = a.Client.Update(ctx, workload)
if err != nil {
logger.Error(err, "Could not update Workload")
a.Recorder.Event(workload, "Warning", "WorkloadNotUpdated", fmt.Sprintf("Could not update KeptnWorkload / Namespace: %s, Name: %s ", workload.Namespace, workload.Name))
span.SetStatus(codes.Error, err.Error())
return err
}
a.Recorder.Event(workload, "Normal", "WorkloadUpdated", fmt.Sprintf("KeptnWorkload updated / Namespace: %s, Name: %s ", workload.Namespace, workload.Name))
return nil
}
func (a *PodMutatingWebhook) handleApp(ctx context.Context, logger logr.Logger, pod *corev1.Pod, namespace string) error {
ctx, span := a.Tracer.Start(ctx, "create_app", trace.WithSpanKind(trace.SpanKindProducer))
defer span.End()
newAppCreationRequest := a.generateAppCreationRequest(ctx, pod, namespace)
appCreationRequest := &klcv1alpha2.KeptnAppCreationRequest{}
err := a.Client.Get(ctx, types.NamespacedName{Namespace: namespace, Name: newAppCreationRequest.Name}, appCreationRequest)
if errors.IsNotFound(err) {
logger.Info("Creating app creation request", "appCreationRequest", appCreationRequest.Name)
appCreationRequest = newAppCreationRequest
err = a.Client.Create(ctx, appCreationRequest)
if err != nil {
logger.Error(err, "Could not create App")
a.Recorder.Event(appCreationRequest, "Warning", "AppNotCreated", fmt.Sprintf("Could not create KeptnAppCreationRequest / Namespace: %s, Name: %s ", appCreationRequest.Namespace, appCreationRequest.Name))
span.SetStatus(codes.Error, err.Error())
return err
}
a.Recorder.Event(appCreationRequest, "Normal", "AppCreated", fmt.Sprintf("KeptnAppCreationRequest created / Namespace: %s, Name: %s ", appCreationRequest.Namespace, appCreationRequest.Name))
return nil
}
if err != nil {
span.SetStatus(codes.Error, err.Error())
return fmt.Errorf("could not fetch App"+": %+v", err)
}
if reflect.DeepEqual(appCreationRequest.Spec, newAppCreationRequest.Spec) {
logger.Info("Pod not changed, not updating anything")
return nil
}
logger.Info("Pod changed, updating app")
appCreationRequest.Spec = newAppCreationRequest.Spec
err = a.Client.Update(ctx, appCreationRequest)
if err != nil {
logger.Error(err, "Could not update App")
a.Recorder.Event(appCreationRequest, "Warning", "AppNotUpdated", fmt.Sprintf("Could not update KeptnAppCreationRequest / Namespace: %s, Name: %s ", appCreationRequest.Namespace, appCreationRequest.Name))
span.SetStatus(codes.Error, err.Error())
return err
}
a.Recorder.Event(appCreationRequest, "Normal", "AppUpdated", fmt.Sprintf("KeptnAppCreationRequest updated / Namespace: %s, Name: %s ", appCreationRequest.Namespace, appCreationRequest.Name))
return nil
}
func (a *PodMutatingWebhook) generateWorkload(ctx context.Context, pod *corev1.Pod, namespace string) *klcv1alpha2.KeptnWorkload {
version, _ := getLabelOrAnnotation(&pod.ObjectMeta, apicommon.VersionAnnotation, apicommon.K8sRecommendedVersionAnnotations)
applicationName, _ := getLabelOrAnnotation(&pod.ObjectMeta, apicommon.AppAnnotation, apicommon.K8sRecommendedAppAnnotations)
var preDeploymentTasks []string
var postDeploymentTasks []string
var preDeploymentEvaluation []string
var postDeploymentEvaluation []string
if annotations, found := getLabelOrAnnotation(&pod.ObjectMeta, apicommon.PreDeploymentTaskAnnotation, ""); found {
preDeploymentTasks = strings.Split(annotations, ",")
}
if annotations, found := getLabelOrAnnotation(&pod.ObjectMeta, apicommon.PostDeploymentTaskAnnotation, ""); found {
postDeploymentTasks = strings.Split(annotations, ",")
}
if annotations, found := getLabelOrAnnotation(&pod.ObjectMeta, apicommon.PreDeploymentEvaluationAnnotation, ""); found {
preDeploymentEvaluation = strings.Split(annotations, ",")
}
if annotations, found := getLabelOrAnnotation(&pod.ObjectMeta, apicommon.PostDeploymentEvaluationAnnotation, ""); found {
postDeploymentEvaluation = strings.Split(annotations, ",")
}
// create TraceContext
// follow up with a Keptn propagator that JSON-encoded the OTel map into our own key
traceContextCarrier := propagation.MapCarrier{}
otel.GetTextMapPropagator().Inject(ctx, traceContextCarrier)
ownerRef := a.getOwnerReference(&pod.ObjectMeta)
return &klcv1alpha2.KeptnWorkload{
ObjectMeta: metav1.ObjectMeta{
Name: a.getWorkloadName(pod),
Namespace: namespace,
Annotations: traceContextCarrier,
OwnerReferences: []metav1.OwnerReference{
ownerRef,
},
},
Spec: klcv1alpha2.KeptnWorkloadSpec{
AppName: applicationName,
Version: version,
ResourceReference: klcv1alpha2.ResourceReference{UID: ownerRef.UID, Kind: ownerRef.Kind, Name: ownerRef.Name},
PreDeploymentTasks: preDeploymentTasks,
PostDeploymentTasks: postDeploymentTasks,
PreDeploymentEvaluations: preDeploymentEvaluation,
PostDeploymentEvaluations: postDeploymentEvaluation,
},
}
}
func (a *PodMutatingWebhook) generateAppCreationRequest(ctx context.Context, pod *corev1.Pod, namespace string) *klcv1alpha2.KeptnAppCreationRequest {
appName := a.getAppName(pod)
// create TraceContext
// follow up with a Keptn propagator that JSON-encoded the OTel map into our own key
traceContextCarrier := propagation.MapCarrier{}
otel.GetTextMapPropagator().Inject(ctx, traceContextCarrier)
return &klcv1alpha2.KeptnAppCreationRequest{
ObjectMeta: metav1.ObjectMeta{
Name: appName,
Namespace: namespace,
Annotations: traceContextCarrier,
},
Spec: klcv1alpha2.KeptnAppCreationRequestSpec{
AppName: appName,
},
}
}
func (a *PodMutatingWebhook) generateApp(ctx context.Context, pod *corev1.Pod, namespace string) *klcv1alpha2.KeptnApp {
version, _ := getLabelOrAnnotation(&pod.ObjectMeta, apicommon.VersionAnnotation, apicommon.K8sRecommendedVersionAnnotations)
appName := a.getAppName(pod)
// create TraceContext
// follow up with a Keptn propagator that JSON-encoded the OTel map into our own key
traceContextCarrier := propagation.MapCarrier{}
otel.GetTextMapPropagator().Inject(ctx, traceContextCarrier)
return &klcv1alpha2.KeptnApp{
ObjectMeta: metav1.ObjectMeta{
Name: appName,
Namespace: namespace,
Annotations: traceContextCarrier,
},
Spec: klcv1alpha2.KeptnAppSpec{
Version: version,
PreDeploymentTasks: []string{},
PostDeploymentTasks: []string{},
PreDeploymentEvaluations: []string{},
PostDeploymentEvaluations: []string{},
Workloads: []klcv1alpha2.KeptnWorkloadRef{
{
Name: appName,
Version: version,
},
},
},
}
}
func (a *PodMutatingWebhook) getWorkloadName(pod *corev1.Pod) string {
workloadName, _ := getLabelOrAnnotation(&pod.ObjectMeta, apicommon.WorkloadAnnotation, apicommon.K8sRecommendedWorkloadAnnotations)
applicationName, _ := getLabelOrAnnotation(&pod.ObjectMeta, apicommon.AppAnnotation, apicommon.K8sRecommendedAppAnnotations)
return strings.ToLower(applicationName + "-" + workloadName)
}
func (a *PodMutatingWebhook) getAppName(pod *corev1.Pod) string {
applicationName, _ := getLabelOrAnnotation(&pod.ObjectMeta, apicommon.AppAnnotation, apicommon.K8sRecommendedAppAnnotations)
return strings.ToLower(applicationName)
}
func (a *PodMutatingWebhook) getOwnerReference(resource *metav1.ObjectMeta) metav1.OwnerReference {
reference := metav1.OwnerReference{}
if len(resource.OwnerReferences) != 0 {
for _, owner := range resource.OwnerReferences {
if owner.Kind == "ReplicaSet" || owner.Kind == "Deployment" || owner.Kind == "StatefulSet" || owner.Kind == "DaemonSet" {
reference.UID = owner.UID
reference.Kind = owner.Kind
reference.Name = owner.Name
reference.APIVersion = owner.APIVersion
}
}
}
return reference
}
func getLabelOrAnnotation(resource *metav1.ObjectMeta, primaryAnnotation string, secondaryAnnotation string) (string, bool) {
if resource.Annotations[primaryAnnotation] != "" {
return resource.Annotations[primaryAnnotation], true
}
if resource.Labels[primaryAnnotation] != "" {
return resource.Labels[primaryAnnotation], true
}
if secondaryAnnotation == "" {
return "", false
}
if resource.Annotations[secondaryAnnotation] != "" {
return resource.Annotations[secondaryAnnotation], true
}
if resource.Labels[secondaryAnnotation] != "" {
return resource.Labels[secondaryAnnotation], true
}
return "", false
}
func setMapKey(myMap map[string]string, key, value string) {
if myMap == nil {
return
}
if value != "" {
myMap[key] = value
}
}