Skip to content
This repository was archived by the owner on Mar 26, 2021. It is now read-only.

Commit e65464d

Browse files
author
Kubernetes Submit Queue
authored
Merge pull request #46283 from ktsakalozos/feature/nodeport-port
Automatic merge from submit-queue Adding option to set the federation api server port if nodeport is set **What this PR does / why we need it**: Kubefed will deploy the respected services and then it will do a health check. Prior to this patch if the user selects the nodeport a random port is opened. In environments where firewalls are in place this random port selection will cause the health check to fail. With this patch we enable users to designate a specific port, after for example opening it on their firewall. **Which issue this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close that issue when PR gets merged)*: fixes #46021 **Special notes for your reviewer**: **Release note**: ``` Kubefed init allows for setting port in Nodeport configuration ```
2 parents a892cdd + 92c6b6c commit e65464d

File tree

1 file changed

+31
-10
lines changed

1 file changed

+31
-10
lines changed

pkg/kubefed/init/init.go

+31-10
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ const (
8484

8585
apiserverServiceTypeFlag = "api-server-service-type"
8686
apiserverAdvertiseAddressFlag = "api-server-advertise-address"
87+
apiserverPortFlag = "api-server-port"
8788

8889
dnsProviderSecretName = "federation-dns-provider.conf"
8990

@@ -149,6 +150,8 @@ type initFederationOptions struct {
149150
apiServerServiceTypeString string
150151
apiServerServiceType v1.ServiceType
151152
apiServerAdvertiseAddress string
153+
apiServerNodePortPort int32
154+
apiServerNodePortPortPtr *int32
152155
apiServerEnableHTTPBasicAuth bool
153156
apiServerEnableTokenAuth bool
154157
}
@@ -167,6 +170,7 @@ func (o *initFederationOptions) Bind(flags *pflag.FlagSet, defaultServerImage, d
167170
flags.StringVar(&o.controllerManagerOverridesString, "controllermanager-arg-overrides", "", "comma separated list of federation-controller-manager arguments to override: Example \"--arg1=value1,--arg2=value2...\"")
168171
flags.StringVar(&o.apiServerServiceTypeString, apiserverServiceTypeFlag, string(v1.ServiceTypeLoadBalancer), "The type of service to create for federation API server. Options: 'LoadBalancer' (default), 'NodePort'.")
169172
flags.StringVar(&o.apiServerAdvertiseAddress, apiserverAdvertiseAddressFlag, "", "Preferred address to advertise api server nodeport service. Valid only if '"+apiserverServiceTypeFlag+"=NodePort'.")
173+
flags.Int32Var(&o.apiServerNodePortPort, apiserverPortFlag, 0, "Preferred port to use for api server nodeport service (0 for random port assignment). Valid only if '"+apiserverServiceTypeFlag+"=NodePort'.")
170174
flags.BoolVar(&o.apiServerEnableHTTPBasicAuth, "apiserver-enable-basic-auth", false, "Enables HTTP Basic authentication for the federation-apiserver. Defaults to false.")
171175
flags.BoolVar(&o.apiServerEnableTokenAuth, "apiserver-enable-token-auth", false, "Enables token authentication for the federation-apiserver. Defaults to false.")
172176
}
@@ -233,6 +237,18 @@ func (i *initFederation) Complete(cmd *cobra.Command, args []string) error {
233237
}
234238
}
235239

240+
if i.options.apiServerNodePortPort != 0 {
241+
if i.options.apiServerServiceType != v1.ServiceTypeNodePort {
242+
return fmt.Errorf("%s should be passed only with '%s=NodePort'", apiserverPortFlag, apiserverServiceTypeFlag)
243+
}
244+
i.options.apiServerNodePortPortPtr = &i.options.apiServerNodePortPort
245+
} else {
246+
i.options.apiServerNodePortPortPtr = nil
247+
}
248+
if i.options.apiServerNodePortPort < 0 || i.options.apiServerNodePortPort > 65535 {
249+
return fmt.Errorf("Please provide a valid port number for %s", apiserverPortFlag)
250+
}
251+
236252
i.options.apiServerOverrides, err = marshallOverrides(i.options.apiServerOverridesString)
237253
if err != nil {
238254
return fmt.Errorf("error marshalling --apiserver-arg-overrides: %v", err)
@@ -296,7 +312,7 @@ func (i *initFederation) Run(cmdOut io.Writer, config util.AdminConfig) error {
296312

297313
fmt.Fprint(cmdOut, "Creating federation control plane service...")
298314
glog.V(4).Info("Creating federation control plane service")
299-
svc, ips, hostnames, err := createService(cmdOut, hostClientset, i.commonOptions.FederationSystemNamespace, serverName, i.commonOptions.Name, i.options.apiServerAdvertiseAddress, i.options.apiServerServiceType, i.options.dryRun)
315+
svc, ips, hostnames, err := createService(cmdOut, hostClientset, i.commonOptions.FederationSystemNamespace, serverName, i.commonOptions.Name, i.options.apiServerAdvertiseAddress, i.options.apiServerNodePortPortPtr, i.options.apiServerServiceType, i.options.dryRun)
300316
if err != nil {
301317
return err
302318
}
@@ -446,7 +462,16 @@ func createNamespace(clientset client.Interface, federationName, namespace strin
446462
return clientset.Core().Namespaces().Create(ns)
447463
}
448464

449-
func createService(cmdOut io.Writer, clientset client.Interface, namespace, svcName, federationName, apiserverAdvertiseAddress string, apiserverServiceType v1.ServiceType, dryRun bool) (*api.Service, []string, []string, error) {
465+
func createService(cmdOut io.Writer, clientset client.Interface, namespace, svcName, federationName, apiserverAdvertiseAddress string, apiserverPort *int32, apiserverServiceType v1.ServiceType, dryRun bool) (*api.Service, []string, []string, error) {
466+
port := api.ServicePort{
467+
Name: "https",
468+
Protocol: "TCP",
469+
Port: 443,
470+
TargetPort: intstr.FromString(apiServerSecurePortName),
471+
}
472+
if apiserverServiceType == v1.ServiceTypeNodePort && apiserverPort != nil {
473+
port.NodePort = *apiserverPort
474+
}
450475
svc := &api.Service{
451476
ObjectMeta: metav1.ObjectMeta{
452477
Name: svcName,
@@ -457,14 +482,7 @@ func createService(cmdOut io.Writer, clientset client.Interface, namespace, svcN
457482
Spec: api.ServiceSpec{
458483
Type: api.ServiceType(apiserverServiceType),
459484
Selector: apiserverSvcSelector,
460-
Ports: []api.ServicePort{
461-
{
462-
Name: "https",
463-
Protocol: "TCP",
464-
Port: 443,
465-
TargetPort: intstr.FromString(apiServerSecurePortName),
466-
},
467-
},
485+
Ports: []api.ServicePort{port},
468486
},
469487
}
470488

@@ -474,6 +492,9 @@ func createService(cmdOut io.Writer, clientset client.Interface, namespace, svcN
474492

475493
var err error
476494
svc, err = clientset.Core().Services(namespace).Create(svc)
495+
if err != nil {
496+
return nil, nil, nil, err
497+
}
477498

478499
ips := []string{}
479500
hostnames := []string{}

0 commit comments

Comments
 (0)