-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathadvanced_molecule.go
539 lines (488 loc) · 16.2 KB
/
advanced_molecule.go
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
// Copyright 2020 The Operator-SDK Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ansible
import (
"fmt"
"os/exec"
"path/filepath"
"strings"
log "github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/runtime/schema"
kbutil "sigs.k8s.io/kubebuilder/v3/pkg/plugin/util"
"github.com/operator-framework/ansible-operator-plugins/hack/generate/samples/internal/pkg"
"github.com/operator-framework/ansible-operator-plugins/pkg/testutils/command"
"github.com/operator-framework/ansible-operator-plugins/pkg/testutils/e2e"
"github.com/operator-framework/ansible-operator-plugins/pkg/testutils/sample"
)
func ImplementAdvancedMolecule(sample sample.Sample, image string) {
log.Info("enabling multigroup support")
err := e2e.AllowProjectBeMultiGroup(sample)
pkg.CheckError("updating PROJECT file", err)
inventoryRoleTask := filepath.Join(sample.Dir(), "roles", "inventorytest", "tasks", "main.yml")
log.Info("inserting code to inventory role task")
const inventoryRoleTaskFragment = `
- when: sentinel | test
block:
- kubernetes.core.k8s:
definition:
apiVersion: v1
kind: ConfigMap
metadata:
name: inventory-cm
namespace: '{{ meta.namespace }}'
data:
sentinel: '{{ sentinel }}'
groups: '{{ groups | to_nice_yaml }}'`
err = kbutil.ReplaceInFile(
inventoryRoleTask,
"# tasks file for InventoryTest",
inventoryRoleTaskFragment)
pkg.CheckError("replacing inventory task", err)
log.Info("updating inventorytest sample")
err = kbutil.ReplaceInFile(
filepath.Join(sample.Dir(), "config", "samples", "test_v1alpha1_inventorytest.yaml"),
"name: inventorytest-sample",
inventorysampleFragment)
pkg.CheckError("updating inventorytest sample", err)
log.Info("updating spec of inventorytest sample")
err = kbutil.ReplaceInFile(
filepath.Join(sample.Dir(), "config", "samples", "test_v1alpha1_inventorytest.yaml"),
"# TODO(user): Add fields here",
"size: 3")
pkg.CheckError("updating spec of inventorytest sample", err)
log.Info("enabling metrics in the manager")
err = kbutil.UncommentCode(
filepath.Join(sample.Dir(), "config", "default", "kustomization.yaml"),
"#- path: manager_metrics_patch.yaml", "#")
pkg.CheckError("enabling metrics endpoint", err)
removeFixmeFromPlaybooks(sample.Dir(), sample.GVKs())
updatePlaybooks(sample.Dir())
addMocksFromTestdata(sample.Dir(), sample.CommandContext())
updateDockerfile(sample.Dir())
updateConfig(sample.Dir())
}
func updateConfig(dir string) {
log.Info("adding customized roles")
const cmRolesFragment = ` ##
## Base operator rules
##
- apiGroups:
- ""
resources:
- configmaps
- namespaces
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- apps
resources:
- configmaps
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
#+kubebuilder:scaffold:rules`
err := kbutil.ReplaceInFile(
filepath.Join(dir, "config", "rbac", "role.yaml"),
"#+kubebuilder:scaffold:rules",
cmRolesFragment)
pkg.CheckError("adding customized roles", err)
log.Info("adding manager arg")
const ansibleVaultArg = `
- --ansible-args='--vault-password-file /opt/ansible/pwd.yml'`
err = kbutil.InsertCode(
filepath.Join(dir, "config", "manager", "manager.yaml"),
"- --leader-election-id=advanced-molecule-operator",
ansibleVaultArg)
pkg.CheckError("adding manager arg", err)
log.Info("adding manager env")
const managerEnv = `
- name: ANSIBLE_DEBUG_LOGS
value: "TRUE"
- name: ANSIBLE_INVENTORY
value: /opt/ansible/inventory`
err = kbutil.InsertCode(
filepath.Join(dir, "config", "manager", "manager.yaml"),
"value: explicit",
managerEnv)
pkg.CheckError("adding manager env", err)
log.Info("adding vaulting args to the proxy auth")
const managerAuthArgs = `
- "--ansible-args='--vault-password-file /opt/ansible/pwd.yml'"`
err = kbutil.InsertCode(
filepath.Join(dir, "config", "default", "manager_metrics_patch.yaml"),
"- \"--leader-elect\"",
managerAuthArgs)
pkg.CheckError("adding vaulting args to the proxy auth", err)
log.Info("adding task to not pull image to the config/testing")
err = kbutil.ReplaceInFile(
filepath.Join(dir, "config", "testing", "kustomization.yaml"),
"- manager_image.yaml",
"- manager_image.yaml\n- pull_policy/Never.yaml")
pkg.CheckError("adding task to not pull image to the config/testing", err)
}
func addMocksFromTestdata(dir string, cc command.CommandContext) {
testDataAbsPath, err := filepath.Abs("hack/generate/samples/ansible/testdata")
pkg.CheckError("absolute path for testdata", err)
log.Info("adding ansible.cfg")
cmd := exec.Command("cp", filepath.Join(testDataAbsPath, "ansible.cfg"), dir)
_, err = cc.Run(cmd)
pkg.CheckError("adding ansible.cfg", err)
log.Info("adding plugins/")
cmd = exec.Command("cp", "-r", filepath.Join(testDataAbsPath, "plugins/"), filepath.Join(dir, "plugins/"))
_, err = cc.Run(cmd)
pkg.CheckError("adding plugins/", err)
log.Info("adding fixture_collection/")
cmd = exec.Command("cp", "-r", filepath.Join(testDataAbsPath, "fixture_collection/"), filepath.Join(dir, "fixture_collection/"))
_, err = cc.Run(cmd)
pkg.CheckError("adding fixture_collection/", err)
log.Info("replacing watches.yaml")
cmd = exec.Command("cp", "-r", filepath.Join(testDataAbsPath, "watches.yaml"), dir)
_, err = cc.Run(cmd)
pkg.CheckError("replacing watches.yaml", err)
log.Info("adding tasks/")
cmd = exec.Command("cp", "-r", filepath.Join(testDataAbsPath, "tasks/"), filepath.Join(dir, "molecule/default/"))
_, err = cc.Run(cmd)
pkg.CheckError("adding tasks/", err)
log.Info("adding secret playbook")
cmd = exec.Command("cp", "-r", filepath.Join(testDataAbsPath, "secret.yml"), filepath.Join(dir, "playbooks/secret.yml"))
_, err = cc.Run(cmd)
pkg.CheckError("adding secret playbook", err)
log.Info("adding inventory/")
cmd = exec.Command("cp", "-r", filepath.Join(testDataAbsPath, "inventory/"), filepath.Join(dir, "inventory/"))
_, err = cc.Run(cmd)
pkg.CheckError("adding inventory/", err)
log.Info("adding finalizer for finalizerconcurrencytest")
cmd = exec.Command("cp", filepath.Join(testDataAbsPath, "/playbooks/finalizerconcurrencyfinalizer.yml"), filepath.Join(dir, "playbooks/finalizerconcurrencyfinalizer.yml"))
_, err = cc.Run(cmd)
pkg.CheckError("adding finalizer for finalizerconccurencytest", err)
}
func updateDockerfile(dir string) {
log.Info("replacing project Dockerfile to use ansible base image with the dev tag")
err := kbutil.ReplaceRegexInFile(
filepath.Join(dir, "Dockerfile"),
"quay.io/operator-framework/ansible-operator:.*",
"quay.io/operator-framework/ansible-operator:dev")
pkg.CheckError("replacing Dockerfile", err)
log.Info("inserting code to Dockerfile")
const dockerfileFragment = `
# Customizations done to check advanced scenarios
COPY inventory/ ${HOME}/inventory/
COPY plugins/ ${HOME}/plugins/
COPY ansible.cfg /etc/ansible/ansible.cfg
COPY fixture_collection/ /tmp/fixture_collection/
USER root
RUN chmod -R ug+rwx /tmp/fixture_collection
USER 1001
RUN ansible-galaxy collection build /tmp/fixture_collection/ --output-path /tmp/fixture_collection/ \
&& ansible-galaxy collection install /tmp/fixture_collection/operator_sdk-test_fixtures-0.0.0.tar.gz
RUN echo abc123 > /opt/ansible/pwd.yml \
&& ansible-vault encrypt_string --vault-password-file /opt/ansible/pwd.yml 'thisisatest' --name 'the_secret' > /opt/ansible/vars.yml
`
err = kbutil.InsertCode(
filepath.Join(dir, "Dockerfile"),
"COPY playbooks/ ${HOME}/playbooks/",
dockerfileFragment)
pkg.CheckError("replacing Dockerfile", err)
}
func updatePlaybooks(dir string) {
log.Info("adding playbook for argstest")
const argsPlaybook = `---
- hosts: localhost
gather_facts: no
collections:
- kubernetes.core
tasks:
- name: Get the decrypted message variable
include_vars:
file: /opt/ansible/vars.yml
name: the_secret
- name: Create configmap
k8s:
definition:
apiVersion: v1
kind: ConfigMap
metadata:
name: '{{ meta.name }}'
namespace: '{{ meta.namespace }}'
data:
msg: The decrypted value is {{the_secret.the_secret}}
`
err := kbutil.ReplaceInFile(
filepath.Join(dir, "playbooks", "argstest.yml"),
originalPlaybookFragment,
argsPlaybook)
pkg.CheckError("adding playbook for argstest", err)
log.Info("adding playbook for casetest")
const casePlaybook = `---
- hosts: localhost
gather_facts: no
collections:
- kubernetes.core
tasks:
- name: Create configmap
k8s:
definition:
apiVersion: v1
kind: ConfigMap
metadata:
name: '{{ meta.name }}'
namespace: '{{ meta.namespace }}'
data:
shouldBeCamel: '{{ camelCaseVar | default("false") }}'
`
err = kbutil.ReplaceInFile(
filepath.Join(dir, "playbooks", "casetest.yml"),
originalPlaybookFragment,
casePlaybook)
pkg.CheckError("adding playbook for casetest", err)
log.Info("adding playbook for inventorytest")
const inventoryPlaybook = `---
- hosts: test
gather_facts: no
tasks:
- import_role:
name: "inventorytest"
- hosts: localhost
gather_facts: no
tasks:
- command: echo hello
- debug: msg='{{ "hello" | test }}'`
err = kbutil.ReplaceInFile(
filepath.Join(dir, "playbooks", "inventorytest.yml"),
"---\n- hosts: localhost\n gather_facts: no\n collections:\n - kubernetes.core\n - operator_sdk.util\n tasks:\n - import_role:\n name: \"inventorytest\"",
inventoryPlaybook)
pkg.CheckError("adding playbook for inventorytest", err)
log.Info("adding playbook for reconciliationtest")
const reconciliationPlaybook = `---
- hosts: localhost
gather_facts: no
collections:
- kubernetes.core
tasks:
- name: retrieve configmap
k8s_info:
api_version: v1
kind: ConfigMap
namespace: '{{ meta.namespace }}'
name: '{{ meta.name }}'
register: configmap
- name: create configmap
k8s:
definition:
apiVersion: v1
kind: ConfigMap
metadata:
name: '{{ meta.name }}'
namespace: '{{ meta.namespace }}'
data:
iterations: '1'
when: configmap.resources|length == 0
- name: Update ConfigMap
k8s:
definition:
apiVersion: v1
kind: ConfigMap
metadata:
name: '{{ meta.name }}'
namespace: '{{ meta.namespace }}'
data:
iterations: '{{ (configmap.resources.0.data.iterations|int) + 1 }}'
when: configmap.resources|length > 0 and (configmap.resources.0.data.iterations|int) < 5
- name: retrieve configmap
k8s_info:
api_version: v1
kind: ConfigMap
namespace: '{{ meta.namespace }}'
name: '{{ meta.name }}'
register: configmap
- name: Using the requeue_after module
operator_sdk.util.requeue_after:
time: 1s
when: configmap.resources|length > 0 and (configmap.resources.0.data.iterations|int) < 5
`
err = kbutil.ReplaceInFile(
filepath.Join(dir, "playbooks", "reconciliationtest.yml"),
originalPlaybookFragment,
reconciliationPlaybook)
pkg.CheckError("adding playbook for reconciliationtest", err)
log.Info("adding playbook for selectortest")
const selectorPlaybook = `---
- hosts: localhost
gather_facts: no
collections:
- kubernetes.core
tasks:
- name: Create configmap
k8s:
definition:
apiVersion: v1
kind: ConfigMap
metadata:
name: '{{ meta.name }}'
namespace: '{{ meta.namespace }}'
data:
hello: "world"
`
err = kbutil.ReplaceInFile(
filepath.Join(dir, "playbooks", "selectortest.yml"),
originalPlaybookFragment,
selectorPlaybook)
pkg.CheckError("adding playbook for selectortest", err)
log.Info("adding playbook for subresourcestest")
const subresourcesPlaybook = `---
- hosts: localhost
gather_facts: no
collections:
- kubernetes.core
- operator_sdk.util
tasks:
- name: Deploy busybox pod
k8s:
definition:
apiVersion: v1
kind: Pod
metadata:
name: '{{ meta.name }}-busybox'
namespace: '{{ meta.namespace }}'
spec:
containers:
- image: busybox
name: sleep
args:
- "/bin/sh"
- "-c"
- "while true ; do echo '{{ log_message }}' ; sleep 5 ; done"
wait: yes
- name: Execute command in busybox pod
k8s_exec:
namespace: '{{ meta.namespace }}'
pod: '{{ meta.name }}-busybox'
command: '{{ exec_command }}'
register: exec_result
- name: Get logs from busybox pod
k8s_log:
name: '{{ meta.name }}-busybox'
namespace: '{{ meta.namespace }}'
register: log_result
- name: Write results to resource status
k8s_status:
api_version: test.example.com/v1alpha1
kind: SubresourcesTest
name: '{{ meta.name }}'
namespace: '{{ meta.namespace }}'
status:
execCommandStdout: '{{ exec_result.stdout.strip() }}'
execCommandStderr: '{{ exec_result.stderr.strip() }}'
logs: '{{ log_result.log }}'
`
err = kbutil.ReplaceInFile(
filepath.Join(dir, "playbooks", "subresourcestest.yml"),
originalPlaybookFragment,
subresourcesPlaybook)
pkg.CheckError("adding playbook for subresourcestest", err)
log.Info("adding playbook for clusterannotationtest")
const clusterAnnotationTest = `---
- hosts: localhost
gather_facts: no
collections:
- kubernetes.core
tasks:
- name: create externalnamespace
k8s:
name: "externalnamespace"
api_version: v1
kind: "Namespace"
definition:
metadata:
labels:
foo: bar
- name: create configmap
k8s:
definition:
apiVersion: v1
kind: ConfigMap
metadata:
namespace: "externalnamespace"
name: '{{ meta.name }}'
data:
foo: bar
`
err = kbutil.ReplaceInFile(
filepath.Join(dir, "playbooks", "clusterannotationtest.yml"),
originalPlaybookFragment,
clusterAnnotationTest)
pkg.CheckError("adding playbook for clusterannotationtest", err)
log.Info("adding playbook for finalizerconcurrencytest")
const finalizerConcurrencyTest = `---
- hosts: localhost
gather_facts: no
collections:
- kubernetes.core
- operator_sdk.util
tasks:
- debug:
msg: "Pausing until configmap exists"
- name: Wait for configmap
k8s_info:
apiVersion: v1
kind: ConfigMap
name: unpause-reconciliation
namespace: osdk-test
wait: yes
wait_sleep: 10
wait_timeout: 360
- debug:
msg: "Unpause!"
`
err = kbutil.ReplaceInFile(
filepath.Join(dir, "playbooks", "finalizerconcurrencytest.yml"),
originalPlaybookFragment,
finalizerConcurrencyTest)
pkg.CheckError("adding playbook for finalizerconcurrencytest", err)
}
func removeFixmeFromPlaybooks(dir string, gvks []schema.GroupVersionKind) {
for _, gvk := range gvks {
k := strings.ToLower(gvk.Kind)
task := fmt.Sprintf("%s_test.yml", k)
logMsgForKind := fmt.Sprintf("removing FIXME assert from %s", task)
log.Info(logMsgForKind)
err := kbutil.ReplaceInFile(
filepath.Join(dir, "molecule", "default", "tasks", task),
fixmeAssert,
"")
pkg.CheckError(logMsgForKind, err)
}
}
const originalPlaybookFragment = `---
- hosts: localhost
gather_facts: no
collections:
- kubernetes.core
- operator_sdk.util
tasks: []
`
const inventorysampleFragment = `name: inventorytest-sample
annotations:
"ansible.sdk.operatorframework.io/verbosity": "0"`