@@ -19,12 +19,12 @@ import (
19
19
)
20
20
21
21
const (
22
- PrepareDownscalePathKey = "grafana.com/prepare-downscale-http-path"
23
- PrepareDownscalePortKey = "grafana.com/prepare-downscale-http-port"
24
- PrepareDownscaleLabelKey = "grafana.com/prepare-downscale"
25
- PrepareDownscaleLabelValue = "true"
26
- PrepareDownscaleWebhookPath = "/admission/prepare-downscale"
27
- RolloutGroupLabelKey = "rollout-group"
22
+ PrepareDownscalePathAnnotationKey = "grafana.com/prepare-downscale-http-path"
23
+ PrepareDownscalePortAnnotationKey = "grafana.com/prepare-downscale-http-port"
24
+ PrepareDownscaleLabelKey = "grafana.com/prepare-downscale"
25
+ PrepareDownscaleLabelValue = "true"
26
+ PrepareDownscaleWebhookPath = "/admission/prepare-downscale"
27
+ RolloutGroupLabelKey = "rollout-group"
28
28
)
29
29
30
30
func PrepareDownscale (ctx context.Context , logger log.Logger , ar v1.AdmissionReview , api * kubernetes.Clientset ) * v1.AdmissionResponse {
@@ -102,26 +102,43 @@ func prepareDownscale(ctx context.Context, logger log.Logger, ar v1.AdmissionRev
102
102
return allowWarn (logger , fmt .Sprintf ("unsupported type %T, allowing the change" , o ))
103
103
}
104
104
105
+ var annotations map [string ]string
106
+ switch o := oldObj .(type ) {
107
+ case * appsv1.Deployment :
108
+ annotations = o .Annotations
109
+ case * appsv1.StatefulSet :
110
+ annotations = o .Annotations
111
+ case * appsv1.ReplicaSet :
112
+ annotations = o .Annotations
113
+ case * autoscalingv1.Scale :
114
+ annotations , err = getResourceAnnotations (ctx , ar , api )
115
+ if err != nil {
116
+ return allowBecauseCannotGetResource (ar , logger , err )
117
+ }
118
+ default :
119
+ return allowWarn (logger , fmt .Sprintf ("unsupported type %T, allowing the change" , o ))
120
+ }
121
+
105
122
if lbls [PrepareDownscaleLabelKey ] != PrepareDownscaleLabelValue {
106
123
// Not labeled, nothing to do.
107
124
return & v1.AdmissionResponse {Allowed : true }
108
125
}
109
126
110
- port := lbls [ PrepareDownscalePortKey ]
127
+ port := annotations [ PrepareDownscalePortAnnotationKey ]
111
128
if port == "" {
112
- level .Warn (logger ).Log ("msg" , fmt .Sprintf ("downscale not allowed because the %v label is not set or empty" , PrepareDownscalePortKey ))
129
+ level .Warn (logger ).Log ("msg" , fmt .Sprintf ("downscale not allowed because the %v annotation is not set or empty" , PrepareDownscalePortAnnotationKey ))
113
130
return deny (
114
- "downscale of %s/%s in %s from %d to %d replicas is not allowed because the %v label is not set or empty." ,
115
- ar .Request .Resource .Resource , ar .Request .Name , ar .Request .Namespace , * oldReplicas , * newReplicas , PrepareDownscalePortKey ,
131
+ "downscale of %s/%s in %s from %d to %d replicas is not allowed because the %v annotation is not set or empty." ,
132
+ ar .Request .Resource .Resource , ar .Request .Name , ar .Request .Namespace , * oldReplicas , * newReplicas , PrepareDownscalePortAnnotationKey ,
116
133
)
117
134
}
118
135
119
- path := lbls [ PrepareDownscalePathKey ]
136
+ path := annotations [ PrepareDownscalePathAnnotationKey ]
120
137
if path == "" {
121
- level .Warn (logger ).Log ("msg" , fmt .Sprintf ("downscale not allowed because the %v label is not set or empty" , PrepareDownscalePathKey ))
138
+ level .Warn (logger ).Log ("msg" , fmt .Sprintf ("downscale not allowed because the %v annotation is not set or empty" , PrepareDownscalePathAnnotationKey ))
122
139
return deny (
123
- "downscale of %s/%s in %s from %d to %d replicas is not allowed because the %v label is not set or empty." ,
124
- ar .Request .Resource .Resource , ar .Request .Name , ar .Request .Namespace , * oldReplicas , * newReplicas , PrepareDownscalePathKey ,
140
+ "downscale of %s/%s in %s from %d to %d replicas is not allowed because the %v annotation is not set or empty." ,
141
+ ar .Request .Resource .Resource , ar .Request .Name , ar .Request .Namespace , * oldReplicas , * newReplicas , PrepareDownscalePathAnnotationKey ,
125
142
)
126
143
}
127
144
@@ -234,3 +251,15 @@ func deny(msg string, args ...any) *v1.AdmissionResponse {
234
251
},
235
252
}
236
253
}
254
+
255
+ func getResourceAnnotations (ctx context.Context , ar v1.AdmissionReview , api kubernetes.Interface ) (map [string ]string , error ) {
256
+ switch ar .Request .Resource .Resource {
257
+ case "statefulsets" :
258
+ obj , err := api .AppsV1 ().StatefulSets (ar .Request .Namespace ).Get (ctx , ar .Request .Name , metav1.GetOptions {})
259
+ if err != nil {
260
+ return nil , err
261
+ }
262
+ return obj .Annotations , nil
263
+ }
264
+ return nil , fmt .Errorf ("unsupported resource %s" , ar .Request .Resource .Resource )
265
+ }
0 commit comments