forked from aws-samples/cdk-eks-blueprints-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
269 lines (245 loc) · 10.9 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import * as blueprints from '@aws-quickstart/eks-blueprints';
import { getSecretValue } from '@aws-quickstart/eks-blueprints/dist/utils/secrets-manager-utils';
import * as cdk from 'aws-cdk-lib';
import { StackProps } from 'aws-cdk-lib';
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import * as eks from 'aws-cdk-lib/aws-eks';
import { Construct } from 'constructs';
// Team implementations
import * as team from '../teams/pipeline-multi-env-gitops';
export function populateWithContextDefaults(app: cdk.App, defaultAccount: string, defaultRegion: string) {
// Populate Context Defaults for the pipeline account
let pipeline_account = app.node.tryGetContext('pipeline_account');
pipeline_account = pipeline_account ?? defaultAccount;
let pipeline_region = app.node.tryGetContext('pipeline_region');
pipeline_region = pipeline_region ?? defaultRegion;
const pipelineEnv: cdk.Environment = { account: pipeline_account, region: pipeline_region };
// Populate Context Defaults for the Development account
let dev_account = app.node.tryGetContext('dev_account');
dev_account = dev_account ?? defaultAccount;
let dev_region = app.node.tryGetContext('dev_region');
dev_region = dev_region ?? defaultRegion;
const devEnv: cdk.Environment = { account: dev_account, region: dev_region };
// Populate Context Defaults for the Production account
let prod_account = app.node.tryGetContext('prod_account');
prod_account = prod_account ?? defaultAccount;
let prod_region = app.node.tryGetContext('prod_region');
prod_region = prod_region?? defaultRegion;
const prodEnv: cdk.Environment = { account: prod_account, region: prod_region };
return { devEnv, pipelineEnv, prodEnv };
}
export interface PipelineMultiEnvGitopsProps {
/**
* The CDK environment where dev&test, prod, and piplines will be deployed to
*/
devEnv: cdk.Environment;
prodEnv: cdk.Environment;
pipelineEnv: cdk.Environment;
}
export default class PipelineMultiEnvGitops {
readonly DEFAULT_ENV: cdk.Environment =
{
account: process.env.CDK_DEFAULT_ACCOUNT,
region: process.env.CDK_DEFAULT_REGION,
};
async buildAsync(scope: Construct, id: string, pipelineProps: PipelineMultiEnvGitopsProps, props?: StackProps) {
// environments IDs consts
const DEV_ENV_ID = `dev-${pipelineProps.devEnv.region}`
const TEST_ENV_ID = `test-${pipelineProps.devEnv.region}`
const PROD_ENV_ID = `prod-${pipelineProps.prodEnv.region}`
// build teams per environments
const devTeams = createTeamList('dev', scope, pipelineProps.devEnv.account!);
const testTeams = createTeamList('test', scope, pipelineProps.devEnv.account!);
const prodTeams = createTeamList('prod', scope, pipelineProps.prodEnv.account!);
try {
// github-token is needed for CDK Pipeline functionality
await getSecretValue('github-token', pipelineProps.pipelineEnv.region!); // Exclamation mark is used to avoid msg: ts(2345)
}
catch (error) {
throw new Error(`github-token secret must be setup in AWS Secrets Manager for the GitHub pipeline.
The GitHub Personal Access Token should have these scopes:
* **repo** - to read the repository
* * **admin:repo_hook** - if you plan to use webhooks (true by default)
* @see https://docs.aws.amazon.com/codepipeline/latest/userguide/GitHub-create-personal-token-CLI.html`);
}
const clusterVersion = eks.KubernetesVersion.V1_21;
/* eslint-disable */
const blueMNG = new blueprints.MngClusterProvider({
id: "primary-mng-blue",
version: clusterVersion,
minSize: 1,
maxSize: 100,
nodeGroupCapacityType: eks.CapacityType.SPOT,
instanceTypes: [
new ec2.InstanceType("m5.2xlarge"),
new ec2.InstanceType("m5a.2xlarge"),
new ec2.InstanceType("m5ad.2xlarge"),
new ec2.InstanceType("m5d.2xlarge"),
],
});
const greenMNG = new blueprints.MngClusterProvider({
id: "primary-mng-green",
version: clusterVersion,
minSize: 1,
maxSize: 100,
nodeGroupCapacityType: eks.CapacityType.SPOT,
instanceTypes: [
new ec2.InstanceType("m5.xlarge"),
new ec2.InstanceType("m5a.xlarge"),
new ec2.InstanceType("m5ad.xlarge"),
new ec2.InstanceType("m5d.xlarge"),
],
});
const blueprint = blueprints.EksBlueprint.builder()
.version(clusterVersion)
.clusterProvider(
// blueMNG,
greenMNG,
)
.addOns(
// default addons for all environments
new blueprints.CertManagerAddOn,
new blueprints.AdotCollectorAddOn,
new blueprints.SecretsStoreAddOn,
new blueprints.AwsLoadBalancerControllerAddOn,
new blueprints.NginxAddOn,
new blueprints.AppMeshAddOn({
enableTracing: true
}),
new blueprints.CalicoOperatorAddOn,
new blueprints.MetricsServerAddOn,
new blueprints.ClusterAutoScalerAddOn(),
new blueprints.CloudWatchAdotAddOn,
new blueprints.XrayAddOn,
);
// Argo configuration per environment
const devArgoAddonConfig = createArgoAddonConfig('dev', '[email protected]:aws-samples/eks-blueprints-workloads.git');
const testArgoAddonConfig = createArgoAddonConfig('test', '[email protected]:aws-samples/eks-blueprints-workloads.git');
const prodArgoAddonConfig = createArgoAddonConfig('prod', '[email protected]:aws-samples/eks-blueprints-workloads.git');
try {
// const { gitOwner, gitRepositoryName } = await getRepositoryData();
const gitOwner = 'aws-samples';
const gitRepositoryName = 'cdk-eks-blueprints-patterns';
blueprints.CodePipelineStack.builder()
.name("eks-blueprint-pipeline")
.owner(gitOwner)
.repository({
repoUrl: gitRepositoryName,
credentialsSecretName: 'github-token',
targetRevision: 'main',
})
.wave({
id: "dev-test",
stages: [
{
id: DEV_ENV_ID,
stackBuilder: blueprint
.clone(pipelineProps.devEnv.region, pipelineProps.devEnv.account)
.name(DEV_ENV_ID)
.teams(...devTeams)
.addOns(
devArgoAddonConfig,
)
},
{
id: TEST_ENV_ID,
stackBuilder: blueprint
.clone(pipelineProps.devEnv.region, pipelineProps.devEnv.account)
.name(TEST_ENV_ID)
.teams(...testTeams)
.addOns(
testArgoAddonConfig,
)
},
],
props: {
post: [new blueprints.pipelines.cdkpipelines.ManualApprovalStep('manual-approval-before-production')]
}
})
.wave({
id: "prod",
stages: [
{
id: PROD_ENV_ID,
stackBuilder: blueprint
.clone(pipelineProps.prodEnv.region, pipelineProps.prodEnv.account)
.name(PROD_ENV_ID)
.teams(...prodTeams)
.addOns(
prodArgoAddonConfig,
)
},
]
})
.build(scope, "eks-blueprint-pipeline-stack", props);
} catch (error) {
console.log(error)
}
}
}
function createTeamList(environments: string, scope: Construct, account: string): Array<blueprints.Team> {
const teamsList = [
new team.CorePlatformTeam(account, environments),
new team.FrontendTeam(account, environments),
new team.BackendNodejsTeam(account, environments),
new team.BackendCrystalTeam(account, environments),
];
return teamsList;
}
function createArgoAddonConfig(environment: string, repoUrl: string): blueprints.ArgoCDAddOn {
interface argoProjectParams {
githubOrg: string,
githubRepository: string,
projectNamespace: string
}
let argoAdditionalProject: Array<Record<string, unknown>> = [];
const projectNameList: argoProjectParams[] =
[
{ githubOrg: 'aws-containers', githubRepository: 'ecsdemo-frontend', projectNamespace: 'ecsdemo-frontend' },
{ githubOrg: 'aws-containers', githubRepository: 'ecsdemo-nodejs', projectNamespace: 'ecsdemo-nodejs' },
{ githubOrg: 'aws-containers', githubRepository: 'ecsdemo-crystal', projectNamespace: 'ecsdemo-crystal' },
];
projectNameList.forEach(element => {
argoAdditionalProject.push(
{
name: element.githubRepository,
namespace: "argocd",
destinations: [{
namespace: element.projectNamespace,
server: "https://kubernetes.default.svc"
}],
sourceRepos: [
`[email protected]:${element.githubOrg}/${element.githubRepository}.git`,
`[email protected]:aws-samples/eks-blueprints-workloads.git`,
],
}
);
});
const argoConfig = new blueprints.ArgoCDAddOn(
{
bootstrapRepo: {
repoUrl: repoUrl,
path: `multi-repo/argo-app-of-apps/${environment}`,
targetRevision: 'main',
credentialsSecretName: 'github-ssh-key',
credentialsType: 'SSH'
},
bootstrapValues: {
service: {
type: 'LoadBalancer'
},
spec: {
ingress: {
host: 'dev.blueprint.com',
},
},
},
values: {
server: {
additionalProjects: argoAdditionalProject,
}
}
}
)
return argoConfig
}