Skip to content

Commit

Permalink
Add install namespace to built in values (#533)
Browse files Browse the repository at this point in the history
Signed-off-by: zhujian <jiazhu@redhat.com>
  • Loading branch information
zhujian7 authored Jun 19, 2024
1 parent 237baa1 commit c5729ae
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 15 deletions.
3 changes: 2 additions & 1 deletion pkg/addon/templateagent/template_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions pkg/addon/templateagent/template_agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 10 additions & 4 deletions pkg/addon/templateagent/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
32 changes: 22 additions & 10 deletions pkg/addon/templateagent/values_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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{
{
Expand All @@ -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",
Expand All @@ -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",
Expand Down

0 comments on commit c5729ae

Please sign in to comment.