-
Notifications
You must be signed in to change notification settings - Fork 108
/
pod_annotation.go
145 lines (127 loc) · 5.77 KB
/
pod_annotation.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
package handlers
import (
"context"
"log"
argov1alpha1 "github.com/argoproj/argo-rollouts/pkg/apis/rollouts/v1alpha1"
"github.com/go-logr/logr"
apicommon "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1/common"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)
type PodAnnotationHandler struct {
Client client.Client
Log logr.Logger
}
func (p *PodAnnotationHandler) IsAnnotated(ctx context.Context, req *admission.Request, pod *corev1.Pod) bool {
podIsAnnotated := isPodAnnotated(pod)
if !podIsAnnotated {
p.Log.Info("Pod is not annotated, check for parent annotations...")
podIsAnnotated = p.copyAnnotationsIfParentAnnotated(ctx, req, pod)
}
return podIsAnnotated
}
func (p *PodAnnotationHandler) copyAnnotationsIfParentAnnotated(ctx context.Context, req *admission.Request, pod *corev1.Pod) bool {
podOwner := GetOwnerReference(&pod.ObjectMeta)
if podOwner.UID == "" {
return false
}
switch podOwner.Kind {
case "ReplicaSet":
rs := &appsv1.ReplicaSet{}
if err := p.Client.Get(ctx, types.NamespacedName{Namespace: req.Namespace, Name: podOwner.Name}, rs); err != nil {
return false
}
rsOwner := GetOwnerReference(&rs.ObjectMeta)
if rsOwner.UID == "" {
return false
}
if rsOwner.Kind == "Rollout" {
ro := &argov1alpha1.Rollout{}
objectContainerMetaData := p.fetchParent(ctx, types.NamespacedName{Name: rsOwner.Name, Namespace: req.Namespace}, ro)
return copyResourceLabelsIfPresent(objectContainerMetaData, pod)
}
dp := &appsv1.Deployment{}
objectContainerMetaData := p.fetchParent(ctx, types.NamespacedName{Name: rsOwner.Name, Namespace: req.Namespace}, dp)
return copyResourceLabelsIfPresent(objectContainerMetaData, pod)
case "StatefulSet":
sts := &appsv1.StatefulSet{}
objectContainerMetaData := p.fetchParent(ctx, types.NamespacedName{Name: podOwner.Name, Namespace: req.Namespace}, sts)
return copyResourceLabelsIfPresent(objectContainerMetaData, pod)
case "DaemonSet":
ds := &appsv1.DaemonSet{}
objectContainerMetaData := p.fetchParent(ctx, types.NamespacedName{Name: podOwner.Name, Namespace: req.Namespace}, ds)
return copyResourceLabelsIfPresent(objectContainerMetaData, pod)
default:
return false
}
}
func (p *PodAnnotationHandler) fetchParent(ctx context.Context, name types.NamespacedName, objectContainer client.Object) *metav1.ObjectMeta {
if err := p.Client.Get(ctx, name, objectContainer); err != nil {
p.Log.Info("Could not find pod parent")
return nil
}
return &metav1.ObjectMeta{
Labels: objectContainer.GetLabels(),
Annotations: objectContainer.GetAnnotations(),
}
}
func copyResourceLabelsIfPresent(sourceResource *metav1.ObjectMeta, targetPod *corev1.Pod) bool {
if sourceResource == nil {
return false
}
var workloadName, appName, version, preDeploymentChecks, postDeploymentChecks, preEvaluationChecks, postEvaluationChecks string
var gotWorkloadName, gotVersion bool
initEmptyAnnotations(&targetPod.ObjectMeta, 7)
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, "")
containerName, _ := GetLabelOrAnnotation(sourceResource, apicommon.ContainerNameAnnotation, "")
metadata, _ := GetLabelOrAnnotation(sourceResource, apicommon.MetadataAnnotation, "")
if gotWorkloadName {
setMapKey(targetPod.Annotations, apicommon.WorkloadAnnotation, workloadName)
if !gotVersion {
version, err := calculateVersion(targetPod, containerName)
if err != nil {
log.Println(err)
return false
}
setMapKey(targetPod.Annotations, apicommon.VersionAnnotation, version)
} 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)
setMapKey(targetPod.Annotations, apicommon.MetadataAnnotation, metadata)
return true
}
return false
}
func isPodAnnotated(pod *corev1.Pod) bool {
containerName, _ := GetLabelOrAnnotation(&pod.ObjectMeta, apicommon.ContainerNameAnnotation, "")
_, gotWorkloadAnnotation := GetLabelOrAnnotation(&pod.ObjectMeta, apicommon.WorkloadAnnotation, apicommon.K8sRecommendedWorkloadAnnotations)
_, gotVersionAnnotation := GetLabelOrAnnotation(&pod.ObjectMeta, apicommon.VersionAnnotation, apicommon.K8sRecommendedVersionAnnotations)
if gotWorkloadAnnotation {
if !gotVersionAnnotation {
initEmptyAnnotations(&pod.ObjectMeta, 1)
version, err := calculateVersion(pod, containerName)
if err != nil {
log.Println(err)
} else {
pod.Annotations[apicommon.VersionAnnotation] = version
}
}
return true
}
return false
}