diff --git a/pkg/addon/templateagent/template_agent.go b/pkg/addon/templateagent/template_agent.go index dc9615327..fad41364e 100644 --- a/pkg/addon/templateagent/template_agent.go +++ b/pkg/addon/templateagent/template_agent.go @@ -36,7 +36,8 @@ const ( // to convert it to Values by JsonStructToValues. // the built-in values can not be overridden by getValuesFuncs type templateCRDBuiltinValues struct { - ClusterName string `json:"CLUSTER_NAME,omitempty"` + ClusterName string `json:"CLUSTER_NAME,omitempty"` + InstallNamespace string `json:"INSTALL_NAMESPACE,omitempty"` } // templateDefaultValues includes the default values for crd template agentAddon. diff --git a/pkg/addon/templateagent/template_agent_test.go b/pkg/addon/templateagent/template_agent_test.go index 911422840..5d349dd20 100644 --- a/pkg/addon/templateagent/template_agent_test.go +++ b/pkg/addon/templateagent/template_agent_test.go @@ -180,6 +180,7 @@ func TestAddonTemplateAgentManifests(t *testing.T) { {Name: "LOG_LEVEL", Value: "4"}, {Name: "HUB_KUBECONFIG", Value: "/managed/hub-kubeconfig/kubeconfig"}, {Name: "CLUSTER_NAME", Value: clusterName}, + {Name: "INSTALL_NAMESPACE", Value: "test-install-namespace"}, } if !equality.Semantic.DeepEqual(envs, expectedEnvs) { t.Errorf("unexpected envs %v", envs) diff --git a/pkg/addon/templateagent/values.go b/pkg/addon/templateagent/values.go index 7ac5a7563..5ed72619c 100644 --- a/pkg/addon/templateagent/values.go +++ b/pkg/addon/templateagent/values.go @@ -96,12 +96,12 @@ func (a *CRDTemplateAgentAddon) getValues( overrideValues = addonfactory.MergeValues(overrideValues, publicValues) } } - builtinSortedKeys, builtinValues, err := a.getBuiltinValues(cluster, addon) + builtinSortedKeys, builtinValues, err := a.getBuiltinValues(cluster, addon, privateValues) if err != nil { return presetValues, overrideValues, privateValues, nil } - // builtinValues only contains CLUSTER_NAME, and it should override overrideValues if CLUSTER_NAME - // is also set in the overrideValues, since CLUSTER_NAME should not be set externally. + // builtinValues contains CLUSTER_NAME and INSTALL_NAMESPACE, and it should override overrideValues if + // the contained values are also set in the overrideValues, since these values should not be set externally. overrideValues = addonfactory.MergeValues(overrideValues, builtinValues) for k, v := range overrideValues { @@ -124,10 +124,16 @@ func (a *CRDTemplateAgentAddon) getValues( func (a *CRDTemplateAgentAddon) getBuiltinValues( cluster *clusterv1.ManagedCluster, - _ *addonapiv1alpha1.ManagedClusterAddOn) ([]string, addonfactory.Values, error) { + _ *addonapiv1alpha1.ManagedClusterAddOn, + privateValues map[string]interface{}) ([]string, addonfactory.Values, error) { builtinValues := templateCRDBuiltinValues{} builtinValues.ClusterName = cluster.GetName() + namespace, ok := privateValues[InstallNamespacePrivateValueKey] + if ok && namespace != nil { + builtinValues.InstallNamespace = namespace.(string) + } + value, err := addonfactory.JsonStructToValues(builtinValues) if err != nil { return nil, nil, err diff --git a/pkg/addon/templateagent/values_test.go b/pkg/addon/templateagent/values_test.go index 3cbe878c4..f4c9ab524 100644 --- a/pkg/addon/templateagent/values_test.go +++ b/pkg/addon/templateagent/values_test.go @@ -149,9 +149,14 @@ func TestGetValues(t *testing.T) { name: "CLUSTER_NAME", value: "test-cluster", }, + { + name: "INSTALL_NAMESPACE", + value: "default-ns", + }, }, expectedOverride: map[string]interface{}{ - "CLUSTER_NAME": "test-cluster", + "CLUSTER_NAME": "test-cluster", + "INSTALL_NAMESPACE": "default-ns", }, expectedPrivate: map[string]interface{}{ InstallNamespacePrivateValueKey: "default-ns", @@ -169,10 +174,15 @@ func TestGetValues(t *testing.T) { name: "CLUSTER_NAME", value: "test-cluster", }, + { + name: "INSTALL_NAMESPACE", + value: "default-ns", + }, }, expectedOverride: map[string]interface{}{ - "CLUSTER_NAME": "test-cluster", - "key1": "value1", + "CLUSTER_NAME": "test-cluster", + "INSTALL_NAMESPACE": "default-ns", + "key1": "value1", }, expectedPrivate: map[string]interface{}{ InstallNamespacePrivateValueKey: "default-ns", @@ -188,8 +198,7 @@ func TestGetValues(t *testing.T) { }, }, values: addonfactory.Values{ - InstallNamespacePrivateValueKey: "default-ns", - "HUB_KUBECONFIG": "/managed/hub-kubeconfig/kubeconfig-test", + "HUB_KUBECONFIG": "/managed/hub-kubeconfig/kubeconfig-test", }, expectedPreset: orderedValues{ { @@ -205,9 +214,7 @@ func TestGetValues(t *testing.T) { "HUB_KUBECONFIG": "/managed/hub-kubeconfig/kubeconfig-test", "CLUSTER_NAME": "test-cluster", }, - expectedPrivate: map[string]interface{}{ - InstallNamespacePrivateValueKey: "default-ns", - }, + expectedPrivate: map[string]interface{}{}, }, { name: "builtIn value should not be overridden", @@ -232,10 +239,15 @@ func TestGetValues(t *testing.T) { name: "CLUSTER_NAME", value: "test-cluster", }, + { + name: "INSTALL_NAMESPACE", + value: "default-ns", + }, }, expectedOverride: map[string]interface{}{ - "HUB_KUBECONFIG": "/managed/hub-kubeconfig/kubeconfig-test", - "CLUSTER_NAME": "test-cluster", + "HUB_KUBECONFIG": "/managed/hub-kubeconfig/kubeconfig-test", + "CLUSTER_NAME": "test-cluster", + "INSTALL_NAMESPACE": "default-ns", }, expectedPrivate: map[string]interface{}{ InstallNamespacePrivateValueKey: "default-ns",