Skip to content

Commit 0695055

Browse files
amisevsksleshchenko
authored andcommitted
Fail controller startup when conflicting webhook definition present
Fail startup of controller if a webhook configuration exists and has a different clientConfig from the expected one (i.e. points at a service in a different namespace. This avoids issues of multiple controllers being installed on the cluster. Signed-off-by: Angel Misevski <[email protected]>
1 parent 918e8d6 commit 0695055

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

build/make/deploy.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ restart_webhook:
8787
$(K8S_CLI) rollout restart -n $(NAMESPACE) deployment/devworkspace-webhook-server
8888

8989
### uninstall: Removes the controller resources from the cluster
90-
uninstall: generate_deployment
90+
uninstall: _print_vars generate_deployment
9191
# It's safer to delete all workspaces before deleting the controller; otherwise we could
9292
# leave workspaces in a hanging state if we add finalizers.
9393
$(K8S_CLI) delete devworkspaces.workspace.devfile.io --all-namespaces --all --wait || true

pkg/webhook/init_cfg.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ import (
1616
"context"
1717
"fmt"
1818

19+
admv1 "k8s.io/api/admissionregistration/v1"
20+
apierrors "k8s.io/apimachinery/pkg/api/errors"
21+
"k8s.io/apimachinery/pkg/types"
1922
crclient "sigs.k8s.io/controller-runtime/pkg/client"
2023

2124
"github.com/devfile/devworkspace-operator/webhook/workspace"
22-
apierrors "k8s.io/apimachinery/pkg/api/errors"
2325
)
2426

2527
// WebhookCfgsInit initializes the webhook that denies everything until webhook server is started successfully
@@ -30,11 +32,25 @@ func WebhookCfgsInit(client crclient.Client, ctx context.Context, namespace stri
3032
if err != nil {
3133
if apierrors.IsAlreadyExists(err) {
3234
log.Info(fmt.Sprintf("Mutating webhooks configuration %s already exists", configuration.Name))
33-
return nil
35+
return checkExistingConfigForConflict(client, ctx, namespace)
3436
} else {
3537
return err
3638
}
3739
}
3840
log.Info(fmt.Sprintf("Created webhooks configuration %s", configuration.Name))
3941
return nil
4042
}
43+
44+
func checkExistingConfigForConflict(client crclient.Client, ctx context.Context, serviceNamespace string) error {
45+
existingCfg := &admv1.MutatingWebhookConfiguration{}
46+
err := client.Get(ctx, types.NamespacedName{Name: workspace.MutateWebhookCfgName}, existingCfg)
47+
if err != nil {
48+
return err
49+
}
50+
for _, webhook := range existingCfg.Webhooks {
51+
if webhook.ClientConfig.Service.Namespace != serviceNamespace {
52+
return fmt.Errorf("conflicting webhook definition found on cluster, webhook %s clientConfig points at namespace %s", webhook.Name, webhook.ClientConfig.Service.Namespace)
53+
}
54+
}
55+
return nil
56+
}

0 commit comments

Comments
 (0)