Skip to content

Commit 02ea947

Browse files
Use annotations for the http port and path (#52)
* Use annotations for the http port and path --------- Signed-off-by: Michel Hollands <[email protected]> Co-authored-by: Arve Knudsen <[email protected]>
1 parent 716e332 commit 02ea947

File tree

4 files changed

+53
-20
lines changed

4 files changed

+53
-20
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## main / unreleased
44

5-
* [ENHANCEMENT] Add an `/admission/prepare-downscale` mutating admission webhook that prepares pods for shutdown before downscaling. #47
5+
* [ENHANCEMENT] Add an `/admission/prepare-downscale` mutating admission webhook that prepares pods for shutdown before downscaling. #47 #52
66
* [ENHANCEMENT] Update Go to `1.20.3`. #48
77
* [ENHANCEMENT] Updated dependencies, including: #49
88
* `github.com/k3d-io/k3d/v5` from `v5.4.7` to `v5.4.9`

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,9 @@ Upscaling requests or requests that don't change the number of replicas are appr
244244
For downscaling requests the following labels have to be present on the object:
245245

246246
- `grafana.com/prepare-downscale`
247+
248+
The following annotations also have to be present:
249+
247250
- `grafana.com/prepare-downscale-http-path`
248251
- `grafana.com/prepare-downscale-http-port`
249252

pkg/admission/prep_downscale.go

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ import (
1919
)
2020

2121
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"
2828
)
2929

3030
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
102102
return allowWarn(logger, fmt.Sprintf("unsupported type %T, allowing the change", o))
103103
}
104104

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+
105122
if lbls[PrepareDownscaleLabelKey] != PrepareDownscaleLabelValue {
106123
// Not labeled, nothing to do.
107124
return &v1.AdmissionResponse{Allowed: true}
108125
}
109126

110-
port := lbls[PrepareDownscalePortKey]
127+
port := annotations[PrepareDownscalePortAnnotationKey]
111128
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))
113130
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,
116133
)
117134
}
118135

119-
path := lbls[PrepareDownscalePathKey]
136+
path := annotations[PrepareDownscalePathAnnotationKey]
120137
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))
122139
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,
125142
)
126143
}
127144

@@ -234,3 +251,15 @@ func deny(msg string, args ...any) *v1.AdmissionResponse {
234251
},
235252
}
236253
}
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+
}

pkg/admission/prep_downscale_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,18 +146,18 @@ func testPrepDownscaleWebhook(t *testing.T, oldReplicas, newReplicas int, option
146146
path := "/prepare-downscale"
147147
oldParams := templateParams{
148148
Replicas: oldReplicas,
149-
DownScalePathKey: PrepareDownscalePathKey,
149+
DownScalePathKey: PrepareDownscalePathAnnotationKey,
150150
DownScalePath: path,
151-
DownScalePortKey: PrepareDownscalePortKey,
151+
DownScalePortKey: PrepareDownscalePortAnnotationKey,
152152
DownScalePort: u.Port(),
153153
DownScaleLabelKey: PrepareDownscaleLabelKey,
154154
}
155155

156156
newParams := templateParams{
157157
Replicas: newReplicas,
158-
DownScalePathKey: PrepareDownscalePathKey,
158+
DownScalePathKey: PrepareDownscalePathAnnotationKey,
159159
DownScalePath: path,
160-
DownScalePortKey: PrepareDownscalePortKey,
160+
DownScalePortKey: PrepareDownscalePortAnnotationKey,
161161
DownScalePort: u.Port(),
162162
DownScaleLabelKey: PrepareDownscaleLabelKey,
163163
}
@@ -269,9 +269,10 @@ metadata:
269269
name: web
270270
labels:
271271
{{.DownScaleLabelKey}}: "true"
272+
rollout-group: "ingester"
273+
annotations:
272274
{{.DownScalePathKey}}: {{.DownScalePath}}
273275
{{.DownScalePortKey}}: "{{.DownScalePort}}"
274-
rollout-group: "ingester"
275276
spec:
276277
selector:
277278
matchLabels:

0 commit comments

Comments
 (0)