Skip to content

Commit

Permalink
Add feature flag
Browse files Browse the repository at this point in the history
  • Loading branch information
cecheta committed Feb 14, 2023
1 parent bb38e36 commit 5f20658
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 13 deletions.
7 changes: 6 additions & 1 deletion packages/@aws-cdk/aws-codedeploy/lib/ecs/deployment-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as ecs from '@aws-cdk/aws-ecs';
import * as elbv2 from '@aws-cdk/aws-elasticloadbalancingv2';
import * as iam from '@aws-cdk/aws-iam';
import * as cdk from '@aws-cdk/core';
import { CODEDEPLOY_REMOVE_ALARMS_FROM_DEPLOYMENT_GROUP } from '@aws-cdk/cx-api';
import { Construct } from 'constructs';
import { CfnDeploymentGroup } from '../codedeploy.generated';
import { ImportedDeploymentGroupBase, DeploymentGroupBase } from '../private/base-deployment-group';
Expand Down Expand Up @@ -240,6 +241,8 @@ export class EcsDeploymentGroup extends DeploymentGroupBase implements IEcsDeplo
}
}

const removeAlarmsFromDeploymentGroup = cdk.FeatureFlags.of(this).isEnabled(CODEDEPLOY_REMOVE_ALARMS_FROM_DEPLOYMENT_GROUP);

const resource = new CfnDeploymentGroup(this, 'Resource', {
applicationName: this.application.applicationName,
serviceRoleArn: this.role.roleArn,
Expand All @@ -257,7 +260,9 @@ export class EcsDeploymentGroup extends DeploymentGroupBase implements IEcsDeplo
produce: () => this.renderBlueGreenDeploymentConfiguration(props.blueGreenDeploymentConfig),
}),
loadBalancerInfo: cdk.Lazy.any({ produce: () => this.renderLoadBalancerInfo(props.blueGreenDeploymentConfig) }),
alarmConfiguration: cdk.Lazy.any({ produce: () => renderAlarmConfiguration(this.alarms, props.ignorePollAlarmsFailure) }),
alarmConfiguration: cdk.Lazy.any({
produce: () => renderAlarmConfiguration(this.alarms, props.ignorePollAlarmsFailure, removeAlarmsFromDeploymentGroup),
}),
autoRollbackConfiguration: cdk.Lazy.any({ produce: () => renderAutoRollbackConfiguration(this.alarms, props.autoRollback) }),
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as cloudwatch from '@aws-cdk/aws-cloudwatch';
import * as iam from '@aws-cdk/aws-iam';
import * as lambda from '@aws-cdk/aws-lambda';
import * as cdk from '@aws-cdk/core';
import { CODEDEPLOY_REMOVE_ALARMS_FROM_DEPLOYMENT_GROUP } from '@aws-cdk/cx-api';
import { Construct } from 'constructs';
import { CfnDeploymentGroup } from '../codedeploy.generated';
import { ImportedDeploymentGroupBase, DeploymentGroupBase } from '../private/base-deployment-group';
Expand Down Expand Up @@ -164,6 +165,8 @@ export class LambdaDeploymentGroup extends DeploymentGroupBase implements ILambd
this.role.addManagedPolicy(iam.ManagedPolicy.fromAwsManagedPolicyName('service-role/AWSCodeDeployRoleForLambdaLimited'));
this.deploymentConfig = this._bindDeploymentConfig(props.deploymentConfig || LambdaDeploymentConfig.CANARY_10PERCENT_5MINUTES);

const removeAlarmsFromDeploymentGroup = cdk.FeatureFlags.of(this).isEnabled(CODEDEPLOY_REMOVE_ALARMS_FROM_DEPLOYMENT_GROUP);

const resource = new CfnDeploymentGroup(this, 'Resource', {
applicationName: this.application.applicationName,
serviceRoleArn: this.role.roleArn,
Expand All @@ -173,7 +176,9 @@ export class LambdaDeploymentGroup extends DeploymentGroupBase implements ILambd
deploymentType: 'BLUE_GREEN',
deploymentOption: 'WITH_TRAFFIC_CONTROL',
},
alarmConfiguration: cdk.Lazy.any({ produce: () => renderAlarmConfiguration(this.alarms, props.ignorePollAlarmsFailure) }),
alarmConfiguration: cdk.Lazy.any({
produce: () => renderAlarmConfiguration(this.alarms, props.ignorePollAlarmsFailure, removeAlarmsFromDeploymentGroup),
}),
autoRollbackConfiguration: cdk.Lazy.any({ produce: () => renderAutoRollbackConfiguration(this.alarms, props.autoRollback) }),
});

Expand Down
24 changes: 17 additions & 7 deletions packages/@aws-cdk/aws-codedeploy/lib/private/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,23 @@ export function arnForDeploymentConfig(name: string, resource?: IResource): stri
});
}

export function renderAlarmConfiguration(alarms: cloudwatch.IAlarm[], ignorePollAlarmFailure?: boolean):
CfnDeploymentGroup.AlarmConfigurationProperty {
return {
alarms: alarms.length > 0 ? alarms.map(a => ({ name: a.alarmName })) : undefined,
enabled: alarms.length > 0,
ignorePollAlarmFailure,
};
export function renderAlarmConfiguration(alarms: cloudwatch.IAlarm[], ignorePollAlarmFailure: boolean | undefined, removeAlarms = true):
CfnDeploymentGroup.AlarmConfigurationProperty | undefined {
if (removeAlarms) {
return {
alarms: alarms.length > 0 ? alarms.map(a => ({ name: a.alarmName })) : undefined,
enabled: alarms.length > 0,
ignorePollAlarmFailure,
};
}

return alarms.length === 0
? undefined
: {
alarms: alarms.map(a => ({ name: a.alarmName })),
enabled: true,
ignorePollAlarmFailure,
};
}

export function deploymentConfig(name: string): IBaseDeploymentConfig & IPredefinedDeploymentConfig {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as ec2 from '@aws-cdk/aws-ec2';
import * as iam from '@aws-cdk/aws-iam';
import * as s3 from '@aws-cdk/aws-s3';
import * as cdk from '@aws-cdk/core';
import { CODEDEPLOY_REMOVE_ALARMS_FROM_DEPLOYMENT_GROUP } from '@aws-cdk/cx-api';
import { Construct } from 'constructs';
import { CfnDeploymentGroup } from '../codedeploy.generated';
import { ImportedDeploymentGroupBase, DeploymentGroupBase } from '../private/base-deployment-group';
Expand Down Expand Up @@ -267,6 +268,8 @@ export class ServerDeploymentGroup extends DeploymentGroupBase implements IServe

this.alarms = props.alarms || [];

const removeAlarmsFromDeploymentGroup = cdk.FeatureFlags.of(this).isEnabled(CODEDEPLOY_REMOVE_ALARMS_FROM_DEPLOYMENT_GROUP);

const resource = new CfnDeploymentGroup(this, 'Resource', {
applicationName: this.application.applicationName,
deploymentGroupName: this.physicalName,
Expand All @@ -282,7 +285,9 @@ export class ServerDeploymentGroup extends DeploymentGroupBase implements IServe
},
ec2TagSet: this.ec2TagSet(props.ec2InstanceTags),
onPremisesTagSet: this.onPremiseTagSet(props.onPremiseInstanceTags),
alarmConfiguration: cdk.Lazy.any({ produce: () => renderAlarmConfiguration(this.alarms, props.ignorePollAlarmsFailure) }),
alarmConfiguration: cdk.Lazy.any({
produce: () => renderAlarmConfiguration(this.alarms, props.ignorePollAlarmsFailure, removeAlarmsFromDeploymentGroup),
}),
autoRollbackConfiguration: cdk.Lazy.any({ produce: () => renderAutoRollbackConfiguration(this.alarms, props.autoRollback) }),
});

Expand Down
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-codedeploy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
"@aws-cdk/aws-s3": "0.0.0",
"@aws-cdk/core": "0.0.0",
"@aws-cdk/custom-resources": "0.0.0",
"@aws-cdk/cx-api": "0.0.0",
"constructs": "^10.0.0"
},
"homepage": "https://github.com/aws/aws-cdk",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Template } from '@aws-cdk/assertions';
import { Match, Template } from '@aws-cdk/assertions';
import * as cloudwatch from '@aws-cdk/aws-cloudwatch';
import * as ec2 from '@aws-cdk/aws-ec2';
import * as ecs from '@aws-cdk/aws-ecs';
Expand Down Expand Up @@ -51,6 +51,7 @@ describe('CodeDeploy ECS DeploymentGroup', () => {

test('can be created with default configuration', () => {
const stack = new cdk.Stack();
stack.node.setContext('@aws-cdk/aws-codedeploy:removeAlarmsFromDeploymentGroup', true);

new codedeploy.EcsDeploymentGroup(stack, 'MyDG', {
service: mockEcsService(stack),
Expand All @@ -73,6 +74,10 @@ describe('CodeDeploy ECS DeploymentGroup', () => {
'Arn',
],
},
AlarmConfiguration: {
Enabled: false,
Alarms: Match.absent(),
},
AutoRollbackConfiguration: {
Enabled: true,
Events: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ function mockAlias(stack: cdk.Stack) {
describe('CodeDeploy Lambda DeploymentGroup', () => {
test('can be created with default AllAtOnce IN_PLACE configuration', () => {
const stack = new cdk.Stack();
stack.node.setContext('@aws-cdk/aws-codedeploy:removeAlarmsFromDeploymentGroup', true);
const application = new codedeploy.LambdaApplication(stack, 'MyApp');
const alias = mockAlias(stack);
new codedeploy.LambdaDeploymentGroup(stack, 'MyDG', {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Template } from '@aws-cdk/assertions';
import { Match, Template } from '@aws-cdk/assertions';
import * as autoscaling from '@aws-cdk/aws-autoscaling';
import * as cloudwatch from '@aws-cdk/aws-cloudwatch';
import * as ec2 from '@aws-cdk/aws-ec2';
Expand All @@ -24,6 +24,23 @@ describe('CodeDeploy Server Deployment Group', () => {
});
});

test('can create a deployment group with no alarms', () => {
const stack = new cdk.Stack();
stack.node.setContext('@aws-cdk/aws-codedeploy:removeAlarmsFromDeploymentGroup', true);

const application = new codedeploy.ServerApplication(stack, 'MyApp');
new codedeploy.ServerDeploymentGroup(stack, 'MyDG', {
application,
});

Template.fromStack(stack).hasResourceProperties('AWS::CodeDeploy::DeploymentGroup', {
AlarmConfiguration: {
Enabled: false,
Alarms: Match.absent(),
},
});
});

test('creating an application with physical name if needed', () => {
const stack = new cdk.Stack(undefined, undefined, { env: { account: '12345', region: 'us-test-1' } });
const stack2 = new cdk.Stack(undefined, undefined, { env: { account: '12346', region: 'us-test-2' } });
Expand Down
19 changes: 18 additions & 1 deletion packages/@aws-cdk/cx-api/FEATURE_FLAGS.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Flags come in three types:
| [@aws-cdk/aws-s3:serverAccessLogsUseBucketPolicy](#aws-cdkaws-s3serveraccesslogsusebucketpolicy) | Use S3 Bucket Policy instead of ACLs for Server Access Logging | 2.59.0 | (fix) |
| [@aws-cdk/aws-iam:importedRoleStackSafeDefaultPolicyName](#aws-cdkaws-iamimportedrolestacksafedefaultpolicyname) | Enable this feature to by default create default policy names for imported roles that depend on the stack the role is in. | 2.60.0 | (fix) |
| [@aws-cdk/customresources:installLatestAwsSdkDefault](#aws-cdkcustomresourcesinstalllatestawssdkdefault) | Whether to install the latest SDK by default in AwsCustomResource | 2.60.0 | (default) |
| [@aws-cdk/aws-codedeploy:removeAlarmsFromDeploymentGroup](#aws-cdkaws-codedeployremovealarmsfromdeploymentgroup) | Remove CloudWatch alarms from deployment group | V2NEXT | (fix) |
| [@aws-cdk/aws-rds:databaseProxyUniqueResourceName](#aws-cdkaws-rdsdatabaseproxyuniqueresourcename) | Use unique resource name for Database Proxy | V2NEXT | (fix) |

<!-- END table -->
Expand Down Expand Up @@ -78,7 +79,8 @@ The following json shows the current recommended set of flags, as `cdk init` wou
"@aws-cdk/aws-s3:serverAccessLogsUseBucketPolicy": true,
"@aws-cdk/aws-route53-patters:useCertificate": true,
"@aws-cdk/customresources:installLatestAwsSdkDefault": false,
"@aws-cdk/aws-rds:databaseProxyUniqueResourceName": true
"@aws-cdk/aws-rds:databaseProxyUniqueResourceName": true,
"@aws-cdk/aws-codedeploy:removeAlarmsFromDeploymentGroup": true
}
}
```
Expand Down Expand Up @@ -780,6 +782,21 @@ flag on a resource-by-resource basis to enable it if necessary.
**Compatibility with old behavior:** Set installLatestAwsSdk: true on all resources that need it.


### @aws-cdk/aws-codedeploy:removeAlarmsFromDeploymentGroup

*Remove CloudWatch alarms from deployment group* (fix)

Enable this flag to be able to remove all CloudWatch alarms from a deployment group by removing
the alarms from the construct. If this flag is not set, removing all alarms from the construct
will still leave the alarms configured for the deployment group.


| Since | Default | Recommended |
| ----- | ----- | ----- |
| (not in v1) | | |
| V2NEXT | `false` | `true` |


### @aws-cdk/aws-rds:databaseProxyUniqueResourceName

*Use unique resource name for Database Proxy* (fix)
Expand Down
14 changes: 14 additions & 0 deletions packages/@aws-cdk/cx-api/lib/features.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export const S3_SERVER_ACCESS_LOGS_USE_BUCKET_POLICY = '@aws-cdk/aws-s3:serverAc
export const ROUTE53_PATTERNS_USE_CERTIFICATE = '@aws-cdk/aws-route53-patters:useCertificate';
export const AWS_CUSTOM_RESOURCE_LATEST_SDK_DEFAULT = '@aws-cdk/customresources:installLatestAwsSdkDefault';
export const DATABASE_PROXY_UNIQUE_RESOURCE_NAME = '@aws-cdk/aws-rds:databaseProxyUniqueResourceName';
export const CODEDEPLOY_REMOVE_ALARMS_FROM_DEPLOYMENT_GROUP = '@aws-cdk/aws-codedeploy:removeAlarmsFromDeploymentGroup';

export const FLAGS: Record<string, FlagInfo> = {
//////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -645,6 +646,19 @@ export const FLAGS: Record<string, FlagInfo> = {
introducedIn: { v2: 'V2NEXT' },
recommendedValue: true,
},

//////////////////////////////////////////////////////////////////////
[CODEDEPLOY_REMOVE_ALARMS_FROM_DEPLOYMENT_GROUP]: {
type: FlagType.BugFix,
summary: 'Remove CloudWatch alarms from deployment group',
detailsMd: `
Enable this flag to be able to remove all CloudWatch alarms from a deployment group by removing
the alarms from the construct. If this flag is not set, removing all alarms from the construct
will still leave the alarms configured for the deployment group.
`,
introducedIn: { v2: 'V2NEXT' },
recommendedValue: true,
},
};

const CURRENT_MV = 'v2';
Expand Down

0 comments on commit 5f20658

Please sign in to comment.