Skip to content

commands/operator-sdk: rename to cmd/operator-sdk and reorganize #1185

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 15 commits into from
Mar 28, 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
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ endif

VERSION = $(shell git describe --dirty --tags --always)
REPO = github.com/operator-framework/operator-sdk
BUILD_PATH = $(REPO)/commands/operator-sdk
BUILD_PATH = $(REPO)/cmd/operator-sdk
PKGS = $(shell go list ./... | grep -v /vendor/)
SOURCES = $(shell find . -name '*.go' -not -path "*/vendor/*")

Expand Down Expand Up @@ -85,7 +85,7 @@ test/sanity:
./hack/tests/sanity-check.sh

test/unit:
$(Q)go test -count=1 -short ./commands/...
$(Q)go test -count=1 -short ./cmd/...
$(Q)go test -count=1 -short ./pkg/...

test/subcommand: test/subcommand/test-local test/subcommand/scorecard
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package add
import (
"fmt"

"github.com/operator-framework/operator-sdk/commands/operator-sdk/cmd/generate"
"github.com/operator-framework/operator-sdk/cmd/operator-sdk/internal/genutil"
"github.com/operator-framework/operator-sdk/internal/util/projutil"
"github.com/operator-framework/operator-sdk/pkg/scaffold"
"github.com/operator-framework/operator-sdk/pkg/scaffold/input"
Expand All @@ -32,7 +32,7 @@ var (
headerFile string
)

func NewApiCmd() *cobra.Command {
func newAddApiCmd() *cobra.Command {
apiCmd := &cobra.Command{
Use: "api",
Short: "Adds a new api definition under pkg/apis",
Expand Down Expand Up @@ -124,12 +124,12 @@ func apiRun(cmd *cobra.Command, args []string) error {
}

// Run k8s codegen for deepcopy
if err := generate.K8sCodegen(headerFile); err != nil {
if err := genutil.K8sCodegen(headerFile); err != nil {
return err
}

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

Expand Down
16 changes: 7 additions & 9 deletions commands/operator-sdk/cmd/add.go → cmd/operator-sdk/add/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,21 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd
package add

import (
"github.com/operator-framework/operator-sdk/commands/operator-sdk/cmd/add"

"github.com/spf13/cobra"
)

func NewAddCmd() *cobra.Command {
upCmd := &cobra.Command{
func NewCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "add",
Short: "Adds a controller or resource to the project",
Long: "",
}

upCmd.AddCommand(add.NewApiCmd())
upCmd.AddCommand(add.NewControllerCmd())
upCmd.AddCommand(add.NewAddCRDCmd())
return upCmd
cmd.AddCommand(newAddApiCmd())
cmd.AddCommand(newAddControllerCmd())
cmd.AddCommand(newAddCRDCmd())
return cmd
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"github.com/spf13/cobra"
)

func NewControllerCmd() *cobra.Command {
func newAddControllerCmd() *cobra.Command {
controllerCmd := &cobra.Command{
Use: "controller",
Short: "Adds a new controller pkg",
Expand Down
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd
package build

import "testing"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd
package build

import (
"errors"
Expand Down Expand Up @@ -41,7 +41,7 @@ var (
dockerBuildArgs string
)

func NewBuildCmd() *cobra.Command {
func NewCmd() *cobra.Command {
buildCmd := &cobra.Command{
Use: "build <image>",
Short: "Compiles code and builds artifacts",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"github.com/spf13/cobra"
)

func NewBashCmd() *cobra.Command {
func newBashCmd() *cobra.Command {
return &cobra.Command{
Use: "bash",
Short: "Generate bash completions",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,18 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd
package completion

import (
"github.com/spf13/cobra"

"github.com/operator-framework/operator-sdk/commands/operator-sdk/cmd/completion"
)

func NewCompletionCmd() *cobra.Command {
func NewCmd() *cobra.Command {
completionCmd := &cobra.Command{
Use: "completion",
Short: "Generators for shell completions",
}
completionCmd.AddCommand(completion.NewZshCmd())
completionCmd.AddCommand(completion.NewBashCmd())
completionCmd.AddCommand(newZshCmd())
completionCmd.AddCommand(newBashCmd())
return completionCmd
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"github.com/spf13/cobra"
)

func NewZshCmd() *cobra.Command {
func newZshCmd() *cobra.Command {
return &cobra.Command{
Use: "zsh",
Short: "Generate zsh completions",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,19 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd
package generate

import (
"github.com/operator-framework/operator-sdk/commands/operator-sdk/cmd/generate"

"github.com/spf13/cobra"
)

func NewGenerateCmd() *cobra.Command {
func NewCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "generate <generator>",
Short: "Invokes specific generator",
Long: `The operator-sdk generate command invokes specific generator to generate code as needed.`,
}
cmd.AddCommand(generate.NewGenerateK8SCmd())
cmd.AddCommand(generate.NewGenerateOpenAPICmd())
cmd.AddCommand(newGenerateK8SCmd())
cmd.AddCommand(newGenerateOpenAPICmd())
return cmd
}
61 changes: 61 additions & 0 deletions cmd/operator-sdk/generate/k8s.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// 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 generate

import (
"fmt"

"github.com/operator-framework/operator-sdk/cmd/operator-sdk/internal/genutil"
"github.com/operator-framework/operator-sdk/internal/util/projutil"

"github.com/spf13/cobra"
)

func newGenerateK8SCmd() *cobra.Command {
k8sCmd := &cobra.Command{
Use: "k8s",
Short: "Generates Kubernetes code for custom resource",
Long: `k8s generator generates code for custom resources given the API
specs in pkg/apis/<group>/<version> directories to comply with kube-API
requirements. Go code is generated under
pkg/apis/<group>/<version>/zz_generated.deepcopy.go.
Example:
$ operator-sdk generate k8s
$ tree pkg/apis
pkg/apis/
└── app
└── v1alpha1
├── zz_generated.deepcopy.go
`,
RunE: k8sFunc,
}

k8sCmd.Flags().StringVar(&headerFile, "header-file", "", "Path to file containing headers for generated files.")

return k8sCmd
}

func k8sFunc(cmd *cobra.Command, args []string) error {
if len(args) != 0 {
return fmt.Errorf("command %s doesn't accept any arguments", cmd.CommandPath())
}

// Only Go projects can generate k8s deepcopy code.
if err := projutil.CheckGoProjectCmd(cmd); err != nil {
return err
}

return genutil.K8sCodegen(headerFile)
}
62 changes: 62 additions & 0 deletions cmd/operator-sdk/generate/openapi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// 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 generate

import (
"fmt"

"github.com/operator-framework/operator-sdk/cmd/operator-sdk/internal/genutil"
"github.com/spf13/cobra"
)

var headerFile string

func newGenerateOpenAPICmd() *cobra.Command {
openAPICmd := &cobra.Command{
Use: "openapi",
Short: "Generates OpenAPI specs for API's",
Long: `generate openapi generates OpenAPI validation specs in Go from tagged types
in all pkg/apis/<group>/<version> directories. Go code is generated under
pkg/apis/<group>/<version>/zz_generated.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 generate openapi
$ tree pkg/apis
pkg/apis/
└── app
└── v1alpha1
├── zz_generated.openapi.go
$ tree deploy/crds
├── deploy/crds/app_v1alpha1_appservice_cr.yaml
├── deploy/crds/app_v1alpha1_appservice_crd.yaml
`,
RunE: openAPIFunc,
}

openAPICmd.Flags().StringVar(&headerFile, "header-file", "", "Path to file containing headers for generated files.")

return openAPICmd
}

func openAPIFunc(cmd *cobra.Command, args []string) error {
if len(args) != 0 {
return fmt.Errorf("command %s doesn't accept any arguments", cmd.CommandPath())
}

return genutil.OpenAPIGen(headerFile)
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
log "github.com/sirupsen/logrus"
)

func BuildCodegenBinaries(genDirs []string, binDir, codegenSrcDir string) error {
func buildCodegenBinaries(genDirs []string, binDir, codegenSrcDir string) error {
for _, gd := range genDirs {
err := runGoBuildCodegen(binDir, codegenSrcDir, gd)
if err != nil {
Expand Down Expand Up @@ -56,7 +56,7 @@ func runGoBuildCodegen(binDir, repoDir, genDir string) error {

// ParseGroupVersions parses the layout of pkg/apis to return a map of
// API groups to versions.
func ParseGroupVersions() (map[string][]string, error) {
func parseGroupVersions() (map[string][]string, error) {
gvs := make(map[string][]string)
groups, err := ioutil.ReadDir(scaffold.ApisDir)
if err != nil {
Expand Down Expand Up @@ -89,7 +89,7 @@ func ParseGroupVersions() (map[string][]string, error) {
// 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 {
func createFQApis(pkg string, gvs map[string][]string) string {
gn := 0
fqb := &strings.Builder{}
for g, vs := range gvs {
Expand All @@ -107,7 +107,7 @@ func CreateFQApis(pkg string, gvs map[string][]string) string {
return fqb.String()
}

func WithHeaderFile(hf string, f func(string) error) (err error) {
func withHeaderFile(hf string, f func(string) error) (err error) {
if hf == "" {
hf, err = createEmptyTmpFile()
if err != nil {
Expand Down
Loading