Skip to content

Add check for API rule validation and fix type for Memcached test data #2113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion doc/user-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,14 @@ type MemcachedSpec struct {
Size int32 `json:"size"`
}
type MemcachedStatus struct {
// Nodes are the names of the memcached pods
// Nodes are the names of the memcached pods
// +listType=set
Nodes []string `json:"nodes"`
}
```

**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.

After modifying the `*_types.go` file always run the following command to update the generated code for that resource type:

```sh
Expand Down
16 changes: 15 additions & 1 deletion hack/tests/scaffolding/scaffold-memcached.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ func main() {
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)
Expand All @@ -139,6 +140,7 @@ func main() {
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 {") {
Expand All @@ -147,13 +149,16 @@ func main() {
break
}
}

for lineNum, line := range memcachedTypesFileLines {
if strings.Contains(string(line), "type MemcachedStatus struct {") {
memcachedTypesFileLinesIntermediate := append(memcachedTypesFileLines[:lineNum+1], []byte("\tNodes []string `json:\"nodes\"`"))
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)
}
Expand All @@ -174,6 +179,15 @@ func main() {
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 {
Expand Down