Merge pull request #117088 from nckturner/public-arg-webhook

Export WebhookHandler struct because some CCMs use Run directly
This commit is contained in:
Kubernetes Prow Robot 2023-04-04 18:07:39 -07:00 committed by GitHub
commit 330b5a2b8d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 39 deletions

View File

@ -137,7 +137,7 @@ func (cb *CommandBuilder) BuildCommand() *cobra.Command {
completedConfig := config.Complete() completedConfig := config.Complete()
cloud := cb.cloudInitializer(completedConfig) cloud := cb.cloudInitializer(completedConfig)
controllerInitializers := ConstructControllerInitializers(cb.controllerInitFuncConstructors, completedConfig, cloud) 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 { if err := Run(completedConfig, cloud, controllerInitializers, webhooks, cb.stopCh); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err) fmt.Fprintf(os.Stderr, "%v\n", err)

View File

@ -106,7 +106,7 @@ the cloud specific control loops shipped with Kubernetes.`,
cloud := cloudInitializer(completedConfig) cloud := cloudInitializer(completedConfig)
controllerInitializers := ConstructControllerInitializers(controllerInitFuncConstructors, completedConfig, cloud) 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) fmt.Fprintf(os.Stderr, "%v\n", err)
return err return err
} }
@ -161,7 +161,7 @@ the cloud specific control loops shipped with Kubernetes.`,
} }
// Run runs the ExternalCMServer. This should never exit. // 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 { stopCh <-chan struct{}) error {
// To help debugging, immediately log version // To help debugging, immediately log version
klog.Infof("Version: %+v", version.Get()) klog.Infof("Version: %+v", version.Get())

View File

@ -58,29 +58,29 @@ type WebhookConfig struct {
AdmissionHandler func(*admissionv1.AdmissionRequest) (*admissionv1.AdmissionResponse, error) AdmissionHandler func(*admissionv1.AdmissionRequest) (*admissionv1.AdmissionResponse, error)
} }
type webhookHandler struct { type WebhookHandler struct {
name string Name string
path string Path string
http.Handler http.Handler
admissionHandler func(*admissionv1.AdmissionRequest) (*admissionv1.AdmissionResponse, error) AdmissionHandler func(*admissionv1.AdmissionRequest) (*admissionv1.AdmissionResponse, error)
completedConfig *config.CompletedConfig CompletedConfig *config.CompletedConfig
cloud cloudprovider.Interface Cloud cloudprovider.Interface
} }
func newWebhookHandlers(webhookConfigs map[string]WebhookConfig, completedConfig *config.CompletedConfig, cloud cloudprovider.Interface) map[string]webhookHandler { func NewWebhookHandlers(webhookConfigs map[string]WebhookConfig, completedConfig *config.CompletedConfig, cloud cloudprovider.Interface) map[string]WebhookHandler {
webhookHandlers := make(map[string]webhookHandler) webhookHandlers := make(map[string]WebhookHandler)
for name, config := range webhookConfigs { for name, config := range webhookConfigs {
if !genericcontrollermanager.IsControllerEnabled(name, WebhooksDisabledByDefault, completedConfig.ComponentConfig.Webhook.Webhooks) { if !genericcontrollermanager.IsControllerEnabled(name, WebhooksDisabledByDefault, completedConfig.ComponentConfig.Webhook.Webhooks) {
klog.Warningf("Webhook %q is disabled", name) klog.Warningf("Webhook %q is disabled", name)
continue continue
} }
klog.Infof("Webhook enabled: %q", name) klog.Infof("Webhook enabled: %q", name)
webhookHandlers[name] = webhookHandler{ webhookHandlers[name] = WebhookHandler{
name: name, Name: name,
path: config.Path, Path: config.Path,
admissionHandler: config.AdmissionHandler, AdmissionHandler: config.AdmissionHandler,
completedConfig: completedConfig, CompletedConfig: completedConfig,
cloud: cloud, Cloud: cloud,
} }
} }
return webhookHandlers return webhookHandlers
@ -91,17 +91,17 @@ func WebhookNames(webhooks map[string]WebhookConfig) []string {
return ret.List() 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") mux := mux.NewPathRecorderMux("controller-manager-webhook")
for _, handler := range webhooks { for _, handler := range webhooks {
mux.Handle(handler.path, handler) mux.Handle(handler.Path, handler)
} }
return mux 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() ctx := context.Background()
klog.Infof("Received validation request: %q", r.RequestURI) 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) latency := time.Since(start)
if statusCode != 0 { if statusCode != 0 {
recordRequestTotal(ctx, strconv.Itoa(statusCode), h.name) recordRequestTotal(ctx, strconv.Itoa(statusCode), h.Name)
recordRequestLatency(ctx, strconv.Itoa(statusCode), h.name, latency.Seconds()) recordRequestLatency(ctx, strconv.Itoa(statusCode), h.Name, latency.Seconds())
return return
} }
if err != nil { if err != nil {
recordRequestTotal(ctx, "<error>", h.name) recordRequestTotal(ctx, "<error>", h.Name)
recordRequestLatency(ctx, "<error>", h.name, latency.Seconds()) recordRequestLatency(ctx, "<error>", h.Name, latency.Seconds())
} }
}() }()
@ -135,7 +135,7 @@ func (h webhookHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return return
} }
admissionResponse, err = h.admissionHandler(in.Request) admissionResponse, err = h.AdmissionHandler(in.Request)
if err != nil { if err != nil {
e := fmt.Sprintf("error generating admission response: %v", err) e := fmt.Sprintf("error generating admission response: %v", err)
klog.Errorf(e) klog.Errorf(e)

View File

@ -38,7 +38,7 @@ func TestWebhookEnableDisable(t *testing.T) {
desc string desc string
webhookConfigs map[string]WebhookConfig webhookConfigs map[string]WebhookConfig
completedConfig *config.CompletedConfig completedConfig *config.CompletedConfig
expected map[string]webhookHandler expected map[string]WebhookHandler
}{ }{
{ {
"Webhooks Enabled", "Webhooks Enabled",
@ -47,9 +47,9 @@ func TestWebhookEnableDisable(t *testing.T) {
"webhook-b": {Path: "/path/b", AdmissionHandler: noOpAdmissionHandler}, "webhook-b": {Path: "/path/b", AdmissionHandler: noOpAdmissionHandler},
}, },
newConfig(cpconfig.WebhookConfiguration{Webhooks: []string{"webhook-a", "webhook-b"}}), newConfig(cpconfig.WebhookConfiguration{Webhooks: []string{"webhook-a", "webhook-b"}}),
map[string]webhookHandler{ map[string]WebhookHandler{
"webhook-a": {path: "/path/a", admissionHandler: noOpAdmissionHandler}, "webhook-a": {Path: "/path/a", AdmissionHandler: noOpAdmissionHandler},
"webhook-b": {path: "/path/b", 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}, "webhook-b": {Path: "/path/b", AdmissionHandler: noOpAdmissionHandler},
}, },
newConfig(cpconfig.WebhookConfiguration{Webhooks: []string{"webhook-a"}}), newConfig(cpconfig.WebhookConfiguration{Webhooks: []string{"webhook-a"}}),
map[string]webhookHandler{ map[string]WebhookHandler{
"webhook-a": {path: "/path/a", admissionHandler: noOpAdmissionHandler}, "webhook-a": {Path: "/path/a", AdmissionHandler: noOpAdmissionHandler},
}, },
}, },
{ {
@ -70,8 +70,8 @@ func TestWebhookEnableDisable(t *testing.T) {
"webhook-b": {Path: "/path/b", AdmissionHandler: noOpAdmissionHandler}, "webhook-b": {Path: "/path/b", AdmissionHandler: noOpAdmissionHandler},
}, },
newConfig(cpconfig.WebhookConfiguration{Webhooks: []string{"webhook-a", "-webhook-b"}}), newConfig(cpconfig.WebhookConfiguration{Webhooks: []string{"webhook-a", "-webhook-b"}}),
map[string]webhookHandler{ map[string]WebhookHandler{
"webhook-a": {path: "/path/a", admissionHandler: noOpAdmissionHandler}, "webhook-a": {Path: "/path/a", AdmissionHandler: noOpAdmissionHandler},
}, },
}, },
{ {
@ -81,7 +81,7 @@ func TestWebhookEnableDisable(t *testing.T) {
"webhook-b": {Path: "/path/b", AdmissionHandler: noOpAdmissionHandler}, "webhook-b": {Path: "/path/b", AdmissionHandler: noOpAdmissionHandler},
}, },
newConfig(cpconfig.WebhookConfiguration{Webhooks: []string{"-webhook-b"}}), newConfig(cpconfig.WebhookConfiguration{Webhooks: []string{"-webhook-b"}}),
map[string]webhookHandler{}, map[string]WebhookHandler{},
}, },
{ {
"Webhooks Enabled Glob", "Webhooks Enabled Glob",
@ -90,15 +90,15 @@ func TestWebhookEnableDisable(t *testing.T) {
"webhook-b": {Path: "/path/b", AdmissionHandler: noOpAdmissionHandler}, "webhook-b": {Path: "/path/b", AdmissionHandler: noOpAdmissionHandler},
}, },
newConfig(cpconfig.WebhookConfiguration{Webhooks: []string{"*"}}), newConfig(cpconfig.WebhookConfiguration{Webhooks: []string{"*"}}),
map[string]webhookHandler{ map[string]WebhookHandler{
"webhook-a": {path: "/path/a", admissionHandler: noOpAdmissionHandler}, "webhook-a": {Path: "/path/a", AdmissionHandler: noOpAdmissionHandler},
"webhook-b": {path: "/path/b", admissionHandler: noOpAdmissionHandler}, "webhook-b": {Path: "/path/b", AdmissionHandler: noOpAdmissionHandler},
}, },
}, },
} }
for _, tc := range cases { for _, tc := range cases {
t.Logf("Running %q", tc.desc) 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) { if !webhookHandlersEqual(actual, tc.expected) {
t.Fatalf( t.Fatalf(
"FAILED: %q\n---\nActual:\n%s\nExpected:\n%s\ntc.webhookConfigs:\n%s\ntc.completedConfig:\n%s\n", "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() return cfg.Complete()
} }
func webhookHandlersEqual(actual, expected map[string]webhookHandler) bool { func webhookHandlersEqual(actual, expected map[string]WebhookHandler) bool {
if len(actual) != len(expected) { if len(actual) != len(expected) {
return false return false
} }