Skip to content

Commit

Permalink
feat: Add global client, accessible via webhooks (#353)
Browse files Browse the repository at this point in the history
**Reason for Change**:
Add global client, accessible via webhooks

In this PR I also
Separate setup and execution phase of webhook server, allows us to
handle setup errors and setup context before attempting to run the
webhook server
  • Loading branch information
ishaansehgal99 authored Apr 16, 2024
1 parent 9e2ac24 commit e2f5e25
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
34 changes: 18 additions & 16 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package main

import (
"flag"
"github.com/azure/kaito/pkg/k8sclient"
"os"
"strconv"
"time"
Expand Down Expand Up @@ -98,8 +99,10 @@ func main() {
exitWithErrorFunc()
}

k8sclient.SetGlobalClient(mgr.GetClient())

if err = (&controllers.WorkspaceReconciler{
Client: mgr.GetClient(),
Client: k8sclient.GetGlobalClient(),
Log: log.Log.WithName("controllers").WithName("Workspace"),
Scheme: mgr.GetScheme(),
Recorder: mgr.GetEventRecorderFor("KAITO-Workspace-controller"),
Expand All @@ -120,21 +123,20 @@ func main() {

if enableWebhook {
klog.InfoS("starting webhook reconcilers")
go func() {
p, err := strconv.Atoi(os.Getenv(WebhookServicePort))
if err != nil {
klog.ErrorS(err, "unable to parse the webhook port number")
exitWithErrorFunc()
}
ctx := webhook.WithOptions(signals.NewContext(), webhook.Options{
ServiceName: os.Getenv(WebhookServiceName),
Port: p,
SecretName: "workspace-webhook-cert",
})
ctx = sharedmain.WithHealthProbesDisabled(ctx)
ctx = sharedmain.WithHADisabled(ctx)
sharedmain.MainWithConfig(ctx, "webhook", ctrl.GetConfigOrDie(), webhooks.NewWebhooks()...)
}()
p, err := strconv.Atoi(os.Getenv(WebhookServicePort))
if err != nil {
klog.ErrorS(err, "unable to parse the webhook port number")
exitWithErrorFunc()
}
ctx := webhook.WithOptions(signals.NewContext(), webhook.Options{
ServiceName: os.Getenv(WebhookServiceName),
Port: p,
SecretName: "workspace-webhook-cert",
})
ctx = sharedmain.WithHealthProbesDisabled(ctx)
ctx = sharedmain.WithHADisabled(ctx)
go sharedmain.MainWithConfig(ctx, "webhook", ctrl.GetConfigOrDie(), webhooks.NewWebhooks()...)

// wait 2 seconds to allow reconciling webhookconfiguration and service endpoint.
time.Sleep(2 * time.Second)
}
Expand Down
17 changes: 17 additions & 0 deletions pkg/k8sclient/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
package k8sclient

import (
"sigs.k8s.io/controller-runtime/pkg/client"
)

var Client client.Client

func SetGlobalClient(c client.Client) {
Client = c
}

func GetGlobalClient() client.Client {
return Client
}

0 comments on commit e2f5e25

Please sign in to comment.