/
memcached_molecule.go
128 lines (102 loc) · 5.89 KB
/
memcached_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
// 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"
"os/exec"
"path/filepath"
"strings"
log "github.com/sirupsen/logrus"
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/sample"
)
func ImplementMemcachedMolecule(sample sample.Sample, image string) {
for _, gvk := range sample.GVKs() {
moleculeTaskPath := filepath.Join(sample.Dir(), "molecule", "default", "tasks",
fmt.Sprintf("%s_test.yml", strings.ToLower(gvk.Kind)))
fmt.Println("MOLECULE TASK PATH:", moleculeTaskPath)
if gvk.Kind == "Memcached" {
log.Info("insert molecule task to ensure that ConfigMap will be deleted")
err := kbutil.InsertCode(moleculeTaskPath, targetMoleculeCheckDeployment, molecuTaskToCheckConfigMap)
pkg.CheckError("replacing memcached task to add config map check", err)
log.Info("insert molecule task to ensure to check secret")
err = kbutil.InsertCode(moleculeTaskPath, memcachedCustomStatusMoleculeTarget, testSecretMoleculeCheck)
pkg.CheckError("replacing memcached task to add secret check", err)
log.Info("insert molecule task to ensure to foo ")
err = kbutil.InsertCode(moleculeTaskPath, testSecretMoleculeCheck, testFooMoleculeCheck)
pkg.CheckError("replacing memcached task to add foo check", err)
log.Info("insert molecule task to check custom metrics")
err = kbutil.InsertCode(moleculeTaskPath, testFooMoleculeCheck, customMetricsTest)
pkg.CheckError("replacing memcached task to add foo check", err)
log.Info("adding Memcached mock task to the role with black list")
err = kbutil.InsertCode(filepath.Join(sample.Dir(), "roles", strings.ToLower(gvk.Kind), "tasks", "main.yml"),
roleFragment, memcachedWithBlackListTask)
pkg.CheckError("replacing in tasks/main.yml", err)
}
if gvk.Kind != "Memcached" {
log.Info("updating spec of kind: ", gvk.Kind)
err := kbutil.ReplaceInFile(
filepath.Join(sample.Dir(), "config", "samples", fmt.Sprintf("%s_%s_%s.yaml", gvk.Group, gvk.Version, strings.ToLower(gvk.Kind))),
"# TODO(user): Add fields here",
"foo: bar")
pkg.CheckError("updating spec of "+fmt.Sprintf("%s_%s_%s.yaml", gvk.Group, gvk.Version, strings.ToLower(gvk.Kind)), err)
log.Infof("removing FIXME asserts from %s_test.yml", strings.ToLower(gvk.Kind))
err = kbutil.ReplaceInFile(filepath.Join(sample.Dir(), "molecule", "default", "tasks", fmt.Sprintf("%s_test.yml", strings.ToLower(gvk.Kind))),
fixmeAssert, "")
pkg.CheckError(fmt.Sprintf("replacing %s_test.yml", strings.ToLower(gvk.Kind)), err)
}
}
log.Info("replacing project Dockerfile to use ansible base image with the dev tag")
err := kbutil.ReplaceRegexInFile(filepath.Join(sample.Dir(), "Dockerfile"), "quay.io/operator-framework/ansible-operator:.*", "quay.io/operator-framework/ansible-operator:dev")
pkg.CheckError("replacing Dockerfile", err)
log.Info("adding RBAC permissions")
err = kbutil.ReplaceInFile(filepath.Join(sample.Dir(), "config", "rbac", "role.yaml"),
"#+kubebuilder:scaffold:rules", rolesForBaseOperator)
pkg.CheckError("replacing in role.yml", err)
log.Info("adding task to delete config map")
err = kbutil.ReplaceInFile(filepath.Join(sample.Dir(), "roles", "memfin", "tasks", "main.yml"),
"# tasks file for Memfin", taskToDeleteConfigMap)
pkg.CheckError("replacing in tasks/main.yml", err)
log.Info("adding to watches finalizer and blacklist")
err = kbutil.ReplaceInFile(filepath.Join(sample.Dir(), "watches.yaml"),
"playbook: playbooks/memcached.yml", memcachedWatchCustomizations)
pkg.CheckError("replacing in watches", err)
log.Info("removing ignore group for the secret from watches as an workaround to work with core types")
err = kbutil.ReplaceInFile(filepath.Join(sample.Dir(), "watches.yaml"),
"ignore.example.com", "\"\"")
pkg.CheckError("replacing the watches file", err)
log.Info("removing molecule test for the Secret since it is a core type")
cmd := exec.Command("rm", "-rf", filepath.Join(sample.Dir(), "molecule", "default", "tasks", "secret_test.yml"))
_, err = sample.CommandContext().Run(cmd)
pkg.CheckError("removing secret test file", err)
log.Info("adding Secret task to the role")
err = kbutil.ReplaceInFile(filepath.Join(sample.Dir(), "roles", "secret", "tasks", "main.yml"),
originalTaskSecret, taskForSecret)
pkg.CheckError("replacing in secret/tasks/main.yml file", err)
log.Info("adding ManageStatus == false for role secret")
err = kbutil.ReplaceInFile(filepath.Join(sample.Dir(), "watches.yaml"),
"role: secret", manageStatusFalseForRoleSecret)
pkg.CheckError("replacing in watches.yaml", err)
// prevent high load of controller caused by watching all the secrets in the cluster
watchNamespacePatchFileName := "watch_namespace_patch.yaml"
log.Info("adding WATCH_NAMESPACE env patch to watch own namespace")
err = os.WriteFile(filepath.Join(sample.Dir(), "config", "testing", watchNamespacePatchFileName), []byte(watchNamespacePatch), 0644)
pkg.CheckError("adding watch_namespace_patch.yaml", err)
log.Info("adding WATCH_NAMESPACE env patch to patch list to be applied")
err = kbutil.InsertCode(filepath.Join(sample.Dir(), "config", "testing", "kustomization.yaml"), "patchesStrategicMerge:",
fmt.Sprintf("\n- %s", watchNamespacePatchFileName))
pkg.CheckError("inserting in kustomization.yaml", err)
}