Skip to content

Commit 872277b

Browse files
feat(eks): cluster logging (#18112)
Fixes #4159 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 6860fec commit 872277b

File tree

6 files changed

+88
-0
lines changed

6 files changed

+88
-0
lines changed

packages/@aws-cdk/aws-eks/README.md

+25
Original file line numberDiff line numberDiff line change
@@ -1397,6 +1397,31 @@ Kubernetes [endpoint access](#endpoint-access), you must also specify:
13971397
* `kubectlPrivateSubnetIds` - a list of private VPC subnets IDs that will be used
13981398
to access the Kubernetes endpoint.
13991399

1400+
## Logging
1401+
1402+
EKS supports cluster logging for 5 different types of events:
1403+
1404+
* API requests to the cluster.
1405+
* Cluster access via the Kubernetes API.
1406+
* Authentication requests into the cluster.
1407+
* State of cluster controllers.
1408+
* Scheduling decisions.
1409+
1410+
You can enable logging for each one separately using the `clusterLogging`
1411+
property. For example:
1412+
1413+
```ts
1414+
const cluster = new eks.Cluster(this, 'Cluster', {
1415+
// ...
1416+
version: eks.KubernetesVersion.V1_21,
1417+
clusterLogging: [
1418+
eks.ClusterLoggingTypes.API,
1419+
eks.ClusterLoggingTypes.AUTHENTICATOR,
1420+
eks.ClusterLoggingTypes.SCHEDULER,
1421+
],
1422+
});
1423+
```
1424+
14001425
## Known Issues and Limitations
14011426

14021427
* [One cluster per stack](https://github.com/aws/aws-cdk/issues/10073)

packages/@aws-cdk/aws-eks/lib/cluster-resource-handler/cluster.ts

+4
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,10 @@ function parseProps(props: any): aws.EKS.CreateClusterRequest {
285285
parsed.resourcesVpcConfig.endpointPublicAccess = parsed.resourcesVpcConfig.endpointPublicAccess === 'true';
286286
}
287287

288+
if (typeof (parsed.logging?.clusterLogging[0].enabled) === 'string') {
289+
parsed.logging.clusterLogging[0].enabled = parsed.logging.clusterLogging[0].enabled === 'true';
290+
}
291+
288292
return parsed;
289293

290294
}

packages/@aws-cdk/aws-eks/lib/cluster-resource.ts

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export interface ClusterResourceProps {
2929
readonly onEventLayer?: lambda.ILayerVersion;
3030
readonly clusterHandlerSecurityGroup?: ec2.ISecurityGroup;
3131
readonly tags?: { [key: string]: string };
32+
readonly logging?: { [key: string]: [ { [key: string]: any } ] };
3233
}
3334

3435
/**
@@ -91,6 +92,7 @@ export class ClusterResource extends CoreConstruct {
9192
publicAccessCidrs: props.publicAccessCidrs,
9293
},
9394
tags: props.tags,
95+
logging: props.logging,
9496
},
9597
AssumeRoleArn: this.adminRole.roleArn,
9698

packages/@aws-cdk/aws-eks/lib/cluster.ts

+44
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,13 @@ export interface ClusterProps extends ClusterOptions {
757757
* @default - none
758758
*/
759759
readonly tags?: { [key: string]: string };
760+
761+
/**
762+
* The cluster log types which you want to enable.
763+
*
764+
* @default - none
765+
*/
766+
readonly clusterLogging?: ClusterLoggingTypes[];
760767
}
761768

762769
/**
@@ -815,6 +822,32 @@ export class KubernetesVersion {
815822
private constructor(public readonly version: string) { }
816823
}
817824

825+
/**
826+
* EKS cluster logging types
827+
*/
828+
export enum ClusterLoggingTypes {
829+
/**
830+
* Logs pertaining to API requests to the cluster.
831+
*/
832+
API = 'api',
833+
/**
834+
* Logs pertaining to cluster access via the Kubernetes API.
835+
*/
836+
AUDIT = 'audit',
837+
/**
838+
* Logs pertaining to authentication requests into the cluster.
839+
*/
840+
AUTHENTICATOR = 'authenticator',
841+
/**
842+
* Logs pertaining to state of cluster controllers.
843+
*/
844+
CONTROLLER_MANAGER = 'controllerManager',
845+
/**
846+
* Logs pertaining to scheduling decisions.
847+
*/
848+
SCHEDULER = 'scheduler',
849+
}
850+
818851
abstract class ClusterBase extends Resource implements ICluster {
819852
public abstract readonly connections: ec2.Connections;
820853
public abstract readonly vpc: ec2.IVpc;
@@ -1253,6 +1286,8 @@ export class Cluster extends ClusterBase {
12531286

12541287
private readonly version: KubernetesVersion;
12551288

1289+
private readonly logging?: { [key: string]: [ { [key: string]: any } ] };
1290+
12561291
/**
12571292
* A dummy CloudFormation resource that is used as a wait barrier which
12581293
* represents that the cluster is ready to receive "kubectl" commands.
@@ -1313,6 +1348,14 @@ export class Cluster extends ClusterBase {
13131348
// Get subnetIds for all selected subnets
13141349
const subnetIds = Array.from(new Set(flatten(selectedSubnetIdsPerGroup)));
13151350

1351+
this.logging = props.clusterLogging ? {
1352+
clusterLogging: [
1353+
{
1354+
enabled: true,
1355+
types: Object.values(props.clusterLogging),
1356+
},
1357+
],
1358+
} : undefined;
13161359

13171360
this.endpointAccess = props.endpointAccess ?? EndpointAccess.PUBLIC_AND_PRIVATE;
13181361
this.kubectlEnvironment = props.kubectlEnvironment;
@@ -1379,6 +1422,7 @@ export class Cluster extends ClusterBase {
13791422
clusterHandlerSecurityGroup: this.clusterHandlerSecurityGroup,
13801423
onEventLayer: this.onEventLayer,
13811424
tags: props.tags,
1425+
logging: this.logging,
13821426
});
13831427

13841428
if (this.endpointAccess._config.privateAccess && privateSubnets.length !== 0) {

packages/@aws-cdk/aws-eks/test/integ.eks-cluster.expected.json

+8
Original file line numberDiff line numberDiff line change
@@ -954,6 +954,14 @@
954954
},
955955
"tags": {
956956
"foo": "bar"
957+
},
958+
"logging": {
959+
"clusterLogging": [
960+
{
961+
"enabled": true,
962+
"types": [ "api", "authenticator", "scheduler" ]
963+
}
964+
]
957965
}
958966
},
959967
"AssumeRoleArn": {

packages/@aws-cdk/aws-eks/test/integ.eks-cluster.ts

+5
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ class EksClusterStack extends TestStack {
4141
tags: {
4242
foo: 'bar',
4343
},
44+
clusterLogging: [
45+
eks.ClusterLoggingTypes.API,
46+
eks.ClusterLoggingTypes.AUTHENTICATOR,
47+
eks.ClusterLoggingTypes.SCHEDULER,
48+
],
4449
});
4550

4651
this.assertFargateProfile();

0 commit comments

Comments
 (0)