-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
scaffold-memcached.go
226 lines (202 loc) · 8.26 KB
/
scaffold-memcached.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
// Copyright 2019 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 main
import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"github.com/operator-framework/operator-sdk/internal/util/fileutil"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
// TODO: Migrate most/all of the cli commands to the bash script instead of keeping them here
const (
sdkRepo = "github.com/operator-framework/operator-sdk"
operatorName = "memcached-operator"
testRepo = "github.com/example-inc/" + operatorName
)
func main() {
localRepo := flag.String("local-repo", "", "Path to local SDK repository being tested. Only use when running e2e tests locally")
imageName := flag.String("image-name", "", "Name of image being used for tests")
noPull := flag.Bool("local-image", false, "Disable pulling images as image is local")
flag.Parse()
// get global framework variables
sdkTestE2EDir, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
defer func() {
if err := os.Chdir(sdkTestE2EDir); err != nil {
log.Errorf("Failed to change back to original working directory: (%v)", err)
}
}()
localSDKPath := *localRepo
if localSDKPath == "" {
localSDKPath = sdkTestE2EDir
}
log.Print("Creating new operator project")
cmdOut, err := exec.Command("operator-sdk",
"new",
operatorName,
"--repo", testRepo,
"--skip-validation").CombinedOutput()
if err != nil {
log.Fatalf("Error: %v\nCommand Output: %s\n", err, string(cmdOut))
}
if err := os.Chdir(operatorName); err != nil {
log.Fatalf("Failed to change to %s directory: (%v)", operatorName, err)
}
// Always use the local SDK path in the go.mod "replace" line.
modBytes, err := insertGoModReplaceDir(sdkRepo, localSDKPath)
if err != nil {
log.Fatalf("Failed to insert go.mod replace: %v", err)
}
log.Printf("go.mod: %v", string(modBytes))
cmdOut, err = exec.Command("go", "build", "./...").CombinedOutput()
if err != nil {
log.Fatalf("Command \"go build ./...\" failed after modifying go.mod: %v\nCommand Output:\n%v", err, string(cmdOut))
}
// Set replicas to 2 to test leader election. In production, this should
// almost always be set to 1, because there isn't generally value in having
// a hot spare operator process.
opYaml, err := ioutil.ReadFile("deploy/operator.yaml")
if err != nil {
log.Fatalf("Could not read deploy/operator.yaml: %v", err)
}
newOpYaml := bytes.Replace(opYaml, []byte("replicas: 1"), []byte("replicas: 2"), 1)
err = ioutil.WriteFile("deploy/operator.yaml", newOpYaml, 0644)
if err != nil {
log.Fatalf("Could not write deploy/operator.yaml: %v", err)
}
cmd := exec.Command("operator-sdk",
"add",
"api",
"--api-version=cache.example.com/v1alpha1",
"--kind=Memcached")
cmd.Env = os.Environ()
cmdOut, err = cmd.CombinedOutput()
if err != nil {
log.Fatalf("Error: %v\nCommand Output: %s\n", err, string(cmdOut))
}
cmdOut, err = exec.Command("operator-sdk",
"add",
"controller",
"--api-version=cache.example.com/v1alpha1",
"--kind=Memcached").CombinedOutput()
if err != nil {
log.Fatalf("Error: %v\nCommand Output: %s\n", err, string(cmdOut))
}
tmplFiles := map[string]string{
filepath.Join(localSDKPath, "example/memcached-operator/memcached_controller.go.tmpl"): "pkg/controller/memcached/memcached_controller.go",
filepath.Join(localSDKPath, "test/e2e/_incluster-test-code/main_test.go"): "test/e2e/main_test.go",
filepath.Join(localSDKPath, "test/e2e/_incluster-test-code/memcached_test.go"): "test/e2e/memcached_test.go",
}
for src, dst := range tmplFiles {
if err := os.MkdirAll(filepath.Dir(dst), fileutil.DefaultDirFileMode); err != nil {
log.Fatalf("Could not create template destination directory: %s", err)
}
cmdOut, err = exec.Command("cp", src, dst).CombinedOutput()
if err != nil {
log.Fatalf("Error: %v\nCommand Output: %s\n", err, string(cmdOut))
}
}
memcachedTypesFile, err := ioutil.ReadFile("pkg/apis/cache/v1alpha1/memcached_types.go")
if err != nil {
log.Fatalf("Could not read pkg/apis/cache/v1alpha1/memcached_types.go: %v", err)
}
memcachedTypesFileLines := bytes.Split(memcachedTypesFile, []byte("\n"))
for lineNum, line := range memcachedTypesFileLines {
if strings.Contains(string(line), "type MemcachedSpec struct {") {
memcachedTypesFileLinesIntermediate := append(memcachedTypesFileLines[:lineNum+1], []byte("\tSize int32 `json:\"size\"`"))
memcachedTypesFileLines = append(memcachedTypesFileLinesIntermediate, memcachedTypesFileLines[lineNum+3:]...)
break
}
}
for lineNum, line := range memcachedTypesFileLines {
if strings.Contains(string(line), "type MemcachedStatus struct {") {
memcachedTypesFileLinesIntermediate := append(memcachedTypesFileLines[:lineNum+1], []byte("\t// +listType=set"))
memcachedTypesFileLinesIntermediate = append(memcachedTypesFileLinesIntermediate, []byte("\tNodes []string `json:\"nodes\"`"))
memcachedTypesFileLines = append(memcachedTypesFileLinesIntermediate, memcachedTypesFileLines[lineNum+3:]...)
break
}
}
if err := os.Remove("pkg/apis/cache/v1alpha1/memcached_types.go"); err != nil {
log.Fatalf("Failed to remove old memcached_type.go file: (%v)", err)
}
err = ioutil.WriteFile("pkg/apis/cache/v1alpha1/memcached_types.go", bytes.Join(memcachedTypesFileLines, []byte("\n")), fileutil.DefaultFileMode)
if err != nil {
log.Fatalf("Could not write to pkg/apis/cache/v1alpha1/memcached_types.go: %v", err)
}
log.Print("Generating k8s")
cmdOut, err = exec.Command("operator-sdk", "generate", "k8s").CombinedOutput()
if err != nil {
log.Fatalf("Error: %v\nCommand Output: %s\n", err, string(cmdOut))
}
log.Print("Generating openapi")
cmdOut, err = exec.Command("operator-sdk", "generate", "openapi").CombinedOutput()
if err != nil {
log.Fatalf("Error: %v\nCommand Output: %s\n", err, string(cmdOut))
}
// TODO(camilamacedo86) Move this test to a unit test in
// `cmd/operator-sdk/internal/genutil/`. Unit tests are
// faster and are run more often during development, so it
// would be an improvement to implement this test there.
log.Print("Checking API rule violations")
if strings.Contains(string(cmdOut), "API rule violation") {
log.Fatalf("Error: %v\nCommand Output: %s\n", "API rule violations :", string(cmdOut))
}
log.Print("Pulling new dependencies with go mod")
cmdOut, err = exec.Command("go", "build", "./...").CombinedOutput()
if err != nil {
log.Fatalf("Command \"go build ./...\" failed: %v\nCommand Output:\n%v", err, string(cmdOut))
}
operatorYAML, err := ioutil.ReadFile("deploy/operator.yaml")
if err != nil {
log.Fatalf("Could not read deploy/operator.yaml: %v", err)
}
if *imageName == "" {
*imageName = "quay.io/example/memcached-operator:v0.0.1"
}
if *noPull {
operatorYAML = bytes.Replace(operatorYAML, []byte("imagePullPolicy: Always"), []byte("imagePullPolicy: Never"), 1)
}
operatorYAML = bytes.Replace(operatorYAML, []byte("REPLACE_IMAGE"), []byte(*imageName), 1)
err = ioutil.WriteFile("deploy/operator.yaml", operatorYAML, fileutil.DefaultFileMode)
if err != nil {
log.Fatalf("Failed to write to deploy/operator.yaml: %v", err)
}
}
func insertGoModReplaceDir(repo, path string) ([]byte, error) {
modBytes, err := ioutil.ReadFile("go.mod")
if err != nil {
return nil, errors.Wrap(err, "failed to read go.mod")
}
// Remove all replace lines in go.mod.
replaceRe := regexp.MustCompile(fmt.Sprintf("(replace )?%s =>.+", repo))
modBytes = replaceRe.ReplaceAll(modBytes, nil)
// Append the desired replace to the end of go.mod's bytes.
sdkReplace := fmt.Sprintf("replace %s => %s", repo, path)
modBytes = append(modBytes, []byte("\n"+sdkReplace)...)
err = ioutil.WriteFile("go.mod", modBytes, fileutil.DefaultFileMode)
if err != nil {
return nil, errors.Wrap(err, "failed to write go.mod before replacing SDK repo")
}
return modBytes, nil
}