Skip to content

Commit

Permalink
chore: Convert CDL values on tags for SelectorTerms (#5237)
Browse files Browse the repository at this point in the history
  • Loading branch information
engedaam authored Dec 6, 2023
1 parent 56aaec1 commit 9735883
Show file tree
Hide file tree
Showing 2 changed files with 248 additions and 30 deletions.
67 changes: 47 additions & 20 deletions pkg/utils/nodeclass/nodeclass.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,23 @@ func NewSubnetSelectorTerms(subnetSelector map[string]string) (terms []v1beta1.S
}
// Each of these slices needs to be pre-populated with the "0" element so that we can properly generate permutations
ids := []string{""}
tags := map[string]string{}
tagSet := []map[string]string{make(map[string]string)}
for k, v := range subnetSelector {
switch k {
case "aws-ids", "aws::ids":
ids = lo.Map(strings.Split(v, ","), func(s string, _ int) string { return strings.Trim(s, " ") })
default:
tags[k] = v
tagSet = createSelectorTags(k, v, tagSet)
}
}
// If there are some "special" keys used, we have to represent the old selector as multiple terms
for _, id := range ids {
terms = append(terms, v1beta1.SubnetSelectorTerm{
Tags: tags,
ID: id,
})
for _, tag := range tagSet {
terms = append(terms, v1beta1.SubnetSelectorTerm{
Tags: tag,
ID: id,
})
}
}
return terms
}
Expand All @@ -93,21 +95,23 @@ func NewSecurityGroupSelectorTerms(securityGroupSelector map[string]string) (ter
}
// Each of these slices needs to be pre-populated with the "0" element so that we can properly generate permutations
ids := []string{""}
tags := map[string]string{}
tagSet := []map[string]string{make(map[string]string)}
for k, v := range securityGroupSelector {
switch k {
case "aws-ids", "aws::ids":
ids = lo.Map(strings.Split(v, ","), func(s string, _ int) string { return strings.Trim(s, " ") })
default:
tags[k] = v
tagSet = createSelectorTags(k, v, tagSet)
}
}
// If there are some "special" keys used, we have to represent the old selector as multiple terms
for _, id := range ids {
terms = append(terms, v1beta1.SecurityGroupSelectorTerm{
Tags: tags,
ID: id,
})
for _, tag := range tagSet {
terms = append(terms, v1beta1.SecurityGroupSelectorTerm{
Tags: tag,
ID: id,
})
}
}
return terms
}
Expand All @@ -120,7 +124,7 @@ func NewAMISelectorTerms(amiSelector map[string]string) (terms []v1beta1.AMISele
ids := []string{""}
names := []string{""}
owners := []string{""}
tags := map[string]string{}
tagSet := []map[string]string{make(map[string]string)}
for k, v := range amiSelector {
switch k {
case "aws-ids", "aws::ids":
Expand All @@ -130,19 +134,21 @@ func NewAMISelectorTerms(amiSelector map[string]string) (terms []v1beta1.AMISele
case "aws::owners":
owners = lo.Map(strings.Split(v, ","), func(s string, _ int) string { return strings.Trim(s, " ") })
default:
tags[k] = v
tagSet = createSelectorTags(k, v, tagSet)
}
}
// If there are some "special" keys used, we have to represent the old selector as multiple terms
for _, owner := range owners {
for _, id := range ids {
for _, name := range names {
terms = append(terms, v1beta1.AMISelectorTerm{
Tags: tags,
ID: id,
Name: name,
Owner: owner,
})
for _, tag := range tagSet {
terms = append(terms, v1beta1.AMISelectorTerm{
Tags: tag,
ID: id,
Name: name,
Owner: owner,
})
}
}
}
}
Expand Down Expand Up @@ -273,3 +279,24 @@ func HashAnnotation(nodeClass *v1beta1.EC2NodeClass) map[string]string {
}
return map[string]string{v1beta1.AnnotationNodeClassHash: nodeClass.Hash()}
}

func createSelectorTags(k string, v string, tagSet []map[string]string) (res []map[string]string) {
cdlValue := lo.Map(strings.Split(v, ","), func(s string, _ int) string { return strings.Trim(s, " ") })
for _, val := range cdlValue {
for _, tag := range tagSet {
tempTag := deepCopyMap(tag)
tempTag[k] = val
res = append(res, tempTag)
}
}

return res
}

func deepCopyMap(m map[string]string) map[string]string {
result := map[string]string{}
for k, v := range m {
result[k] = v
}
return result
}
Loading

0 comments on commit 9735883

Please sign in to comment.