Skip to content

Commit

Permalink
chore: use an argument for webhook being enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
pmalek committed Jun 12, 2024
1 parent e67d935 commit b4bec06
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
6 changes: 3 additions & 3 deletions controller/controlplane/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ func (r *Reconciler) ensureWebhookResources(
}

log.Trace(logger, "ensuring admission webhook service", cp)
res, admissionWebhookService, err := r.ensureAdmissionWebhookService(ctx, logger, r.Client, cp)
res, admissionWebhookService, err := r.ensureAdmissionWebhookService(ctx, r.Client, cp, webhookEnabled)
if err != nil {
return "", res, fmt.Errorf("failed to ensure admission webhook service: %w", err)
}
Expand All @@ -496,7 +496,7 @@ func (r *Reconciler) ensureWebhookResources(
}

log.Trace(logger, "ensuring admission webhook certificate", cp)
res, admissionWebhookCertificateSecret, err := r.ensureAdmissionWebhookCertificateSecret(ctx, logger, cp, admissionWebhookService)
res, admissionWebhookCertificateSecret, err := r.ensureAdmissionWebhookCertificateSecret(ctx, cp, admissionWebhookService, webhookEnabled)
if err != nil {
return "", res, err
}
Expand All @@ -510,7 +510,7 @@ func (r *Reconciler) ensureWebhookResources(
}

log.Trace(logger, "ensuring admission webhook configuration", cp)
res, err = r.ensureValidatingWebhookConfiguration(ctx, cp, admissionWebhookCertificateSecret, admissionWebhookService)
res, err = r.ensureValidatingWebhookConfiguration(ctx, cp, admissionWebhookCertificateSecret, admissionWebhookService, webhookEnabled)
if err != nil {
return "", res, err
}
Expand Down
11 changes: 6 additions & 5 deletions controller/controlplane/controller_reconciler_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,9 +452,9 @@ func (r *Reconciler) ensureAdminMTLSCertificateSecret(
// ControlPlane's admission webhook.
func (r *Reconciler) ensureAdmissionWebhookCertificateSecret(
ctx context.Context,
logger logr.Logger,
cp *operatorv1beta1.ControlPlane,
admissionWebhookService *corev1.Service,
webhookEnabled bool,
) (
op.Result,
*corev1.Secret,
Expand All @@ -468,7 +468,7 @@ func (r *Reconciler) ensureAdmissionWebhookCertificateSecret(
matchingLabels := client.MatchingLabels{
consts.SecretUsedByServiceLabel: consts.ControlPlaneServiceKindWebhook,
}
if !isAdmissionWebhookEnabled(ctx, r.Client, logger, cp) {
if !webhookEnabled {
labels := k8sresources.GetManagedLabelForOwner(cp)
labels[consts.SecretUsedByServiceLabel] = consts.ControlPlaneServiceKindWebhook
secrets, err := k8sutils.ListSecretsForOwner(ctx, r.Client, cp.GetUID(), matchingLabels)
Expand Down Expand Up @@ -594,9 +594,9 @@ func (r *Reconciler) ensureOwnedValidatingWebhookConfigurationDeleted(ctx contex

func (r *Reconciler) ensureAdmissionWebhookService(
ctx context.Context,
logger logr.Logger,
cl client.Client,
controlPlane *operatorv1beta1.ControlPlane,
webhookEnabled bool,
) (op.Result, *corev1.Service, error) {
matchingLabels := k8sresources.GetManagedLabelForOwner(controlPlane)
matchingLabels[consts.ControlPlaneServiceLabel] = consts.ControlPlaneServiceKindWebhook
Expand All @@ -612,7 +612,7 @@ func (r *Reconciler) ensureAdmissionWebhookService(
return op.Noop, nil, fmt.Errorf("failed listing admission webhook Services for ControlPlane %s/%s: %w", controlPlane.Namespace, controlPlane.Name, err)
}

if !isAdmissionWebhookEnabled(ctx, cl, logger, controlPlane) {
if !webhookEnabled {
for _, svc := range services {
svc := svc
if err := cl.Delete(ctx, &svc); err != nil && !k8serrors.IsNotFound(err) {
Expand Down Expand Up @@ -673,6 +673,7 @@ func (r *Reconciler) ensureValidatingWebhookConfiguration(
cp *operatorv1beta1.ControlPlane,
certSecret *corev1.Secret,
webhookService *corev1.Service,
webhookEnabled bool,
) (op.Result, error) {
logger := log.GetLogger(ctx, "controlplane.ensureValidatingWebhookConfiguration", r.DevelopmentMode)

Expand All @@ -696,7 +697,7 @@ func (r *Reconciler) ensureValidatingWebhookConfiguration(
return op.Noop, errors.New("number of validatingWebhookConfigurations reduced")
}

if !isAdmissionWebhookEnabled(ctx, r.Client, logger, cp) {
if !webhookEnabled {
for _, webhookConfiguration := range validatingWebhookConfigurations {
if err := r.Client.Delete(ctx, &webhookConfiguration); err != nil && !k8serrors.IsNotFound(err) {
return op.Noop, fmt.Errorf("failed deleting ControlPlane admission webhook ValidatingWebhookConfiguration %s: %w", webhookConfiguration.Name, err)
Expand Down
10 changes: 5 additions & 5 deletions controller/controlplane/controller_reconciler_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,14 @@ func Test_ensureValidatingWebhookConfiguration(t *testing.T) {
},
}

res, err := r.ensureValidatingWebhookConfiguration(ctx, cp, certSecret, webhookSvc)
res, err := r.ensureValidatingWebhookConfiguration(ctx, cp, certSecret, webhookSvc, true)
require.NoError(t, err)
require.Equal(t, op.Created, res)

require.NoError(t, r.Client.List(ctx, &webhooks))
require.Len(t, webhooks.Items, 1)

res, err = r.ensureValidatingWebhookConfiguration(ctx, cp, certSecret, webhookSvc)
res, err = r.ensureValidatingWebhookConfiguration(ctx, cp, certSecret, webhookSvc, true)
require.NoError(t, err)
require.Equal(t, op.Noop, res)
},
Expand Down Expand Up @@ -146,14 +146,14 @@ func Test_ensureValidatingWebhookConfiguration(t *testing.T) {
},
}

res, err := r.ensureValidatingWebhookConfiguration(ctx, cp, certSecret, webhookSvc)
res, err := r.ensureValidatingWebhookConfiguration(ctx, cp, certSecret, webhookSvc, true)
require.NoError(t, err)
require.Equal(t, res, op.Created)

require.NoError(t, r.Client.List(ctx, &webhooks))
require.Len(t, webhooks.Items, 1, "webhook configuration should be created")

res, err = r.ensureValidatingWebhookConfiguration(ctx, cp, certSecret, webhookSvc)
res, err = r.ensureValidatingWebhookConfiguration(ctx, cp, certSecret, webhookSvc, true)
require.NoError(t, err)
require.Equal(t, res, op.Noop)

Expand All @@ -165,7 +165,7 @@ func Test_ensureValidatingWebhookConfiguration(t *testing.T) {
}

t.Log("running ensureValidatingWebhookConfiguration to enforce ObjectMeta")
res, err = r.ensureValidatingWebhookConfiguration(ctx, cp, certSecret, webhookSvc)
res, err = r.ensureValidatingWebhookConfiguration(ctx, cp, certSecret, webhookSvc, true)
require.NoError(t, err)
require.Equal(t, res, op.Updated)

Expand Down

0 comments on commit b4bec06

Please sign in to comment.