Skip to content

Commit e1c79ca

Browse files
Add check for API rule validation and fix type for Memcached test data (#2113)
* add api rules validation * doc: note improvement review * add TODO a requested in the review * Update doc/user-guide.md Co-Authored-By: Joe Lanford <[email protected]>
1 parent 289ee29 commit e1c79ca

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

doc/user-guide.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,14 @@ type MemcachedSpec struct {
8585
Size int32 `json:"size"`
8686
}
8787
type MemcachedStatus struct {
88-
// Nodes are the names of the memcached pods
88+
// Nodes are the names of the memcached pods
89+
// +listType=set
8990
Nodes []string `json:"nodes"`
9091
}
9192
```
9293

94+
**NOTE:** Comment directives, such as +listType=set, are necessary in certain situations to avoid API rule violations when generating OpenAPI files. See https://godoc.org/k8s.io/kube-openapi/pkg/idl to learn more.
95+
9396
After modifying the `*_types.go` file always run the following command to update the generated code for that resource type:
9497

9598
```sh

hack/tests/scaffolding/scaffold-memcached.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ func main() {
121121
filepath.Join(localSDKPath, "test/e2e/_incluster-test-code/main_test.go"): "test/e2e/main_test.go",
122122
filepath.Join(localSDKPath, "test/e2e/_incluster-test-code/memcached_test.go"): "test/e2e/memcached_test.go",
123123
}
124+
124125
for src, dst := range tmplFiles {
125126
if err := os.MkdirAll(filepath.Dir(dst), fileutil.DefaultDirFileMode); err != nil {
126127
log.Fatalf("Could not create template destination directory: %s", err)
@@ -135,6 +136,7 @@ func main() {
135136
if err != nil {
136137
log.Fatalf("Could not read pkg/apis/cache/v1alpha1/memcached_types.go: %v", err)
137138
}
139+
138140
memcachedTypesFileLines := bytes.Split(memcachedTypesFile, []byte("\n"))
139141
for lineNum, line := range memcachedTypesFileLines {
140142
if strings.Contains(string(line), "type MemcachedSpec struct {") {
@@ -143,13 +145,16 @@ func main() {
143145
break
144146
}
145147
}
148+
146149
for lineNum, line := range memcachedTypesFileLines {
147150
if strings.Contains(string(line), "type MemcachedStatus struct {") {
148-
memcachedTypesFileLinesIntermediate := append(memcachedTypesFileLines[:lineNum+1], []byte("\tNodes []string `json:\"nodes\"`"))
151+
memcachedTypesFileLinesIntermediate := append(memcachedTypesFileLines[:lineNum+1], []byte("\t// +listType=set"))
152+
memcachedTypesFileLinesIntermediate = append(memcachedTypesFileLinesIntermediate, []byte("\tNodes []string `json:\"nodes\"`"))
149153
memcachedTypesFileLines = append(memcachedTypesFileLinesIntermediate, memcachedTypesFileLines[lineNum+3:]...)
150154
break
151155
}
152156
}
157+
153158
if err := os.Remove("pkg/apis/cache/v1alpha1/memcached_types.go"); err != nil {
154159
log.Fatalf("Failed to remove old memcached_type.go file: (%v)", err)
155160
}
@@ -170,6 +175,15 @@ func main() {
170175
log.Fatalf("Error: %v\nCommand Output: %s\n", err, string(cmdOut))
171176
}
172177

178+
// TODO(camilamacedo86) Move this test to a unit test in
179+
// `cmd/operator-sdk/internal/genutil/`. Unit tests are
180+
// faster and are run more often during development, so it
181+
// would be an improvement to implement this test there.
182+
log.Print("Checking API rule violations")
183+
if strings.Contains(string(cmdOut), "API rule violation") {
184+
log.Fatalf("Error: %v\nCommand Output: %s\n", "API rule violations :", string(cmdOut))
185+
}
186+
173187
log.Print("Pulling new dependencies with go mod")
174188
cmdOut, err = exec.Command("go", "build", "./...").CombinedOutput()
175189
if err != nil {

0 commit comments

Comments
 (0)