Skip to content
This repository was archived by the owner on Oct 28, 2024. It is now read-only.

🐛 Fix webhook issue when there are multiple replication of controller #217

Closed
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
9 changes: 5 additions & 4 deletions virtualcluster/cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,12 @@ func main() {
}

if enableWebhook == true {
log.Info("setting up webhooks")
if err := webhook.AddToManager(mgr, mgrOpt.CertDir); err != nil {
log.Error(err, "unable to register webhooks to the manager")
os.Exit(1)
webhookHandler := &webhook.WebhookHandler{
Manager: mgr,
CertDir: mgrOpt.CertDir,
}

mgr.Add(webhookHandler)
}

// Start the Cmd
Expand Down
18 changes: 17 additions & 1 deletion virtualcluster/pkg/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,33 @@ limitations under the License.
package webhook

import (
"context"

"sigs.k8s.io/controller-runtime/pkg/manager"
)

// AddToManagerFuncs is a list of functions to add all Controllers to the Manager
var AddToManagerFuncs []func(manager.Manager, string) error

type WebhookHandler struct {
Manager manager.Manager
CertDir string
}

func (wh *WebhookHandler) Start(context context.Context) error {
addToManager(wh.Manager, wh.CertDir)
return nil
}

func (wh *WebhookHandler) NeedLeaderElection() bool {
return true
}

// AddToManager adds all Controllers to the Manager
// +kubebuilder:rbac:groups=admissionregistration.k8s.io,resources=mutatingwebhookconfigurations;validatingwebhookconfigurations,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups="",resources=secrets,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups="",resources=services,verbs=get;list;watch;create;update;patch;delete
func AddToManager(m manager.Manager, certDir string) error {
func addToManager(m manager.Manager, certDir string) error {
for _, f := range AddToManagerFuncs {
if err := f(m, certDir); err != nil {
return err
Expand Down