diff --git a/provisioner/template.go b/provisioner/template.go index 9eb81363..e33fe8b4 100644 --- a/provisioner/template.go +++ b/provisioner/template.go @@ -139,6 +139,7 @@ func renderTemplate(context *templateContext, file string) (string, error) { "kubernetesSizeToKiloBytes": kubernetesSizeToKiloBytes, "indexedList": indexedList, "zoneDistributedNodePoolGroups": zoneDistributedNodePoolGroups, + "nodePoolGroupsProfile": nodePoolGroupsProfile, "certificateExpiry": certificateExpiry, "sumQuantities": sumQuantities, "awsValidID": awsValidID, @@ -671,6 +672,17 @@ func poolsDistributed(dedicated string, pools []*api.NodePool) bool { // // The default pool is represented with an empty string as the key. func zoneDistributedNodePoolGroups(nodePools []*api.NodePool) map[string]bool { + poolGroups := groupNodePoolsByPurpose(nodePools) + result := make(map[string]bool) + for group, pools := range poolGroups { + if poolsDistributed(group, pools) { + result[group] = true + } + } + return result +} + +func groupNodePoolsByPurpose(nodePools []*api.NodePool) map[string][]*api.NodePool { poolGroups := make(map[string][]*api.NodePool) for _, pool := range nodePools { @@ -686,13 +698,25 @@ func zoneDistributedNodePoolGroups(nodePools []*api.NodePool) map[string]bool { } } - result := make(map[string]bool) + return poolGroups +} + +// nodePoolGroupsProfile groups node-pools by the dedicated label, each node-pool-group is mapped to its node-pool profile +// in case all pools of a group do not share the same profile, the group is mapped to empty string +func nodePoolGroupsProfile(nodePools []*api.NodePool) map[string]string { + poolGroups := groupNodePoolsByPurpose(nodePools) + profiles := make(map[string]string) for group, pools := range poolGroups { - if poolsDistributed(group, pools) { - result[group] = true + groupProfile := pools[0].Profile + for _, pool := range pools[1:] { + if pool.Profile != groupProfile { + groupProfile = "" + break + } } + profiles[group] = groupProfile } - return result + return profiles } // certificateExpiry returns the notAfter timestamp of a PEM-encoded certificate in the RFC3339 format diff --git a/provisioner/template_test.go b/provisioner/template_test.go index bb6c34c2..1cf351d2 100644 --- a/provisioner/template_test.go +++ b/provisioner/template_test.go @@ -1052,3 +1052,93 @@ func TestAWSValidID(t *testing.T) { require.NoError(t, err) require.Equal(t, "aws__12345678910__eu-central-1__kube-1", result) } + +func TestNodePoolGroupsProfile(t *testing.T) { + for _, tc := range []struct { + name string + input []*api.NodePool + expected map[string]string + }{ + { + name: "application-1 dedicated pools share the same profile", + input: []*api.NodePool{ + { + Name: "example-1", + Profile: "profile-1", + ConfigItems: map[string]string{ + "labels": "dedicated=application-1", + }, + }, + { + Name: "example-2", + Profile: "profile-1", + ConfigItems: map[string]string{ + "labels": "dedicated=application-1", + }, + }, + }, + expected: map[string]string{ + "application-1": "profile-1", + }, + }, + { + name: "application-2 dedicated pools do not share the same profile", + input: []*api.NodePool{ + { + Name: "example-1", + Profile: "profile-2", + ConfigItems: map[string]string{ + "labels": "dedicated=application-2", + }, + }, + { + Name: "example-2", + Profile: "profile-1", + ConfigItems: map[string]string{ + "labels": "dedicated=application-2", + }, + }, + }, + expected: map[string]string{ + "application-2": "", + }, + }, + { + name: "default pools share the same profile", + input: []*api.NodePool{ + { + Name: "example-1", + Profile: "profile-1", + }, + { + Name: "example-2", + Profile: "profile-1", + }, + }, + expected: map[string]string{ + "": "profile-1", + }, + }, + { + name: "default pools do not share the same profile", + input: []*api.NodePool{ + { + Name: "example-1", + Profile: "profile-2", + }, + { + Name: "example-2", + Profile: "profile-1", + }, + }, + expected: map[string]string{ + "": "", + }, + }, + } { + t.Run(tc.name, func(t *testing.T) { + output := nodePoolGroupsProfile(tc.input) + require.Equal(t, tc.expected, output) + }) + } +}