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

unregister existed configuration when pod restarted with the same name #98

Merged
merged 2 commits into from
Mar 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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/imports/imports_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
_ "k8s.io/api/admissionregistration/v1"
_ "k8s.io/api/apps/v1"
_ "k8s.io/api/core/v1"
_ "k8s.io/apimachinery/pkg/api/errors"
_ "k8s.io/apimachinery/pkg/api/resource"
_ "k8s.io/apimachinery/pkg/apis/meta/v1"
_ "k8s.io/apimachinery/pkg/runtime"
Expand Down
21 changes: 20 additions & 1 deletion internal/k8s/selfregister.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// Copyright (c) 2021 Doc.ai and/or its affiliates.
// Copyright (c) 2022 Doc.ai and/or its affiliates.
//
// Copyright (c) 2022 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand All @@ -24,6 +26,7 @@ import (

"go.uber.org/zap"
admissionv1 "k8s.io/api/admissionregistration/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
admissionregistrationv1 "k8s.io/client-go/kubernetes/typed/admissionregistration/v1"
Expand Down Expand Up @@ -56,6 +59,22 @@ func (a *AdmissionWebhookRegisterClient) Register(ctx context.Context, c *config
a.once.Do(a.initializeClient)
a.Logger.Infof("Starting to register MutatingWebhookConfiguration based config: %#v", c)
defer a.Logger.Infof("Register for config %#v is done", c)

// When node is restarted, all apps started in pods with old names.
// Then we already have configuration with the same name but it can't be reused
// because it contains different certs (certs are regenerated on every program restart)
_, errExisting := a.client.MutatingWebhookConfigurations().Get(ctx, c.Name, metav1.GetOptions{})
if errExisting == nil {
a.Logger.Infof("Found existing MutatingWebhookConfiguration %s, unregistering", c.Name)
err := a.Unregister(ctx, c)
if err != nil {
return err
}
} else if !apierrors.IsNotFound(errExisting) {
a.Logger.Errorf("Failed to search for existing MutatingWebhookConfiguration %s", c.Name)
return errExisting
}

path := "/mutate"
policy := admissionv1.Fail
sideEffects := admissionv1.SideEffectClassNone
Expand Down