/
eks-job-definition.ts
218 lines (198 loc) · 7.76 KB
/
eks-job-definition.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
import { Construct } from 'constructs';
import { CfnJobDefinition } from './batch.generated';
import { EksContainerDefinition, EmptyDirVolume, HostPathVolume, SecretPathVolume } from './eks-container-definition';
import { baseJobDefinitionProperties, IJobDefinition, JobDefinitionBase, JobDefinitionProps } from './job-definition-base';
import { ArnFormat, Lazy, Stack } from '../../core';
/**
* A JobDefinition that uses Eks orchestration
*/
export interface IEksJobDefinition extends IJobDefinition {
/**
* The container this Job Definition will run
*/
readonly container: EksContainerDefinition;
/**
* The DNS Policy of the pod used by this Job Definition
*
* @see https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/#pod-s-dns-policy
*
* @default `DnsPolicy.CLUSTER_FIRST`
*/
readonly dnsPolicy?: DnsPolicy;
/**
* If specified, the Pod used by this Job Definition will use the host's network IP address.
* Otherwise, the Kubernetes pod networking model is enabled.
* Most AWS Batch workloads are egress-only and don't require the overhead of IP allocation for each pod for incoming connections.
*
* @default true
*
* @see https://kubernetes.io/docs/concepts/security/pod-security-policy/#host-namespaces
* @see https://kubernetes.io/docs/concepts/workloads/pods/#pod-networking
*/
readonly useHostNetwork?: boolean;
/**
* The name of the service account that's used to run the container.
* service accounts are Kubernetes method of identification and authentication,
* roughly analogous to IAM users.
*
* @see https://docs.aws.amazon.com/eks/latest/userguide/service-accounts.html
* @see https://kubernetes.io/docs/tasks/configure-pod-container/configure-service-account/
* @see https://docs.aws.amazon.com/eks/latest/userguide/associate-service-account-role.html
*
* @default - the default service account of the container
*/
readonly serviceAccount?: string;
}
/**
* Props for EksJobDefinition
*/
export interface EksJobDefinitionProps extends JobDefinitionProps {
/**
* The container this Job Definition will run
*/
readonly container: EksContainerDefinition;
/**
* The DNS Policy of the pod used by this Job Definition
*
* @see https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/#pod-s-dns-policy
*
* @default `DnsPolicy.CLUSTER_FIRST`
*/
readonly dnsPolicy?: DnsPolicy;
/**
* If specified, the Pod used by this Job Definition will use the host's network IP address.
* Otherwise, the Kubernetes pod networking model is enabled.
* Most AWS Batch workloads are egress-only and don't require the overhead of IP allocation for each pod for incoming connections.
*
* @default true
*
* @see https://kubernetes.io/docs/concepts/security/pod-security-policy/#host-namespaces
* @see https://kubernetes.io/docs/concepts/workloads/pods/#pod-networking
*/
readonly useHostNetwork?: boolean;
/**
* The name of the service account that's used to run the container.
* service accounts are Kubernetes method of identification and authentication,
* roughly analogous to IAM users.
*
* @see https://docs.aws.amazon.com/eks/latest/userguide/service-accounts.html
* @see https://kubernetes.io/docs/tasks/configure-pod-container/configure-service-account/
* @see https://docs.aws.amazon.com/eks/latest/userguide/associate-service-account-role.html
*
* @default - the default service account of the container
*/
readonly serviceAccount?: string;
}
/**
* The DNS Policy for the pod used by the Job Definition
*
* @see https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/#pod-s-dns-policy
*/
export enum DnsPolicy {
/**
* The Pod inherits the name resolution configuration from the node that the Pods run on
*/
DEFAULT = 'Default',
/**
* Any DNS query that does not match the configured cluster domain suffix, such as `"www.kubernetes.io"`,
* is forwarded to an upstream nameserver by the DNS server.
* Cluster administrators may have extra stub-domain and upstream DNS servers configured.
*/
CLUSTER_FIRST = 'ClusterFirst',
/**
* For Pods running with `hostNetwork`, you should explicitly set its DNS policy to `CLUSTER_FIRST_WITH_HOST_NET`.
* Otherwise, Pods running with `hostNetwork` and `CLUSTER_FIRST` will fallback to the behavior of the `DEFAULT` policy.
*/
CLUSTER_FIRST_WITH_HOST_NET = 'ClusterFirstWithHostNet',
}
/**
* A JobDefinition that uses Eks orchestration
*
* @resource AWS::Batch::JobDefinition
*/
export class EksJobDefinition extends JobDefinitionBase implements IEksJobDefinition {
/**
* Import an EksJobDefinition by its arn
*/
public static fromEksJobDefinitionArn(scope: Construct, id: string, eksJobDefinitionArn: string): IEksJobDefinition {
const stack = Stack.of(scope);
const jobDefinitionName = stack.splitArn(eksJobDefinitionArn, ArnFormat.SLASH_RESOURCE_NAME).resourceName!;
class Import extends JobDefinitionBase implements IJobDefinition {
public readonly jobDefinitionArn = eksJobDefinitionArn;
public readonly jobDefinitionName = jobDefinitionName;
public readonly enabled = true;
public readonly container = {} as any;
}
return new Import(scope, id);
}
public readonly container: EksContainerDefinition;
public readonly dnsPolicy?: DnsPolicy;
public readonly useHostNetwork?: boolean;
public readonly serviceAccount?: string;
public readonly jobDefinitionArn: string;
public readonly jobDefinitionName: string;
constructor(scope: Construct, id: string, props: EksJobDefinitionProps) {
super(scope, id, props);
this.container = props.container;
this.dnsPolicy = props.dnsPolicy;
this.useHostNetwork = props.useHostNetwork;
this.serviceAccount = props.serviceAccount;
const resource = new CfnJobDefinition(this, 'Resource', {
...baseJobDefinitionProperties(this),
type: 'container',
jobDefinitionName: props.jobDefinitionName,
eksProperties: {
podProperties: {
containers: [
this.container._renderContainerDefinition(),
],
dnsPolicy: this.dnsPolicy,
hostNetwork: this.useHostNetwork,
serviceAccountName: this.serviceAccount,
volumes: Lazy.any({
produce: () => {
if (this.container.volumes.length === 0) {
return undefined;
}
return this.container.volumes.map((volume) => {
if (EmptyDirVolume.isEmptyDirVolume(volume)) {
return {
name: volume.name,
emptyDir: {
medium: volume.medium,
sizeLimit: volume.sizeLimit ? volume.sizeLimit.toMebibytes().toString() + 'Mi' : undefined,
},
};
}
if (HostPathVolume.isHostPathVolume(volume)) {
return {
name: volume.name,
hostPath: {
path: volume.path,
},
};
}
if (SecretPathVolume.isSecretPathVolume(volume)) {
return {
name: volume.name,
secret: {
optional: volume.optional,
secretName: volume.secretName,
},
};
}
throw new Error('unknown volume type');
});
},
}),
},
},
});
this.jobDefinitionArn = this.getResourceArnAttribute(resource.ref, {
service: 'batch',
resource: 'job-definition',
resourceName: this.physicalName,
});
this.jobDefinitionName = this.getResourceNameAttribute(resource.ref);
}
}