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

Managed Node Group Launch Template Improvements #1225

Closed
Show file tree
Hide file tree
Changes from all commits
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
42 changes: 42 additions & 0 deletions examples/examples_nodejs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,48 @@ func TestAccMNG_DiskSize(t *testing.T) {
programTestWithExtraOptions(t, &test, nil)
}

func TestAccMNG_Gpu(t *testing.T) {
test := getJSBaseOptions(t).
With(integration.ProgramTestOptions{
Dir: path.Join(getCwd(t), "tests", "nodegroup-options"),
ExtraRuntimeValidation: func(t *testing.T, info integration.RuntimeValidationStackInfo) {
utils.RunEKSSmokeTest(t,
info.Deployment.Resources,
info.Outputs["kubeconfig"],
)
},
})

programTestWithExtraOptions(t, &test, nil)
}

func TestAccMNG_AmiOptions(t *testing.T) {
test := getJSBaseOptions(t).
With(integration.ProgramTestOptions{
Dir: path.Join(getCwd(t), "tests", "managed-ng-ami-options", "ami-id"),
ExtraRuntimeValidation: func(t *testing.T, info integration.RuntimeValidationStackInfo) {
utils.RunEKSSmokeTest(t,
info.Deployment.Resources,
info.Outputs["kubeconfig"],
)
},
EditDirs: []integration.EditDir{
{
Dir: path.Join(getCwd(t), "managed-ng-ami-options", "gpu"),
Additive: true,
ExtraRuntimeValidation: func(t *testing.T, info integration.RuntimeValidationStackInfo) {
utils.RunEKSSmokeTest(t,
info.Deployment.Resources,
info.Outputs["kubeconfig"],
)
},
},
},
})

programTestWithExtraOptions(t, &test, nil)
}

func TestAccTags(t *testing.T) {
test := getJSBaseOptions(t).
With(integration.ProgramTestOptions{
Expand Down
3 changes: 3 additions & 0 deletions examples/tests/managed-ng-ami-options/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: managed-ng-ami-options
description: Tests that various AMI related options can be set on managed nodegroup.
runtime: nodejs
6 changes: 6 additions & 0 deletions examples/tests/managed-ng-ami-options/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# tests/managed-ng-ami-options

Tests that various AMI related options can be set on managed nodegroup
Includes:
- amiId
- gpu
53 changes: 53 additions & 0 deletions examples/tests/managed-ng-ami-options/ami-id/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import * as awsx from '@pulumi/awsx';
import * as eks from "@pulumi/eks";
import * as pulumi from "@pulumi/pulumi";
import * as iam from "../iam";
import {GetParameterCommand, SSMClient} from "@aws-sdk/client-ssm";

const eksVpc = new awsx.ec2.Vpc("eks-vpc", {
enableDnsHostnames: true,
cidrBlock: "10.0.0.0/16",
});

// IAM roles for the node groups.
const role = iam.createRole("example-role");

const projectName = pulumi.getProject();

const cluster = new eks.Cluster(`${projectName}`, {
skipDefaultNodeGroup: true,
deployDashboard: false,
vpcId: eksVpc.vpcId,
// Public subnets will be used for load balancers
publicSubnetIds: eksVpc.publicSubnetIds,
// Private subnets will be used for cluster nodes
privateSubnetIds: eksVpc.privateSubnetIds,
instanceRoles: [role],
});

// Export the cluster's kubeconfig.
export const kubeconfig = cluster.kubeconfig;

// Find the recommended AMI for the EKS node group.
// See https://docs.aws.amazon.com/eks/latest/userguide/retrieve-ami-id.html
async function getEksAmiId(k8sVersion: string): Promise<string> {
const client = new SSMClient();
Copy link
Contributor

Choose a reason for hiding this comment

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

You can do that in pulumi natively. See here for an example:

const ami = pulumi.interpolate`/aws/service/eks/optimized-ami/${cluster.core.cluster.version}/amazon-linux-2/recommended/image_id`.apply(name =>
aws.ssm.getParameter({ name }, { async: true })
).apply(result => result.value);

const parameterName = `/aws/service/eks/optimized-ami/${k8sVersion}/amazon-linux/recommended/image_id`;
const command = new GetParameterCommand({ Name: parameterName });
const response = await client.send(command);

if (!response.Parameter || !response.Parameter.Value) {
throw new Error(`Could not find EKS optimized AMI for Kubernetes version ${k8sVersion}`);
}

return response.Parameter.Value;
}

const amiId = cluster.eksCluster.version.apply(version => pulumi.output(getEksAmiId(version)));

// Create a managed node group using a cluster as input.
eks.createManagedNodeGroup(`${projectName}-managed-ng`, {
Copy link
Contributor

Choose a reason for hiding this comment

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

What do you think about combining the ami and gpu tests into one?
One test with two nodegroups should be fine.

We need to balance the number of eks clusters we're creating and here we're interested in the node group.

cluster: cluster,
nodeRole: role,
amiId: amiId,
});
35 changes: 35 additions & 0 deletions examples/tests/managed-ng-ami-options/gpu/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import * as awsx from '@pulumi/awsx';
import * as eks from "@pulumi/eks";
import * as pulumi from "@pulumi/pulumi";
import * as iam from "../iam";

const eksVpc = new awsx.ec2.Vpc("eks-vpc", {
enableDnsHostnames: true,
cidrBlock: "10.0.0.0/16",
});

// IAM roles for the node groups.
const role = iam.createRole("example-role");

const projectName = pulumi.getProject();

const cluster = new eks.Cluster(`${projectName}`, {
skipDefaultNodeGroup: true,
deployDashboard: false,
vpcId: eksVpc.vpcId,
// Public subnets will be used for load balancers
publicSubnetIds: eksVpc.publicSubnetIds,
// Private subnets will be used for cluster nodes
privateSubnetIds: eksVpc.privateSubnetIds,
instanceRoles: [role],
});

// Export the cluster's kubeconfig.
export const kubeconfig = cluster.kubeconfig;

// Create a managed node group using a cluster as input.
eks.createManagedNodeGroup(`${projectName}-managed-ng`, {
cluster: cluster,
nodeRole: role,
gpu: true
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this have any special instance type requirements or can we launch a GPU optimized AMI on a basic t3 instance for example?

});
39 changes: 39 additions & 0 deletions examples/tests/managed-ng-ami-options/iam.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import * as aws from "@pulumi/aws";
import * as pulumi from "@pulumi/pulumi";
import * as iam from "./iam";

const managedPolicyArns: string[] = [
"arn:aws:iam::aws:policy/AmazonEKSWorkerNodePolicy",
"arn:aws:iam::aws:policy/AmazonEKS_CNI_Policy",
"arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly",
];

// Creates a role and attaches the EKS worker node IAM managed policies
export function createRole(name: string): aws.iam.Role {
const role = new aws.iam.Role(name, {
assumeRolePolicy: aws.iam.assumeRolePolicyForPrincipal({
Service: "ec2.amazonaws.com",
}),
});

let counter = 0;
for (const policy of managedPolicyArns) {
// Create RolePolicyAttachment without returning it.
const rpa = new aws.iam.RolePolicyAttachment(`${name}-policy-${counter++}`,
{ policyArn: policy, role: role },
);
}

return role;
}

// Creates a collection of IAM roles.
export function createRoles(name: string, quantity: number): aws.iam.Role[] {
const roles: aws.iam.Role[] = [];

for (let i = 0; i < quantity; i++) {
roles.push(iam.createRole(`${name}-role-${i}`));
}

return roles;
}
14 changes: 14 additions & 0 deletions examples/tests/managed-ng-ami-options/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "managed-ng-ami-options",
"devDependencies": {
"typescript": "^4.0.0",
"@types/node": "latest"
},
"dependencies": {
"@pulumi/pulumi": "^3.0.0",
"@pulumi/aws": "^6.0.0",
"@pulumi/eks": "latest",
"@pulumi/awsx": "^2.0.0",
"@aws-sdk/client-ssm": "^3.637.0"
}
}
25 changes: 25 additions & 0 deletions examples/tests/managed-ng-ami-options/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"compilerOptions": {
"outDir": "bin",
"target": "es6",
"lib": [
"es6"
],
"module": "commonjs",
"moduleResolution": "node",
"declaration": true,
"sourceMap": true,
"stripInternal": true,
"experimentalDecorators": true,
"pretty": true,
"noFallthroughCasesInSwitch": true,
"noImplicitAny": true,
"noImplicitReturns": true,
"forceConsistentCasingInFileNames": true,
"strictNullChecks": true
},
"files": [
"ami-id/index.ts",
"gpu/index.ts",
]
}
37 changes: 36 additions & 1 deletion nodejs/eks/nodegroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1544,6 +1544,33 @@ export type ManagedNodeGroupOptions = Omit<
* - maxSize: 2
*/
scalingConfig?: pulumi.Input<awsInputs.eks.NodeGroupScalingConfig>;

/**
* The AMI ID to use for the worker nodes.
*
* Defaults to the latest recommended EKS Optimized Linux AMI from the
* AWS Systems Manager Parameter Store.
*
* Note: `amiId` and `gpu` are mutually exclusive.
*
* See for more details:
* - https://docs.aws.amazon.com/eks/latest/userguide/eks-optimized-ami.html.
*/
amiId?: pulumi.Input<string>;
JustASquid marked this conversation as resolved.
Show resolved Hide resolved

/**
* Use the latest recommended EKS Optimized Linux AMI with GPU support for
* the worker nodes from the AWS Systems Manager Parameter Store.
*
* Defaults to false.
*
* Note: `gpu` and `amiId` are mutually exclusive.
*
* See for more details:
* - https://docs.aws.amazon.com/eks/latest/userguide/eks-optimized-ami.html.
* - https://docs.aws.amazon.com/eks/latest/userguide/retrieve-ami-id.html
*/
gpu?: pulumi.Input<boolean>;
};

/**
Expand Down Expand Up @@ -1668,6 +1695,14 @@ function createManagedNodeGroupInternal(
);
}

if (args.amiId && args.gpu) {
throw new pulumi.ResourceError("amiId and gpu are mutually exclusive.", parent);
}

if (args.amiType && args.gpu) {
throw new pulumi.ResourceError("amiType and gpu are mutually exclusive.", parent);
}

let roleArn: pulumi.Input<string>;
if (args.nodeRoleArn) {
roleArn = args.nodeRoleArn;
Expand Down Expand Up @@ -1740,7 +1775,7 @@ function createManagedNodeGroupInternal(
}

let launchTemplate: aws.ec2.LaunchTemplate | undefined;
if (args.kubeletExtraArgs || args.bootstrapExtraArgs || args.enableIMDSv2) {
if (args.kubeletExtraArgs || args.bootstrapExtraArgs || args.enableIMDSv2 || args.gpu || args.amiId || args.amiType) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we need to create a custom launch template if amiType is set? That's a valid input for aws.eks.NodeGroup on its own.

Aside from that, the list here gets a bit long now. What do you think about creating a method for deciding whether to create a custom launch template?

launchTemplate = createMNGCustomLaunchTemplate(name, args, core, parent, provider);

// Disk size is specified in the launch template.
Expand Down
15 changes: 15 additions & 0 deletions provider/cmd/pulumi-gen-eks/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,21 @@ func generateSchema() schema.PackageSpec {
"(https://docs.aws.amazon.com/eks/latest/APIReference/API_Nodegroup.html#AmazonEKS-Type-Nodegroup-amiType) " +
"for valid AMI Types. This provider will only perform drift detection if a configuration value is provided.",
},
"amiId": {
Copy link
Contributor

Choose a reason for hiding this comment

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

You'll need to run make build and check in the changes to the schema and sdk folder. Otherwise those don't take effect.

Because of the disparity between the nodejs SDK and the rest of the SDKs it would be good to add a test in another language as well (e.g. Golang). Let me know if you need help with converting the test you've created into another language

TypeSpec: schema.TypeSpec{Type: "string"},
Description: "The AMI ID to use for the worker nodes.\n\nDefaults to the latest recommended " +
"EKS Optimized Linux AMI from the AWS Systems Manager Parameter Store.\n\nNote: " +
"`amiId` and `gpu` are mutually exclusive.\n\nSee for more details:\n" +
"- https://docs.aws.amazon.com/eks/latest/userguide/eks-optimized-ami.html.",
},
"gpu": {
TypeSpec: schema.TypeSpec{Type: "boolean"},
Description: "Use the latest recommended EKS Optimized Linux AMI with GPU support for the " +
"worker nodes from the AWS Systems Manager Parameter Store.\n\nDefaults to false.\n\n" +
"Note: `gpu` and `amiId` are mutually exclusive.\n\nSee for more details:\n" +
"- https://docs.aws.amazon.com/eks/latest/userguide/eks-optimized-ami.html\n" +
"- https://docs.aws.amazon.com/eks/latest/userguide/retrieve-ami-id.html",
},
"capacityType": {
TypeSpec: schema.TypeSpec{Type: "string"},
Description: "Type of capacity associated with the EKS Node Group. " +
Expand Down