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 11, 2024
1 parent ed92913 commit 8f7ca16
Showing 1 changed file with 40 additions and 7 deletions.
47 changes: 40 additions & 7 deletions pkg/cloudprovider/cloudprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
stderrors "errors"
"fmt"
"strings"
"text/template"
"time"

ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
Expand Down Expand Up @@ -104,7 +105,11 @@ func (c *CloudProvider) Create(ctx context.Context, nodeClaim *karpv1.NodeClaim)
if len(instanceTypes) == 0 {
return nil, cloudprovider.NewInsufficientCapacityError(fmt.Errorf("all requested instance types were unavailable during launch"))
}
instance, err := c.instanceProvider.Create(ctx, nodeClass, nodeClaim, getTags(ctx, nodeClass, nodeClaim), instanceTypes)
tags, err := getTags(ctx, nodeClass, nodeClaim)
if err != nil {
return nil, err
}
instance, err := c.instanceProvider.Create(ctx, nodeClass, nodeClaim, tags, instanceTypes)
if err != nil {
conditionMessage := "Error creating instance"
var createError *cloudprovider.CreateError
Expand Down Expand Up @@ -234,16 +239,44 @@ func (c *CloudProvider) GetSupportedNodeClasses() []status.Object {
return []status.Object{&v1.EC2NodeClass{}}
}

func getTags(ctx context.Context, nodeClass *v1.EC2NodeClass, nodeClaim *karpv1.NodeClaim) map[string]string {
func getTags(ctx context.Context, nodeClass *v1.EC2NodeClass, nodeClaim *karpv1.NodeClaim) (map[string]string, error) {
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,
}
return lo.Assign(lo.OmitBy(nodeClass.Spec.Tags, func(key string, _ string) bool {
tags := lo.Assign(lo.OmitBy(nodeClass.Spec.Tags, func(key string, _ string) bool {
return strings.HasPrefix(key, "kubernetes.io/cluster/")
}), 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 tags, nil
}

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

0 comments on commit 8f7ca16

Please sign in to comment.