-
Notifications
You must be signed in to change notification settings - Fork 66
/
strimzi.bash
436 lines (400 loc) · 12.6 KB
/
strimzi.bash
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#!/usr/bin/env bash
function install_strimzi_operator {
logger.info "Installing Strimzi Kafka operator"
ensure_catalog_pods_running
if ! oc get ns kafka &>/dev/null; then
oc create namespace kafka
fi
oc -n kafka apply --selector strimzi.io/crd-install=true -f "https://github.com/strimzi/strimzi-kafka-operator/releases/download/${STRIMZI_VERSION}/strimzi-cluster-operator-${STRIMZI_VERSION}.yaml"
curl -L "https://github.com/strimzi/strimzi-kafka-operator/releases/download/${STRIMZI_VERSION}/strimzi-cluster-operator-${STRIMZI_VERSION}.yaml" \
| sed 's/namespace: .*/namespace: kafka/' \
| oc -n kafka apply -f -
# Wait for the CRD we need to actually be active
oc wait crd --timeout=-1s kafkas.kafka.strimzi.io --for=condition=Established
}
function install_strimzi_cluster {
logger.info "Applying Strimzi Cluster file"
cat <<-EOF | oc apply -f -
apiVersion: kafka.strimzi.io/v1beta2
kind: Kafka
metadata:
name: my-cluster
namespace: kafka
annotations:
strimzi.io/node-pools: enabled
strimzi.io/kraft: enabled
spec:
kafka:
version: 3.7.1
replicas: 3
listeners:
# PLAINTEXT
- name: plain
port: 9092
type: internal
tls: false
# SSL
- name: tls
port: 9093
type: internal
tls: true
authentication:
type: tls
# protocol=SASL_SSL
# sasl.mechanism=SCRAM-SHA-512
- name: saslssl
port: 9094
type: internal
tls: true
authentication:
type: scram-sha-512
# protocol=SASL_PLAINTEXT
# sasl.mechanism=SCRAM-SHA-512
- name: saslplain
port: 9095
type: internal
tls: false
authentication:
type: scram-sha-512
# TLS no auth
- name: tlsnoauth
port: 9096
type: internal
tls: true
authorization:
superUsers:
- ANONYMOUS
type: simple
config:
offsets.topic.replication.factor: 3
transaction.state.log.replication.factor: 3
transaction.state.log.min.isr: 2
inter.broker.protocol.version: "3.7"
auto.create.topics.enable: "false"
entityOperator:
topicOperator: {}
userOperator: {}
---
apiVersion: kafka.strimzi.io/v1beta2
kind: KafkaNodePool
metadata:
name: kafka
namespace: kafka
labels:
strimzi.io/cluster: my-cluster
spec:
replicas: 3
roles:
- controller
- broker
storage:
type: jbod
volumes:
- id: 0
type: persistent-claim
size: 100Gi
deleteClaim: false
resources:
requests:
memory: 2Gi
cpu: "300m"
limits:
memory: 4Gi
cpu: "4"
EOF
logger.info "Waiting for Strimzi cluster to become ready"
oc wait kafka --all --timeout=-1s --for=condition=Ready -n kafka
}
function install_strimzi_users {
logger.info "Applying Strimzi TLS Admin user"
cat <<-EOF | oc apply -f -
apiVersion: kafka.strimzi.io/v1beta2
kind: KafkaUser
metadata:
name: my-tls-user
namespace: kafka
labels:
strimzi.io/cluster: my-cluster
spec:
authentication:
type: tls
authorization:
type: simple
acls:
# Example ACL rules for consuming from a topic.
- resource:
type: topic
name: "*"
operation: Read
host: "*"
- resource:
type: topic
name: "*"
operation: Describe
host: "*"
- resource:
type: group
name: "*"
operation: Read
host: "*"
- resource:
type: group
name: "*"
operation: Delete
host: "*"
# Example ACL rules for producing to a topic.
- resource:
type: topic
name: "*"
operation: Write
host: "*"
- resource:
type: topic
name: "*"
operation: Create
host: "*"
- resource:
type: topic
name: "*"
operation: Describe
host: "*"
# Required ACL rule to be able to delete topics
- resource:
type: topic
name: "*"
operation: Delete
host: "*"
EOF
logger.info "Applying Strimzi SASL Admin User"
cat <<-EOF | oc apply -f -
apiVersion: kafka.strimzi.io/v1beta2
kind: KafkaUser
metadata:
name: my-sasl-user
namespace: kafka
labels:
strimzi.io/cluster: my-cluster
spec:
authentication:
type: scram-sha-512
authorization:
type: simple
acls:
# Example ACL rules for consuming from knative-messaging-kafka using consumer group my-group
- resource:
type: topic
name: "*"
operation: Read
host: "*"
- resource:
type: topic
name: "*"
operation: Describe
host: "*"
- resource:
type: group
name: "*"
operation: Read
host: "*"
- resource:
type: group
name: "*"
operation: Delete
host: "*"
# Example ACL rules for producing to topic knative-messaging-kafka
- resource:
type: topic
name: "*"
operation: Write
host: "*"
- resource:
type: topic
name: "*"
operation: Create
host: "*"
- resource:
type: topic
name: "*"
operation: Describe
host: "*"
# Required ACL rule to be able to delete topics
- resource:
type: topic
name: "*"
operation: Delete
host: "*"
EOF
logger.info "Applying Strimzi SASL Restricted User"
cat <<-EOF | oc apply -f -
apiVersion: kafka.strimzi.io/v1beta2
kind: KafkaUser
metadata:
name: my-restricted-sasl-user
namespace: kafka
labels:
strimzi.io/cluster: my-cluster
spec:
authentication:
type: scram-sha-512
authorization:
type: simple
acls:
# Example ACL rules for Broker with names following knative default brokers.topic.template
- resource:
type: topic
name: knative-broker-
patternType: prefix
operations:
- Create
- Describe
- Read
- Write
- Delete
host: "*"
# Example ACL rules for Consumer Group ID following knative default triggers.consumergroup.template
- resource:
type: group
name: knative-trigger-
patternType: prefix
operations:
- Read
host: "*"
EOF
logger.info "Waiting for Strimzi admin users to become ready"
oc wait kafkauser --all --timeout=-1s --for=condition=Ready -n kafka
logger.info "Deleting existing Kafka user secrets"
oc delete secret -n default my-tls-secret --ignore-not-found
oc delete secret -n default my-sasl-secret --ignore-not-found
oc delete secret -n "${EVENTING_NAMESPACE}" strimzi-tls-secret --ignore-not-found
oc delete secret -n "${EVENTING_NAMESPACE}" strimzi-sasl-secret --ignore-not-found
oc delete secret -n "${EVENTING_NAMESPACE}" strimzi-sasl-secret-legacy --ignore-not-found
oc delete secret -n "${EVENTING_NAMESPACE}" strimzi-tls-secret-legacy --ignore-not-found
logger.info "Creating a Secret, containing TLS from Strimzi"
STRIMZI_CRT=$(oc -n kafka get secret my-cluster-cluster-ca-cert --template='{{index .data "ca.crt"}}' | base64 --decode )
TLSUSER_CRT=$(oc -n kafka get secret my-tls-user --template='{{index .data "user.crt"}}' | base64 --decode )
TLSUSER_KEY=$(oc -n kafka get secret my-tls-user --template='{{index .data "user.key"}}' | base64 --decode )
oc create secret --namespace default generic my-tls-secret \
--from-literal=ca.crt="$STRIMZI_CRT" \
--from-literal=user.crt="$TLSUSER_CRT" \
--from-literal=user.key="$TLSUSER_KEY"
logger.info "Creating a Secret, containing SASL from Strimzi"
SASL_PASSWD=$(oc -n kafka get secret my-sasl-user --template='{{index .data "password"}}' | base64 --decode )
oc create secret --namespace default generic my-sasl-secret \
--from-literal=ca.crt="$STRIMZI_CRT" \
--from-literal=password="$SASL_PASSWD" \
--from-literal=saslType="SCRAM-SHA-512" \
--from-literal=user="my-sasl-user"
oc create secret --namespace "${EVENTING_NAMESPACE}" generic strimzi-tls-secret \
--from-literal=ca.crt="$STRIMZI_CRT" \
--from-literal=user.crt="$TLSUSER_CRT" \
--from-literal=user.key="$TLSUSER_KEY" \
--from-literal=protocol="SSL" \
--dry-run=client -o yaml | oc apply -n "${EVENTING_NAMESPACE}" -f -
oc create secret --namespace "${EVENTING_NAMESPACE}" generic strimzi-sasl-secret \
--from-literal=ca.crt="$STRIMZI_CRT" \
--from-literal=password="$SASL_PASSWD" \
--from-literal=user="my-sasl-user" \
--from-literal=protocol="SASL_SSL" \
--from-literal=sasl.mechanism="SCRAM-SHA-512" \
--from-literal=saslType="SCRAM-SHA-512" \
--dry-run=client -o yaml | oc apply -n "${EVENTING_NAMESPACE}" -f -
oc create secret --namespace "${EVENTING_NAMESPACE}" generic strimzi-sasl-secret-legacy \
--from-literal=ca.crt="$STRIMZI_CRT" \
--from-literal=password="$SASL_PASSWD" \
--from-literal=user="my-sasl-user" \
--from-literal=saslType="SCRAM-SHA-512" \
--dry-run=client -o yaml | oc apply -n "${EVENTING_NAMESPACE}" -f -
oc create secret --namespace "${EVENTING_NAMESPACE}" generic strimzi-sasl-plain-secret \
--from-literal=password="$SASL_PASSWD" \
--from-literal=user="my-sasl-user" \
--from-literal=protocol="SASL_PLAINTEXT" \
--from-literal=sasl.mechanism="SCRAM-SHA-512" \
--from-literal=saslType="SCRAM-SHA-512" \
--dry-run=client -o yaml | oc apply -n "${EVENTING_NAMESPACE}" -f -
oc create secret --namespace "${EVENTING_NAMESPACE}" generic strimzi-sasl-plain-secret-legacy \
--from-literal=password="$SASL_PASSWD" \
--from-literal=username="my-sasl-user" \
--from-literal=saslType="SCRAM-SHA-512" \
--dry-run=client -o yaml | oc apply -n "${EVENTING_NAMESPACE}" -f -
}
function install_kafka_ui {
logger.info "Installing Kafka UI"
cat <<-EOF | oc apply -f -
---
apiVersion: v1
kind: Pod
metadata:
name: kafka-ui
namespace: kafka
labels:
app: kafka-ui
spec:
securityContext:
runAsNonRoot: true
seccompProfile:
type: RuntimeDefault
containers:
- env:
- name: KAFKA_CLUSTERS_0_BOOTSTRAPSERVERS
value: my-cluster-kafka-bootstrap.kafka.svc:9092
- name: KAFKA_CLUSTERS_0_NAME
value: my-cluster
image: quay.io/openshift-knative/kafka-ui:0.1.0
name: user-container
securityContext:
runAsUser: 1000
allowPrivilegeEscalation: false
capabilities:
drop:
- ALL
EOF
oc -n kafka expose pod kafka-ui --port=8080
oc -n kafka expose service kafka-ui
timeout 600 "[[ \$(oc get -n kafka route.route.openshift.io/kafka-ui -ojsonpath='{.status.ingress[0].host}') == '' ]]" || return 2
logger.success "Kafka UI URL: $(oc get -n kafka route.route.openshift.io/kafka-ui -ojsonpath='{.status.ingress[0].host}')"
}
function install_strimzi {
logger.info "Strimzi install"
oc create namespace "${EVENTING_NAMESPACE}" --dry-run=client -o yaml | oc apply -f -
install_strimzi_operator
install_strimzi_cluster
install_strimzi_users
install_kafka_ui
}
function delete_kafka_ui {
logger.info 'Deleting Kafka UI'
oc delete -n kafka route.route.openshift.io/kafka-ui
oc delete -n kafka service/kafka-ui
oc delete -n kafka pod/kafka-ui
}
function delete_strimzi_users {
logger.info "Deleting Kafka user secrets"
oc delete secret -n default my-tls-secret
oc delete secret -n default my-sasl-secret
logger.info "Deleting Strimzi users"
oc -n kafka delete kafkauser.kafka.strimzi.io my-sasl-user
oc -n kafka delete kafkauser.kafka.strimzi.io my-tls-user
logger.info "Waiting for Strimzi users to get deleted"
timeout 600 "[[ \$(oc get kafkausers -n kafka -o jsonpath='{.items}') != '[]' ]]" || return 2
}
function delete_strimzi_cluster {
logger.info "Deleting Strimzi cluster"
oc delete kafka -n kafka my-cluster
logger.info "Waiting for Strimzi cluster to get deleted"
timeout 600 "[[ \$(oc get kafkas -n kafka -o jsonpath='{.items}') != '[]' ]]" || return 2
}
function delete_strimzi_operator {
logger.info "Deleting Strimzi Kafka operator"
curl -L "https://github.com/strimzi/strimzi-kafka-operator/releases/download/${STRIMZI_VERSION}/strimzi-cluster-operator-${STRIMZI_VERSION}.yaml" \
| sed 's/namespace: .*/namespace: kafka/' \
| oc -n kafka delete -f -
oc -n kafka delete --selector strimzi.io/crd-install=true -f "https://github.com/strimzi/strimzi-kafka-operator/releases/download/${STRIMZI_VERSION}/strimzi-cluster-operator-${STRIMZI_VERSION}.yaml"
oc delete namespace kafka
}
function uninstall_strimzi {
logger.info "Strimzi uninstall"
delete_kafka_ui
delete_strimzi_users
delete_strimzi_cluster
delete_strimzi_operator
}