forked from pulumi/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jenkins.ts
263 lines (242 loc) · 8.17 KB
/
jenkins.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
import * as pulumi from "@pulumi/pulumi";
import * as k8s from "@pulumi/kubernetes";
import * as input from "@pulumi/kubernetes/types/input";
function createDeploymentArgs(args: JenkinsArgs): input.apps.v1.Deployment {
const image = args.image || {
registry: "docker.io",
repository: "bitnami/jenkins",
tag: "2.121.2",
pullPolicy: "IfNotPresent",
};
// This object is a projection of the Kubernetes object model into the Pulumi object model.
// Its structure is derived from the Deployment object in the Kubernetes API.
return {
metadata: {
name: args.name,
},
spec: {
replicas: 1,
selector: {
matchLabels: {
app: args.name,
}
},
template: {
metadata: {
labels: {
app: args.name,
},
},
spec: {
volumes: [
{
name: "jenkins-data",
persistentVolumeClaim: {
claimName: args.name,
}
},
],
containers: [
{
name: args.name,
image: `${image.registry}/${image.repository}:${image.tag}`,
imagePullPolicy: image.pullPolicy,
env: [
{
name: "JENKINS_USERNAME",
value: args.credentials.username,
},
{
name: "JENKINS_PASSWORD",
valueFrom: {
secretKeyRef: {
name: args.name,
key: "jenkins-password",
},
},
},
],
ports: [
{
name: "http",
containerPort: 8080,
},
{
name: "https",
containerPort: 8443,
},
],
livenessProbe: {
httpGet: {
path: "/",
port: "http",
},
initialDelaySeconds: 180,
timeoutSeconds: 5,
failureThreshold: 6,
},
readinessProbe: {
httpGet: {
path: "/",
port: "http",
},
initialDelaySeconds: 90,
timeoutSeconds: 5,
periodSeconds: 6,
},
volumeMounts: [
{
name: "jenkins-data",
mountPath: "/bitnami/jenkins",
}
],
resources: {
requests: {
memory: args.resources.memory,
cpu: args.resources.cpu,
},
},
} // container
] // containers
} // spec
} // template
} // spec
} // deployment
}
/**
* ComponentResource for a Jenkins instance running in a Kubernetes cluster.
*/
export class Instance extends pulumi.ComponentResource {
constructor(args: JenkinsArgs, opts?: pulumi.ResourceOptions) {
super("jenkins:jenkins:Instance", args.name, args, opts);
// The Secret will contain the root password for this instance.
const secret = new k8s.core.v1.Secret(`${args.name}-secret`, {
metadata: {
name: args.name,
},
type: "Opaque",
data: {
"jenkins-password": Buffer.from(args.credentials.password).toString("base64"),
},
}, { parent: this });
// The PVC provides persistent storage for Jenkins state.
const pvc = new k8s.core.v1.PersistentVolumeClaim(`${args.name}-pvc`, {
metadata: {
name: args.name,
annotations: {
"volume.beta.kubernetes.io/storage-class": `${args.storageClass || "standard" }`
},
},
spec: {
accessModes: ["ReadWriteOnce"],
resources: {
requests: {
storage: "8Gi",
},
},
},
}, { parent: this });
// The Deployment describes the desired state for our Jenkins setup.
const deploymentArgs = createDeploymentArgs(args);
const deployment = new k8s.apps.v1.Deployment(`${args.name}-deploy`, deploymentArgs, { parent: this });
// The Service exposes Jenkins to the external internet by providing load-balanced ingress for HTTP and HTTPS.
const service = new k8s.core.v1.Service(`${args.name}-service`, {
metadata: {
name: args.name,
},
spec: {
type: "LoadBalancer",
ports: [
{
name: "http",
port: 80,
targetPort: "http",
},
{
name: "https",
port: 443,
targetPort: "https",
}
],
selector: {
app: args.name,
}
}
}, { parent: this });
// This component resource has no outputs.
this.registerOutputs({});
}
}
/**
* Arguments for Jenkins instances.
*/
export interface JenkinsArgs {
/**
* The name of the instance. All Kubernetes objects will be tagged with this name
* in their metadata.
*/
readonly name: string,
/**
* Credentials for accessing the created Jenkins instance.
*/
readonly credentials: JenkinsCredentials,
/**
* The Docker image to use to launch this instance of Jenkins.
*/
readonly image?: JenkinsImage,
/**
* Resource requests for this instance.
*/
readonly resources: JenkinsResources,
/**
* Storage class to use for the persistent volume claim.
*/
readonly storageClass?: string,
}
/**
* Credentials to access the newly-created Jenkins instance.
*/
export interface JenkinsCredentials {
/**
* Username for the root user.
*/
readonly username: string,
/**
* Password for the root user.
*/
readonly password: string,
}
/**
* The image to use when launching Jenkins.
*/
export interface JenkinsImage {
/**
* The registry from which to draw Docker images.
*/
readonly registry: string,
/**
* The Docker repository name for the target image.
*/
readonly repository: string,
/**
* The Docker image tag for the target image.
*/
readonly tag: string,
/**
* Pull policy for this image.
*/
readonly pullPolicy: string,
}
/**
* Resource requests for this Jenkins instance.
*/
export interface JenkinsResources {
/**
* Requested memory.
*/
readonly memory: string;
/**
* Requested CPU.
*/
readonly cpu: string;
}