From 3e142c38d84341595ca824d0b7d4249822ccf8ab Mon Sep 17 00:00:00 2001 From: Michi Mutsuzaki Date: Tue, 13 Jun 2023 03:55:03 +0000 Subject: [PATCH] helm mode: Handle azure.resourceGroup Helm value Read Azure resource group name from azure.resourceGroup Helm value so that azureRetrieveAKSClusterInfo() doesn't fail in Helm mode. Signed-off-by: Michi Mutsuzaki --- install/azure.go | 28 +++++++++++++++++++++++++++- install/azure_test.go | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 install/azure_test.go diff --git a/install/azure.go b/install/azure.go index 4ae3ac8f9a..c1cee7f62f 100644 --- a/install/azure.go +++ b/install/azure.go @@ -9,6 +9,8 @@ import ( "fmt" "github.com/cilium/cilium/pkg/versioncheck" + "helm.sh/helm/v3/pkg/cli" + "helm.sh/helm/v3/pkg/getter" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -81,6 +83,27 @@ func (k *K8sInstaller) azureRetrieveSubscriptionID() error { return nil } +// setAzureResourceGroupFromHelmValue checks if azure.resourceGroup Helm value is set, +// and overwrites params.Azure.ResourceGroupName if it is set. +func (k *K8sInstaller) setAzureResourceGroupFromHelmValue() error { + values, err := k.params.HelmOpts.MergeValues(getter.All(cli.New())) + if err != nil { + return err + } + azure, ok := values["azure"].(map[string]interface{}) + if !ok { + return nil + } + resourceGroupName, ok := azure["resourceGroup"].(string) + if !ok { + return nil + } + if resourceGroupName != "" { + k.params.Azure.ResourceGroupName = resourceGroupName + } + return nil +} + // `az aks create` requires an existing resource group in which to create a // new AKS cluster, but a single resource group may hold multiple AKS clusters. // @@ -110,8 +133,11 @@ func (k *K8sInstaller) azureRetrieveAKSClusterInfo() error { return nil } + if err := k.setAzureResourceGroupFromHelmValue(); err != nil { + return err + } if k.params.Azure.ResourceGroupName == "" { - k.Log("❌ Azure resource group is required, please specify --azure-resource-group") + k.Log("❌ Azure resource group is required, please specify --azure-resource-group or azure.resourceGroup Helm value") return fmt.Errorf("missing Azure resource group name") } diff --git a/install/azure_test.go b/install/azure_test.go new file mode 100644 index 0000000000..8fa43dc2cd --- /dev/null +++ b/install/azure_test.go @@ -0,0 +1,39 @@ +// SPDX-License-Identifier: Apache-2.0 +// Copyright Authors of Cilium + +package install + +import ( + "fmt" + "testing" + + "github.com/stretchr/testify/assert" + "helm.sh/helm/v3/pkg/cli/values" +) + +func TestK8sInstaller_setAzureResourceGroupFromHelmValue(t *testing.T) { + nameFromFlag := "name-from-flag" + nameFromHelmValue := "name-from-helm-value" + + // Test the case where --azure-resource-group flag is set. + installer := &K8sInstaller{ + params: Parameters{ + Azure: AzureParameters{ResourceGroupName: nameFromFlag}, + }, + } + err := installer.setAzureResourceGroupFromHelmValue() + assert.NoError(t, err) + assert.Equal(t, nameFromFlag, installer.params.Azure.ResourceGroupName) + + // Test the case where azure.resourceGroup Helm value is set. + installer = &K8sInstaller{ + params: Parameters{ + HelmOpts: values.Options{ + Values: []string{fmt.Sprintf("azure.resourceGroup=%s", nameFromHelmValue)}, + }, + }, + } + err = installer.setAzureResourceGroupFromHelmValue() + assert.NoError(t, err) + assert.Equal(t, nameFromHelmValue, installer.params.Azure.ResourceGroupName) +}