-
Notifications
You must be signed in to change notification settings - Fork 473
/
references.go
246 lines (203 loc) · 9.67 KB
/
references.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
// Copyright 2021 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package references
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"strings"
appsv1 "k8s.io/api/apps/v1"
appsv1beta1 "k8s.io/api/apps/v1beta1"
appsv1beta2 "k8s.io/api/apps/v1beta2"
batchv1 "k8s.io/api/batch/v1"
batchv1beta1 "k8s.io/api/batch/v1beta1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
resourcesv1alpha1 "github.com/gardener/gardener/pkg/apis/resources/v1alpha1"
"github.com/gardener/gardener/pkg/utils"
)
const (
// LabelKeyGarbageCollectable is a constant for a label key on a Secret or ConfigMap resource which makes
// the GRM's garbage collector controller considering it for potential deletion in case it is unused by any
// workload.
LabelKeyGarbageCollectable = "resources.gardener.cloud/garbage-collectable-reference"
// LabelValueGarbageCollectable is a constant for a label value on a Secret or ConfigMap resource which
// makes the GRM's garbage collector controller considering it for potential deletion in case it is unused by any
// workload.
LabelValueGarbageCollectable = "true"
delimiter = "-"
// AnnotationKeyPrefix is a constant for the prefix used in annotations keys to indicate references to
// other resources.
AnnotationKeyPrefix = "reference.resources.gardener.cloud/"
// KindConfigMap is a constant for the 'configmap' kind used in reference annotations.
KindConfigMap = "configmap"
// KindSecret is a constant for the 'secret' kind used in reference annotations.
KindSecret = "secret"
)
// AnnotationKey computes a reference annotation key based on the given object kind and object name.
func AnnotationKey(kind, name string) string {
var (
h = sha256.Sum256([]byte(name))
sha256hex = hex.EncodeToString(h[:])
)
return AnnotationKeyPrefix + kind + delimiter + sha256hex[:8]
}
// KindFromAnnotationKey computes the object kind and object name based on the given reference annotation key. If
// the key is not valid then both return values will be empty.
func KindFromAnnotationKey(key string) string {
if !strings.HasPrefix(key, AnnotationKeyPrefix) {
return ""
}
var (
withoutPrefix = strings.TrimPrefix(key, AnnotationKeyPrefix)
split = strings.Split(withoutPrefix, delimiter)
)
if len(split) != 2 {
return ""
}
return split[0]
}
// InjectAnnotations injects annotations into the annotation maps based
// on the referenced ConfigMaps/Secrets appearing in:
// - pod template spec's `.volumes[]` or `.containers[].envFrom[]` or `.containers[].env[].valueFrom[]` or `.imagePullSecrets[]` lists
// - managed resource spec's `.secretRefs`
//
// Additional reference annotations can be specified via the variadic parameter (expected format is that returned by
// `AnnotationKey`).
func InjectAnnotations(obj runtime.Object, additional ...string) error {
switch o := obj.(type) {
case *corev1.Pod:
referenceAnnotations := computeAnnotations(o.Spec, additional...)
o.Annotations = mergeAnnotations(o.Annotations, referenceAnnotations)
case *appsv1.Deployment:
referenceAnnotations := computeAnnotations(o.Spec.Template.Spec, additional...)
o.Annotations = mergeAnnotations(o.Annotations, referenceAnnotations)
o.Spec.Template.Annotations = mergeAnnotations(o.Spec.Template.Annotations, referenceAnnotations)
case *appsv1beta2.Deployment:
referenceAnnotations := computeAnnotations(o.Spec.Template.Spec, additional...)
o.Annotations = mergeAnnotations(o.Annotations, referenceAnnotations)
o.Spec.Template.Annotations = mergeAnnotations(o.Spec.Template.Annotations, referenceAnnotations)
case *appsv1beta1.Deployment:
referenceAnnotations := computeAnnotations(o.Spec.Template.Spec, additional...)
o.Annotations = mergeAnnotations(o.Annotations, referenceAnnotations)
o.Spec.Template.Annotations = mergeAnnotations(o.Spec.Template.Annotations, referenceAnnotations)
case *appsv1.StatefulSet:
referenceAnnotations := computeAnnotations(o.Spec.Template.Spec, additional...)
o.Annotations = mergeAnnotations(o.Annotations, referenceAnnotations)
o.Spec.Template.Annotations = mergeAnnotations(o.Spec.Template.Annotations, referenceAnnotations)
case *appsv1beta2.StatefulSet:
referenceAnnotations := computeAnnotations(o.Spec.Template.Spec, additional...)
o.Annotations = mergeAnnotations(o.Annotations, referenceAnnotations)
o.Spec.Template.Annotations = mergeAnnotations(o.Spec.Template.Annotations, referenceAnnotations)
case *appsv1beta1.StatefulSet:
referenceAnnotations := computeAnnotations(o.Spec.Template.Spec, additional...)
o.Annotations = mergeAnnotations(o.Annotations, referenceAnnotations)
o.Spec.Template.Annotations = mergeAnnotations(o.Spec.Template.Annotations, referenceAnnotations)
case *appsv1.DaemonSet:
referenceAnnotations := computeAnnotations(o.Spec.Template.Spec, additional...)
o.Annotations = mergeAnnotations(o.Annotations, referenceAnnotations)
o.Spec.Template.Annotations = mergeAnnotations(o.Spec.Template.Annotations, referenceAnnotations)
case *appsv1beta2.DaemonSet:
referenceAnnotations := computeAnnotations(o.Spec.Template.Spec, additional...)
o.Annotations = mergeAnnotations(o.Annotations, referenceAnnotations)
o.Spec.Template.Annotations = mergeAnnotations(o.Spec.Template.Annotations, referenceAnnotations)
case *batchv1.Job:
referenceAnnotations := computeAnnotations(o.Spec.Template.Spec, additional...)
o.Annotations = mergeAnnotations(o.Annotations, referenceAnnotations)
o.Spec.Template.Annotations = mergeAnnotations(o.Spec.Template.Annotations, referenceAnnotations)
case *batchv1.CronJob:
referenceAnnotations := computeAnnotations(o.Spec.JobTemplate.Spec.Template.Spec, additional...)
o.Annotations = mergeAnnotations(o.Annotations, referenceAnnotations)
o.Spec.JobTemplate.Annotations = mergeAnnotations(o.Spec.JobTemplate.Annotations, referenceAnnotations)
o.Spec.JobTemplate.Spec.Template.Annotations = mergeAnnotations(o.Spec.JobTemplate.Spec.Template.Annotations, referenceAnnotations)
case *batchv1beta1.CronJob:
referenceAnnotations := computeAnnotations(o.Spec.JobTemplate.Spec.Template.Spec, additional...)
o.Annotations = mergeAnnotations(o.Annotations, referenceAnnotations)
o.Spec.JobTemplate.Annotations = mergeAnnotations(o.Spec.JobTemplate.Annotations, referenceAnnotations)
o.Spec.JobTemplate.Spec.Template.Annotations = mergeAnnotations(o.Spec.JobTemplate.Spec.Template.Annotations, referenceAnnotations)
case *resourcesv1alpha1.ManagedResource:
referenceAnnotations := computeAnnotationsFromLocalObjRefs(o.Spec.SecretRefs, KindSecret, additional...)
o.Annotations = mergeAnnotations(o.Annotations, referenceAnnotations)
default:
return fmt.Errorf("unhandled object type %T", obj)
}
return nil
}
func computeAnnotationsFromLocalObjRefs(refs []corev1.LocalObjectReference, kind string, additional ...string) map[string]string {
out := make(map[string]string, len(refs)+len(additional))
for _, ref := range refs {
out[AnnotationKey(kind, ref.Name)] = ref.Name
}
for _, v := range additional {
out[v] = ""
}
return out
}
func computeAnnotations(spec corev1.PodSpec, additional ...string) map[string]string {
out := make(map[string]string)
for _, container := range spec.Containers {
for _, env := range container.EnvFrom {
if env.SecretRef != nil {
out[AnnotationKey(KindSecret, env.SecretRef.Name)] = env.SecretRef.Name
}
if env.ConfigMapRef != nil {
out[AnnotationKey(KindConfigMap, env.ConfigMapRef.Name)] = env.ConfigMapRef.Name
}
}
for _, env := range container.Env {
if env.ValueFrom != nil && env.ValueFrom.SecretKeyRef != nil {
out[AnnotationKey(KindSecret, env.ValueFrom.SecretKeyRef.Name)] = env.ValueFrom.SecretKeyRef.Name
}
if env.ValueFrom != nil && env.ValueFrom.ConfigMapKeyRef != nil {
out[AnnotationKey(KindConfigMap, env.ValueFrom.ConfigMapKeyRef.Name)] = env.ValueFrom.ConfigMapKeyRef.Name
}
}
}
for _, volume := range spec.Volumes {
if volume.Secret != nil {
out[AnnotationKey(KindSecret, volume.Secret.SecretName)] = volume.Secret.SecretName
}
if volume.ConfigMap != nil {
out[AnnotationKey(KindConfigMap, volume.ConfigMap.Name)] = volume.ConfigMap.Name
}
if volume.Projected != nil {
for _, source := range volume.Projected.Sources {
if source.Secret != nil {
out[AnnotationKey(KindSecret, source.Secret.Name)] = source.Secret.Name
}
if source.ConfigMap != nil {
out[AnnotationKey(KindConfigMap, source.ConfigMap.Name)] = source.ConfigMap.Name
}
}
}
}
for _, imagePullSecret := range spec.ImagePullSecrets {
out[AnnotationKey(KindSecret, imagePullSecret.Name)] = imagePullSecret.Name
}
for _, v := range additional {
out[v] = ""
}
return out
}
func mergeAnnotations(oldAnnotations, newAnnotations map[string]string) map[string]string {
// Remove all existing annotations with the AnnotationKeyPrefix to make sure that no longer referenced resources
// do not remain in the annotations.
old := make(map[string]string, len(oldAnnotations))
for k, v := range oldAnnotations {
if !strings.HasPrefix(k, AnnotationKeyPrefix) {
old[k] = v
}
}
return utils.MergeStringMaps(old, newAnnotations)
}