Skip to content

Commit

Permalink
add template function
Browse files Browse the repository at this point in the history
Signed-off-by: Mahmoud Gaballah <[email protected]>
  • Loading branch information
myaser committed Aug 17, 2023
1 parent 3e4bf00 commit 242e75d
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 4 deletions.
32 changes: 28 additions & 4 deletions provisioner/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down
90 changes: 90 additions & 0 deletions provisioner/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
}

0 comments on commit 242e75d

Please sign in to comment.