Skip to content

Commit

Permalink
Add templating for NodeClass tags
Browse files Browse the repository at this point in the history
Allowed properties:
- ClusterName
- NodePoolName
- NodeClassName

E.G.: `{{ .ClusterName }}/{{ .NodePoolName }}/{{ .NodeClassName}}`

Signed-off-by: Sylvain Rabot <[email protected]>
  • Loading branch information
sylr committed Dec 14, 2024
1 parent e0cd8a4 commit cbf35a1
Showing 1 changed file with 35 additions and 5 deletions.
40 changes: 35 additions & 5 deletions pkg/cloudprovider/cloudprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"context"
stderrors "errors"
"fmt"
"strings"
"text/template"
"time"

ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
Expand Down Expand Up @@ -248,13 +250,41 @@ func getTags(ctx context.Context, nodeClass *v1.EC2NodeClass, nodeClaim *karpv1.
}); found {
return nil, fmt.Errorf("%q tag does not pass tag validation requirements", offendingTag)
}
clusterName := options.FromContext(ctx).ClusterName
nodePoolName := nodeClaim.Labels[karpv1.NodePoolLabelKey]
nodeClassName := nodeClass.Name
staticTags := map[string]string{
fmt.Sprintf("kubernetes.io/cluster/%s", options.FromContext(ctx).ClusterName): "owned",
karpv1.NodePoolLabelKey: nodeClaim.Labels[karpv1.NodePoolLabelKey],
v1.EKSClusterNameTagKey: options.FromContext(ctx).ClusterName,
v1.LabelNodeClass: nodeClass.Name,
fmt.Sprintf("kubernetes.io/cluster/%s", clusterName): "owned",
karpv1.NodePoolLabelKey: nodePoolName,
v1.EKSClusterNameTagKey: clusterName,
v1.LabelNodeClass: nodeClassName,
}
tags := lo.Assign(nodeClass.Spec.Tags, staticTags)

t := template.New("ec2tags")
sb := strings.Builder{}
type tdata struct {
ClusterName string
NodePoolName string
NodeClassName string
}
for k, v := range tags {
sb.Reset()
if _, err := t.Parse(v); err != nil {
return nil, fmt.Errorf("unable to parse tag %s value `%s` as a go template: %w", k, v, err)
}
err := t.Execute(&sb, tdata{
ClusterName: clusterName,
NodePoolName: nodePoolName,
NodeClassName: nodeClassName,
})
if err != nil {
return nil, fmt.Errorf("unable to execute tag %s value `%s` as a go template: %w", k, v, err)
}
tags[k] = sb.String()
}
return lo.Assign(nodeClass.Spec.Tags, staticTags), nil

return tags, nil
}

func (c *CloudProvider) RepairPolicies() []cloudprovider.RepairPolicy {
Expand Down

0 comments on commit cbf35a1

Please sign in to comment.