Skip to content

Commit 0e4648d

Browse files
committed
Do not bind webhook port if webhooks are not present
1 parent 8119e57 commit 0e4648d

File tree

3 files changed

+144
-2
lines changed

3 files changed

+144
-2
lines changed

staging/src/k8s.io/cloud-provider/options/options.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ func (o *CloudControllerManagerOptions) ApplyTo(c *config.Config, allControllers
201201
}
202202
}
203203
if o.WebhookServing != nil {
204-
if err = o.WebhookServing.ApplyTo(&c.WebhookSecureServing); err != nil {
204+
if err = o.WebhookServing.ApplyTo(&c.WebhookSecureServing, c.ComponentConfig.Webhook); err != nil {
205205
return err
206206
}
207207
}

staging/src/k8s.io/cloud-provider/options/options_test.go

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,7 @@ func TestCreateConfig(t *testing.T) {
434434

435435
// Don't check
436436
c.SecureServing = nil
437+
assert.NotNil(t, c.WebhookSecureServing, "webhook secureserving shouldn't be nil")
437438
c.WebhookSecureServing = nil
438439
c.Authentication = apiserver.AuthenticationInfo{}
439440
c.Authorization = apiserver.AuthorizationInfo{}
@@ -451,6 +452,143 @@ func TestCreateConfig(t *testing.T) {
451452
}
452453
}
453454

455+
func TestCreateConfigWithoutWebHooks(t *testing.T) {
456+
fs := pflag.NewFlagSet("addflagstest", pflag.ContinueOnError)
457+
458+
s, err := NewCloudControllerManagerOptions()
459+
if err != nil {
460+
t.Errorf("unexpected err: %v", err)
461+
}
462+
463+
for _, f := range s.Flags([]string{""}, []string{""}, nil, []string{""}, []string{""}).FlagSets {
464+
fs.AddFlagSet(f)
465+
}
466+
467+
tmpdir, err := os.MkdirTemp("", "options_test")
468+
if err != nil {
469+
t.Fatalf("%s", err)
470+
}
471+
defer func() {
472+
if err := os.RemoveAll(tmpdir); err != nil {
473+
t.Error(err)
474+
}
475+
}()
476+
477+
args := []string{
478+
"--allocate-node-cidrs=true",
479+
"--authorization-always-allow-paths=",
480+
"--bind-address=0.0.0.0",
481+
"--secure-port=10200",
482+
fmt.Sprintf("--cert-dir=%s/certs", tmpdir),
483+
"--cloud-provider=aws",
484+
"--cluster-cidr=1.2.3.4/24",
485+
"--cluster-name=k8s",
486+
"--configure-cloud-routes=false",
487+
"--contention-profiling=true",
488+
"--controller-start-interval=2m",
489+
"--controllers=foo,bar",
490+
"--concurrent-node-syncs=1",
491+
"--http2-max-streams-per-connection=47",
492+
"--kube-api-burst=101",
493+
"--kube-api-content-type=application/vnd.kubernetes.protobuf",
494+
"--kube-api-qps=50.0",
495+
"--leader-elect=false",
496+
"--leader-elect-lease-duration=30s",
497+
"--leader-elect-renew-deadline=15s",
498+
"--leader-elect-resource-lock=configmap",
499+
"--leader-elect-retry-period=5s",
500+
"--master=192.168.4.20",
501+
"--min-resync-period=100m",
502+
"--node-status-update-frequency=10m",
503+
"--profiling=false",
504+
"--route-reconciliation-period=30s",
505+
"--use-service-account-credentials=false",
506+
}
507+
err = fs.Parse(args)
508+
if err != nil {
509+
t.Errorf("error parsing the arguments, error : %v", err)
510+
}
511+
512+
fs.VisitAll(func(f *pflag.Flag) {
513+
fmt.Printf("%s: %s\n", f.Name, f.Value)
514+
})
515+
516+
c, err := s.Config([]string{"foo", "bar"}, []string{}, nil, []string{"foo", "bar", "baz"}, []string{})
517+
if err != nil {
518+
t.Errorf("error generating config, error : %v", err)
519+
}
520+
521+
expected := &appconfig.Config{
522+
ComponentConfig: cpconfig.CloudControllerManagerConfiguration{
523+
Generic: cmconfig.GenericControllerManagerConfiguration{
524+
Address: "0.0.0.0",
525+
MinResyncPeriod: metav1.Duration{Duration: 100 * time.Minute},
526+
ClientConnection: componentbaseconfig.ClientConnectionConfiguration{
527+
ContentType: "application/vnd.kubernetes.protobuf",
528+
QPS: 50.0,
529+
Burst: 101,
530+
},
531+
ControllerStartInterval: metav1.Duration{Duration: 2 * time.Minute},
532+
LeaderElection: componentbaseconfig.LeaderElectionConfiguration{
533+
ResourceLock: "configmap",
534+
LeaderElect: false,
535+
LeaseDuration: metav1.Duration{Duration: 30 * time.Second},
536+
RenewDeadline: metav1.Duration{Duration: 15 * time.Second},
537+
RetryPeriod: metav1.Duration{Duration: 5 * time.Second},
538+
ResourceName: "cloud-controller-manager",
539+
ResourceNamespace: "kube-system",
540+
},
541+
Controllers: []string{"foo", "bar"},
542+
Debugging: componentbaseconfig.DebuggingConfiguration{
543+
EnableProfiling: false,
544+
EnableContentionProfiling: true,
545+
},
546+
LeaderMigration: cmconfig.LeaderMigrationConfiguration{},
547+
},
548+
KubeCloudShared: cpconfig.KubeCloudSharedConfiguration{
549+
RouteReconciliationPeriod: metav1.Duration{Duration: 30 * time.Second},
550+
NodeMonitorPeriod: metav1.Duration{Duration: 5 * time.Second},
551+
ClusterName: "k8s",
552+
ClusterCIDR: "1.2.3.4/24",
553+
AllocateNodeCIDRs: true,
554+
CIDRAllocatorType: "RangeAllocator",
555+
ConfigureCloudRoutes: false,
556+
CloudProvider: cpconfig.CloudProviderConfiguration{
557+
Name: "aws",
558+
CloudConfigFile: "",
559+
},
560+
},
561+
ServiceController: serviceconfig.ServiceControllerConfiguration{
562+
ConcurrentServiceSyncs: 1,
563+
},
564+
NodeController: nodeconfig.NodeControllerConfiguration{ConcurrentNodeSyncs: 1},
565+
NodeStatusUpdateFrequency: metav1.Duration{Duration: 10 * time.Minute},
566+
Webhook: cpconfig.WebhookConfiguration{},
567+
},
568+
SecureServing: nil,
569+
WebhookSecureServing: nil,
570+
Authentication: apiserver.AuthenticationInfo{},
571+
Authorization: apiserver.AuthorizationInfo{},
572+
}
573+
574+
// Don't check
575+
c.SecureServing = nil
576+
c.Authentication = apiserver.AuthenticationInfo{}
577+
c.Authorization = apiserver.AuthorizationInfo{}
578+
c.SharedInformers = nil
579+
c.VersionedClient = nil
580+
c.ClientBuilder = nil
581+
c.EventRecorder = nil
582+
c.EventBroadcaster = nil
583+
c.Kubeconfig = nil
584+
c.Client = nil
585+
c.LoopbackClientConfig = nil
586+
587+
if !reflect.DeepEqual(expected, c) {
588+
t.Errorf("Got different config than expected.\nDifference detected on:\n%s", cmp.Diff(expected, c))
589+
}
590+
}
591+
454592
func TestCloudControllerManagerAliases(t *testing.T) {
455593
opts, err := NewCloudControllerManagerOptions()
456594
if err != nil {

staging/src/k8s.io/cloud-provider/options/webhook.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,14 +151,18 @@ func (o *WebhookServingOptions) Validate() []error {
151151
return allErrors
152152
}
153153

154-
func (o *WebhookServingOptions) ApplyTo(cfg **server.SecureServingInfo) error {
154+
func (o *WebhookServingOptions) ApplyTo(cfg **server.SecureServingInfo, webhookCfg config.WebhookConfiguration) error {
155155
if o == nil {
156156
return nil
157157
}
158158

159159
if o.BindPort <= 0 {
160160
return nil
161161
}
162+
// no need to bind to the address if there are no webhook enabled.
163+
if len(webhookCfg.Webhooks) == 0 {
164+
return nil
165+
}
162166

163167
var err error
164168
var listener net.Listener

0 commit comments

Comments
 (0)