Skip to content

Commit

Permalink
feat: add validating and mutating webhooks configuration watches to A…
Browse files Browse the repository at this point in the history
…dmissionPolicy and ClusterAdmissionPolicy controllers

Signed-off-by: Fabrizio Sestito <fabrizio.sestito@suse.com>
  • Loading branch information
fabriziosestito committed Nov 14, 2023
1 parent 68a47c0 commit 6c6021b
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 2 deletions.
48 changes: 47 additions & 1 deletion controllers/admissionpolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/kubewarden/kubewarden-controller/internal/pkg/constants"
"github.com/kubewarden/kubewarden-controller/internal/pkg/naming"
policiesv1 "github.com/kubewarden/kubewarden-controller/pkg/apis/policies/v1"
admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -88,8 +89,15 @@ func (r *AdmissionPolicyReconciler) SetupWithManager(mgr ctrl.Manager) error {
&policiesv1.PolicyServer{},
handler.EnqueueRequestsFromMapFunc(r.findAdmissionPoliciesForPolicyServer),
).
Watches(
&admissionregistrationv1.ValidatingWebhookConfiguration{},
handler.EnqueueRequestsFromMapFunc(r.findAdmissionPolicyForWebhookConfiguration),
).
Watches(
&admissionregistrationv1.MutatingWebhookConfiguration{},
handler.EnqueueRequestsFromMapFunc(r.findAdmissionPolicyForWebhookConfiguration),
).
Complete(r)

if err != nil {
return errors.Join(errors.New("failed enrolling controller with manager"), err)
}
Expand Down Expand Up @@ -148,3 +156,41 @@ func (r *AdmissionPolicyReconciler) findAdmissionPoliciesForPolicyServer(ctx con
}
return r.findAdmissionPoliciesForConfigMap(&configMap)
}

func (r *AdmissionPolicyReconciler) findAdmissionPolicyForWebhookConfiguration(_ context.Context, webhookConfiguration client.Object) []reconcile.Request {
if _, found := webhookConfiguration.GetLabels()["kubewarden"]; !found {
return []reconcile.Request{}
}

policyScope, found := webhookConfiguration.GetLabels()["kubewardenPolicyScope"]
if !found {
r.Log.Error(nil, "Found a webhook configuration without a scope label", "name", webhookConfiguration.GetName())
return []reconcile.Request{}
}

// Filter out ClusterAdmissionPolicies
if policyScope != "namespace" {
return []reconcile.Request{}
}

policyNamespace, found := webhookConfiguration.GetAnnotations()["kubewardenPolicyNamespace"]
if !found {
r.Log.Error(nil, "Found a webhook configuration without a namespace annotation", "name", webhookConfiguration.GetName())
return []reconcile.Request{}
}

policyName, found := webhookConfiguration.GetAnnotations()["kubewardenPolicyName"]
if !found {
r.Log.Error(nil, "Found webhook configuration without a policy name annotation", "name", webhookConfiguration.GetName())
return []reconcile.Request{}
}

return []reconcile.Request{
{
NamespacedName: client.ObjectKey{
Name: policyName,
Namespace: policyNamespace,
},
},
}
}
41 changes: 40 additions & 1 deletion controllers/clusteradmissionpolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/kubewarden/kubewarden-controller/internal/pkg/constants"
"github.com/kubewarden/kubewarden-controller/internal/pkg/naming"
policiesv1 "github.com/kubewarden/kubewarden-controller/pkg/apis/policies/v1"
admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -88,8 +89,15 @@ func (r *ClusterAdmissionPolicyReconciler) SetupWithManager(mgr ctrl.Manager) er
&policiesv1.PolicyServer{},
handler.EnqueueRequestsFromMapFunc(r.findClusterAdmissionPoliciesForPolicyServer),
).
Watches(
&admissionregistrationv1.ValidatingWebhookConfiguration{},
handler.EnqueueRequestsFromMapFunc(r.findClusterAdmissionPolicyForWebhookConfiguration),
).
Watches(
&admissionregistrationv1.MutatingWebhookConfiguration{},
handler.EnqueueRequestsFromMapFunc(r.findClusterAdmissionPolicyForWebhookConfiguration),
).
Complete(r)

if err != nil {
return errors.Join(errors.New("failed enrolling controller with manager"), err)
}
Expand Down Expand Up @@ -145,3 +153,34 @@ func (r *ClusterAdmissionPolicyReconciler) findClusterAdmissionPoliciesForPolicy
}
return r.findClusterAdmissionPoliciesForConfigMap(&configMap)
}

func (r *ClusterAdmissionPolicyReconciler) findClusterAdmissionPolicyForWebhookConfiguration(_ context.Context, webhookConfiguration client.Object) []reconcile.Request {
if _, found := webhookConfiguration.GetLabels()["kubewarden"]; !found {
return []reconcile.Request{}
}

policyScope, found := webhookConfiguration.GetLabels()["kubewardenPolicyScope"]
if !found {
r.Log.Error(nil, "Found a webhook configuration without a scope label", "name", webhookConfiguration.GetName())
return []reconcile.Request{}
}

// Filter out ClusterAdmissionPolicies
if policyScope != "cluster" {
return []reconcile.Request{}
}

policyName, found := webhookConfiguration.GetAnnotations()["kubewardenPolicyName"]
if !found {
r.Log.Error(nil, "Found webhook configuration without a policy name annotation", "name", webhookConfiguration.GetName())
return []reconcile.Request{}
}

return []reconcile.Request{
{
NamespacedName: client.ObjectKey{
Name: policyName,
},
},
}
}

0 comments on commit 6c6021b

Please sign in to comment.