-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathk8s-manifest.ts
73 lines (67 loc) · 2.17 KB
/
k8s-manifest.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
import { Construct, CustomResource, Stack } from '@aws-cdk/core';
import { ICluster } from './cluster';
import { KubectlProvider } from './kubectl-provider';
/**
* Properties for KubernetesManifest
*/
export interface KubernetesManifestProps {
/**
* The EKS cluster to apply this manifest to.
*
* [disable-awslint:ref-via-interface]
*/
readonly cluster: ICluster;
/**
* The manifest to apply.
*
* Consists of any number of child resources.
*
* When the resources are created/updated, this manifest will be applied to the
* cluster through `kubectl apply` and when the resources or the stack is
* deleted, the resources in the manifest will be deleted through `kubectl delete`.
*
* @example
*
* [{
* apiVersion: 'v1',
* kind: 'Pod',
* metadata: { name: 'mypod' },
* spec: {
* containers: [ { name: 'hello', image: 'paulbouwer/hello-kubernetes:1.5', ports: [ { containerPort: 8080 } ] } ]
* }
* }]
*
*/
readonly manifest: any[];
}
/**
* Represents a manifest within the Kubernetes system.
*
* Alternatively, you can use `cluster.addManifest(resource[, resource, ...])`
* to define resources on this cluster.
*
* Applies/deletes the manifest using `kubectl`.
*/
export class KubernetesManifest extends Construct {
/**
* The CloudFormation reosurce type.
*/
public static readonly RESOURCE_TYPE = 'Custom::AWSCDK-EKS-KubernetesResource';
constructor(scope: Construct, id: string, props: KubernetesManifestProps) {
super(scope, id);
const stack = Stack.of(this);
const provider = KubectlProvider.getOrCreate(this, props.cluster);
new CustomResource(this, 'Resource', {
serviceToken: provider.serviceToken,
resourceType: KubernetesManifest.RESOURCE_TYPE,
properties: {
// `toJsonString` enables embedding CDK tokens in the manifest and will
// render a CloudFormation-compatible JSON string (similar to
// StepFunctions, CloudWatch Dashboards etc).
Manifest: stack.toJsonString(props.manifest),
ClusterName: props.cluster.clusterName,
RoleArn: provider.roleArn, // TODO: bake into provider's environment
},
});
}
}