Skip to content

Commit ea440b5

Browse files
committed
webhook for STS
1 parent 6797380 commit ea440b5

9 files changed

Lines changed: 206 additions & 202 deletions

File tree

slice/cmd/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,8 +298,8 @@ func setupControllers(mgr ctrl.Manager, certsReady chan struct{}, activationTime
298298
setupLog.Error(err, "Unable to create webhook", "webhook", "LeaderWorkerSet")
299299
os.Exit(1)
300300
}
301-
if err := webhooks.SetupPodWebhookWithManager(mgr); err != nil {
302-
setupLog.Error(err, "Unable to create webhook", "webhook", "Pod")
301+
if err := webhooks.SetupStatefulSetWebhookWithManager(mgr); err != nil {
302+
setupLog.Error(err, "Unable to create webhook", "webhook", "StatefulSet")
303303
os.Exit(1)
304304
}
305305

slice/config/dev/manager_config_patch.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
value: --zap-devel
55
- op: add
66
path: /spec/template/spec/containers/0/args/0
7-
value: --zap-log-level=3
7+
value: --zap-log-level=5
88
- op: add
99
path: /spec/template/spec/containers/0/args/0
1010
value: --retry-delay-on-slice-failure=0s

slice/config/webhook/kustomization.yaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,5 @@ resources:
22
- manifests.yaml
33
- service.yaml
44

5-
patches:
6-
- path: pod_webhook_patch.yaml
7-
85
configurations:
96
- kustomizeconfig.yaml

slice/config/webhook/manifests.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,16 @@ webhooks:
6767
service:
6868
name: webhook-service
6969
namespace: system
70-
path: /mutate--v1-pod
70+
path: /mutate-apps-v1-statefulset
7171
failurePolicy: Fail
72-
name: mpod.slice-controller.kb.io
72+
name: mstatefulset.slice-controller.kb.io
7373
rules:
7474
- apiGroups:
75-
- ""
75+
- apps
7676
apiVersions:
7777
- v1
7878
operations:
7979
- CREATE
8080
resources:
81-
- pods
81+
- statefulsets
8282
sideEffects: None

slice/config/webhook/pod_webhook_patch.yaml

Lines changed: 0 additions & 17 deletions
This file was deleted.

slice/internal/webhooks/pod_webhook.go

Lines changed: 0 additions & 60 deletions
This file was deleted.

slice/internal/webhooks/pod_webhook_test.go

Lines changed: 0 additions & 115 deletions
This file was deleted.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
Copyright The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package webhooks
18+
19+
import (
20+
"context"
21+
22+
appsv1 "k8s.io/api/apps/v1"
23+
ctrl "sigs.k8s.io/controller-runtime"
24+
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
25+
26+
"tpu-slice-controller/internal/core"
27+
)
28+
29+
type StatefulSetWebhook struct{}
30+
31+
func SetupStatefulSetWebhookWithManager(mgr ctrl.Manager) error {
32+
return ctrl.NewWebhookManagedBy(mgr, &appsv1.StatefulSet{}).
33+
WithDefaulter(&StatefulSetWebhook{}).
34+
Complete()
35+
}
36+
37+
// +kubebuilder:webhook:path=/mutate-apps-v1-statefulset,mutating=true,failurePolicy=fail,sideEffects=None,groups=apps,resources=statefulsets,verbs=create,versions=v1,name=mstatefulset.slice-controller.kb.io,admissionReviewVersions=v1
38+
39+
var _ admission.Defaulter[*appsv1.StatefulSet] = &StatefulSetWebhook{}
40+
41+
func (r *StatefulSetWebhook) Default(ctx context.Context, sts *appsv1.StatefulSet) error {
42+
log := ctrl.LoggerFrom(ctx).WithName("statefulset-webhook")
43+
log.V(5).Info("Defaulting StatefulSet")
44+
45+
if !core.IsRelevantPodTemplateSpec(sts.Spec.Template) {
46+
log.V(5).Info("Skipping non-TPUv7 StatefulSet")
47+
return nil
48+
}
49+
50+
tpuTopology := core.GetTPUTopology(sts.Spec.Template)
51+
if sts.Spec.Template.Spec.NodeSelector == nil {
52+
sts.Spec.Template.Spec.NodeSelector = make(map[string]string)
53+
}
54+
if _, ok := sts.Spec.Template.Spec.NodeSelector[core.TPUTopologyAnnotation]; !ok {
55+
sts.Spec.Template.Spec.NodeSelector[core.TPUTopologyAnnotation] = tpuTopology
56+
}
57+
58+
return nil
59+
}

0 commit comments

Comments
 (0)