Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add group annotation to WebhookConfiguration and filter watch requests #893

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions internal/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const (
WebhookConfigurationPolicyScopeLabelKey = "kubewardenPolicyScope"
WebhookConfigurationPolicyNameAnnotationKey = "kubewardenPolicyName"
WebhookConfigurationPolicyNamespaceAnnotationKey = "kubewardenPolicyNamespace"
WebhookConfigurationPolicyGroupAnnotationKey = "kubewardenPolicyGroup"

// Scope.
NamespacePolicyScope = "namespace"
Expand Down
2 changes: 1 addition & 1 deletion internal/controller/admissionpolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,5 @@ func (r *AdmissionPolicyReconciler) findAdmissionPoliciesForPod(ctx context.Cont
}

func (r *AdmissionPolicyReconciler) findAdmissionPolicyForWebhookConfiguration(_ context.Context, webhookConfiguration client.Object) []reconcile.Request {
return findPolicyForWebhookConfiguration(webhookConfiguration, r.Log)
return findPolicyForWebhookConfiguration(webhookConfiguration, false, r.Log)
}
2 changes: 1 addition & 1 deletion internal/controller/admissionpolicygroup_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,5 @@ func (r *AdmissionPolicyGroupReconciler) findAdmissionPoliciesForPod(ctx context
}

func (r *AdmissionPolicyGroupReconciler) findAdmissionPolicyForWebhookConfiguration(_ context.Context, webhookConfiguration client.Object) []reconcile.Request {
return findPolicyForWebhookConfiguration(webhookConfiguration, r.Log)
return findPolicyForWebhookConfiguration(webhookConfiguration, true, r.Log)
}
2 changes: 1 addition & 1 deletion internal/controller/clusteradmissionpolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,5 @@ func (r *ClusterAdmissionPolicyReconciler) findClusterAdmissionPoliciesForPod(ct
}

func (r *ClusterAdmissionPolicyReconciler) findClusterAdmissionPolicyForWebhookConfiguration(_ context.Context, webhookConfiguration client.Object) []reconcile.Request {
return findClusterPolicyForWebhookConfiguration(webhookConfiguration, r.Log)
return findClusterPolicyForWebhookConfiguration(webhookConfiguration, false, r.Log)
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,5 @@ func (r *ClusterAdmissionPolicyGroupReconciler) findClusterAdmissionPoliciesForP
}

func (r *ClusterAdmissionPolicyGroupReconciler) findClusterAdmissionPolicyForWebhookConfiguration(_ context.Context, webhookConfiguration client.Object) []reconcile.Request {
return findClusterPolicyForWebhookConfiguration(webhookConfiguration, r.Log)
return findClusterPolicyForWebhookConfiguration(webhookConfiguration, true, r.Log)
}
18 changes: 16 additions & 2 deletions internal/controller/policy_subreconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@
return findClusterPoliciesForConfigMap(&configMap)
}

func findClusterPolicyForWebhookConfiguration(webhookConfiguration client.Object, log logr.Logger) []reconcile.Request {
func findClusterPolicyForWebhookConfiguration(webhookConfiguration client.Object, isGroup bool, log logr.Logger) []reconcile.Request {
// Pre v1.16.0
_, kubwardenLabelExists := webhookConfiguration.GetLabels()["kubewarden"]
// From v1.16.0 on we are using the recommended label "app.kubernetes.io/part-of"
Expand All @@ -358,6 +358,13 @@
return []reconcile.Request{}
}

if isGroup {
policyGroupAnnotation := webhookConfiguration.GetAnnotations()[constants.WebhookConfigurationPolicyGroupAnnotationKey]
if policyGroupAnnotation != "true" {

Check failure on line 363 in internal/controller/policy_subreconciler.go

View workflow job for this annotation

GitHub Actions / Golangci-lint

string `true` has 5 occurrences, make it a constant (goconst)
return []reconcile.Request{}
}
}

policyScope, found := webhookConfiguration.GetLabels()[constants.WebhookConfigurationPolicyScopeLabelKey]
if !found {
log.Info("Found a webhook configuration without a scope label, reconciling it",
Expand Down Expand Up @@ -386,7 +393,7 @@
}
}

func findPolicyForWebhookConfiguration(webhookConfiguration client.Object, log logr.Logger) []reconcile.Request {
func findPolicyForWebhookConfiguration(webhookConfiguration client.Object, isGroup bool, log logr.Logger) []reconcile.Request {
// Pre v1.16.0
_, kubwardenLabelExists := webhookConfiguration.GetLabels()["kubewarden"]
// From v1.16.0 on we are using the recommended label "app.kubernetes.io/part-of"
Expand All @@ -395,6 +402,13 @@
return []reconcile.Request{}
}

if isGroup {
policyGroupAnnotation := webhookConfiguration.GetAnnotations()[constants.WebhookConfigurationPolicyGroupAnnotationKey]
if policyGroupAnnotation != "true" {
return []reconcile.Request{}
}
}

policyScope, found := webhookConfiguration.GetLabels()[constants.WebhookConfigurationPolicyScopeLabelKey]
if !found {
log.Info("Found a webhook configuration without a scope label, reconciling it", "name", webhookConfiguration.GetName())
Expand Down
3 changes: 3 additions & 0 deletions internal/controller/policy_subreconciler_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

//+kubebuilder:rbac:groups=admissionregistration.k8s.io,resources=validatingwebhookconfigurations,verbs=create;delete;list;patch;watch

//nolint:dupl // This function is similar to the other reconcileMutatingWebhookConfiguration

Check failure on line 21 in internal/controller/policy_subreconciler_webhook.go

View workflow job for this annotation

GitHub Actions / Golangci-lint

directive `//nolint:dupl // This function is similar to the other reconcileMutatingWebhookConfiguration` is unused for linter "dupl" (nolintlint)
func (r *policySubReconciler) reconcileValidatingWebhookConfiguration(
ctx context.Context,
policy policiesv1.Policy,
Expand Down Expand Up @@ -60,6 +60,9 @@
constants.WebhookConfigurationPolicyNameAnnotationKey: policy.GetName(),
constants.WebhookConfigurationPolicyNamespaceAnnotationKey: policy.GetNamespace(),
}
if _, ok := policy.(policiesv1.PolicyGroup); ok {
webhook.Annotations[constants.WebhookConfigurationPolicyGroupAnnotationKey] = "true"
}
webhook.Webhooks = []admissionregistrationv1.ValidatingWebhook{
{
Name: policy.GetUniqueName() + ".kubewarden.admission",
Expand Down Expand Up @@ -108,7 +111,7 @@

//+kubebuilder:rbac:groups=admissionregistration.k8s.io,resources=mutatingwebhookconfigurations,verbs=create;delete;list;patch;watch

//nolint:dupl // This function is similar to the other reconcileValidatingWebhookConfiguration

Check failure on line 114 in internal/controller/policy_subreconciler_webhook.go

View workflow job for this annotation

GitHub Actions / Golangci-lint

directive `//nolint:dupl // This function is similar to the other reconcileValidatingWebhookConfiguration` is unused for linter "dupl" (nolintlint)
func (r *policySubReconciler) reconcileMutatingWebhookConfiguration(
ctx context.Context,
policy policiesv1.Policy,
Expand Down
Loading