From 873127e3b04f16aedbbfb36e43876f2419242507 Mon Sep 17 00:00:00 2001 From: Nick Turner Date: Wed, 22 Mar 2023 15:57:38 +0000 Subject: [PATCH] Export WebhookHandler struct because some CCMs use Run directly --- .../src/k8s.io/cloud-provider/app/builder.go | 2 +- .../cloud-provider/app/controllermanager.go | 4 +- .../src/k8s.io/cloud-provider/app/webhooks.go | 44 +++++++++---------- .../cloud-provider/app/webhooks_test.go | 28 ++++++------ 4 files changed, 39 insertions(+), 39 deletions(-) diff --git a/staging/src/k8s.io/cloud-provider/app/builder.go b/staging/src/k8s.io/cloud-provider/app/builder.go index 35806c3caee..6a0ae472907 100644 --- a/staging/src/k8s.io/cloud-provider/app/builder.go +++ b/staging/src/k8s.io/cloud-provider/app/builder.go @@ -137,7 +137,7 @@ func (cb *CommandBuilder) BuildCommand() *cobra.Command { completedConfig := config.Complete() cloud := cb.cloudInitializer(completedConfig) controllerInitializers := ConstructControllerInitializers(cb.controllerInitFuncConstructors, completedConfig, cloud) - webhooks := newWebhookHandlers(cb.webhookConfigs, completedConfig, cloud) + webhooks := NewWebhookHandlers(cb.webhookConfigs, completedConfig, cloud) if err := Run(completedConfig, cloud, controllerInitializers, webhooks, cb.stopCh); err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) diff --git a/staging/src/k8s.io/cloud-provider/app/controllermanager.go b/staging/src/k8s.io/cloud-provider/app/controllermanager.go index c87e6aa67b4..295a420a8b1 100644 --- a/staging/src/k8s.io/cloud-provider/app/controllermanager.go +++ b/staging/src/k8s.io/cloud-provider/app/controllermanager.go @@ -106,7 +106,7 @@ the cloud specific control loops shipped with Kubernetes.`, cloud := cloudInitializer(completedConfig) controllerInitializers := ConstructControllerInitializers(controllerInitFuncConstructors, completedConfig, cloud) - if err := Run(completedConfig, cloud, controllerInitializers, make(map[string]webhookHandler), stopCh); err != nil { + if err := Run(completedConfig, cloud, controllerInitializers, make(map[string]WebhookHandler), stopCh); err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) return err } @@ -161,7 +161,7 @@ the cloud specific control loops shipped with Kubernetes.`, } // Run runs the ExternalCMServer. This should never exit. -func Run(c *cloudcontrollerconfig.CompletedConfig, cloud cloudprovider.Interface, controllerInitializers map[string]InitFunc, webhooks map[string]webhookHandler, +func Run(c *cloudcontrollerconfig.CompletedConfig, cloud cloudprovider.Interface, controllerInitializers map[string]InitFunc, webhooks map[string]WebhookHandler, stopCh <-chan struct{}) error { // To help debugging, immediately log version klog.Infof("Version: %+v", version.Get()) diff --git a/staging/src/k8s.io/cloud-provider/app/webhooks.go b/staging/src/k8s.io/cloud-provider/app/webhooks.go index e3a5a3aeb0b..523fe6ef375 100644 --- a/staging/src/k8s.io/cloud-provider/app/webhooks.go +++ b/staging/src/k8s.io/cloud-provider/app/webhooks.go @@ -58,29 +58,29 @@ type WebhookConfig struct { AdmissionHandler func(*admissionv1.AdmissionRequest) (*admissionv1.AdmissionResponse, error) } -type webhookHandler struct { - name string - path string +type WebhookHandler struct { + Name string + Path string http.Handler - admissionHandler func(*admissionv1.AdmissionRequest) (*admissionv1.AdmissionResponse, error) - completedConfig *config.CompletedConfig - cloud cloudprovider.Interface + AdmissionHandler func(*admissionv1.AdmissionRequest) (*admissionv1.AdmissionResponse, error) + CompletedConfig *config.CompletedConfig + Cloud cloudprovider.Interface } -func newWebhookHandlers(webhookConfigs map[string]WebhookConfig, completedConfig *config.CompletedConfig, cloud cloudprovider.Interface) map[string]webhookHandler { - webhookHandlers := make(map[string]webhookHandler) +func NewWebhookHandlers(webhookConfigs map[string]WebhookConfig, completedConfig *config.CompletedConfig, cloud cloudprovider.Interface) map[string]WebhookHandler { + webhookHandlers := make(map[string]WebhookHandler) for name, config := range webhookConfigs { if !genericcontrollermanager.IsControllerEnabled(name, WebhooksDisabledByDefault, completedConfig.ComponentConfig.Webhook.Webhooks) { klog.Warningf("Webhook %q is disabled", name) continue } klog.Infof("Webhook enabled: %q", name) - webhookHandlers[name] = webhookHandler{ - name: name, - path: config.Path, - admissionHandler: config.AdmissionHandler, - completedConfig: completedConfig, - cloud: cloud, + webhookHandlers[name] = WebhookHandler{ + Name: name, + Path: config.Path, + AdmissionHandler: config.AdmissionHandler, + CompletedConfig: completedConfig, + Cloud: cloud, } } return webhookHandlers @@ -91,17 +91,17 @@ func WebhookNames(webhooks map[string]WebhookConfig) []string { return ret.List() } -func newHandler(webhooks map[string]webhookHandler) *mux.PathRecorderMux { +func newHandler(webhooks map[string]WebhookHandler) *mux.PathRecorderMux { mux := mux.NewPathRecorderMux("controller-manager-webhook") for _, handler := range webhooks { - mux.Handle(handler.path, handler) + mux.Handle(handler.Path, handler) } return mux } -func (h webhookHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { +func (h WebhookHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { ctx := context.Background() klog.Infof("Received validation request: %q", r.RequestURI) @@ -116,14 +116,14 @@ func (h webhookHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { latency := time.Since(start) if statusCode != 0 { - recordRequestTotal(ctx, strconv.Itoa(statusCode), h.name) - recordRequestLatency(ctx, strconv.Itoa(statusCode), h.name, latency.Seconds()) + recordRequestTotal(ctx, strconv.Itoa(statusCode), h.Name) + recordRequestLatency(ctx, strconv.Itoa(statusCode), h.Name, latency.Seconds()) return } if err != nil { - recordRequestTotal(ctx, "", h.name) - recordRequestLatency(ctx, "", h.name, latency.Seconds()) + recordRequestTotal(ctx, "", h.Name) + recordRequestLatency(ctx, "", h.Name, latency.Seconds()) } }() @@ -135,7 +135,7 @@ func (h webhookHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { return } - admissionResponse, err = h.admissionHandler(in.Request) + admissionResponse, err = h.AdmissionHandler(in.Request) if err != nil { e := fmt.Sprintf("error generating admission response: %v", err) klog.Errorf(e) diff --git a/staging/src/k8s.io/cloud-provider/app/webhooks_test.go b/staging/src/k8s.io/cloud-provider/app/webhooks_test.go index d86bef1b042..2487f26a966 100644 --- a/staging/src/k8s.io/cloud-provider/app/webhooks_test.go +++ b/staging/src/k8s.io/cloud-provider/app/webhooks_test.go @@ -38,7 +38,7 @@ func TestWebhookEnableDisable(t *testing.T) { desc string webhookConfigs map[string]WebhookConfig completedConfig *config.CompletedConfig - expected map[string]webhookHandler + expected map[string]WebhookHandler }{ { "Webhooks Enabled", @@ -47,9 +47,9 @@ func TestWebhookEnableDisable(t *testing.T) { "webhook-b": {Path: "/path/b", AdmissionHandler: noOpAdmissionHandler}, }, newConfig(cpconfig.WebhookConfiguration{Webhooks: []string{"webhook-a", "webhook-b"}}), - map[string]webhookHandler{ - "webhook-a": {path: "/path/a", admissionHandler: noOpAdmissionHandler}, - "webhook-b": {path: "/path/b", admissionHandler: noOpAdmissionHandler}, + map[string]WebhookHandler{ + "webhook-a": {Path: "/path/a", AdmissionHandler: noOpAdmissionHandler}, + "webhook-b": {Path: "/path/b", AdmissionHandler: noOpAdmissionHandler}, }, }, { @@ -59,8 +59,8 @@ func TestWebhookEnableDisable(t *testing.T) { "webhook-b": {Path: "/path/b", AdmissionHandler: noOpAdmissionHandler}, }, newConfig(cpconfig.WebhookConfiguration{Webhooks: []string{"webhook-a"}}), - map[string]webhookHandler{ - "webhook-a": {path: "/path/a", admissionHandler: noOpAdmissionHandler}, + map[string]WebhookHandler{ + "webhook-a": {Path: "/path/a", AdmissionHandler: noOpAdmissionHandler}, }, }, { @@ -70,8 +70,8 @@ func TestWebhookEnableDisable(t *testing.T) { "webhook-b": {Path: "/path/b", AdmissionHandler: noOpAdmissionHandler}, }, newConfig(cpconfig.WebhookConfiguration{Webhooks: []string{"webhook-a", "-webhook-b"}}), - map[string]webhookHandler{ - "webhook-a": {path: "/path/a", admissionHandler: noOpAdmissionHandler}, + map[string]WebhookHandler{ + "webhook-a": {Path: "/path/a", AdmissionHandler: noOpAdmissionHandler}, }, }, { @@ -81,7 +81,7 @@ func TestWebhookEnableDisable(t *testing.T) { "webhook-b": {Path: "/path/b", AdmissionHandler: noOpAdmissionHandler}, }, newConfig(cpconfig.WebhookConfiguration{Webhooks: []string{"-webhook-b"}}), - map[string]webhookHandler{}, + map[string]WebhookHandler{}, }, { "Webhooks Enabled Glob", @@ -90,15 +90,15 @@ func TestWebhookEnableDisable(t *testing.T) { "webhook-b": {Path: "/path/b", AdmissionHandler: noOpAdmissionHandler}, }, newConfig(cpconfig.WebhookConfiguration{Webhooks: []string{"*"}}), - map[string]webhookHandler{ - "webhook-a": {path: "/path/a", admissionHandler: noOpAdmissionHandler}, - "webhook-b": {path: "/path/b", admissionHandler: noOpAdmissionHandler}, + map[string]WebhookHandler{ + "webhook-a": {Path: "/path/a", AdmissionHandler: noOpAdmissionHandler}, + "webhook-b": {Path: "/path/b", AdmissionHandler: noOpAdmissionHandler}, }, }, } for _, tc := range cases { t.Logf("Running %q", tc.desc) - actual := newWebhookHandlers(tc.webhookConfigs, tc.completedConfig, cloud) + actual := NewWebhookHandlers(tc.webhookConfigs, tc.completedConfig, cloud) if !webhookHandlersEqual(actual, tc.expected) { t.Fatalf( "FAILED: %q\n---\nActual:\n%s\nExpected:\n%s\ntc.webhookConfigs:\n%s\ntc.completedConfig:\n%s\n", @@ -121,7 +121,7 @@ func newConfig(webhookConfig cpconfig.WebhookConfiguration) *config.CompletedCon return cfg.Complete() } -func webhookHandlersEqual(actual, expected map[string]webhookHandler) bool { +func webhookHandlersEqual(actual, expected map[string]WebhookHandler) bool { if len(actual) != len(expected) { return false }