Skip to content

Create CRD validation specs in CRD manifests and as Go code #869

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 49 commits into from
Jan 31, 2019
Merged
Show file tree
Hide file tree
Changes from 34 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
c8cccc8
commands/.../generate/*: build and call openapi-gen, and re-scaffold …
estroz Dec 19, 2018
0e5fb86
fix comment
estroz Dec 19, 2018
7db89b6
only use controller-tools CRD generator if in a Go project
estroz Dec 19, 2018
82c09e2
change dir in unit test so Go project is detected
estroz Dec 19, 2018
cdb791c
add names and only overwrite dstCrd when a file at path was generated
estroz Dec 20, 2018
2aeb74b
use generated crd if one isn't present locally
estroz Dec 20, 2018
e78a483
add test case for non-Go crd generation
estroz Dec 20, 2018
03f39de
internal/util/*util/*: remove pkg/scaffold imports, which cause cycles
estroz Dec 20, 2018
ac572ae
test/e2e/memcached_test.go: symlink pkg and internal
estroz Dec 20, 2018
39dd0f9
pkg/scaffold/*: use a testData dir for scaffold tests instead of test…
estroz Dec 20, 2018
590d95e
rename test dir to avoid import cycles during e2e test
estroz Dec 20, 2018
05c7318
pkg/scaffold/crd.go: add IsOperatorGo field for controller-tools crd …
estroz Dec 20, 2018
397a9fe
use test/test-framework code as test project
estroz Dec 20, 2018
ccce673
revert removing scaffold constants from internal/util
estroz Dec 21, 2018
b06c742
revert SrcDir change and check IsOperatorGo first
estroz Dec 21, 2018
70db2ca
Merge branch 'master' into crd-validation
estroz Dec 21, 2018
3db3e2f
change function name
estroz Dec 21, 2018
94021e5
pkg/scaffold/crd.go: simplify unmarshalling crd
estroz Jan 3, 2019
df3df61
use Golang initialism/acronym conventions for naming CR(D) types/vari…
estroz Jan 10, 2019
003dc22
Merge branch 'master' into crd-validation
estroz Jan 12, 2019
63f5730
Merge branch 'master' into crd-validation
estroz Jan 15, 2019
d15010c
Gopkg.lock: revendor
estroz Jan 15, 2019
394b756
commands/.../openapi.go: return error from CLI func instead of log.Fatal
estroz Jan 15, 2019
8275efa
Merge branch 'master' into crd-validation
estroz Jan 16, 2019
fbec75b
fix kube-openapi revision
estroz Jan 22, 2019
5113e09
Merge branch 'master' into crd-validation
estroz Jan 22, 2019
b5e915d
pkg/scaffold/gopkgtoml*.go: force sigs.k8s.io/controller-tools/pkg/cr…
estroz Jan 22, 2019
0796cb3
Merge branch 'master' into crd-validation
estroz Jan 23, 2019
53d1b59
Merge branch 'master' into crd-validation
estroz Jan 23, 2019
2367e09
Merge branch 'master' into crd-validation
estroz Jan 24, 2019
3c307d6
Merge branch 'master' into crd-validation
estroz Jan 25, 2019
64688b0
remove extra kube-openapi override
estroz Jan 25, 2019
e38a2f4
pkg/scaffold/types*.go: add note on adding custom validation
estroz Jan 26, 2019
92b416f
commands/.../api.go: comment on what is generated in add api command …
estroz Jan 26, 2019
3c379d0
commands/.../openapi.go: remove go header file from openapi-gen
estroz Jan 28, 2019
334156e
new() -> &...{}
estroz Jan 28, 2019
d0cbe0c
correct verbosity settings for deepcopy and openapi generators
estroz Jan 28, 2019
5390096
Merge branch 'master' into crd-validation
estroz Jan 28, 2019
2d4b621
commands/operator-sdk/cmd/add/crd.go: only skip overwriting CRD's/CR'…
estroz Jan 28, 2019
2c76037
Merge branch 'skip-crd' into crd-validation
estroz Jan 28, 2019
9b04d84
Merge branch 'master' into crd-validation
estroz Jan 28, 2019
1da3de9
Merge branch 'master' into crd-validation
estroz Jan 29, 2019
cc36958
verbose code generation in e2e test
estroz Jan 29, 2019
93229be
add --header-file for Go boilerplate file path
estroz Jan 30, 2019
b46c19a
update openapi command comment
estroz Jan 30, 2019
d8108ef
Merge branch 'master' into crd-validation
estroz Jan 31, 2019
35ff825
override go-openapi/spec to avoid new project dep solve errors
estroz Jan 31, 2019
4c50988
Merge branch 'master' into crd-validation
estroz Jan 31, 2019
446d2f1
revendor
estroz Jan 31, 2019
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
38 changes: 38 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion commands/operator-sdk/cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ func NewAddCmd() *cobra.Command {

upCmd.AddCommand(add.NewApiCmd())
upCmd.AddCommand(add.NewControllerCmd())
upCmd.AddCommand(add.NewAddCrdCmd())
upCmd.AddCommand(add.NewAddCRDCmd())
return upCmd
}
30 changes: 24 additions & 6 deletions commands/operator-sdk/cmd/add/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,17 @@ func NewApiCmd() *cobra.Command {
Use: "api",
Short: "Adds a new api definition under pkg/apis",
Long: `operator-sdk add api --kind=<kind> --api-version=<group/version> creates the
api definition for a new custom resource under pkg/apis. This command must be run from the project root directory.
If the api already exists at pkg/apis/<group>/<version> then the command will not overwrite and return an error.
api definition for a new custom resource under pkg/apis. This command must be
run from the project root directory. If the api already exists at
pkg/apis/<group>/<version> then the command will not overwrite and return an
error.

This command runs Kubernetes deepcopy and OpenAPI V3 generators on tagged
types in all paths under pkg/apis. Go code is generated under
pkg/apis/<group>/<version>/zz_generated.{deepcopy,openapi}.go. CRD's are
generated, or updated if they exist for a particular group + version + kind,
under deploy/crds/<group>_<version>_<kind>_crd.yaml; OpenAPI V3 validation YAML
is generated as a 'validation' object.

Example:
$ operator-sdk add api --api-version=app.example.com/v1alpha1 --kind=AppService
Expand All @@ -49,8 +58,12 @@ Example:
└── v1alpha1
├── doc.go
├── register.go
├── types.go

├── appservice_types.go
├── zz_generated.deepcopy.go
├── zz_generated.openapi.go
$ tree deploy/crds
├── deploy/crds/app_v1alpha1_appservice_cr.yaml
├── deploy/crds/app_v1alpha1_appservice_crd.yaml
`,
RunE: apiRun,
}
Expand Down Expand Up @@ -96,8 +109,8 @@ func apiRun(cmd *cobra.Command, args []string) error {
&scaffold.AddToScheme{Resource: r},
&scaffold.Register{Resource: r},
&scaffold.Doc{Resource: r},
&scaffold.Cr{Resource: r},
&scaffold.Crd{Resource: r},
&scaffold.CR{Resource: r},
&scaffold.CRD{Resource: r, IsOperatorGo: projutil.IsOperatorGo()},
)
if err != nil {
return fmt.Errorf("api scaffold failed: (%v)", err)
Expand All @@ -113,6 +126,11 @@ func apiRun(cmd *cobra.Command, args []string) error {
return err
}

// Generate a validation spec for the new CRD.
if err := generate.OpenAPIGen(); err != nil {
return err
}

log.Info("API generation complete.")
return nil
}
8 changes: 4 additions & 4 deletions commands/operator-sdk/cmd/add/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import (
"github.com/spf13/cobra"
)

// NewAddCrdCmd - add crd command
func NewAddCrdCmd() *cobra.Command {
// NewAddCRDCmd - add crd command
func NewAddCRDCmd() *cobra.Command {
crdCmd := &cobra.Command{
Use: "crd",
Short: "Adds a Custom Resource Definition (CRD) and the Custom Resource (CR) files",
Expand Down Expand Up @@ -78,8 +78,8 @@ func crdFunc(cmd *cobra.Command, args []string) error {

s := scaffold.Scaffold{}
err = s.Execute(cfg,
&scaffold.Crd{Resource: resource},
&scaffold.Cr{Resource: resource},
&scaffold.CRD{Resource: resource, IsOperatorGo: projutil.IsOperatorGo()},
&scaffold.CR{Resource: resource},
)
if err != nil {
return fmt.Errorf("crd scaffold failed: (%v)", err)
Expand Down
1 change: 1 addition & 0 deletions commands/operator-sdk/cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ func NewGenerateCmd() *cobra.Command {
Long: `The operator-sdk generate command invokes specific generator to generate code as needed.`,
}
cmd.AddCommand(generate.NewGenerateK8SCmd())
cmd.AddCommand(generate.NewGenerateOpenAPICmd())
return cmd
}
110 changes: 110 additions & 0 deletions commands/operator-sdk/cmd/generate/internal/genutil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// Copyright 2018 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 genutil

import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/operator-framework/operator-sdk/pkg/scaffold"
)

func BuildCodegenBinaries(genDirs []string, binDir, codegenSrcDir string) error {
for _, gd := range genDirs {
err := runGoBuildCodegen(binDir, codegenSrcDir, gd)
if err != nil {
return err
}
}
return nil
}

func runGoBuildCodegen(binDir, repoDir, genDir string) error {
binPath := filepath.Join(binDir, filepath.Base(genDir))
installCmd := exec.Command("go", "build", "-o", binPath, genDir)
installCmd.Dir = repoDir
isVerbose := false
if gf, ok := os.LookupEnv("GOFLAGS"); ok && len(gf) != 0 {
installCmd.Env = append(os.Environ(), "GOFLAGS="+gf)
if strings.Contains(gf, "-v") {
isVerbose = true
}
}
if isVerbose {
installCmd.Stdout = os.Stdout
installCmd.Stderr = os.Stderr
} else {
installCmd.Stdout = ioutil.Discard
installCmd.Stderr = ioutil.Discard
}
return installCmd.Run()
}

// ParseGroupVersions parses the layout of pkg/apis to return a map of
// API groups to versions.
func ParseGroupVersions() (map[string][]string, error) {
gvs := make(map[string][]string)
groups, err := ioutil.ReadDir(scaffold.ApisDir)
if err != nil {
return nil, fmt.Errorf("could not read pkg/apis directory to find api Versions: %v", err)
}

for _, g := range groups {
if g.IsDir() {
groupDir := filepath.Join(scaffold.ApisDir, g.Name())
versions, err := ioutil.ReadDir(groupDir)
if err != nil {
return nil, fmt.Errorf("could not read %s directory to find api Versions: %v", groupDir, err)
}

gvs[g.Name()] = make([]string, 0)
for _, v := range versions {
if v.IsDir() && scaffold.ResourceVersionRegexp.MatchString(v.Name()) {
gvs[g.Name()] = append(gvs[g.Name()], v.Name())
}
}
}
}

if len(gvs) == 0 {
return nil, fmt.Errorf("no groups or versions found in %s", scaffold.ApisDir)
}
return gvs, nil
}

// CreateFQApis return a string of all fully qualified pkg + groups + versions
// of pkg and gvs in the format:
// "pkg/groupA/v1,pkg/groupA/v2,pkg/groupB/v1"
func CreateFQApis(pkg string, gvs map[string][]string) string {
gn := 0
fqb := &strings.Builder{}
for g, vs := range gvs {
for vn, v := range vs {
fqb.WriteString(filepath.Join(pkg, g, v))
if vn < len(vs)-1 {
fqb.WriteString(",")
}
}
if gn < len(gvs)-1 {
fqb.WriteString(",")
}
gn++
}
return fqb.String()
}
Loading