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

AMIFamily Defaulting and Validation #1296

Merged
merged 5 commits into from
Feb 9, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 2 additions & 10 deletions pkg/cloudprovider/aws/ami.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,9 @@ func (p *AMIProvider) getBottlerocketAlias(version string, instanceType cloudpro

func (p *AMIProvider) getSSMQuery(ctx context.Context, constraints *v1alpha1.Constraints, instanceType cloudprovider.InstanceType, version string) string {
bwagner5 marked this conversation as resolved.
Show resolved Hide resolved
ssmQuery := p.getAL2Alias(version, instanceType)
if constraints.AMIFamily != nil {
if *constraints.AMIFamily == v1alpha1.OperatingSystemBottleRocket {
ssmQuery = p.getBottlerocketAlias(version, instanceType)
} else if *constraints.AMIFamily == v1alpha1.OperatingSystemEKSOptimized {
ssmQuery = p.getAL2Alias(version, instanceType)
} else {
logging.FromContext(ctx).Warnf("AMIFamily was set, but was not one of %s or %s. Setting to %s as the default.", v1alpha1.OperatingSystemEKSOptimized, v1alpha1.OperatingSystemBottleRocket, v1alpha1.OperatingSystemEKSOptimized)
ssmQuery = p.getAL2Alias(version, instanceType)
}
if strings.EqualFold(aws.StringValue(constraints.AMIFamily), v1alpha1.AMIFamilyBottlerocket) {
bwagner5 marked this conversation as resolved.
Show resolved Hide resolved
ssmQuery = p.getBottlerocketAlias(version, instanceType)
bwagner5 marked this conversation as resolved.
Show resolved Hide resolved
}

return ssmQuery
}

Expand Down
8 changes: 8 additions & 0 deletions pkg/cloudprovider/aws/apis/v1alpha1/provider_defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
func (c *Constraints) Default(ctx context.Context) {
c.defaultArchitecture()
c.defaultCapacityTypes()
c.defaultAMIFamily()
}

func (c *Constraints) defaultCapacityTypes() {
Expand Down Expand Up @@ -54,3 +55,10 @@ func (c *Constraints) defaultArchitecture() {
Values: []string{v1alpha5.ArchitectureAmd64},
})
}

func (c *Constraints) defaultAMIFamily() {
if c.AMIFamily != nil {
return
}
c.AMIFamily = &AMIFamilyEKSOptimized
}
10 changes: 9 additions & 1 deletion pkg/cloudprovider/aws/apis/v1alpha1/provider_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func (a *AWS) validate() (errs *apis.FieldError) {
a.validateSecurityGroups(),
a.validateTags(),
a.validateMetadataOptions(),
a.validateAMIFamily(),
)
}

Expand Down Expand Up @@ -121,9 +122,16 @@ func (a *AWS) validateHTTPTokens() *apis.FieldError {
return a.validateStringEnum(*a.MetadataOptions.HTTPTokens, "httpTokens", ec2.LaunchTemplateHttpTokensState_Values())
}

func (a *AWS) validateAMIFamily() *apis.FieldError {
if a.AMIFamily == nil {
return nil
}
return a.validateStringEnum(*a.AMIFamily, "amiFamily", SupportedAMIFamilies)
}

func (a *AWS) validateStringEnum(value, field string, validValues []string) *apis.FieldError {
for _, validValue := range validValues {
if value == validValue {
if strings.EqualFold(value, validValue) {
bwagner5 marked this conversation as resolved.
Show resolved Hide resolved
return nil
}
}
Expand Down
8 changes: 6 additions & 2 deletions pkg/cloudprovider/aws/apis/v1alpha1/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ var (
AWSRestrictedLabelDomains = []string{
"k8s.aws",
}
OperatingSystemBottleRocket = "Bottlerocket"
OperatingSystemEKSOptimized = "EKSOptimized"
AMIFamilyBottlerocket = "Bottlerocket"
AMIFamilyEKSOptimized = "EKSOptimized"
Copy link
Contributor

Choose a reason for hiding this comment

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

Reminder to make this EKSOptimizedLinux

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can't we just support Operating system separately and keep AMIFamily the actual family? So if AMIFamily was EKSOptimized but the pod requested Windows, we'd just give them windows EKS Optimized?

Copy link
Contributor

@ellistarn ellistarn Feb 8, 2022

Choose a reason for hiding this comment

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

This assumes a certain amount of parity between EKSOptimized. In general, I agree with you. IIRC, this was a request from @suket22.

SupportedAMIFamilies = []string{
AMIFamilyBottlerocket,
AMIFamilyEKSOptimized,
}
)

var (
Expand Down
42 changes: 10 additions & 32 deletions pkg/cloudprovider/aws/launchtemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,19 +107,9 @@ func (p *LaunchTemplateProvider) Get(ctx context.Context, constraints *v1alpha1.
// Construct launch templates
launchTemplates := map[string][]cloudprovider.InstanceType{}
for amiID, instanceTypes := range amis {
// Get userData for Node
var userData string
if constraints.AMIFamily != nil {
if strings.EqualFold(*constraints.AMIFamily, v1alpha1.OperatingSystemBottleRocket) {
userData, err = p.getBottleRocketUserData(ctx, constraints, additionalLabels)
} else if strings.EqualFold(*constraints.AMIFamily, v1alpha1.OperatingSystemEKSOptimized) {
userData, err = p.getEKSOptimizedUserData(ctx, constraints, instanceTypes, additionalLabels)
} else {
logging.FromContext(ctx).Warnf("AMIFamily was set, but was not one of %s or %s. Setting to %s as the default.", v1alpha1.OperatingSystemEKSOptimized, v1alpha1.OperatingSystemBottleRocket, v1alpha1.OperatingSystemEKSOptimized)
userData, err = p.getEKSOptimizedUserData(ctx, constraints, instanceTypes, additionalLabels)
}
} else {
userData, err = p.getEKSOptimizedUserData(ctx, constraints, instanceTypes, additionalLabels)
userData, err := p.getEKSOptimizedUserData(ctx, constraints, instanceTypes, additionalLabels)
bwagner5 marked this conversation as resolved.
Show resolved Hide resolved
if strings.EqualFold(aws.StringValue(constraints.AMIFamily), v1alpha1.AMIFamilyBottlerocket) {
userData, err = p.getBottlerocketUserData(ctx, constraints, additionalLabels)
}
if err != nil {
bwagner5 marked this conversation as resolved.
Show resolved Hide resolved
return nil, err
Expand Down Expand Up @@ -252,19 +242,11 @@ func sortedKeys(m map[string]string) []string {
return keys
}

func (p *LaunchTemplateProvider) getBottleRocketUserData(ctx context.Context, constraints *v1alpha1.Constraints, additionalLabels map[string]string) (string, error) {
var userData string
userData += fmt.Sprintf(`[settings.kubernetes]
cluster-name = "%s"
api-server = "%s"
`,
injection.GetOptions(ctx).ClusterName,
injection.GetOptions(ctx).ClusterEndpoint)

func (p *LaunchTemplateProvider) getBottlerocketUserData(ctx context.Context, constraints *v1alpha1.Constraints, additionalLabels map[string]string) (string, error) {
userData := fmt.Sprintf("[settings.kubernetes]\ncluster-name = \"%s\"\napi-server = \"%s\"\n", injection.GetOptions(ctx).ClusterName, injection.GetOptions(ctx).ClusterEndpoint)
if constraints.KubeletConfiguration.ClusterDNS != nil {
userData += fmt.Sprintf("cluster-dns-ip = \"%s\"", constraints.KubeletConfiguration.ClusterDNS)
userData += fmt.Sprintf("cluster-dns-ip = \"%s\"\n", constraints.KubeletConfiguration.ClusterDNS)
}

caBundle, err := p.GetCABundle(ctx)
if err != nil {
return "", fmt.Errorf("getting ca bundle for user data, %w", err)
Expand All @@ -275,22 +257,18 @@ api-server = "%s"

nodeLabelArgs := functional.UnionStringMaps(additionalLabels, constraints.Labels)
if len(nodeLabelArgs) > 0 {
userData += `[settings.kubernetes.node-labels]
`
userData += "[settings.kubernetes.node-labels]\n"
for key, val := range nodeLabelArgs {
userData += fmt.Sprintf("\"%s\" = \"%s\"", key, val)
userData += fmt.Sprintf("\"%s\" = \"%s\"\n", key, val)
}
}

if len(constraints.Taints) > 0 {
userData += `[settings.kubernetes.node-taints]
`
userData += "[settings.kubernetes.node-taints]\n"
sorted := sortedTaints(constraints.Taints)
for _, taint := range sorted {
userData += fmt.Sprintf("%s=%s:%s", taint.Key, taint.Value, taint.Effect)
userData += fmt.Sprintf("%s=%s:%s\n", taint.Key, taint.Value, taint.Effect)
}
}

return base64.StdEncoding.EncodeToString([]byte(userData)), nil
}

Expand Down