Skip to content

Commit

Permalink
helm: uniform retrieval of nested helm value
Browse files Browse the repository at this point in the history
Let's replace the custom logic to retrieve a specific helm value with
the unstructured.NestedString method, to ensure consistency among the
different call sites. Additionally, this change fixes the retrieval
of the routing mode configured by the user, which was previously
never obtained correctly, due to a format mismatch.

Signed-off-by: Marco Iorio <[email protected]>
  • Loading branch information
giorio94 authored and michi-covalent committed Mar 12, 2024
1 parent fa12a6f commit 9f61556
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 39 deletions.
28 changes: 5 additions & 23 deletions install/autodetect.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/cilium/cilium-cli/k8s"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

type validationCheck interface {
Expand Down Expand Up @@ -39,25 +40,13 @@ func (p Parameters) checkDisabled(name string) bool {
return false
}

func (k *K8sInstaller) detectDatapathMode() error {
func (k *K8sInstaller) detectDatapathMode(helmValues map[string]interface{}) error {
if k.params.DatapathMode != "" {
k.Log("ℹ️ Custom datapath mode: %s", k.params.DatapathMode)
return nil
}

vals, err := k.getHelmValues()
if err != nil {
return err
}

routingMode := ""
for _, val := range vals {
val, ok := val.(string)
if ok && strings.HasPrefix(val, "routingMode") {
routingMode = strings.Split(val, "=")[1]
}

}
routingMode, _, _ := unstructured.NestedString(helmValues, "routingMode")
if routingMode == "native" {
k.params.DatapathMode = DatapathNative
return nil
Expand Down Expand Up @@ -105,14 +94,7 @@ func (k *K8sInstaller) autodetect(ctx context.Context) {
}

func getClusterName(helmValues map[string]interface{}) string {
cluster, ok := helmValues["cluster"].(map[string]interface{})
if !ok {
return ""
}
clusterName, ok := cluster["name"].(string)
if !ok {
return ""
}
clusterName, _, _ := unstructured.NestedString(helmValues, "cluster", "name")
return clusterName
}

Expand Down Expand Up @@ -152,7 +134,7 @@ func (k *K8sInstaller) autodetectAndValidate(ctx context.Context, helmValues map
k.Log("ℹ️ Using cluster name %q", k.params.ClusterName)
}

if err := k.detectDatapathMode(); err != nil {
if err := k.detectDatapathMode(helmValues); err != nil {
return err
}

Expand Down
11 changes: 3 additions & 8 deletions install/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"helm.sh/helm/v3/pkg/cli"
"helm.sh/helm/v3/pkg/getter"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

type accountInfo struct {
Expand Down Expand Up @@ -83,14 +84,8 @@ func (k *K8sInstaller) setAzureResourceGroupFromHelmValue() error {
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
}

resourceGroupName, _, _ := unstructured.NestedString(values, "azure", "resourceGroup")
if resourceGroupName != "" {
k.params.Azure.ResourceGroupName = resourceGroupName
}
Expand Down
10 changes: 2 additions & 8 deletions install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/yaml"

Expand Down Expand Up @@ -176,14 +177,7 @@ func (k *K8sInstaller) listVersions() error {
}

func getChainingMode(values map[string]interface{}) string {
cni, ok := values["cni"].(map[string]interface{})
if !ok {
return ""
}
chainingMode, ok := cni["chainingMode"].(string)
if !ok {
return ""
}
chainingMode, _, _ := unstructured.NestedString(values, "cni", "chainingMode")
return chainingMode
}

Expand Down

0 comments on commit 9f61556

Please sign in to comment.