From 1ffcba7d839e57a9b1194e9cb90855e781184ab2 Mon Sep 17 00:00:00 2001 From: Konstantinos Tsakalozos Date: Tue, 23 May 2017 14:00:47 +0300 Subject: [PATCH 1/5] Adding option to set the federation api server port if nodeport is set --- federation/pkg/kubefed/init/init.go | 37 +++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/federation/pkg/kubefed/init/init.go b/federation/pkg/kubefed/init/init.go index 826ef8f66df..cd7e554db35 100644 --- a/federation/pkg/kubefed/init/init.go +++ b/federation/pkg/kubefed/init/init.go @@ -84,6 +84,7 @@ const ( apiserverServiceTypeFlag = "api-server-service-type" apiserverAdvertiseAddressFlag = "api-server-advertise-address" + apiserverPortFlag = "api-server-port" dnsProviderSecretName = "federation-dns-provider.conf" @@ -147,6 +148,7 @@ type initFederationOptions struct { apiServerServiceTypeString string apiServerServiceType v1.ServiceType apiServerAdvertiseAddress string + apiServerNodePortsPort int32 apiServerEnableHTTPBasicAuth bool apiServerEnableTokenAuth bool } @@ -163,6 +165,7 @@ func (o *initFederationOptions) Bind(flags *pflag.FlagSet, defaultImage string) flags.StringVar(&o.controllerManagerOverridesString, "controllermanager-arg-overrides", "", "comma separated list of federation-controller-manager arguments to override: Example \"--arg1=value1,--arg2=value2...\"") flags.StringVar(&o.apiServerServiceTypeString, apiserverServiceTypeFlag, string(v1.ServiceTypeLoadBalancer), "The type of service to create for federation API server. Options: 'LoadBalancer' (default), 'NodePort'.") flags.StringVar(&o.apiServerAdvertiseAddress, apiserverAdvertiseAddressFlag, "", "Preferred address to advertise api server nodeport service. Valid only if '"+apiserverServiceTypeFlag+"=NodePort'.") + flags.Int32Var(&o.apiServerNodePortsPort, apiserverPortFlag , 0, "Preferred port to use for api server nodeport service (0 for random port assignment). Valid only if '"+apiserverServiceTypeFlag+"=NodePort'.") flags.BoolVar(&o.apiServerEnableHTTPBasicAuth, "apiserver-enable-basic-auth", false, "Enables HTTP Basic authentication for the federation-apiserver. Defaults to false.") flags.BoolVar(&o.apiServerEnableTokenAuth, "apiserver-enable-token-auth", false, "Enables token authentication for the federation-apiserver. Defaults to false.") } @@ -229,6 +232,15 @@ func (i *initFederation) Complete(cmd *cobra.Command, args []string) error { } } + if i.options.apiServerNodePortsPort != 0 { + if i.options.apiServerServiceType != v1.ServiceTypeNodePort { + return fmt.Errorf("%s should be passed only with '%s=NodePort'", apiserverPortFlag, apiserverServiceTypeFlag) + } + } + if i.options.apiServerNodePortsPort < 0 { + return fmt.Errorf("Please provide a valid port number for %s", apiserverPortFlag) + } + i.options.apiServerOverrides, err = marshallOverrides(i.options.apiServerOverridesString) if err != nil { return fmt.Errorf("error marshalling --apiserver-arg-overrides: %v", err) @@ -292,7 +304,7 @@ func (i *initFederation) Run(cmdOut io.Writer, config util.AdminConfig) error { fmt.Fprint(cmdOut, "Creating federation control plane service...") glog.V(4).Info("Creating federation control plane service") - svc, ips, hostnames, err := createService(cmdOut, hostClientset, i.commonOptions.FederationSystemNamespace, serverName, i.commonOptions.Name, i.options.apiServerAdvertiseAddress, i.options.apiServerServiceType, i.options.dryRun) + svc, ips, hostnames, err := createService(cmdOut, hostClientset, i.commonOptions.FederationSystemNamespace, serverName, i.commonOptions.Name, i.options.apiServerAdvertiseAddress, i.options.apiServerNodePortsPort, i.options.apiServerServiceType, i.options.dryRun) if err != nil { return err } @@ -442,7 +454,16 @@ func createNamespace(clientset client.Interface, federationName, namespace strin return clientset.Core().Namespaces().Create(ns) } -func createService(cmdOut io.Writer, clientset client.Interface, namespace, svcName, federationName, apiserverAdvertiseAddress string, apiserverServiceType v1.ServiceType, dryRun bool) (*api.Service, []string, []string, error) { +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) { + port := api.ServicePort { + Name: "https", + Protocol: "TCP", + Port: 443, + TargetPort: intstr.FromString(apiServerSecurePortName), + } + if apiserverServiceType == v1.ServiceTypeNodePort { + port.NodePort = apiserverPort + } svc := &api.Service{ ObjectMeta: metav1.ObjectMeta{ Name: svcName, @@ -453,14 +474,7 @@ func createService(cmdOut io.Writer, clientset client.Interface, namespace, svcN Spec: api.ServiceSpec{ Type: api.ServiceType(apiserverServiceType), Selector: apiserverSvcSelector, - Ports: []api.ServicePort{ - { - Name: "https", - Protocol: "TCP", - Port: 443, - TargetPort: intstr.FromString(apiServerSecurePortName), - }, - }, + Ports: []api.ServicePort{port}, }, } @@ -470,6 +484,9 @@ func createService(cmdOut io.Writer, clientset client.Interface, namespace, svcN var err error svc, err = clientset.Core().Services(namespace).Create(svc) + if err != nil { + return svc, nil, nil, err + } ips := []string{} hostnames := []string{} From 462b8d87cc611cf3d23c10312f4f6efff57c3648 Mon Sep 17 00:00:00 2001 From: Konstantinos Tsakalozos Date: Tue, 23 May 2017 16:15:49 +0300 Subject: [PATCH 2/5] Check uper limit of port and ensure 0 corresponds to random port --- federation/pkg/kubefed/init/init.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/federation/pkg/kubefed/init/init.go b/federation/pkg/kubefed/init/init.go index cd7e554db35..ee2f4214045 100644 --- a/federation/pkg/kubefed/init/init.go +++ b/federation/pkg/kubefed/init/init.go @@ -237,7 +237,7 @@ func (i *initFederation) Complete(cmd *cobra.Command, args []string) error { return fmt.Errorf("%s should be passed only with '%s=NodePort'", apiserverPortFlag, apiserverServiceTypeFlag) } } - if i.options.apiServerNodePortsPort < 0 { + if i.options.apiServerNodePortsPort < 0 || i.options.apiServerNodePortsPort > 65535 { return fmt.Errorf("Please provide a valid port number for %s", apiserverPortFlag) } @@ -461,7 +461,7 @@ func createService(cmdOut io.Writer, clientset client.Interface, namespace, svcN Port: 443, TargetPort: intstr.FromString(apiServerSecurePortName), } - if apiserverServiceType == v1.ServiceTypeNodePort { + if apiserverServiceType == v1.ServiceTypeNodePort && apiserverPort > 0 { port.NodePort = apiserverPort } svc := &api.Service{ From e78699558bb60eca86e00770a5b2eeb7f041b6c9 Mon Sep 17 00:00:00 2001 From: Konstantinos Tsakalozos Date: Thu, 1 Jun 2017 16:31:01 +0300 Subject: [PATCH 3/5] Do not retunr svc in case of error. Rename apiServerNodePortPort. --- federation/pkg/kubefed/init/init.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/federation/pkg/kubefed/init/init.go b/federation/pkg/kubefed/init/init.go index ee2f4214045..9fa371337e6 100644 --- a/federation/pkg/kubefed/init/init.go +++ b/federation/pkg/kubefed/init/init.go @@ -148,7 +148,7 @@ type initFederationOptions struct { apiServerServiceTypeString string apiServerServiceType v1.ServiceType apiServerAdvertiseAddress string - apiServerNodePortsPort int32 + apiServerNodePortPort int32 apiServerEnableHTTPBasicAuth bool apiServerEnableTokenAuth bool } @@ -165,7 +165,7 @@ func (o *initFederationOptions) Bind(flags *pflag.FlagSet, defaultImage string) flags.StringVar(&o.controllerManagerOverridesString, "controllermanager-arg-overrides", "", "comma separated list of federation-controller-manager arguments to override: Example \"--arg1=value1,--arg2=value2...\"") flags.StringVar(&o.apiServerServiceTypeString, apiserverServiceTypeFlag, string(v1.ServiceTypeLoadBalancer), "The type of service to create for federation API server. Options: 'LoadBalancer' (default), 'NodePort'.") flags.StringVar(&o.apiServerAdvertiseAddress, apiserverAdvertiseAddressFlag, "", "Preferred address to advertise api server nodeport service. Valid only if '"+apiserverServiceTypeFlag+"=NodePort'.") - flags.Int32Var(&o.apiServerNodePortsPort, apiserverPortFlag , 0, "Preferred port to use for api server nodeport service (0 for random port assignment). Valid only if '"+apiserverServiceTypeFlag+"=NodePort'.") + 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'.") flags.BoolVar(&o.apiServerEnableHTTPBasicAuth, "apiserver-enable-basic-auth", false, "Enables HTTP Basic authentication for the federation-apiserver. Defaults to false.") flags.BoolVar(&o.apiServerEnableTokenAuth, "apiserver-enable-token-auth", false, "Enables token authentication for the federation-apiserver. Defaults to false.") } @@ -232,12 +232,12 @@ func (i *initFederation) Complete(cmd *cobra.Command, args []string) error { } } - if i.options.apiServerNodePortsPort != 0 { + if i.options.apiServerNodePortPort != 0 { if i.options.apiServerServiceType != v1.ServiceTypeNodePort { return fmt.Errorf("%s should be passed only with '%s=NodePort'", apiserverPortFlag, apiserverServiceTypeFlag) } } - if i.options.apiServerNodePortsPort < 0 || i.options.apiServerNodePortsPort > 65535 { + if i.options.apiServerNodePortPort < 0 || i.options.apiServerNodePortPort > 65535 { return fmt.Errorf("Please provide a valid port number for %s", apiserverPortFlag) } @@ -304,7 +304,7 @@ func (i *initFederation) Run(cmdOut io.Writer, config util.AdminConfig) error { fmt.Fprint(cmdOut, "Creating federation control plane service...") glog.V(4).Info("Creating federation control plane service") - svc, ips, hostnames, err := createService(cmdOut, hostClientset, i.commonOptions.FederationSystemNamespace, serverName, i.commonOptions.Name, i.options.apiServerAdvertiseAddress, i.options.apiServerNodePortsPort, i.options.apiServerServiceType, i.options.dryRun) + svc, ips, hostnames, err := createService(cmdOut, hostClientset, i.commonOptions.FederationSystemNamespace, serverName, i.commonOptions.Name, i.options.apiServerAdvertiseAddress, i.options.apiServerNodePortPort, i.options.apiServerServiceType, i.options.dryRun) if err != nil { return err } @@ -485,7 +485,7 @@ func createService(cmdOut io.Writer, clientset client.Interface, namespace, svcN var err error svc, err = clientset.Core().Services(namespace).Create(svc) if err != nil { - return svc, nil, nil, err + return nil, nil, nil, err } ips := []string{} From 538e57713e36c9504cc7f2d3bea35edbc849fd83 Mon Sep 17 00:00:00 2001 From: Konstantinos Tsakalozos Date: Tue, 6 Jun 2017 12:20:03 +0300 Subject: [PATCH 4/5] Fixing style errors --- federation/pkg/kubefed/init/init.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/federation/pkg/kubefed/init/init.go b/federation/pkg/kubefed/init/init.go index 9fa371337e6..87cf871eece 100644 --- a/federation/pkg/kubefed/init/init.go +++ b/federation/pkg/kubefed/init/init.go @@ -148,7 +148,7 @@ type initFederationOptions struct { apiServerServiceTypeString string apiServerServiceType v1.ServiceType apiServerAdvertiseAddress string - apiServerNodePortPort int32 + apiServerNodePortPort int32 apiServerEnableHTTPBasicAuth bool apiServerEnableTokenAuth bool } @@ -165,7 +165,7 @@ func (o *initFederationOptions) Bind(flags *pflag.FlagSet, defaultImage string) flags.StringVar(&o.controllerManagerOverridesString, "controllermanager-arg-overrides", "", "comma separated list of federation-controller-manager arguments to override: Example \"--arg1=value1,--arg2=value2...\"") flags.StringVar(&o.apiServerServiceTypeString, apiserverServiceTypeFlag, string(v1.ServiceTypeLoadBalancer), "The type of service to create for federation API server. Options: 'LoadBalancer' (default), 'NodePort'.") flags.StringVar(&o.apiServerAdvertiseAddress, apiserverAdvertiseAddressFlag, "", "Preferred address to advertise api server nodeport service. Valid only if '"+apiserverServiceTypeFlag+"=NodePort'.") - 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'.") + 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'.") flags.BoolVar(&o.apiServerEnableHTTPBasicAuth, "apiserver-enable-basic-auth", false, "Enables HTTP Basic authentication for the federation-apiserver. Defaults to false.") flags.BoolVar(&o.apiServerEnableTokenAuth, "apiserver-enable-token-auth", false, "Enables token authentication for the federation-apiserver. Defaults to false.") } @@ -455,11 +455,11 @@ func createNamespace(clientset client.Interface, federationName, namespace strin } 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) { - port := api.ServicePort { - Name: "https", - Protocol: "TCP", - Port: 443, - TargetPort: intstr.FromString(apiServerSecurePortName), + port := api.ServicePort{ + Name: "https", + Protocol: "TCP", + Port: 443, + TargetPort: intstr.FromString(apiServerSecurePortName), } if apiserverServiceType == v1.ServiceTypeNodePort && apiserverPort > 0 { port.NodePort = apiserverPort @@ -474,7 +474,7 @@ func createService(cmdOut io.Writer, clientset client.Interface, namespace, svcN Spec: api.ServiceSpec{ Type: api.ServiceType(apiserverServiceType), Selector: apiserverSvcSelector, - Ports: []api.ServicePort{port}, + Ports: []api.ServicePort{port}, }, } From 8c1e0593bbc7c3ad8a401c11d0ad596507448579 Mon Sep 17 00:00:00 2001 From: Konstantinos Tsakalozos Date: Tue, 13 Jun 2017 12:58:53 +0300 Subject: [PATCH 5/5] Use a pointer to mark the nodeport port, if any. --- federation/pkg/kubefed/init/init.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/federation/pkg/kubefed/init/init.go b/federation/pkg/kubefed/init/init.go index 87cf871eece..24d822d8d98 100644 --- a/federation/pkg/kubefed/init/init.go +++ b/federation/pkg/kubefed/init/init.go @@ -149,6 +149,7 @@ type initFederationOptions struct { apiServerServiceType v1.ServiceType apiServerAdvertiseAddress string apiServerNodePortPort int32 + apiServerNodePortPortPtr *int32 apiServerEnableHTTPBasicAuth bool apiServerEnableTokenAuth bool } @@ -236,6 +237,9 @@ func (i *initFederation) Complete(cmd *cobra.Command, args []string) error { if i.options.apiServerServiceType != v1.ServiceTypeNodePort { return fmt.Errorf("%s should be passed only with '%s=NodePort'", apiserverPortFlag, apiserverServiceTypeFlag) } + i.options.apiServerNodePortPortPtr = &i.options.apiServerNodePortPort + } else { + i.options.apiServerNodePortPortPtr = nil } if i.options.apiServerNodePortPort < 0 || i.options.apiServerNodePortPort > 65535 { return fmt.Errorf("Please provide a valid port number for %s", apiserverPortFlag) @@ -304,7 +308,7 @@ func (i *initFederation) Run(cmdOut io.Writer, config util.AdminConfig) error { fmt.Fprint(cmdOut, "Creating federation control plane service...") glog.V(4).Info("Creating federation control plane service") - svc, ips, hostnames, err := createService(cmdOut, hostClientset, i.commonOptions.FederationSystemNamespace, serverName, i.commonOptions.Name, i.options.apiServerAdvertiseAddress, i.options.apiServerNodePortPort, i.options.apiServerServiceType, i.options.dryRun) + 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) if err != nil { return err } @@ -454,15 +458,15 @@ func createNamespace(clientset client.Interface, federationName, namespace strin return clientset.Core().Namespaces().Create(ns) } -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) { +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) { port := api.ServicePort{ Name: "https", Protocol: "TCP", Port: 443, TargetPort: intstr.FromString(apiServerSecurePortName), } - if apiserverServiceType == v1.ServiceTypeNodePort && apiserverPort > 0 { - port.NodePort = apiserverPort + if apiserverServiceType == v1.ServiceTypeNodePort && apiserverPort != nil { + port.NodePort = *apiserverPort } svc := &api.Service{ ObjectMeta: metav1.ObjectMeta{