From d0b654e7572501d8a3667100aa611c82fdfb9761 Mon Sep 17 00:00:00 2001 From: Kfir Toledo Date: Tue, 25 Jun 2024 15:43:16 +0300 Subject: [PATCH] Fix lint nested if Signed-off-by: Kfir Toledo --- pkg/bootstrap/platform/k8s.go | 60 +++++++++++++++++++++++------------ 1 file changed, 40 insertions(+), 20 deletions(-) diff --git a/pkg/bootstrap/platform/k8s.go b/pkg/bootstrap/platform/k8s.go index 0f0cc4e1..09ac1f28 100644 --- a/pkg/bootstrap/platform/k8s.go +++ b/pkg/bootstrap/platform/k8s.go @@ -323,7 +323,7 @@ func K8SConfig(config *Config) ([]byte, error) { "dataplanePort": dpapi.ListenPort, } - var k8sConfig, nsConfig, ingressConfig bytes.Buffer + var k8sConfig, nsConfig bytes.Buffer // ClusterLink namespace t := template.Must(template.New("").Parse(nsTemplate)) if err := t.Execute(&nsConfig, args); err != nil { @@ -343,30 +343,15 @@ func K8SConfig(config *Config) ([]byte, error) { } // ClusterLink ingress service - if config.IngressType != "" { - args["ingressPort"] = apis.DefaultExternalPort - args["ingressType"] = config.IngressType - if config.IngressPort != 0 { - if config.IngressType == string(apis.IngressTypeNodePort) { - args["ingressNodePort"] = config.IngressPort - if (config.IngressPort < 30000) || (config.IngressPort > 32767) { - return nil, fmt.Errorf("nodeport number %v is not in the valid range (30000:32767)", config.IngressPort) - } - } else { - args["ingressPort"] = config.IngressPort - } - } - - t := template.Must(template.New("").Parse(ingressTemplate)) - if err := t.Execute(&ingressConfig, args); err != nil { - return nil, fmt.Errorf("cannot create K8s namespace from template: %w", err) - } + ingressConfig, err := k8SIngressConfig(config) + if err != nil { + return nil, err } k8sBytes := nsConfig.Bytes() k8sBytes = append(k8sBytes, certConfig...) k8sBytes = append(k8sBytes, k8sConfig.Bytes()...) - k8sBytes = append(k8sBytes, ingressConfig.Bytes()...) + k8sBytes = append(k8sBytes, ingressConfig...) return k8sBytes, nil } @@ -461,3 +446,38 @@ func K8SEmptyCertificateConfig(config *Config) ([]byte, error) { return certConfig.Bytes(), nil } + +// k8SIngressConfig returns a kubernetes ingress service. +func k8SIngressConfig(config *Config) ([]byte, error) { + var ingressConfig bytes.Buffer + if config.IngressType == "" { + return ingressConfig.Bytes(), nil + } + + args := map[string]interface{}{ + "namespace": config.Namespace, + "ingressPort": apis.DefaultExternalPort, + "ingressType": config.IngressType, + + "dataplaneAppName": dpapp.Name, + "dataplanePort": dpapi.ListenPort, + } + + if config.IngressPort != 0 { + if config.IngressType == string(apis.IngressTypeNodePort) { + args["ingressNodePort"] = config.IngressPort + if (config.IngressPort < 30000) || (config.IngressPort > 32767) { + return nil, fmt.Errorf("nodeport number %v is not in the valid range (30000:32767)", config.IngressPort) + } + } else { + args["ingressPort"] = config.IngressPort + } + } + + t := template.Must(template.New("").Parse(ingressTemplate)) + if err := t.Execute(&ingressConfig, args); err != nil { + return nil, fmt.Errorf("cannot create K8s namespace from template: %w", err) + } + + return ingressConfig.Bytes(), nil +}