Skip to content

Commit

Permalink
Launch Template Custom Tag Propogation to Worker Nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
elamaran11 committed Mar 13, 2023
1 parent 07c6413 commit f7c7213
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
16 changes: 12 additions & 4 deletions examples/blueprint-construct/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,14 @@ export default class BlueprintConstruct {
nodeGroupSubnets: { subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS }
},
{
id: "mng2-customami",
id: "mng2-launchtemplate",
instanceTypes: [new ec2.InstanceType('t3.large')],
nodeGroupCapacityType: CapacityType.SPOT,
desiredSize: 0,
minSize: 0,
customAmi: {
diskSize: 25,
desiredSize: 2,
minSize: 2,
maxSize: 3,
launchTemplate: {
machineImage: ec2.MachineImage.genericLinux({
'us-east-1': 'ami-08e520f5673ee0894',
'us-west-2': 'ami-0403ff342ceb30967',
Expand All @@ -208,6 +210,12 @@ export default class BlueprintConstruct {
'us-gov-east-1':'ami-033eb9bc6daf8bfb1'
}),
userData: userData,
customTags: {
"Name": "Mng2",
"Type": "Managed-Node-Group",
"LaunchTemplate": "Custom",
"Instance": "SPOT"
}
}
}
]
Expand Down
11 changes: 7 additions & 4 deletions lib/cluster-providers/generic-cluster-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import * as constants from './constants';
import { AutoscalingNodeGroup, ManagedNodeGroup } from "./types";
import assert = require('assert');
import { ManagedPolicy } from "aws-cdk-lib/aws-iam";
import { Tags } from "aws-cdk-lib/core/lib/tag-aspect";

export function clusterBuilder() {
return new ClusterBuilder();
Expand Down Expand Up @@ -350,16 +351,18 @@ export class GenericClusterProvider implements ClusterProvider {
}
};

if (nodeGroup.customAmi) {
// Create launch template if custom AMI is provided.
if (nodeGroup.launchTemplate) {
// Create launch template with provided launch template properties
const lt = new ec2.LaunchTemplate(cluster, `${nodeGroup.id}-lt`, {
machineImage: nodeGroup.customAmi?.machineImage,
userData: nodeGroup.customAmi?.userData,
machineImage: nodeGroup.launchTemplate?.machineImage,
userData: nodeGroup.launchTemplate?.userData,
});
utils.setPath(nodegroupOptions, "launchTemplateSpec", {
id: lt.launchTemplateId!,
version: lt.latestVersionNumber,
});
const customTags = Object.entries(nodeGroup.launchTemplate.customTags ?? {});
customTags.forEach(([key, options]) => Tags.of(lt).add(key,options));
delete nodegroupOptions.amiType;
delete nodegroupOptions.releaseVersion;
}
Expand Down
15 changes: 12 additions & 3 deletions lib/cluster-providers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { AutoScalingGroupCapacityOptions } from "aws-cdk-lib/aws-eks";
/**
* Configuration options for the custom AMI.
*/
export interface CustomAmiProps {
export interface LaunchTemplateProps {
/**
* The custom AMI for the node group.
*/
Expand All @@ -15,6 +15,14 @@ export interface CustomAmiProps {
* The userData for worker node when using custom AMI. Only applicable when customAmi is used.
*/
userData?: ec2.UserData;

/**
* Custom Tags for launch template which will propogate to worker nodes.
*/
customTags?: {
[key: string]: string;
}

}


Expand Down Expand Up @@ -60,9 +68,10 @@ export interface ManagedNodeGroup extends Omit<eks.NodegroupOptions, "launchTemp
amiReleaseVersion?: string;

/**
* The custom AMI for the node group. `amiType` and `amiReleaseVersion` will be ignored if this is set.
* The Launch Template properties for the Nodes.
* `amiType` and `amiReleaseVersion` will be ignored if this is set.
*/
customAmi?: CustomAmiProps;
launchTemplate?: LaunchTemplateProps;

/**
* Select either SPOT or ON-DEMAND
Expand Down

0 comments on commit f7c7213

Please sign in to comment.