Skip to content

Commit

Permalink
initial jump to go 1.13
Browse files Browse the repository at this point in the history
  • Loading branch information
joelanford committed Oct 15, 2019
1 parent 4751b95 commit 8e0374c
Show file tree
Hide file tree
Showing 25 changed files with 142 additions and 569 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ cache:
- $HOME/.cache/go-build

go:
- 1.12.x
- 1.13.x

# The `x_base_steps` top-level key is unknown to travis,
# so we can use it to create a bunch of common build step
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ The following workflow is for a new **Helm** operator:
## Prerequisites

- [git][git_tool]
- [go][go_tool] version v1.12+.
- [go][go_tool] version v1.13+.
- [mercurial][mercurial_tool] version 3.9+
- [docker][docker_tool] version 17.03+.
- Alternatively [podman][podman_tool] `v1.2.0+` or [buildah][buildah_tool] `v1.7+`
Expand Down
2 changes: 1 addition & 1 deletion ci/dockerfiles/builder.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM openshift/origin-release:golang-1.12
FROM openshift/origin-release:golang-1.13

WORKDIR /go/src/github.com/operator-framework/operator-sdk
ENV GOPATH=/go PATH=/go/src/github.com/operator-framework/operator-sdk/build:$PATH GOPROXY=https://proxy.golang.org/ GO111MODULE=on
Expand Down
1 change: 0 additions & 1 deletion cmd/operator-sdk/build/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ func buildFunc(cmd *cobra.Command, args []string) error {
PackagePath: path.Join(projutil.GetGoPkg(), filepath.ToSlash(scaffold.ManagerDir)),
Args: args,
Env: goBuildEnv,
GoMod: projutil.IsDepManagerGoMod(),
}
if err := projutil.GoBuild(opts); err != nil {
return fmt.Errorf("failed to build operator binary: (%v)", err)
Expand Down
39 changes: 11 additions & 28 deletions cmd/operator-sdk/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func main() {
log.SetLevel(log.DebugLevel)
log.Debug("Debug logging is set")
}
if err := checkDepManagerForCmd(cmd); err != nil {
if err := checkGoModulesForCmd(cmd); err != nil {
log.Fatal(err)
}
},
Expand Down Expand Up @@ -87,7 +87,7 @@ func main() {
}
}

func checkDepManagerForCmd(cmd *cobra.Command) (err error) {
func checkGoModulesForCmd(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) {
Expand All @@ -105,11 +105,7 @@ func checkDepManagerForCmd(cmd *cobra.Command) (err error) {
return nil
}

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

var commandsToSkip = map[string]struct{}{
Expand Down Expand Up @@ -137,27 +133,14 @@ func skipCheckForCmd(cmd *cobra.Command) (skip bool) {
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. More info: https://github.com/operator-framework/operator-sdk/blob/master/doc/user-guide.md#go-modules`)
}
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)
func checkGoModules() error {
goModOn, err := projutil.GoModOn()
if err != nil {
return err
}
if !goModOn {
return fmt.Errorf(`dependency manager "modules" requires GO111MODULE="on", "auto", or unset.` +
` More info: https://github.com/operator-framework/operator-sdk/blob/master/doc/user-guide.md#go-modules`)
}
return nil
}
43 changes: 5 additions & 38 deletions cmd/operator-sdk/migrate/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,11 @@ import (
"github.com/operator-framework/operator-sdk/internal/pkg/scaffold/input"
"github.com/operator-framework/operator-sdk/internal/util/projutil"

"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

var (
depManager string
headerFile string
repo string
)
Expand All @@ -45,7 +43,6 @@ func NewCmd() *cobra.Command {
RunE: migrateRun,
}

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")

Expand Down Expand Up @@ -76,7 +73,7 @@ func migrateRun(cmd *cobra.Command, args []string) error {
}

func verifyFlags() error {
err := projutil.CheckDepManagerWithRepo(projutil.DepManagerType(depManager), repo)
err := projutil.CheckRepo(repo)
if err != nil {
return err
}
Expand Down Expand Up @@ -119,11 +116,9 @@ func migrateAnsible() error {
s.BoilerplatePath = headerFile
}

if err := scaffoldAnsibleDepManager(s, cfg); err != nil {
return errors.Wrap(err, "migrate Ansible dependency manager file scaffold failed")
}

err = s.Execute(cfg,
&ansible.GoMod{},
&scaffold.Tools{},
&ansible.Main{},
&dockerfile,
&ansible.Entrypoint{},
Expand Down Expand Up @@ -160,11 +155,9 @@ func migrateHelm() error {
s.BoilerplatePath = headerFile
}

if err := scaffoldHelmDepManager(s, cfg); err != nil {
return errors.Wrap(err, "migrate Helm dependency manager file scaffold failed")
}

err := s.Execute(cfg,
&helm.GoMod{},
&scaffold.Tools{},
&helm.Main{},
&helm.DockerfileHybrid{
Watches: true,
Expand All @@ -189,29 +182,3 @@ func renameDockerfile() error {
log.Infof("Renamed Dockerfile to %s and replaced with newer version. Compare the new Dockerfile to your old one and manually migrate any customizations", newDockerfilePath)
return nil
}

func scaffoldHelmDepManager(s *scaffold.Scaffold, cfg *input.Config) error {
var files []input.File
switch m := projutil.DepManagerType(depManager); m {
case projutil.DepManagerDep:
files = append(files, &helm.GopkgToml{})
case projutil.DepManagerGoMod:
files = append(files, &helm.GoMod{}, &scaffold.Tools{})
default:
return projutil.ErrInvalidDepManager(depManager)
}
return s.Execute(cfg, files...)
}

func scaffoldAnsibleDepManager(s *scaffold.Scaffold, cfg *input.Config) error {
var files []input.File
switch m := projutil.DepManagerType(depManager); m {
case projutil.DepManagerDep:
files = append(files, &ansible.GopkgToml{})
case projutil.DepManagerGoMod:
files = append(files, &ansible.GoMod{}, &scaffold.Tools{})
default:
return projutil.ErrInvalidDepManager(depManager)
}
return s.Execute(cfg, files...)
}
105 changes: 37 additions & 68 deletions cmd/operator-sdk/new/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ generates a skeletal app-operator application in $HOME/projects/example.com/app-
newCmd.Flags().StringVar(&apiVersion, "api-version", "", "Kubernetes apiVersion and has a format of $GROUP_NAME/$VERSION (e.g app.example.com/v1alpha1) - used with \"ansible\" or \"helm\" types")
newCmd.Flags().StringVar(&kind, "kind", "", "Kubernetes CustomResourceDefintion kind. (e.g AppService) - used with \"ansible\" or \"helm\" types")
newCmd.Flags().StringVar(&operatorType, "type", "go", "Type of operator to initialize (choices: \"go\", \"ansible\" or \"helm\")")
newCmd.Flags().StringVar(&depManager, "dep-manager", "modules", `Dependency manager the new project will use (choices: "dep", "modules")`)
newCmd.Flags().StringVar(&repo, "repo", "", "Project repository path for Go operators. 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 (e.g. github.com/example-inc/my-opertor)")
newCmd.Flags().BoolVar(&gitInit, "git-init", false, "Initialize the project directory as a git repository (default false)")
newCmd.Flags().StringVar(&headerFile, "header-file", "", "Path to file containing headers for generated Go files. Copied to hack/boilerplate.go.txt")
Expand All @@ -76,7 +75,6 @@ var (
kind string
operatorType string
projectName string
depManager string
headerFile string
repo string
gitInit bool
Expand Down Expand Up @@ -180,26 +178,16 @@ func doGoScaffold() error {
s.BoilerplatePath = headerFile
}

var err error
switch m := projutil.DepManagerType(depManager); m {
case projutil.DepManagerDep:
err = s.Execute(cfg, &scaffold.GopkgToml{})
case projutil.DepManagerGoMod:
if goModOn, merr := projutil.GoModOn(); merr != nil {
return merr
} else if !goModOn {
return errors.New(`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. More info: https://github.com/operator-framework/operator-sdk/blob/master/doc/user-guide.md#go-modules`)
}
err = s.Execute(cfg, &scaffold.GoMod{}, &scaffold.Tools{})
default:
err = projutil.ErrNoDepManager
}
if err != nil {
return fmt.Errorf("dependency manager file scaffold failed: (%v)", err)
if goModOn, merr := projutil.GoModOn(); merr != nil {
return merr
} else if !goModOn {
return errors.New(`dependency manager "modules" requires GO111MODULE="on", "auto", or unset.` +
` More info: https://github.com/operator-framework/operator-sdk/blob/master/doc/user-guide.md#go-modules`)
}

err = s.Execute(cfg,
err := s.Execute(cfg,
&scaffold.GoMod{},
&scaffold.Tools{},
&scaffold.Cmd{},
&scaffold.Dockerfile{},
&scaffold.Entrypoint{},
Expand Down Expand Up @@ -384,12 +372,7 @@ func verifyFlags() error {
return fmt.Errorf("operators of type Go do not use --api-version or --kind")
}

dm := projutil.DepManagerType(depManager)
if !makeVendor && dm == projutil.DepManagerDep {
log.Warnf("--dep-manager=dep requires a vendor directory; ignoring --vendor=false")
}
err := projutil.CheckDepManagerWithRepo(dm, repo)
if err != nil {
if err := projutil.CheckRepo(repo); err != nil {
return err
}
}
Expand Down Expand Up @@ -424,30 +407,20 @@ func execProjCmd(cmd string, args ...string) error {
}

func getDeps() error {
switch m := projutil.DepManagerType(depManager); m {
case projutil.DepManagerDep:
log.Info("Running dep ensure")
if err := execProjCmd("dep", "ensure", "-v"); err != nil {
return err
}
case projutil.DepManagerGoMod:
// Only when a user requests a vendor directory be created should
// "go mod vendor" be run during project initialization.
if makeVendor {
log.Info("Running go mod vendor")
opts := projutil.GoCmdOptions{
Args: []string{"-v"},
Dir: filepath.Join(projutil.MustGetwd(), projectName),
}
if err := projutil.GoCmd("mod vendor", opts); err != nil {
return err
}
} else {
// Avoid done message.
return nil
}
default:
return projutil.ErrInvalidDepManager(depManager)

// Only when a user requests a vendor directory be created should
// "go mod vendor" be run during project initialization.
if !makeVendor {
return nil
}

log.Info("Running go mod vendor")
opts := projutil.GoCmdOptions{
Args: []string{"-v"},
Dir: filepath.Join(projutil.MustGetwd(), projectName),
}
if err := projutil.GoCmd("mod vendor", opts); err != nil {
return err
}
log.Info("Done getting dependencies")
return nil
Expand All @@ -463,24 +436,20 @@ func initGit() error {
}

func validateProject() error {
switch projutil.DepManagerType(depManager) {
case projutil.DepManagerGoMod:
log.Info("Validating project")
// Run "go build ./..." to make sure all packages can be built
// correctly. From "go help build":
//
// When compiling multiple packages or a single non-main package,
// build compiles the packages but discards the resulting object,
// serving only as a check that the packages can be built.
opts := projutil.GoCmdOptions{
PackagePath: "./...",
Dir: filepath.Join(projutil.MustGetwd(), projectName),
}
if err := projutil.GoBuild(opts); err != nil {
return err
}
log.Info("Project validation successful.")
log.Info("Validating project")
// Run "go build ./..." to make sure all packages can be built
// correctly. From "go help build":
//
// When compiling multiple packages or a single non-main package,
// build compiles the packages but discards the resulting object,
// serving only as a check that the packages can be built.
opts := projutil.GoCmdOptions{
PackagePath: "./...",
Dir: filepath.Join(projutil.MustGetwd(), projectName),
}
if err := projutil.GoBuild(opts); err != nil {
return err
}

log.Info("Project validation successful.")
return nil
}
Loading

0 comments on commit 8e0374c

Please sign in to comment.