@@ -52,7 +52,41 @@ func (m *serviceMutator) MutateCreate(ctx context.Context, obj runtime.Object) (
52
52
}
53
53
54
54
func (m * serviceMutator ) MutateUpdate (ctx context.Context , obj runtime.Object , oldObj runtime.Object ) (runtime.Object , error ) {
55
- return obj , nil
55
+ // this mutator only cares about Service objects
56
+ newSvc , ok := obj .(* corev1.Service )
57
+ if ! ok {
58
+ return obj , nil
59
+ }
60
+
61
+ oldSvc , ok := oldObj .(* corev1.Service )
62
+ if ! ok {
63
+ return obj , nil
64
+ }
65
+
66
+ if newSvc .Spec .Type != corev1 .ServiceTypeLoadBalancer {
67
+ return obj , nil
68
+ }
69
+
70
+ // does the old Service object have spec.loadBalancerClass?
71
+ if oldSvc .Spec .LoadBalancerClass != nil && * oldSvc .Spec .LoadBalancerClass != "" {
72
+ // if so, let's inspect the incoming object for the same field
73
+
74
+ // does the new Service object lack spec.loadBalancerClass?
75
+ // if so, set it to the old value
76
+ // if yes, then leave it be because someone wanted it that way, let the user deal with the error
77
+ if newSvc .Spec .LoadBalancerClass == nil || * newSvc .Spec .LoadBalancerClass == "" {
78
+ newSvc .Spec .LoadBalancerClass = oldSvc .Spec .LoadBalancerClass
79
+
80
+ m .logger .Info ("preserved loadBalancerClass" , "service" , newSvc .Name , "loadBalancerClass" , * newSvc .Spec .LoadBalancerClass )
81
+ return newSvc , nil
82
+ }
83
+
84
+ m .logger .Info ("service already has loadBalancerClass, skipping" , "service" , newSvc .Name , "loadBalancerClass" , * newSvc .Spec .LoadBalancerClass )
85
+ return newSvc , nil
86
+ }
87
+
88
+ m .logger .Info ("service did not originally have a loadBalancerClass, skipping" , "service" , newSvc .Name )
89
+ return newSvc , nil
56
90
}
57
91
58
92
// +kubebuilder:webhook:path=/mutate-v1-service,mutating=true,failurePolicy=fail,groups="",resources=services,verbs=create,versions=v1,name=mservice.elbv2.k8s.aws,sideEffects=None,webhookVersions=v1,admissionReviewVersions=v1beta1
0 commit comments