Skip to content

Commit 767cf93

Browse files
authored
fix(eks): fail to update cluster by disabling logging props (#24688)
* fix * update path * delete
1 parent 413b643 commit 767cf93

File tree

3 files changed

+139
-0
lines changed

3 files changed

+139
-0
lines changed

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

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { IsCompleteResponse, OnEventResponse } from '@aws-cdk/custom-resources/l
55
// eslint-disable-next-line import/no-extraneous-dependencies
66
import * as aws from 'aws-sdk';
77
import { EksClient, ResourceEvent, ResourceHandler } from './common';
8+
import { compareLoggingProps } from './compareLogging';
9+
810

911
const MAX_CLUSTER_NAME_LEN = 100;
1012

@@ -25,6 +27,9 @@ export class ClusterResourceHandler extends ResourceHandler {
2527

2628
this.newProps = parseProps(this.event.ResourceProperties);
2729
this.oldProps = event.RequestType === 'Update' ? parseProps(event.OldResourceProperties) : {};
30+
// compare newProps and oldProps and update the newProps by appending disabled LogSetup if any
31+
const compared: Partial<aws.EKS.CreateClusterRequest> = compareLoggingProps(this.oldProps, this.newProps);
32+
this.newProps.logging = compared.logging;
2833
}
2934

3035
// ------
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* This function compares the logging configuration from oldProps and newProps and returns
3+
* the result that contains LogSetup with enabled:false if any.
4+
*
5+
* @param oldProps old properties
6+
* @param newProps new properties
7+
* @returns result with LogSet with enabled:false if any
8+
*/
9+
10+
export function compareLoggingProps(oldProps: Partial<AWS.EKS.CreateClusterRequest>,
11+
newProps: Partial<AWS.EKS.CreateClusterRequest>): Partial<AWS.EKS.CreateClusterRequest> {
12+
const result: Partial<AWS.EKS.CreateClusterRequest> = { logging: {} };
13+
let enabledTypes: AWS.EKS.LogType[] = [];
14+
let disabledTypes: AWS.EKS.LogType[] = [];
15+
16+
if (newProps.logging?.clusterLogging === undefined && oldProps.logging?.clusterLogging === undefined) {
17+
return newProps;
18+
}
19+
// if newProps containes LogSetup
20+
if (newProps.logging && newProps.logging.clusterLogging && newProps.logging.clusterLogging.length > 0) {
21+
enabledTypes = newProps.logging.clusterLogging[0].types!;
22+
// if oldProps contains LogSetup with enabled:true
23+
if (oldProps.logging && oldProps.logging.clusterLogging && oldProps.logging.clusterLogging.length > 0) {
24+
// LogType in oldProp but not in newProp should be considered disabled(enabled:false)
25+
disabledTypes = oldProps.logging!.clusterLogging![0].types!.filter(t => !newProps.logging!.clusterLogging![0].types!.includes(t));
26+
}
27+
} else {
28+
// all enabled:true in oldProps will be enabled:false
29+
disabledTypes = oldProps.logging!.clusterLogging![0].types!;
30+
}
31+
32+
if (enabledTypes.length > 0 || disabledTypes.length > 0) {
33+
result.logging = { clusterLogging: [] };
34+
}
35+
36+
// append the enabled:false LogSetup to the result
37+
if (enabledTypes.length > 0) {
38+
result.logging!.clusterLogging!.push({ types: enabledTypes, enabled: true });
39+
}
40+
// append the enabled:false LogSetup to the result
41+
if (disabledTypes.length > 0) {
42+
result.logging!.clusterLogging!.push({ types: disabledTypes, enabled: false });
43+
}
44+
return result;
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import * as aws from 'aws-sdk';
2+
import * as eks from '../lib';
3+
import { compareLoggingProps } from '../lib/cluster-resource-handler/compareLogging';
4+
5+
describe('compareLoggingProps', () => {
6+
7+
type Props = Partial<aws.EKS.CreateClusterRequest>;
8+
const oldEnabledTypes: aws.EKS.LogTypes = [eks.ClusterLoggingTypes.API, eks.ClusterLoggingTypes.AUDIT];
9+
10+
test('when newProps.logging.clusterLogging is undefined, should disable all types with enabled:true in oldProps', () => {
11+
const oldProps: Props = {
12+
logging: {
13+
clusterLogging: [{ types: oldEnabledTypes, enabled: true }],
14+
},
15+
};
16+
17+
const newProps: Props = {
18+
logging: {},
19+
};
20+
21+
const result = compareLoggingProps(oldProps, newProps);
22+
23+
expect(result.logging?.clusterLogging).toEqual([{ types: oldEnabledTypes, enabled: false }]);
24+
});
25+
26+
test('when newProps.logging is undefined, should disable all types with enabled:true in oldProps', () => {
27+
const oldProps: Props = {
28+
logging: {
29+
clusterLogging: [{ types: oldEnabledTypes, enabled: true }],
30+
},
31+
};
32+
33+
const newProps: Props = {};
34+
35+
const result = compareLoggingProps(oldProps, newProps);
36+
37+
expect(result.logging?.clusterLogging).toEqual([{ types: oldEnabledTypes, enabled: false }]);
38+
});
39+
40+
test('should disable types with enabled:true but not defined as enabled:true in newProps', () => {
41+
const oldProps: Props = {
42+
logging: {
43+
clusterLogging: [{ types: oldEnabledTypes, enabled: true }],
44+
},
45+
};
46+
47+
const newProps: Props = {
48+
logging: {
49+
clusterLogging: [{ types: [eks.ClusterLoggingTypes.AUDIT], enabled: true }],
50+
},
51+
};
52+
53+
const result = compareLoggingProps(oldProps, newProps);
54+
55+
expect(result.logging?.clusterLogging).toEqual([{ types: [eks.ClusterLoggingTypes.AUDIT], enabled: true },
56+
{ types: [eks.ClusterLoggingTypes.API], enabled: false }]);
57+
});
58+
59+
test('when oldProps.logging.clusterLogging is undefined and newProps.logging.clusterLogging is undefined, result should be newProps', () => {
60+
const oldProps: Props = {
61+
logging: {},
62+
};
63+
64+
const newProps: Props = {
65+
logging: {},
66+
};
67+
68+
const result = compareLoggingProps(oldProps, newProps);
69+
70+
expect(result).toEqual(newProps);
71+
});
72+
73+
test('multiple enabled:true types in oldProps with clusterLogging undefined in newProps should all be disabled', () => {
74+
const oldProps: Props = {
75+
logging: {
76+
clusterLogging: [{ types: oldEnabledTypes, enabled: true }],
77+
},
78+
};
79+
80+
const newProps: Props = {
81+
logging: {},
82+
};
83+
84+
const result = compareLoggingProps(oldProps, newProps);
85+
86+
expect(result.logging?.clusterLogging).toEqual([{ types: oldEnabledTypes, enabled: false }]);
87+
});
88+
89+
});

0 commit comments

Comments
 (0)