Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add template function #692

Merged
merged 1 commit into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 49 additions & 16 deletions provisioner/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,18 +131,19 @@ func renderTemplate(context *templateContext, file string) (string, error) {
"nodeCIDRMaxNodes": func(maskSize int64, reserved int64) (int64, error) {
return nodeCIDRMaxNodes(16, maskSize, reserved)
},
"nodeCIDRMaxNodesPodCIDR": nodeCIDRMaxNodes,
"nodeCIDRMaxPods": nodeCIDRMaxPods,
"parseInt64": parseInt64,
"generateJWKSDocument": generateJWKSDocument,
"generateOIDCDiscoveryDocument": generateOIDCDiscoveryDocument,
"kubernetesSizeToKiloBytes": kubernetesSizeToKiloBytes,
"indexedList": indexedList,
"zoneDistributedNodePoolGroups": zoneDistributedNodePoolGroups,
"certificateExpiry": certificateExpiry,
"sumQuantities": sumQuantities,
"awsValidID": awsValidID,
"indent": sprig.GenericFuncMap()["indent"],
"nodeCIDRMaxNodesPodCIDR": nodeCIDRMaxNodes,
"nodeCIDRMaxPods": nodeCIDRMaxPods,
"parseInt64": parseInt64,
"generateJWKSDocument": generateJWKSDocument,
"generateOIDCDiscoveryDocument": generateOIDCDiscoveryDocument,
"kubernetesSizeToKiloBytes": kubernetesSizeToKiloBytes,
"indexedList": indexedList,
"zoneDistributedNodePoolGroups": zoneDistributedNodePoolGroups,
"nodeLifeCycleProviderPerNodePoolGroup": nodeLifeCycleProviderPerNodePoolGroup,
"certificateExpiry": certificateExpiry,
"sumQuantities": sumQuantities,
"awsValidID": awsValidID,
"indent": sprig.GenericFuncMap()["indent"],
}

content, ok := context.fileData[file]
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,34 @@ func zoneDistributedNodePoolGroups(nodePools []*api.NodePool) map[string]bool {
}
}

result := make(map[string]bool)
return poolGroups
}

// nodeLifeCycleProviderPerNodePoolGroup groups node-pools by the dedicated label, each node-pool-group is mapped to
// the provider suitable for its node-pool profile
// in case all pools of a group do not share the same profile, the group is mapped to empty string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this mean in practice? Which provider would be used here by admission-controller? 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

an empty string "" provider will be used, which is practically turning this feature off

func nodeLifeCycleProviderPerNodePoolGroup(nodePools []*api.NodePool) map[string]string {
providers := map[string]string{
"worker-karpenter": "karpenter",
"worker-combined": "zalando",
"worker-splitaz": "zalando",
}
poolGroups := groupNodePoolsByPurpose(nodePools)
provider := make(map[string]string)
for group, pools := range poolGroups {
if poolsDistributed(group, pools) {
result[group] = true
if group == "" {
group = "default"
}
groupProfile := pools[0].Profile
for _, pool := range pools[1:] {
if pool.Profile != groupProfile {
groupProfile = ""
break
}
}
provider[group] = providers[groupProfile]
}
return result
return provider
}

// certificateExpiry returns the notAfter timestamp of a PEM-encoded certificate in the RFC3339 format
Expand Down
112 changes: 112 additions & 0 deletions provisioner/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1052,3 +1052,115 @@ 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: "worker-combined",
ConfigItems: map[string]string{
"labels": "dedicated=application-1",
},
},
{
Name: "example-2",
Profile: "worker-combined",
ConfigItems: map[string]string{
"labels": "dedicated=application-1",
},
},
},
expected: map[string]string{
"application-1": "zalando",
},
},
{
name: "application-1 dedicated pools share the same profile, which is unknown",
input: []*api.NodePool{
{
Name: "example-1",
Profile: "worker-unknown",
ConfigItems: map[string]string{
"labels": "dedicated=application-1",
},
},
{
Name: "example-2",
Profile: "worker-unknown",
ConfigItems: map[string]string{
"labels": "dedicated=application-1",
},
},
},
expected: map[string]string{
"application-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: "worker-karpenter",
},
{
Name: "example-2",
Profile: "worker-karpenter",
},
},
expected: map[string]string{
"default": "karpenter",
},
},
{
name: "default pools do not share the same profile",
input: []*api.NodePool{
{
Name: "example-1",
Profile: "worker-karpenter",
},
{
Name: "example-2",
Profile: "worker-combined",
},
},
expected: map[string]string{
"default": "",
},
},
} {
t.Run(tc.name, func(t *testing.T) {
output := nodeLifeCycleProviderPerNodePoolGroup(tc.input)
require.Equal(t, tc.expected, output)
})
}
}
Loading