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

Fix for #997 - Enable control plane logging with CDK native (dropping custom resource) #998

Merged
merged 3 commits into from
May 3, 2024
Merged
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
5 changes: 4 additions & 1 deletion examples/examples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ builder()
.build(app, "fargate-blueprint");

builder()
.clusterProvider(new bp.MngClusterProvider(publicCluster))
.clusterProvider(new bp.MngClusterProvider({
...publicCluster
}))
.enableControlPlaneLogTypes(bp.ControlPlaneLogType.API, bp.ControlPlaneLogType.AUDIT)
.build(app, "mng-blueprint");

builder()
Expand Down
3 changes: 2 additions & 1 deletion lib/cluster-providers/generic-cluster-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ export class GenericClusterProvider implements ClusterProvider {
/**
* @override
*/
createCluster(scope: Construct, vpc: ec2.IVpc, secretsEncryptionKey: IKey | undefined, kubernetesVersion: eks.KubernetesVersion | undefined): ClusterInfo {
createCluster(scope: Construct, vpc: ec2.IVpc, secretsEncryptionKey?: IKey, kubernetesVersion?: eks.KubernetesVersion, clusterLogging?: eks.ClusterLoggingTypes[]) : ClusterInfo {
const id = scope.node.id;

// Props for the cluster.
Expand All @@ -271,6 +271,7 @@ export class GenericClusterProvider implements ClusterProvider {
vpc,
secretsEncryptionKey,
clusterName,
clusterLogging,
outputClusterName,
version,
vpcSubnets,
Expand Down
5 changes: 3 additions & 2 deletions lib/spi/cluster-contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import { ClusterInfo } from '.';
import { Construct } from "constructs";
import { IVpc } from 'aws-cdk-lib/aws-ec2';
import {IKey} from "aws-cdk-lib/aws-kms";
import { KubernetesVersion } from 'aws-cdk-lib/aws-eks';
import { ClusterLoggingTypes, KubernetesVersion } from 'aws-cdk-lib/aws-eks';



/**
* ClusterProvider is the interface to which all Cluster Providers should conform.
*/
export declare interface ClusterProvider {
createCluster(scope: Construct, vpc: IVpc, secretsEncryptionKey?: IKey, kubernetesVersion?: KubernetesVersion): ClusterInfo;
createCluster(scope: Construct, vpc: IVpc, secretsEncryptionKey?: IKey, kubernetesVersion?: KubernetesVersion, clusterLogging?: ClusterLoggingTypes[] ): ClusterInfo;
}

23 changes: 7 additions & 16 deletions lib/stacks/eks-blueprint-stack.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as cdk from 'aws-cdk-lib';
import { IVpc } from 'aws-cdk-lib/aws-ec2';
import { KubernetesVersion } from 'aws-cdk-lib/aws-eks';
import { ClusterLoggingTypes as ControlPlaneLogType, KubernetesVersion } from 'aws-cdk-lib/aws-eks';
import { Construct } from 'constructs';
import { MngClusterProvider } from '../cluster-providers/mng-cluster-provider';
import { VpcProvider } from '../resource-providers/vpc';
Expand All @@ -15,6 +15,11 @@ import { ArgoGitOpsFactory } from "../addons/argocd/argo-gitops-factory";
/* Default K8s version of EKS Blueprints */
export const DEFAULT_VERSION = KubernetesVersion.V1_29;

/**
* Exporting control plane log type so that customers don't have to import CDK EKS module for blueprint configuration.
*/
export { ControlPlaneLogType };

export class EksBlueprintProps {
/**
* The id for the blueprint.
Expand Down Expand Up @@ -90,15 +95,6 @@ export class BlueprintPropsConstraints implements constraints.ConstraintsType<Ek
name = new constraints.StringConstraint(1, 63);
}

export const enum ControlPlaneLogType {

API = 'api',
AUDIT = 'audit',
AUTHENTICATOR = 'authenticator',
CONTROLLER_MANAGER = 'controllerManager',
SCHEDULER = 'scheduler'
}

/**
* Blueprint builder implements a builder pattern that improves readability (no bloated constructors)
* and allows creating a blueprint in an abstract state that can be applied to various instantiations
Expand Down Expand Up @@ -256,14 +252,9 @@ export class EksBlueprint extends cdk.Stack {
version
});

this.clusterInfo = clusterProvider.createCluster(this, vpcResource!, kmsKeyResource, version);
this.clusterInfo = clusterProvider.createCluster(this, vpcResource!, kmsKeyResource, version, blueprintProps.enableControlPlaneLogTypes);
this.clusterInfo.setResourceContext(resourceContext);

let enableLogTypes: string[] | undefined = blueprintProps.enableControlPlaneLogTypes;
if (enableLogTypes) {
utils.setupClusterLogging(this.clusterInfo.cluster.stack, this.clusterInfo.cluster, enableLogTypes);
}

if (blueprintProps.enableGitOpsMode == spi.GitOpsMode.APPLICATION) {
ArgoGitOpsFactory.enableGitOps();
} else if (blueprintProps.enableGitOpsMode == spi.GitOpsMode.APP_OF_APPS) {
Expand Down
75 changes: 0 additions & 75 deletions lib/utils/cluster-utils.ts
Original file line number Diff line number Diff line change
@@ -1,82 +1,7 @@
import * as eks from "aws-cdk-lib/aws-eks";
import { Stack } from "aws-cdk-lib";
import { AwsCustomResource, AwsCustomResourcePolicy } from "aws-cdk-lib/custom-resources";
import { Construct } from "constructs";
import * as customResource from 'aws-cdk-lib/custom-resources';
import { ClusterInfo } from "../spi";

// Available Control Plane logging types
const CONTROL_PLANE_LOG_TYPES = ['api','audit','authenticator','controllerManager','scheduler'];

elamaran11 marked this conversation as resolved.
Show resolved Hide resolved
// Enables logs for the cluster.
export function setupClusterLogging(stack: Stack, cluster: eks.ICluster, enableLogTypes: string[]): void {
if(!enableLogTypes.every(val => CONTROL_PLANE_LOG_TYPES.includes(val))){
throw new Error('You have included an invalid Control Plane Log Type.');
}
let disableLogTypes = CONTROL_PLANE_LOG_TYPES.filter(item => enableLogTypes.indexOf(item) < 0);

new AwsCustomResource(stack, "ClusterLogsEnabler", {
policy: AwsCustomResourcePolicy.fromSdkCalls({
resources: [`${cluster.clusterArn}/update-config`],
}),

onCreate: {
physicalResourceId: { id: `${cluster.clusterArn}/LogsEnabler` },
service: "EKS",
action: "updateClusterConfig",
region: stack.region,
parameters: {
name: cluster.clusterName,
logging: {
clusterLogging: [
{
enabled: true,
types: enableLogTypes,
},
],
},
},
},
onDelete: {
physicalResourceId: { id: `${cluster.clusterArn}/LogsEnabler` },
service: "EKS",
action: "updateClusterConfig",
region: stack.region,
parameters: {
name: cluster.clusterName,
logging: {
clusterLogging: [
{
enabled: false,
types: CONTROL_PLANE_LOG_TYPES,
},
],
},
},
},
onUpdate: {
physicalResourceId: { id: `${cluster.clusterArn}/LogsEnabler` },
service: "EKS",
action: "updateClusterConfig",
region: stack.region,
parameters: {
name: cluster.clusterName,
logging: {
clusterLogging: [
{
enabled: true,
types: enableLogTypes,
},
{
enabled: false,
types: disableLogTypes,
},
],
},
},
},
});
}

interface Tag {
Key: string;
Expand Down
Loading