From c5594e7e2364212605ff23ca1a9f9b787ec18860 Mon Sep 17 00:00:00 2001 From: Jack Francis Date: Mon, 4 Jun 2018 17:22:48 -0700 Subject: [PATCH] check for kubernetesConfig nil (#3164) --- pkg/acsengine/engine.go | 11 ++++++++++- pkg/acsengine/engine_test.go | 37 ++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/pkg/acsengine/engine.go b/pkg/acsengine/engine.go index ff95d5e86c..504a5321e1 100644 --- a/pkg/acsengine/engine.go +++ b/pkg/acsengine/engine.go @@ -63,6 +63,12 @@ func GenerateClusterID(properties *api.Properties) string { // GenerateKubeConfig returns a JSON string representing the KubeConfig func GenerateKubeConfig(properties *api.Properties, location string) (string, error) { + if properties == nil { + return "", fmt.Errorf("Properties nil in GenerateKubeConfig") + } + if properties.CertificateProfile == nil { + return "", fmt.Errorf("CertificateProfile property may not be nil in GenerateKubeConfig") + } b, err := Asset(kubeConfigJSON) if err != nil { return "", fmt.Errorf("error reading kube config template file %s: %s", kubeConfigJSON, err.Error()) @@ -70,7 +76,10 @@ func GenerateKubeConfig(properties *api.Properties, location string) (string, er kubeconfig := string(b) // variable replacement kubeconfig = strings.Replace(kubeconfig, "{{WrapAsVerbatim \"variables('caCertificate')\"}}", base64.StdEncoding.EncodeToString([]byte(properties.CertificateProfile.CaCertificate)), -1) - if properties.OrchestratorProfile.KubernetesConfig.PrivateCluster != nil && helpers.IsTrueBoolPointer(properties.OrchestratorProfile.KubernetesConfig.PrivateCluster.Enabled) { + if properties.OrchestratorProfile != nil && + properties.OrchestratorProfile.KubernetesConfig != nil && + properties.OrchestratorProfile.KubernetesConfig.PrivateCluster != nil && + helpers.IsTrueBoolPointer(properties.OrchestratorProfile.KubernetesConfig.PrivateCluster.Enabled) { if properties.MasterProfile.Count > 1 { // more than 1 master, use the internal lb IP firstMasterIP := net.ParseIP(properties.MasterProfile.FirstConsecutiveStaticIP).To4() diff --git a/pkg/acsengine/engine_test.go b/pkg/acsengine/engine_test.go index e826a2908d..1128a07e64 100644 --- a/pkg/acsengine/engine_test.go +++ b/pkg/acsengine/engine_test.go @@ -596,3 +596,40 @@ func TestGenerateIpList(t *testing.T) { } } } + +func TestGenerateKubeConfig(t *testing.T) { + locale := gotext.NewLocale(path.Join("..", "..", "translations"), "en_US") + i18n.Initialize(locale) + + apiloader := &api.Apiloader{ + Translator: &i18n.Translator{ + Locale: locale, + }, + } + + testData := "./testdata/simple/kubernetes.json" + + containerService, _, err := apiloader.LoadContainerServiceFromFile(testData, true, false, nil) + if err != nil { + t.Fatalf("Failed to load container service from file: %v", err) + } + kubeConfig, err := GenerateKubeConfig(containerService.Properties, "westus2") + // TODO add actual kubeconfig validation + if len(kubeConfig) < 1 { + t.Fatalf("Got unexpected kubeconfig payload: %v", kubeConfig) + } + if err != nil { + t.Fatalf("Failed to call GenerateKubeConfig with simple Kubernetes config from file: %v", testData) + } + + p := api.Properties{} + _, err = GenerateKubeConfig(&p, "westus2") + if err == nil { + t.Fatalf("Expected an error result from nil Properties child properties") + } + + _, err = GenerateKubeConfig(nil, "westus2") + if err == nil { + t.Fatalf("Expected an error result from nil Properties child properties") + } +}