Skip to content
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

*: remove GOPATH requirement from the SDK and operator projects #1475

Merged
merged 51 commits into from
Jun 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
87859e4
improve repo-getting behaviour
estroz May 22, 2019
9ea15d2
Merge branch 'master' into remove-gopath
estroz May 22, 2019
c4ef2de
cmd/operator-sdk/internal/genutil: use generators directly instead of…
estroz May 22, 2019
e02ba68
revendor
estroz May 22, 2019
33e0c79
Merge branch 'codify-generators' into remove-gopath
estroz May 22, 2019
01522d3
remove code generators from tools.go
estroz May 22, 2019
2b92c73
use relative package path instead of non default deepcopy args
estroz May 22, 2019
a36456e
check if in /home/estroz/go/src before running deepcopy generator
estroz May 22, 2019
fd60dcc
Merge branch 'codify-generators' into remove-gopath
estroz May 22, 2019
e148864
*.md: remove /home/estroz/go references
estroz May 22, 2019
281f0c9
do not rely on /home/estroz/go in scripts, CLI, and tests
estroz May 22, 2019
46ab17a
Merge branch 'master' into remove-gopath
estroz May 23, 2019
bbc2c69
test/e2e/main_test.go: use testArgs struct for test args; add
estroz May 17, 2019
d5be9a3
hack/tests/e2e-{ansible,helm}.sh: remove SDK replace lines in go.mod …
estroz May 22, 2019
fdae734
use --repo for 'operator-sdk new' in e2e
estroz May 23, 2019
81dc7e7
dep manager check for all commands used in project root
estroz May 23, 2019
dc6d236
revendor
estroz May 23, 2019
4a0efa6
use correct repo for new project; cp e2e code templates without repla…
estroz May 24, 2019
37d21eb
get correct SDK dir in e2e
estroz May 23, 2019
f496a8d
Merge branch 'fix-release-e2e' into remove-gopath
estroz May 24, 2019
88daa00
Merge branch 'master' into fix-release-e2e
estroz May 30, 2019
46004c1
Merge branch 'master' into remove-gopath
estroz May 31, 2019
0ff9592
fix comment spelling
estroz May 31, 2019
b6bfd1d
internal/util/projutil: generalize project util functions for GOPATH …
estroz May 31, 2019
7ddb8a4
Merge branch 'master' into fix-release-e2e
estroz Jun 11, 2019
566aa6c
comment out SDK require before parsing
estroz Jun 11, 2019
370ff16
use regex to replace all references to the SDK repo
estroz Jun 11, 2019
b3abfe9
Merge branch 'master' into remove-gopath-projutil
estroz Jun 11, 2019
e23b6fa
rename GOPATH setter util function
estroz Jun 11, 2019
36d9c07
Merge branch 'master' into remove-gopath
estroz Jun 11, 2019
f899b9b
Merge branch 'remove-gopath-projutil' into remove-gopath
estroz Jun 11, 2019
3c70407
Merge branch 'fix-release-e2e' into remove-gopath
estroz Jun 11, 2019
7d7f050
remove unused function
estroz Jun 11, 2019
6dd3de5
Merge branch 'master' into remove-gopath
estroz Jun 13, 2019
ead25b9
updates based on PR comments
estroz Jun 13, 2019
9b1ae6f
exit if go.mod exists but has incorrect permissions or a filesystem e…
estroz Jun 13, 2019
5142a33
Apply suggestions from code review
Jun 14, 2019
29cfc80
updates from PR comments
estroz Jun 14, 2019
17ead14
Merge branch 'master' into remove-gopath
estroz Jun 14, 2019
e848c48
some updates
estroz Jun 14, 2019
e02e08e
Update cmd/operator-sdk/main.go
Jun 14, 2019
4b2926b
Apply suggestions from code review
Jun 17, 2019
e32ab07
Merge branch 'master' into remove-gopath
estroz Jun 17, 2019
65fea0f
refactor dep/repo check into a projutil function
estroz Jun 18, 2019
d540810
Apply suggestions from code review
Jun 18, 2019
a4d1c96
Apply suggestions from code review
Jun 19, 2019
d62b15c
more informative log message
estroz Jun 19, 2019
a90c037
update "go modules" -> "Go modules"
estroz Jun 19, 2019
a1b4420
CHANGELOG.md: add GOPATH change and --repo addition
estroz Jun 19, 2019
a731517
doc/sdk-cli-reference.md: add --repo to new and migrate, and add/remo…
estroz Jun 19, 2019
f2bc52a
Merge branch 'master' into remove-gopath
estroz Jun 19, 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
- Generating and serving info metrics about each custom resource. By default these metrics are exposed on port 8686. ([#1277](https://github.com/operator-framework/operator-sdk/pull/1277))
- Scaffold a `pkg/apis/<group>/group.go` package file to avoid `go/build` errors when running Kubernetes code generators. ([#1401](https://github.com/operator-framework/operator-sdk/pull/1401))
- Adds a new extra variable containing the unmodified CR spec for ansible based operators. [#1563](https://github.com/operator-framework/operator-sdk/pull/1563)
- New flag `--repo` for subcommands [`new`](https://github.com/operator-framework/operator-sdk/blob/master/doc/sdk-cli-reference.md#new) and [`migrate`](https://github.com/operator-framework/operator-sdk/blob/master/doc/sdk-cli-reference.md#migrate) specifies the repository path to be used in Go source files generated by the SDK. This flag can only be used with [Go modules](https://github.com/golang/go/wiki/Modules). ([#1475](https://github.com/operator-framework/operator-sdk/pull/1475))

### Changed

- Remove TypeMeta declaration from the implementation of the objects [#1462](https://github.com/operator-framework/operator-sdk/pull/1462/)
- Relaxed API version format check when parsing `pkg/apis` in code generators. API dir structures can now be of the format `pkg/apis/<group>/<anything>`, where `<anything>` was previously required to be in the Kubernetes version format, ex. `v1alpha1`. ([#1525](https://github.com/operator-framework/operator-sdk/pull/1525))
- The SDK and operator projects will work outside of `$GOPATH/src` when using [Go modules](https://github.com/golang/go/wiki/Modules). ([#1475](https://github.com/operator-framework/operator-sdk/pull/1475))

### Deprecated

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ Follow the steps in the [installation guide][install_guide] to learn how to inst

```sh
# Create an app-operator project that defines the App CR.
$ mkdir -p $GOPATH/src/github.com/example-inc/
$ mkdir -p $HOME/projects/example-inc/
# Create a new app-operator project
$ cd $GOPATH/src/github.com/example-inc/
$ cd $HOME/projects/example-inc/
$ export GO111MODULE=on
$ operator-sdk new app-operator
$ cd app-operator
Expand Down
2 changes: 1 addition & 1 deletion cmd/operator-sdk/add/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func apiRun(cmd *cobra.Command, args []string) error {
absProjectPath := projutil.MustGetwd()

cfg := &input.Config{
Repo: projutil.CheckAndGetProjectGoPkg(),
Repo: projutil.GetGoPkg(),
AbsProjectPath: absProjectPath,
}
s := &scaffold.Scaffold{}
Expand Down
2 changes: 1 addition & 1 deletion cmd/operator-sdk/add/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func controllerRun(cmd *cobra.Command, args []string) error {
}

cfg := &input.Config{
Repo: projutil.CheckAndGetProjectGoPkg(),
Repo: projutil.GetGoPkg(),
AbsProjectPath: projutil.MustGetwd(),
}
s := &scaffold.Scaffold{}
Expand Down
2 changes: 1 addition & 1 deletion cmd/operator-sdk/add/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func verifyCRDFlags() error {
return nil
}

// verifyCRDDeployPath checks if the path <project-name>/deploy sub-directory is exists, and that is rooted under $GOPATH
// verifyCRDDeployPath checks if the <project-name>/deploy directory exists.
func verifyCRDDeployPath() error {
wd, err := os.Getwd()
if err != nil {
Expand Down
7 changes: 3 additions & 4 deletions cmd/operator-sdk/build/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,16 @@ func buildFunc(cmd *cobra.Command, args []string) error {
goBuildEnv = append(goBuildEnv, "CGO_ENABLED=0")
}

trimPath := os.ExpandEnv("all=-trimpath=${GOPATH}")
goTrimFlags := []string{"-gcflags", trimPath, "-asmflags", trimPath}
absProjectPath := projutil.MustGetwd()
projectName := filepath.Base(absProjectPath)

// Don't need to build Go code if a non-Go Operator.
if projutil.IsOperatorGo() {
trimPath := fmt.Sprintf("all=-trimpath=%s", filepath.Dir(absProjectPath))
opts := projutil.GoCmdOptions{
BinName: filepath.Join(absProjectPath, scaffold.BuildBinDir, projectName),
PackagePath: path.Join(projutil.CheckAndGetProjectGoPkg(), filepath.ToSlash(scaffold.ManagerDir)),
Args: goTrimFlags,
PackagePath: path.Join(projutil.GetGoPkg(), filepath.ToSlash(scaffold.ManagerDir)),
Args: []string{"-gcflags", trimPath, "-asmflags", trimPath},
Env: goBuildEnv,
GoMod: projutil.IsDepManagerGoMod(),
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/operator-sdk/internal/genutil/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (
func K8sCodegen() error {
projutil.MustInProjectRoot()

repoPkg := projutil.CheckAndGetProjectGoPkg()
repoPkg := projutil.GetGoPkg()

gvMap, err := parseGroupVersions()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/operator-sdk/internal/genutil/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func OpenAPIGen() error {
projutil.MustInProjectRoot()

absProjectPath := projutil.MustGetwd()
repoPkg := projutil.CheckAndGetProjectGoPkg()
repoPkg := projutil.GetGoPkg()

gvMap, err := parseGroupVersions()
if err != nil {
Expand Down
84 changes: 80 additions & 4 deletions cmd/operator-sdk/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package main

import (
"fmt"
"os"

// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
Expand Down Expand Up @@ -48,14 +49,15 @@ func main() {
Short: "An SDK for building operators with ease",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if viper.GetBool(flags.VerboseOpt) {
err := projutil.SetGoVerbose()
if err != nil {
log.Errorf("Could not set GOFLAGS: (%v)", err)
return
if err := projutil.SetGoVerbose(); err != nil {
log.Fatalf("Could not set GOFLAGS: (%v)", err)
}
log.SetLevel(log.DebugLevel)
log.Debug("Debug logging is set")
}
if err := checkDepManagerForCmd(cmd); err != nil {
log.Fatal(err)
}
},
}

Expand All @@ -82,3 +84,77 @@ func main() {
os.Exit(1)
}
}

func checkDepManagerForCmd(cmd *cobra.Command) (err error) {
// Certain commands are able to be run anywhere or handle this check
// differently in their CLI code.
if skipCheckForCmd(cmd) {
return nil
}
// Do not perform this check if the project is non-Go, as they will not have
// a (Go) dep manager.
if !projutil.IsOperatorGo() {
return nil
}
// Do not perform a dep manager check if the working directory is not in
// the project root, as some sub-commands might not require project root.
// Individual subcommands will perform this check as needed.
if err := projutil.CheckProjectRoot(); err != nil {
return nil
}

dm, err := projutil.GetDepManagerType()
if err != nil {
return err
}
return checkDepManager(dm)
}

var commandsToSkip = map[string]struct{}{
"new": struct{}{}, // Handles this logic in cmd/operator-sdk/new
"migrate": struct{}{}, // Handles this logic in cmd/operator-sdk/migrate
"operator-sdk": struct{}{}, // Alias for "help"
"help": struct{}{},
"completion": struct{}{},
"version": struct{}{},
}

func skipCheckForCmd(cmd *cobra.Command) (skip bool) {
if _, ok := commandsToSkip[cmd.Name()]; ok {
return true
}
cmd.VisitParents(func(pc *cobra.Command) {
if _, ok := commandsToSkip[pc.Name()]; ok {
// The bare "operator-sdk" command will be checked above.
if pc.Name() != "operator-sdk" {
skip = true
}
}
})
return skip
}

func checkDepManager(dm projutil.DepManagerType) error {
switch dm {
case projutil.DepManagerGoMod:
goModOn, err := projutil.GoModOn()
if err != nil {
return err
}
if !goModOn {
return fmt.Errorf(`dependency manager "modules" requires working directory to be in $GOPATH/src` +
` and GO111MODULE=on, or outside of $GOPATH/src and GO111MODULE="on", "auto", or unset`)
}
case projutil.DepManagerDep:
inGopathSrc, err := projutil.WdInGoPathSrc()
if err != nil {
return err
}
if !inGopathSrc {
return fmt.Errorf(`dependency manager "dep" requires working directory to be in $GOPATH/src`)
}
default:
return projutil.ErrInvalidDepManager(dm)
}
return nil
}
37 changes: 19 additions & 18 deletions cmd/operator-sdk/migrate/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
var (
depManager string
headerFile string
repo string
)

// NewCmd returns a command that will add source code to an existing non-go operator
Expand All @@ -46,6 +47,7 @@ func NewCmd() *cobra.Command {

newCmd.Flags().StringVar(&depManager, "dep-manager", "modules", `Dependency manager the new project will use (choices: "dep", "modules")`)
newCmd.Flags().StringVar(&headerFile, "header-file", "", "Path to file containing headers for generated Go files. Copied to hack/boilerplate.go.txt")
newCmd.Flags().StringVar(&repo, "repo", "", "Project repository path. Used as the project's Go import path. This must be set if outside of $GOPATH/src with Go modules, and cannot be set if --dep-manager=dep")

return newCmd
}
Expand All @@ -55,7 +57,13 @@ func NewCmd() *cobra.Command {
func migrateRun(cmd *cobra.Command, args []string) error {
projutil.MustInProjectRoot()

_ = projutil.CheckAndGetProjectGoPkg()
if err := verifyFlags(); err != nil {
return err
}

if repo == "" {
repo = projutil.GetGoPkg()
}

opType := projutil.GetOperatorType()
switch opType {
Expand All @@ -67,12 +75,20 @@ func migrateRun(cmd *cobra.Command, args []string) error {
return fmt.Errorf("operator of type %s cannot be migrated", opType)
}

func verifyFlags() error {
err := projutil.CheckDepManagerWithRepo(projutil.DepManagerType(depManager), repo)
if err != nil {
return err
}
return nil
}

// migrateAnsible runs the migration process for an ansible-based operator
func migrateAnsible() error {
wd := projutil.MustGetwd()

cfg := &input.Config{
Repo: projutil.CheckAndGetProjectGoPkg(),
Repo: repo,
AbsProjectPath: wd,
ProjectName: filepath.Base(wd),
}
Expand Down Expand Up @@ -126,7 +142,7 @@ func migrateHelm() error {
wd := projutil.MustGetwd()

cfg := &input.Config{
Repo: projutil.CheckAndGetProjectGoPkg(),
Repo: repo,
AbsProjectPath: wd,
ProjectName: filepath.Base(wd),
}
Expand Down Expand Up @@ -180,9 +196,6 @@ func scaffoldHelmDepManager(s *scaffold.Scaffold, cfg *input.Config) error {
case projutil.DepManagerDep:
files = append(files, &helm.GopkgToml{})
case projutil.DepManagerGoMod:
if err := goModCheck(); err != nil {
return err
}
files = append(files, &helm.GoMod{}, &scaffold.Tools{})
default:
return projutil.ErrInvalidDepManager(depManager)
Expand All @@ -196,21 +209,9 @@ func scaffoldAnsibleDepManager(s *scaffold.Scaffold, cfg *input.Config) error {
case projutil.DepManagerDep:
files = append(files, &ansible.GopkgToml{})
case projutil.DepManagerGoMod:
if err := goModCheck(); err != nil {
return err
}
files = append(files, &ansible.GoMod{}, &scaffold.Tools{})
default:
return projutil.ErrInvalidDepManager(depManager)
}
return s.Execute(cfg, files...)
}

func goModCheck() error {
goModOn, err := projutil.GoModOn()
if err == nil && !goModOn {
log.Fatal(`Dependency manager "modules" has been selected but go modules are not active. ` +
`Activate modules then run "operator-sdk migrate".`)
}
return err
}
Loading