Skip to content

Commit

Permalink
Merge pull request #528 from aws-quickstart/task/lambda-layer-update
Browse files Browse the repository at this point in the history
Added lambda layers for 1.22 and 1.23
  • Loading branch information
shapirov103 authored Nov 3, 2022
2 parents 18f9f16 + 26e8afb commit c03512b
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 5 deletions.
5 changes: 3 additions & 2 deletions lib/addons/cluster-autoscaler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ const defaultProps: ClusterAutoScalerAddOnProps = {
* Version of the autoscaler, controls the image tag
*/
const versionMap = new Map([
[KubernetesVersion.V1_22, "9.11.0"],
[KubernetesVersion.V1_21, "9.10.8"],
[KubernetesVersion.V1_23, "9.21.0"],
[KubernetesVersion.V1_22, "9.13.1"],
[KubernetesVersion.V1_21, "9.13.1"],
[KubernetesVersion.V1_20, "9.9.2"],
[KubernetesVersion.V1_19, "9.4.0"],
[KubernetesVersion.V1_18, "9.4.0"],
Expand Down
30 changes: 27 additions & 3 deletions lib/cluster-providers/generic-cluster-provider.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@

import { KubectlV22Layer } from "@aws-cdk/lambda-layer-kubectl-v22";
import { KubectlV23Layer } from "@aws-cdk/lambda-layer-kubectl-v23";
import { } from "aws-cdk-lib/";
import * as autoscaling from 'aws-cdk-lib/aws-autoscaling';
import * as ec2 from "aws-cdk-lib/aws-ec2";
import { IVpc } from "aws-cdk-lib/aws-ec2";
import * as eks from "aws-cdk-lib/aws-eks";
import { IKey } from "aws-cdk-lib/aws-kms";
import { ILayerVersion } from "aws-cdk-lib/aws-lambda";
import { Construct } from "constructs";
import { ClusterInfo, ClusterProvider } from "../spi";
import * as utils from "../utils";
import * as constants from './constants';
import { AutoscalingNodeGroup, ManagedNodeGroup } from "./types";
import assert = require('assert');
import {IVpc} from "aws-cdk-lib/aws-ec2";
import {IKey} from "aws-cdk-lib/aws-kms";

export function clusterBuilder() {
return new ClusterBuilder();
Expand Down Expand Up @@ -205,14 +210,17 @@ export class GenericClusterProvider implements ClusterProvider {
const endpointAccess = (privateCluster === true) ? eks.EndpointAccess.PRIVATE : eks.EndpointAccess.PUBLIC_AND_PRIVATE;
const vpcSubnets = this.props.vpcSubnets ?? (privateCluster === true ? [{ subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS }] : undefined);

const defaultOptions = {
const kubectlLayer = this.getKubectlLayer(scope, version);

const defaultOptions: Partial<eks.ClusterProps> = {
vpc,
secretsEncryptionKey,
clusterName,
outputClusterName,
version,
vpcSubnets,
endpointAccess,
kubectlLayer,
defaultCapacity: 0 // we want to manage capacity ourselves
};

Expand Down Expand Up @@ -251,6 +259,22 @@ export class GenericClusterProvider implements ClusterProvider {
return new eks.Cluster(scope, id, clusterOptions);
}

/**
* Can be overridden to provide a custom kubectl layer.
* @param scope
* @param version
* @returns
*/
protected getKubectlLayer(scope: Construct, version: eks.KubernetesVersion) : ILayerVersion | undefined {
switch(version) {
case eks.KubernetesVersion.V1_23:
return new KubectlV23Layer(scope, "kubectllayer23");
case eks.KubernetesVersion.V1_22:
return new KubectlV22Layer(scope, "kubectllayer22");
}
return undefined;
}

/**
* Adds an autoscaling group to the cluster.
* @param cluster
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
"typescript": "~4.8.4"
},
"dependencies": {
"@aws-cdk/lambda-layer-kubectl-v22": "^2.0.0",
"@aws-cdk/lambda-layer-kubectl-v23": "^2.0.0",
"@types/assert": "^1.5.6",
"@types/bcrypt": "^5.0.0",
"@types/lodash.clonedeep": "^4.5.7",
Expand Down
47 changes: 47 additions & 0 deletions test/clusterprovider.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as cdk from 'aws-cdk-lib';
import { Match, Template } from 'aws-cdk-lib/assertions';
import { BlockDeviceVolume, EbsDeviceVolumeType } from 'aws-cdk-lib/aws-autoscaling';
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import { SubnetType } from 'aws-cdk-lib/aws-ec2';
Expand Down Expand Up @@ -148,4 +149,50 @@ test("Asg cluster provider correctly initializes self-managed node group", () =>

expect(blueprint.getClusterInfo().autoscalingGroups).toBeDefined();
expect(blueprint.getClusterInfo().autoscalingGroups!.length).toBe(1);
});

test("Kubectl layer is correctly injected for EKS version 1.22", () => {

const app = new cdk.App();

const stack = blueprints.EksBlueprint.builder()
.account('123456789').region('us-west-2')
.version(KubernetesVersion.V1_22).build(app, "stack-122");

const template = Template.fromStack(stack);

template.hasResource("AWS::Lambda::LayerVersion", {
Properties: {
Description: Match.stringLikeRegexp("/opt/kubectl/kubectl 1.22"),
},
});
});

test("Kubectl layer is correctly injected for EKS version 1.23", () => {

const app = new cdk.App();

const stack = blueprints.EksBlueprint.builder()
.account('123456789').region('us-west-2')
.version(KubernetesVersion.V1_23).build(app, "stack-123");

const template = Template.fromStack(stack);

template.hasResource("AWS::Lambda::LayerVersion", {
Properties: {
Description: Match.stringLikeRegexp("/opt/kubectl/kubectl 1.23"),
},
});
});


test("Kubectl layer is correctly injected for EKS version 1.21 and below", () => {
const app = new cdk.App();

const stackV122 = blueprints.EksBlueprint.builder()
.account('123456789').region('us-west-2')
.version(KubernetesVersion.V1_21).build(app, "stack-122");

const template = Template.fromStack(stackV122);
template.resourceCountIs("AWS::Lambda::LayerVersion", 0);
});

0 comments on commit c03512b

Please sign in to comment.