Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

helm mode: Handle azure.resourceGroup Helm value #1716

Merged
merged 1 commit into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion install/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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.
//
Expand Down Expand Up @@ -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")
}

Expand Down
39 changes: 39 additions & 0 deletions install/azure_test.go
Original file line number Diff line number Diff line change
@@ -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)
}