Skip to content

Commit

Permalink
feat(eks): bottlerocket versoin follows the cluster k8s versoin (#10189)
Browse files Browse the repository at this point in the history
This PR will implicitly select correct bottlerocket variant in consist with the  cluster k8s version. For example, if the cluster k8s version is `1.17`, previously only bottlerocket `aws-k8s-1.15` variant is available. Now the variant will be `aws-k8s-1.17`

BREAKING CHANGE: Clusters previously running k8s version other than `1.15` and bottlerocket AMI(`aws-k8s-1.15` variant) will trigger AMI and node replacement.

Closes: #10188 

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
pahud authored Sep 14, 2020
1 parent 406f665 commit 19638a6
Show file tree
Hide file tree
Showing 5 changed files with 551 additions and 668 deletions.
6 changes: 5 additions & 1 deletion packages/@aws-cdk/aws-eks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@ chart2.node.addDependency(chart1);

[Bottlerocket](https://aws.amazon.com/bottlerocket/) is a Linux-based open-source operating system that is purpose-built by Amazon Web Services for running containers on virtual machines or bare metal hosts. At this moment the managed nodegroup only supports Amazon EKS-optimized AMI but it's possible to create a capacity of self-managed `AutoScalingGroup` running with bottlerocket Linux AMI.

> **NOTICE**: Bottlerocket is in public preview and only available in [some supported AWS regions](https://github.com/bottlerocket-os/bottlerocket/blob/develop/QUICKSTART.md#finding-an-ami).
> **NOTICE**: Bottlerocket is only available in [some supported AWS regions](https://github.com/bottlerocket-os/bottlerocket/blob/develop/QUICKSTART-EKS.md#finding-an-ami).
The following example will create a capacity with self-managed Amazon EC2 capacity of 2 `t3.small` Linux instances running with `Bottlerocket` AMI.

Expand All @@ -739,6 +739,10 @@ cluster.addCapacity('BottlerocketNodes', {
});
```

The Bottlerocket AMI will be auto selected with the variant of different k8s version for the `x86_64` architecture.
For example, if the Amazon EKS cluster version is `1.17`, the Bottlerocket AMI variant will be auto selected as
`aws-k8s-1.17` behind the scene. See [Variants](https://github.com/bottlerocket-os/bottlerocket/blob/develop/README.md#variants) for more details.

To define only Bottlerocket capacity in your cluster, set `defaultCapacity` to `0` when you define the cluster as described above.

Please note Bottlerocket does not allow to customize bootstrap options and `bootstrapOptions` properties is not supported when you create the `Bottlerocket` capacity.
Expand Down
37 changes: 4 additions & 33 deletions packages/@aws-cdk/aws-eks/lib/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { KubernetesObjectValue } from './k8s-object-value';
import { KubernetesPatch } from './k8s-patch';
import { KubectlProvider } from './kubectl-provider';
import { Nodegroup, NodegroupOptions } from './managed-nodegroup';
import { BottleRocketImage } from './private/bottlerocket';
import { ServiceAccount, ServiceAccountOptions } from './service-account';
import { LifecycleLabel, renderAmazonLinuxUserData, renderBottlerocketUserData } from './user-data';

Expand Down Expand Up @@ -1045,7 +1046,9 @@ export class Cluster extends ClusterBase {
...options,
vpc: this.vpc,
machineImage: options.machineImageType === MachineImageType.BOTTLEROCKET ?
new BottleRocketImage() :
new BottleRocketImage({
kubernetesVersion: this.version.version,
}) :
new EksOptimizedImage({
nodeType: nodeTypeForInstanceType(options.instanceType),
cpuArch: cpuArchForInstanceType(options.instanceType),
Expand Down Expand Up @@ -1695,38 +1698,6 @@ export class EksOptimizedImage implements ec2.IMachineImage {
}
}

/**
* Construct an Bottlerocket image from the latest AMI published in SSM
*/
class BottleRocketImage implements ec2.IMachineImage {
private readonly kubernetesVersion?: string;

private readonly amiParameterName: string;

/**
* Constructs a new instance of the BottleRocketImage class.
*/
public constructor() {
// only 1.15 is currently available
this.kubernetesVersion = '1.15';

// set the SSM parameter name
this.amiParameterName = `/aws/service/bottlerocket/aws-k8s-${this.kubernetesVersion}/x86_64/latest/image_id`;
}

/**
* Return the correct image
*/
public getImage(scope: Construct): ec2.MachineImageConfig {
const ami = ssm.StringParameter.valueForStringParameter(scope, this.amiParameterName);
return {
imageId: ami,
osType: ec2.OperatingSystemType.LINUX,
userData: ec2.UserData.custom(''),
};
}
}

// MAINTAINERS: use ./scripts/kube_bump.sh to update LATEST_KUBERNETES_VERSION
const LATEST_KUBERNETES_VERSION = '1.14';

Expand Down
44 changes: 44 additions & 0 deletions packages/@aws-cdk/aws-eks/lib/private/bottlerocket.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import * as ec2 from '@aws-cdk/aws-ec2';
import * as ssm from '@aws-cdk/aws-ssm';
import { Construct } from '@aws-cdk/core';

/**
* Properties for BottleRocketImage
*/
export interface BottleRocketImageProps {
/**
* The Kubernetes version to use
*/
readonly kubernetesVersion: string;
}

/**
* Construct an Bottlerocket image from the latest AMI published in SSM
*/
export class BottleRocketImage implements ec2.IMachineImage {
private readonly kubernetesVersion: string;

private readonly amiParameterName: string;

/**
* Constructs a new instance of the BottleRocketImage class.
*/
public constructor(props: BottleRocketImageProps) {
this.kubernetesVersion = props.kubernetesVersion;

// set the SSM parameter name
this.amiParameterName = `/aws/service/bottlerocket/aws-k8s-${this.kubernetesVersion}/x86_64/latest/image_id`;
}

/**
* Return the correct image
*/
public getImage(scope: Construct): ec2.MachineImageConfig {
const ami = ssm.StringParameter.valueForStringParameter(scope, this.amiParameterName);
return {
imageId: ami,
osType: ec2.OperatingSystemType.LINUX,
userData: ec2.UserData.custom(''),
};
}
}
Loading

0 comments on commit 19638a6

Please sign in to comment.