@@ -7,11 +7,15 @@ import (
77 "strings"
88
99 admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
10+ corev1 "k8s.io/api/core/v1"
1011 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12+ "k8s.io/apimachinery/pkg/types"
1113 "k8s.io/utils/ptr"
1214 "sigs.k8s.io/controller-runtime/pkg/client"
1315 "sigs.k8s.io/controller-runtime/pkg/client/apiutil"
1416 "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
17+
18+ "github.com/openmcp-project/controller-utils/pkg/collections/maps"
1519)
1620
1721func applyValidatingWebhook (ctx context.Context , opts * installOptions , obj client.Object ) error {
@@ -30,6 +34,7 @@ func applyValidatingWebhook(ctx context.Context, opts *installOptions, obj clien
3034 resource := strings .ToLower (gvk .Kind + "s" )
3135
3236 result , err := controllerutil .CreateOrUpdate (ctx , opts .remoteClient , cfg , func () error {
37+ cfg .Labels = maps .Merge (cfg .Labels , opts .managedLabels )
3338 webhook := admissionregistrationv1.ValidatingWebhook {
3439 Name : strings .ToLower (fmt .Sprintf ("v%s.%s" , gvk .Kind , gvk .Group )),
3540 FailurePolicy : ptr .To (admissionregistrationv1 .Fail ),
@@ -88,6 +93,7 @@ func applyMutatingWebhook(ctx context.Context, opts *installOptions, obj client.
8893 resource := strings .ToLower (gvk .Kind + "s" )
8994
9095 result , err := controllerutil .CreateOrUpdate (ctx , opts .remoteClient , cfg , func () error {
96+ cfg .Labels = maps .Merge (cfg .Labels , opts .managedLabels )
9197 webhook := admissionregistrationv1.MutatingWebhook {
9298 Name : strings .ToLower (fmt .Sprintf ("m%s.%s" , gvk .Kind , gvk .Group )),
9399 FailurePolicy : ptr .To (admissionregistrationv1 .Fail ),
@@ -128,3 +134,29 @@ func applyMutatingWebhook(ctx context.Context, opts *installOptions, obj client.
128134 log .Println ("Mutating webhook config" , cfg .Name , result )
129135 return err
130136}
137+
138+ func applyWebhookService (ctx context.Context , opts * installOptions ) error {
139+ svc := & corev1.Service {
140+ ObjectMeta : metav1.ObjectMeta {
141+ Name : opts .webhookService .Name ,
142+ Namespace : opts .webhookService .Namespace ,
143+ },
144+ }
145+
146+ result , err := controllerutil .CreateOrUpdate (ctx , opts .localClient , svc , func () error {
147+ svc .Labels = maps .Merge (svc .Labels , opts .managedLabels )
148+ svc .Spec .Selector = opts .managedService .SelectorLabels
149+ svc .Spec .Type = corev1 .ServiceTypeClusterIP
150+ svc .Spec .Ports = []corev1.ServicePort {
151+ {
152+ Name : "https" ,
153+ Protocol : corev1 .ProtocolTCP ,
154+ Port : opts .webhookServicePort ,
155+ TargetPort : opts .managedService .TargetPort ,
156+ },
157+ }
158+ return nil
159+ })
160+ log .Println ("Webhook service" , types.NamespacedName {Namespace : svc .Namespace , Name : svc .Name }.String (), result )
161+ return err
162+ }
0 commit comments