Skip to content

Commit

Permalink
check for kubernetesConfig nil (Azure#3164)
Browse files Browse the repository at this point in the history
  • Loading branch information
jackfrancis committed Jun 5, 2018
1 parent bed3f27 commit c5594e7
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
11 changes: 10 additions & 1 deletion pkg/acsengine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,23 @@ 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())
}
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()
Expand Down
37 changes: 37 additions & 0 deletions pkg/acsengine/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

0 comments on commit c5594e7

Please sign in to comment.