@@ -6,8 +6,11 @@ import (
66 "log"
77 "strings"
88
9+ "github.com/openmcp-project/controller-utils/pkg/collections/maps"
910 admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
11+ corev1 "k8s.io/api/core/v1"
1012 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
13+ "k8s.io/apimachinery/pkg/types"
1114 "k8s.io/utils/ptr"
1215 "sigs.k8s.io/controller-runtime/pkg/client"
1316 "sigs.k8s.io/controller-runtime/pkg/client/apiutil"
@@ -30,6 +33,7 @@ func applyValidatingWebhook(ctx context.Context, opts *installOptions, obj clien
3033 resource := strings .ToLower (gvk .Kind + "s" )
3134
3235 result , err := controllerutil .CreateOrUpdate (ctx , opts .remoteClient , cfg , func () error {
36+ cfg .Labels = maps .Merge (cfg .Labels , opts .managedLabels )
3337 webhook := admissionregistrationv1.ValidatingWebhook {
3438 Name : strings .ToLower (fmt .Sprintf ("v%s.%s" , gvk .Kind , gvk .Group )),
3539 FailurePolicy : ptr .To (admissionregistrationv1 .Fail ),
@@ -88,6 +92,7 @@ func applyMutatingWebhook(ctx context.Context, opts *installOptions, obj client.
8892 resource := strings .ToLower (gvk .Kind + "s" )
8993
9094 result , err := controllerutil .CreateOrUpdate (ctx , opts .remoteClient , cfg , func () error {
95+ cfg .Labels = maps .Merge (cfg .Labels , opts .managedLabels )
9196 webhook := admissionregistrationv1.MutatingWebhook {
9297 Name : strings .ToLower (fmt .Sprintf ("m%s.%s" , gvk .Kind , gvk .Group )),
9398 FailurePolicy : ptr .To (admissionregistrationv1 .Fail ),
@@ -128,3 +133,29 @@ func applyMutatingWebhook(ctx context.Context, opts *installOptions, obj client.
128133 log .Println ("Mutating webhook config" , cfg .Name , result )
129134 return err
130135}
136+
137+ func applyWebhookService (ctx context.Context , opts * installOptions ) error {
138+ svc := & corev1.Service {
139+ ObjectMeta : metav1.ObjectMeta {
140+ Name : opts .webhookService .Name ,
141+ Namespace : opts .webhookService .Namespace ,
142+ },
143+ }
144+
145+ result , err := controllerutil .CreateOrUpdate (ctx , opts .localClient , svc , func () error {
146+ svc .Labels = maps .Merge (svc .Labels , opts .managedLabels )
147+ svc .Spec .Selector = opts .managedService .SelectorLabels
148+ svc .Spec .Type = corev1 .ServiceTypeClusterIP
149+ svc .Spec .Ports = []corev1.ServicePort {
150+ {
151+ Name : "https" ,
152+ Protocol : corev1 .ProtocolTCP ,
153+ Port : opts .webhookServicePort ,
154+ TargetPort : opts .managedService .TargetPort ,
155+ },
156+ }
157+ return nil
158+ })
159+ log .Println ("Webhook service" , types.NamespacedName {Namespace : svc .Namespace , Name : svc .Name }.String (), result )
160+ return err
161+ }
0 commit comments