Skip to content

Commit

Permalink
cmd/operator-sdk,internal: remove legacy CLI, clean up utils (#3533)
Browse files Browse the repository at this point in the history
internal: clean up utils
  • Loading branch information
Eric Stroczynski authored Jul 27, 2020
1 parent df55ef4 commit 62a8f29
Show file tree
Hide file tree
Showing 44 changed files with 91 additions and 2,809 deletions.
48 changes: 3 additions & 45 deletions cmd/operator-sdk/build/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,9 @@ package build

import (
"fmt"
"os"
"os/exec"
"path"
"path/filepath"
"strings"

"github.com/operator-framework/operator-sdk/internal/scaffold"
kbutil "github.com/operator-framework/operator-sdk/internal/util/kubebuilder"
"github.com/operator-framework/operator-sdk/internal/util/projutil"

"github.com/google/shlex"
Expand Down Expand Up @@ -65,7 +60,7 @@ For example:
"Tool to build OCI images. One of: [docker, podman, buildah]")

// todo: remove when the legacy layout is no longer supported
if !kbutil.HasProjectFile() {
if !projutil.HasProjectFile() {
buildCmd.Flags().StringVar(&goBuildArgs, "go-build-args", "",
"Extra Go build arguments as one string such as \"-ldflags -X=main.xyz=abc\"")
}
Expand Down Expand Up @@ -104,9 +99,8 @@ func buildFunc(cmd *cobra.Command, args []string) error {
}

image := args[0]
projutil.MustInProjectRoot()

if kbutil.HasProjectFile() {
if projutil.HasProjectFile() {
if err := doImageBuild("Dockerfile", image); err != nil {
log.Fatalf("Failed to build image %s: %v", image, err)
}
Expand All @@ -115,48 +109,12 @@ func buildFunc(cmd *cobra.Command, args []string) error {

// todo: remove when the legacy layout is no longer supported
// note that the above if will no longer be required as well.
if err := doLegacyBuild(image); err != nil {
if err := doImageBuild(filepath.Join("build", "Dockerfile"), image); err != nil {
log.Fatalf("Failed to build image %s: %v", image, err)
}
return nil
}

// todo: remove when the legacy layout is no longer supported
// Deprecated: Used just for the legacy layout
// --
// doLegacyBuild will build projects with the legacy layout.
func doLegacyBuild(image string) error {
goBuildEnv := append(os.Environ(), "GOOS=linux")
// If CGO_ENABLED is not set, set it to '0'.
if _, ok := os.LookupEnv("CGO_ENABLED"); !ok {
goBuildEnv = append(goBuildEnv, "CGO_ENABLED=0")
}
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))
args := []string{"-gcflags", trimPath, "-asmflags", trimPath}

if goBuildArgs != "" {
splitArgs := strings.Fields(goBuildArgs)
args = append(args, splitArgs...)
}

opts := projutil.GoCmdOptions{
BinName: filepath.Join(absProjectPath, scaffold.BuildBinDir, projectName),
PackagePath: path.Join(projutil.GetGoPkg(), filepath.ToSlash(scaffold.ManagerDir)),
Args: args,
Env: goBuildEnv,
}
if err := projutil.GoBuild(opts); err != nil {
log.Fatalf("Failed to build operator binary: %v", err)
}
}
return doImageBuild("build/Dockerfile", image)
}

// doImageBuild will execute the build command for the given Dockerfile path and image
func doImageBuild(dockerFilePath, image string) error {
log.Infof("Building OCI image %s", image)
Expand Down
2 changes: 1 addition & 1 deletion cmd/operator-sdk/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ import (
)

var commands = []*cobra.Command{
scorecard.NewCmd(),
build.NewCmd(),
bundle.NewCmd(),
cleanup.NewCmd(),
completion.NewCmd(),
generate.NewCmd(),
olm.NewCmd(),
run.NewCmd(),
scorecard.NewCmd(),
version.NewCmd(),
}

Expand Down
113 changes: 0 additions & 113 deletions cmd/operator-sdk/cli/legacy.go

This file was deleted.

4 changes: 2 additions & 2 deletions cmd/operator-sdk/generate/bundle/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/pflag"

kbutil "github.com/operator-framework/operator-sdk/internal/util/kubebuilder"
"github.com/operator-framework/operator-sdk/internal/util/projutil"
)

//nolint:maligned
Expand Down Expand Up @@ -68,7 +68,7 @@ func NewCmd() *cobra.Command {
c.metadata = true
}

cfg, err := kbutil.ReadConfig()
cfg, err := projutil.ReadConfig()
if err != nil {
return fmt.Errorf("error reading configuration: %v", err)
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/operator-sdk/generate/kustomize/manifests.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (

gencsv "github.com/operator-framework/operator-sdk/internal/generate/clusterserviceversion"
"github.com/operator-framework/operator-sdk/internal/plugins/util/kustomize"
kbutil "github.com/operator-framework/operator-sdk/internal/util/kubebuilder"
"github.com/operator-framework/operator-sdk/internal/util/projutil"
)

Expand Down Expand Up @@ -94,7 +93,7 @@ func newManifestsCmd() *cobra.Command {
}
}

cfg, err := kbutil.ReadConfig()
cfg, err := projutil.ReadConfig()
if err != nil {
return fmt.Errorf("error reading configuration: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/operator-sdk/generate/packagemanifests/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/pflag"

kbutil "github.com/operator-framework/operator-sdk/internal/util/kubebuilder"
"github.com/operator-framework/operator-sdk/internal/util/projutil"
)

//nolint:maligned
Expand Down Expand Up @@ -59,7 +59,7 @@ func NewCmd() *cobra.Command {
return fmt.Errorf("command %s doesn't accept any arguments", cmd.CommandPath())
}

cfg, err := kbutil.ReadConfig()
cfg, err := projutil.ReadConfig()
if err != nil {
log.Fatal(fmt.Errorf("error reading configuration: %v", err))
}
Expand Down
37 changes: 2 additions & 35 deletions cmd/operator-sdk/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,49 +15,16 @@
package main

import (

// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
// to ensure that `exec-entrypoint` and `run` can make use of them.
_ "k8s.io/client-go/plugin/pkg/client/auth"

"github.com/operator-framework/operator-sdk/cmd/operator-sdk/cli"
kbutil "github.com/operator-framework/operator-sdk/internal/util/kubebuilder"
"github.com/operator-framework/operator-sdk/internal/util/projutil"

log "github.com/sirupsen/logrus"

"github.com/operator-framework/operator-sdk/cmd/operator-sdk/cli"
)

func main() {
// Use the new KB CLI when running inside a Kubebuilder project with an existing PROJECT file.
if kbutil.HasProjectFile() {
if err := cli.Run(); err != nil {
log.Fatal(err)
}
return
}

// Use the legacy CLI if inside of a Go/Helm/Ansible legacy project
operatorType := projutil.GetOperatorType()
switch operatorType {
case projutil.OperatorTypeGo, projutil.OperatorTypeHelm, projutil.OperatorTypeAnsible:
// Deprecation warning for Go projects
// TODO/Discuss: UX wise, is displaying this notice on every command that runs
// in the legacy Go projects too loud.
if operatorType == projutil.OperatorTypeGo {
depMsg := "Operator SDK has a new CLI and project layout that is aligned with Kubebuilder.\n" +
"See `operator-sdk init -h` and the following doc on how to scaffold a new project:\n" +
"https://sdk.operatorframework.io/docs/golang/quickstart/\n" +
"To migrate existing projects to the new layout see:\n" +
"https://sdk.operatorframework.io/docs/golang/project_migration_guide/\n"
projutil.PrintDeprecationWarning(depMsg)
}
if err := cli.RunLegacy(); err != nil {
log.Fatal(err)
}
return
}

// Run the KB CLI when not running in either legacy or new projects
if err := cli.Run(); err != nil {
log.Fatal(err)
}
Expand Down
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@ require (
github.com/kr/text v0.1.0
github.com/markbates/inflect v1.0.4
github.com/mattn/go-isatty v0.0.12
github.com/mitchellh/go-homedir v1.1.0
github.com/onsi/ginkgo v1.12.1
github.com/onsi/gomega v1.10.1
github.com/operator-framework/api v0.3.8
github.com/operator-framework/operator-lib v0.0.0-20200724203809-f6728cc91ac6
github.com/operator-framework/operator-registry v1.12.6-0.20200611222234-275301b779f8
github.com/prometheus/client_golang v1.5.1
github.com/rogpeppe/go-internal v1.5.0
github.com/sergi/go-diff v1.0.0
github.com/sirupsen/logrus v1.5.0
github.com/spf13/afero v1.2.2
Expand Down
3 changes: 1 addition & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -658,9 +658,8 @@ github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6So
github.com/rogpeppe/go-internal v1.1.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.3.2/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rogpeppe/go-internal v1.4.0 h1:LUa41nrWTQNGhzdsZ5lTnkwbNjj6rXTdazA1cSdjkOY=
github.com/rogpeppe/go-internal v1.4.0/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rogpeppe/go-internal v1.5.0 h1:Usqs0/lDK/NqTkvrmKSwA/3XkZAs7ZAW/eLeQ2MVBTw=
github.com/rogpeppe/go-internal v1.5.0/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rubenv/sql-migrate v0.0.0-20200212082348-64f95ea68aa3 h1:xkBtI5JktwbW/vf4vopBbhYsRFTGfQWHYXzC0/qYwxI=
github.com/rubenv/sql-migrate v0.0.0-20200212082348-64f95ea68aa3/go.mod h1:rtQlpHw+eR6UrqaS3kX1VYeaCxzCVdimDS7g5Ln4pPc=
github.com/russross/blackfriday v1.5.2 h1:HyvC0ARfnZBqnXwABFeSZHpKvJHJJfPz81GNueLj0oo=
Expand Down
Loading

0 comments on commit 62a8f29

Please sign in to comment.