Skip to content

Commit

Permalink
fix: include capacity type and resource.Quantity in launch template c…
Browse files Browse the repository at this point in the history
…ache key (#5882)
  • Loading branch information
jmdeal committed Mar 26, 2024
1 parent 234a43a commit a5112f1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
15 changes: 15 additions & 0 deletions pkg/providers/amifamily/bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"sort"
"strings"

"github.com/mitchellh/hashstructure/v2"
"github.com/samber/lo"
core "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -99,6 +100,17 @@ func (o Options) nodeLabelArg() string {
return fmt.Sprintf("--node-labels=%q", strings.Join(labelStrings, ","))
}

// TODO: jmdeal@ remove once KubeletConfiguration can be properly hashed
// For more information on the resource.Quantity hash issue: https://github.com/aws/karpenter-provider-aws/issues/5447
func (o Options) HashReservedResources() string {
kubeReservedHash, systemReservedHash := uint64(0), uint64(0)
if kc := o.KubeletConfig; kc != nil {
kubeReservedHash, _ = hashstructure.Hash(resources.StringMap(kc.KubeReserved), hashstructure.FormatV2, &hashstructure.HashOptions{SlicesAsSets: true})
systemReservedHash, _ = hashstructure.Hash(resources.StringMap(kc.SystemReserved), hashstructure.FormatV2, &hashstructure.HashOptions{SlicesAsSets: true})
}
return fmt.Sprintf("%d-%d", kubeReservedHash, systemReservedHash)
}

// joinParameterArgs joins a map of keys and values by their separator. The separator will sit between the
// arguments in a comma-separated list i.e. arg1<sep>val1,arg2<sep>val2
func joinParameterArgs[K comparable, V any](name string, m map[K]V, separator string) string {
Expand All @@ -119,4 +131,7 @@ func joinParameterArgs[K comparable, V any](name string, m map[K]V, separator st
// Examples are the Bottlerocket config and the eks-bootstrap script
type Bootstrapper interface {
Script() (string, error)

// TODO: jmdeal@ remove once KubeletConfiguration can be properly hashed
HashReservedResources() string
}
15 changes: 14 additions & 1 deletion pkg/providers/launchtemplate/launchtemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,20 @@ func (p *Provider) Invalidate(ctx context.Context, ltName string, ltID string) {
}

func launchTemplateName(options *amifamily.LaunchTemplate) string {
hash, err := hashstructure.Hash(options, hashstructure.FormatV2, &hashstructure.HashOptions{SlicesAsSets: true})
// TODO: jmdeal@ remove custom hash struct once BlockDeviceMapping and KubeletConfiguration hashing is fixed, only hash Options
volumeSizeHash, _ := hashstructure.Hash(lo.Reduce(options.BlockDeviceMappings, func(agg string, block *v1beta1.BlockDeviceMapping, _ int) string {
return fmt.Sprintf("%s/%s", agg, block.EBS.VolumeSize)
}, ""), hashstructure.FormatV2, &hashstructure.HashOptions{SlicesAsSets: true})
hashStruct := struct {
Options *amifamily.LaunchTemplate
VolumeSizeHash string
ReservedResourcesHash string
}{
Options: options,
VolumeSizeHash: fmt.Sprint(volumeSizeHash),
ReservedResourcesHash: options.UserData.HashReservedResources(),
}
hash, err := hashstructure.Hash(hashStruct, hashstructure.FormatV2, &hashstructure.HashOptions{SlicesAsSets: true})
if err != nil {
panic(fmt.Sprintf("hashing launch template, %s", err))
}
Expand Down

0 comments on commit a5112f1

Please sign in to comment.