Skip to content

Commit

Permalink
*: clean up and ansible operator new and add crd command fix up
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Hrivnak <[email protected]>
Co-authored-by: John Kim <[email protected]>
Co-authored-by: Dylan Murray <[email protected]>
  • Loading branch information
4 people committed Oct 16, 2018
1 parent cb369dd commit 11ebe84
Show file tree
Hide file tree
Showing 35 changed files with 653 additions and 192 deletions.
12 changes: 0 additions & 12 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,6 @@ go:

jobs:
include:
- before_script: hack/ci/setup-minikube.sh
env:
- CLUSTER=minikube
- CHANGE_MINIKUBE_NONE_USER=true
script: test/test-go.sh
name: Go on minikube
- before_script: hack/ci/setup-minikube.sh
env:
- CLUSTER=minikube
- CHANGE_MINIKUBE_NONE_USER=true
script: test/test-ansible.sh
name: Ansible on minikube
- before_script: hack/ci/setup-openshift.sh
env: CLUSTER=openshift
script: test/test-go.sh
Expand Down
19 changes: 10 additions & 9 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 Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ else
Q = @
endif

VERSION = $(shell git describe --dirty --tags)
VERSION = $(shell git describe --dirty --tags --always)
REPO = github.com/operator-framework/operator-sdk
BUILD_PATH = $(REPO)/commands/operator-sdk
PKGS = $(shell go list ./... | grep -v /vendor/)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ $ kubectl delete -f deploy/app_v1alpha1_appservice_cr.yaml
$ kubectl delete -f deploy/operator.yaml
$ kubectl delete -f deploy/role.yaml
$ kubectl delete -f deploy/role_binding.yaml
$ kubectl delete -f deploy/app_v1alpha1_appservice_crd.yaml
$ kubectl delete -f deploy/crds/app_v1alpha1_appservice_crd.yaml
```

## User Guide
Expand Down
1 change: 1 addition & 0 deletions commands/operator-sdk/cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ func NewAddCmd() *cobra.Command {

upCmd.AddCommand(add.NewApiCmd())
upCmd.AddCommand(add.NewControllerCmd())
upCmd.AddCommand(add.NewAddCrdCmd())
return upCmd
}
13 changes: 11 additions & 2 deletions commands/operator-sdk/cmd/add/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package add

import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -71,14 +72,16 @@ Example:

func apiRun(cmd *cobra.Command, args []string) {
// Create and validate new resource
cmdutil.MustInProjectRoot()
r, err := scaffold.NewResource(apiVersion, kind)
if err != nil {
log.Fatal(err)
}

absProjectPath := cmdutil.MustGetwd()

cfg := &input.Config{
Repo: cmdutil.MustInProjectRoot(),
Repo: cmdutil.CheckAndGetCurrPkg(),
AbsProjectPath: absProjectPath,
}

Expand Down Expand Up @@ -146,7 +149,13 @@ func updateRoleForResource(r *scaffold.Resource, absProjectPath string) error {
role.Rules = append(role.Rules, *pr)
}
// update role.yaml
data, err := yaml.Marshal(&role)
d, err := json.Marshal(&role)
if err != nil {
return fmt.Errorf("failed to marshal role(%+v): %v", role, err)
}
m := &map[string]interface{}{}
err = yaml.Unmarshal(d, m)
data, err := yaml.Marshal(m)
if err != nil {
return fmt.Errorf("failed to marshal role(%+v): %v", role, err)
}
Expand Down
3 changes: 2 additions & 1 deletion commands/operator-sdk/cmd/add/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,15 @@ Example:
}

func controllerRun(cmd *cobra.Command, args []string) {
cmdutil.MustInProjectRoot()
// Create and validate new resource
r, err := scaffold.NewResource(apiVersion, kind)
if err != nil {
log.Fatal(err)
}

cfg := &input.Config{
Repo: cmdutil.MustInProjectRoot(),
Repo: cmdutil.CheckAndGetCurrPkg(),
AbsProjectPath: cmdutil.MustGetwd(),
}

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

package generate
package add

import (
"errors"
"fmt"
"log"
"os"
"path/filepath"
"strings"

cmdError "github.com/operator-framework/operator-sdk/commands/operator-sdk/error"
"github.com/operator-framework/operator-sdk/pkg/generator"
"github.com/operator-framework/operator-sdk/commands/operator-sdk/cmd/cmdutil"
"github.com/operator-framework/operator-sdk/pkg/scaffold"
"github.com/operator-framework/operator-sdk/pkg/scaffold/input"

"github.com/spf13/cobra"
)

var (
apiVersion string
kind string
)

const (
goDir = "GOPATH"
deployCrdDir = "deploy"
)

func NewGenerateCrdCmd() *cobra.Command {
// NewAddCrdCmd - add crd command
func NewAddCrdCmd() *cobra.Command {
crdCmd := &cobra.Command{
Use: "crd",
Short: "Generates a custom resource definition (CRD) and the custom resource (CR) files",
Long: `The operator-sdk generate command will create a custom resource definition (CRD) and the custom resource (CR) files for the specified api-version and kind.
Short: "Adds a custom resource definition (CRD) and the custom resource (CR) files",
Long: `The operator-sdk add crd command will create a custom resource definition (CRD) and the custom resource (CR) files for the specified api-version and kind.
Generated CRD filename: <project-name>/deploy/<group>_<version>_<kind>_crd.yaml
Generated CR filename: <project-name>/deploy/<group>_<version>_<kind>_cr.yaml
Generated CRD filename: <project-name>/deploy/crds/<group>_<version>_<kind>_crd.yaml
Generated CR filename: <project-name>/deploy/crds/<group>_<version>_<kind>_cr.yaml
<project-name>/deploy path must already exist
--api-version and --kind are required flags to generate the new operator application.
Expand All @@ -59,49 +56,63 @@ Generated CR filename: <project-name>/deploy/<group>_<version>_<kind>_cr.yaml
}

func crdFunc(cmd *cobra.Command, args []string) {
cfg := &input.Config{
AbsProjectPath: cmdutil.MustGetwd(),
}
if len(args) != 0 {
cmdError.ExitWithError(cmdError.ExitBadArgs, errors.New("crd command doesn't accept any arguments."))
log.Fatal("crd command doesn't accept any arguments")
}
verifyCrdFlags()
verifyCrdDeployPath()

fmt.Fprintln(os.Stdout, "Generating custom resource definition (CRD) file")

// generate CR/CRD file
wd, err := os.Getwd()
resource, err := scaffold.NewResource(apiVersion, kind)
if err != nil {
cmdError.ExitWithError(cmdError.ExitError, err)
log.Fatalf("%v", err)
}
if err := generator.RenderDeployCrdFiles(filepath.Join(wd, deployCrdDir), apiVersion, kind); err != nil {
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("failed to generate CRD and CR files: (%v)", err))
s := scaffold.Scaffold{}
err = s.Execute(cfg,
&scaffold.Crd{Resource: resource},
&scaffold.Cr{Resource: resource},
)

if err != nil {
log.Fatalf("add scaffold failed: (%v)", err)
}

// update deploy/role.yaml for the given resource r.
if err := updateRoleForResource(resource, cfg.AbsProjectPath); err != nil {
log.Fatalf("failed to update the RBAC manifest for the resource (%v, %v): %v", resource.APIVersion, resource.Kind, err)
}
}

func verifyCrdFlags() {
if len(apiVersion) == 0 {
cmdError.ExitWithError(cmdError.ExitBadArgs, errors.New("--api-version must not have empty value"))
log.Fatal("--api-version must not have empty value")
}
if len(kind) == 0 {
cmdError.ExitWithError(cmdError.ExitBadArgs, errors.New("--kind must not have empty value"))
log.Fatal("--kind must not have empty value")
}
kindFirstLetter := string(kind[0])
if kindFirstLetter != strings.ToUpper(kindFirstLetter) {
cmdError.ExitWithError(cmdError.ExitBadArgs, errors.New("--kind must start with an uppercase letter"))
log.Fatal("--kind must start with an uppercase letter")
}
if strings.Count(apiVersion, "/") != 1 {
cmdError.ExitWithError(cmdError.ExitBadArgs, fmt.Errorf("api-version has wrong format (%v); format must be $GROUP_NAME/$VERSION (e.g app.example.com/v1alpha1)", apiVersion))
log.Fatalf("api-version has wrong format (%v); format must be $GROUP_NAME/$VERSION (e.g app.example.com/v1alpha1)", apiVersion)
}
}

// verifyCrdDeployPath checks if the path <project-name>/deploy sub-directory is exists, and that is rooted under $GOPATH
func verifyCrdDeployPath() {
wd, err := os.Getwd()
if err != nil {
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("failed to determine the full path of the current directory: %v", err))
log.Fatalf("failed to determine the full path of the current directory: %v", err)
}
// check if the deploy sub-directory exist
_, err = os.Stat(filepath.Join(wd, deployCrdDir))
if err != nil {
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("the path (./%v) does not exist. run this command in your project directory", deployCrdDir))
log.Fatalf("the path (./%v) does not exist. run this command in your project directory", deployCrdDir)
}
}
23 changes: 2 additions & 21 deletions commands/operator-sdk/cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ import (
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/operator-framework/operator-sdk/commands/operator-sdk/cmd/cmdutil"
cmdError "github.com/operator-framework/operator-sdk/commands/operator-sdk/error"

"github.com/operator-framework/operator-sdk/commands/operator-sdk/cmd/cmdutil"
"github.com/operator-framework/operator-sdk/pkg/scaffold"
Expand Down Expand Up @@ -137,9 +133,7 @@ func verifyTestManifest(image string) {
}

const (
build = "./build/build.sh"
configYaml = "./config/config.yaml"
mainGo = "./cmd/manager/main.go"
mainGo = "./cmd/manager/main.go"
)

func buildFunc(cmd *cobra.Command, args []string) {
Expand Down Expand Up @@ -197,7 +191,7 @@ func buildFunc(cmd *cobra.Command, args []string) {

absProjectPath := cmdutil.MustGetwd()
cfg := &input.Config{
Repo: cmdutil.MustInProjectRoot(),
Repo: cmdutil.CheckAndGetCurrPkg(),
AbsProjectPath: absProjectPath,
ProjectName: filepath.Base(wd),
}
Expand Down Expand Up @@ -230,16 +224,3 @@ func mainExists() bool {
}
return false
}

func mainExists() bool {
dir, err := os.Getwd()
if err != nil {
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("failed to get current working dir: %v", err))
}
dirSplit := strings.Split(dir, "/")
projectName := dirSplit[len(dirSplit)-1]
if _, err = os.Stat(fmt.Sprintf(mainGo, projectName)); err == nil {
return true
}
return false
}
12 changes: 5 additions & 7 deletions commands/operator-sdk/cmd/cmdutil/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ import (
type OperatorType int

const (
configYaml = "./config/config.yaml"
gopkgToml = "./Gopkg.toml"
tmpDockerfile = "./tmp/build/Dockerfile"
gopkgToml = "./Gopkg.toml"
buildDockerfile = "./build/Dockerfile"
)
const (
// OperatorTypeGo - golang type of operator.
Expand All @@ -47,14 +46,13 @@ const (

// MustInProjectRoot checks if the current dir is the project root and returns the current repo's import path
// e.g github.com/example-inc/app-operator
func MustInProjectRoot() string {
// if the current directory has the "./cmd/manager/main.go" file, then it is safe to say
func MustInProjectRoot() {
// if the current directory has the "./build/dockerfile" file, then it is safe to say
// we are at the project root.
_, err := os.Stat(tmpDockerfile)
_, err := os.Stat(buildDockerfile)
if err != nil && os.IsNotExist(err) {
log.Fatalf("must run command in project root dir: %v", err)
}
return CheckAndGetCurrPkg()
}

func MustGetwd() string {
Expand Down
8 changes: 1 addition & 7 deletions commands/operator-sdk/cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,8 @@ func NewGenerateCmd() *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.
`,
Long: `The operator-sdk generate command invokes specific generator to generate code as needed.`,
}
cmd.AddCommand(generate.NewGenerateK8SCmd())
<<<<<<< HEAD
cmd.AddCommand(generate.NewGenerateOlmCatalogCmd())
cmd.AddCommand(generate.NewGenerateCrdCmd())
=======
>>>>>>> pkg/generator: remove generator
return cmd
}
Loading

0 comments on commit 11ebe84

Please sign in to comment.