Skip to content

Commit 0e4cf67

Browse files
authored
Merge pull request kubernetes#123957 from kmala/ccm
Do not bind webhook port if webhooks are not present
2 parents 3ebdb13 + 0e4648d commit 0e4cf67

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
@@ -436,6 +436,7 @@ func TestCreateConfig(t *testing.T) {
436436

437437
// Don't check
438438
c.SecureServing = nil
439+
assert.NotNil(t, c.WebhookSecureServing, "webhook secureserving shouldn't be nil")
439440
c.WebhookSecureServing = nil
440441
c.Authentication = apiserver.AuthenticationInfo{}
441442
c.Authorization = apiserver.AuthorizationInfo{}
@@ -453,6 +454,143 @@ func TestCreateConfig(t *testing.T) {
453454
}
454455
}
455456

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