Skip to content

Commit

Permalink
bottlerocket validation and some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
bwagner5 committed Feb 8, 2022
1 parent a89b7be commit fd52b52
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 45 deletions.
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 {
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) {
ssmQuery = p.getBottlerocketAlias(version, instanceType)
}

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) {
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"
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)
if strings.EqualFold(aws.StringValue(constraints.AMIFamily), v1alpha1.AMIFamilyBottlerocket) {
userData, err = p.getBottlerocketUserData(ctx, constraints, additionalLabels)
}
if err != nil {
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

0 comments on commit fd52b52

Please sign in to comment.