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 20, 2019
1 parent 1018749 commit da304af
Show file tree
Hide file tree
Showing 30 changed files with 166 additions and 599 deletions.
8 changes: 1 addition & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,12 @@ 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
# YAML anchors which we use in our build jobs.
x_base_steps:
# before_install for jobs that require dep
- &dep_before_install
before_install:
- travis_retry make tidy

# before_install for jobs that require go builds and do not run for doc-only changes
- &go_before_install
before_install:
Expand Down Expand Up @@ -151,7 +146,6 @@ jobs:
name: Unit, Sanity, and Markdown Tests
# Currently, prow/api-ci tests all PRs that target master; use travis for post merge testing and non-master PRs
if: type != pull_request OR branch != master
<<: *dep_before_install
script: make test/sanity test/unit test/markdown
after_success: echo 'Tests Passed'
after_failure: echo 'Failure in unit, sanity, or markdown test'
Expand Down
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,12 @@ 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+`
- [kubectl][kubectl_tool] version v1.11.3+.
- Access to a Kubernetes v1.11.3+ cluster.
- Optional: [dep][dep_tool] version v0.5.0+.
- Optional: [delve](https://github.com/go-delve/delve/tree/master/Documentation/installation) version 1.2.0+ (for `up local --enable-delve`).

## Quick Start
Expand Down Expand Up @@ -181,7 +180,6 @@ Operator SDK is under Apache 2.0 license. See the [LICENSE][license_file] file f
[contrib]: ./CONTRIBUTING.MD
[bug_guide]:./doc/dev/reporting_bugs.md
[license_file]:./LICENSE
[dep_tool]:https://golang.github.io/dep/docs/installation.html
[git_tool]:https://git-scm.com/downloads
[go_tool]:https://golang.org/dl/
[mercurial_tool]:https://www.mercurial-scm.org/downloads
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
45 changes: 14 additions & 31 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,29 +87,25 @@ 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) {
return nil
}
// Do not perform this check if the project is non-Go, as they will not have
// a (Go) dep manager.
// Do not perform this check if the project is non-Go, as they will be
// using go modules.
if !projutil.IsOperatorGo() {
return nil
}
// Do not perform a dep manager check if the working directory is not in
// Do not perform a go modules 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)
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(`using go 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
}
45 changes: 6 additions & 39 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,9 +43,8 @@ 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")
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 (e.g. github.com/example-inc/my-operator)")

return newCmd
}
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...)
}
109 changes: 39 additions & 70 deletions cmd/operator-sdk/new/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,10 @@ 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-operator)")
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 (e.g. github.com/example-inc/my-operator)")
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")
newCmd.Flags().BoolVar(&makeVendor, "vendor", false, "Use a vendor directory for dependencies. This flag only applies when --dep-manager=modules (the default)")
newCmd.Flags().BoolVar(&makeVendor, "vendor", false, "Use a vendor directory for dependencies")
newCmd.Flags().BoolVar(&skipValidation, "skip-validation", false, "Do not validate the resulting project's structure and dependencies. (Only used for --type go)")
newCmd.Flags().BoolVar(&generatePlaybook, "generate-playbook", false, "Generate a playbook skeleton. (Only used for --type ansible)")

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(`using go 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 da304af

Please sign in to comment.