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()
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)

View File

@ -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())

View File

@ -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, "<error>", h.name)
recordRequestLatency(ctx, "<error>", h.name, latency.Seconds())
recordRequestTotal(ctx, "<error>", h.Name)
recordRequestLatency(ctx, "<error>", 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)

View File

@ -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
}