From 26e8afb0af8813180d8ef078586cb31892ff4837 Mon Sep 17 00:00:00 2001 From: Mikhail Shapirov Date: Wed, 2 Nov 2022 18:19:26 -0400 Subject: [PATCH] Added lambda layers for 1.22 and 1.23 --- lib/addons/cluster-autoscaler/index.ts | 5 +- .../generic-cluster-provider.ts | 30 ++++++++++-- package.json | 2 + test/clusterprovider.test.ts | 47 +++++++++++++++++++ 4 files changed, 79 insertions(+), 5 deletions(-) diff --git a/lib/addons/cluster-autoscaler/index.ts b/lib/addons/cluster-autoscaler/index.ts index 2f989cb06..de9f6b9df 100644 --- a/lib/addons/cluster-autoscaler/index.ts +++ b/lib/addons/cluster-autoscaler/index.ts @@ -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"], diff --git a/lib/cluster-providers/generic-cluster-provider.ts b/lib/cluster-providers/generic-cluster-provider.ts index 008b3efb0..37a30cd33 100644 --- a/lib/cluster-providers/generic-cluster-provider.ts +++ b/lib/cluster-providers/generic-cluster-provider.ts @@ -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(); @@ -205,7 +210,9 @@ 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 = { vpc, secretsEncryptionKey, clusterName, @@ -213,6 +220,7 @@ export class GenericClusterProvider implements ClusterProvider { version, vpcSubnets, endpointAccess, + kubectlLayer, defaultCapacity: 0 // we want to manage capacity ourselves }; @@ -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 diff --git a/package.json b/package.json index a804d0088..b361e21a7 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/clusterprovider.test.ts b/test/clusterprovider.test.ts index 559726dbe..f5a8fa88e 100644 --- a/test/clusterprovider.test.ts +++ b/test/clusterprovider.test.ts @@ -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'; @@ -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); }); \ No newline at end of file