Skip to content

Commit

Permalink
Add a template function to get canonical clusterName based on provider
Browse files Browse the repository at this point in the history
Signed-off-by: Mikkel Oscar Lyderik Larsen <[email protected]>
  • Loading branch information
mikkeloscar committed Oct 31, 2024
1 parent 8fa036f commit 0366db4
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
10 changes: 10 additions & 0 deletions provisioner/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ func renderTemplate(context *templateContext, file string) (string, error) {
"split": split,
"join": sprig.GenericFuncMap()["join"],
"eksID": eksID,
"clusterName": clusterName,
"mountUnitName": mountUnitName,
"accountID": accountID,
"portRanges": portRanges,
Expand Down Expand Up @@ -839,3 +840,12 @@ func scaleQuantity(quantityStr string, factor float32) (string, error) {
}
return quantity.String(), nil
}

// clusterName returns the canonical cluster name based on the cluster
// provider.
func clusterName(cluster api.Cluster) (string, error) {
if cluster.Provider == string(ZalandoEKSProvider) {
return cluster.LocalID, nil
}
return cluster.ID, nil
}
35 changes: 35 additions & 0 deletions provisioner/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1554,3 +1554,38 @@ func TestNthAddressFromCIDR(t *testing.T) {
})
}
}

func TestClusterName(t *testing.T) {
for _, tc := range []struct {
name string
cluster api.Cluster
input string
expected string
}{
{
name: "zalando-aws cluster has clusterName == ID",
cluster: api.Cluster{
Provider: string(ZalandoAWSProvider),
ID: "aws:12345678910:eu-central-1:zalando-aws",
LocalID: "zalando-aws",
},
expected: "aws:12345678910:eu-central-1:zalando-aws",
input: `{{ .Values.data.cluster | clusterName }}`,
},
{
name: "zalando-eks cluster has clusterName == LocalID",
cluster: api.Cluster{
Provider: string(ZalandoEKSProvider),
ID: "aws:12345678910:eu-central-1:zalando-eks",
LocalID: "zalando-eks",
},
expected: "zalando-eks",
input: `{{ .Values.data.cluster | clusterName }}`,
},
} {
t.Run(tc.name, func(t *testing.T) {
result, _ := renderSingle(t, tc.input, map[string]interface{}{"cluster": tc.cluster})
require.EqualValues(t, tc.expected, result)
})
}
}

0 comments on commit 0366db4

Please sign in to comment.