Skip to content

Commit

Permalink
*: add global --verbose flag to operator-sdk command (#1361)
Browse files Browse the repository at this point in the history
  • Loading branch information
briantopping authored and hasbro17 committed May 2, 2019
1 parent 690cc27 commit 3490a56
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- New option for [`operator-sdk build --image-builder`](https://github.com/operator-framework/operator-sdk/blob/master/doc/sdk-cli-reference.md#build), which can be used to specify which image builder to use. Adds support for [buildah](https://github.com/containers/buildah/). ([#1311](https://github.com/operator-framework/operator-sdk/pull/1311))
- Manager is now configured with a new `DynamicRESTMapper`, which accounts for the fact that the default `RESTMapper`, which only checks resource types at startup, can't handle the case of first creating a CRD and then an instance of that CRD. ([#1329](https://github.com/operator-framework/operator-sdk/pull/1329))
- Unify CLI debug logging under a global `--verbose` flag ([#1361](https://github.com/operator-framework/operator-sdk/pull/1361))

### Changed

Expand Down
4 changes: 3 additions & 1 deletion cmd/operator-sdk/internal/genutil/genutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ import (
"path/filepath"
"strings"

flags "github.com/operator-framework/operator-sdk/internal/pkg/flags"
"github.com/operator-framework/operator-sdk/internal/pkg/scaffold"
"github.com/operator-framework/operator-sdk/internal/util/projutil"

log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)

func buildCodegenBinaries(genDirs []string, binDir, codegenSrcDir string) error {
Expand All @@ -47,7 +49,7 @@ func runGoBuildCodegen(binDir, repoDir, genDir string) error {
}

// Only print binary build info if verbosity is explicitly set.
if projutil.IsGoVerbose() {
if viper.GetBool(flags.VerboseOpt) {
return projutil.ExecCmd(cmd)
}
cmd.Stdout = ioutil.Discard
Expand Down
21 changes: 21 additions & 0 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 (
"github.com/operator-framework/operator-sdk/internal/util/projutil"
"os"

// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
Expand All @@ -32,8 +33,12 @@ import (
"github.com/operator-framework/operator-sdk/cmd/operator-sdk/test"
"github.com/operator-framework/operator-sdk/cmd/operator-sdk/up"
"github.com/operator-framework/operator-sdk/cmd/operator-sdk/version"
flags "github.com/operator-framework/operator-sdk/internal/pkg/flags"
osdkversion "github.com/operator-framework/operator-sdk/version"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
_ "k8s.io/client-go/plugin/pkg/client/auth"
)

Expand All @@ -42,6 +47,17 @@ func main() {
Use: "operator-sdk",
Short: "An SDK for building operators with ease",
Version: osdkversion.Version,
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
}
log.SetLevel(log.DebugLevel)
log.Debug("Debug logging is set")
}
},
}

root.AddCommand(new.NewCmd())
Expand All @@ -58,6 +74,11 @@ func main() {
root.AddCommand(olmcatalog.NewCmd())
root.AddCommand(version.NewCmd())

root.PersistentFlags().Bool(flags.VerboseOpt, false, "Enable verbose logging")
if err := viper.BindPFlags(root.PersistentFlags()); err != nil {
log.Fatalf("Failed to bind root flags: %v", err)
}

if err := root.Execute(); err != nil {
os.Exit(1)
}
Expand Down
1 change: 0 additions & 1 deletion cmd/operator-sdk/scorecard/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ func NewCmd() *cobra.Command {
scorecardCmd.Flags().String(scorecard.ProxyPullPolicyOpt, "Always", "Pull policy for scorecard proxy image")
scorecardCmd.Flags().String(scorecard.CRDsDirOpt, scaffold.CRDsDir, "Directory containing CRDs (all CRD manifest filenames must have the suffix 'crd.yaml')")
scorecardCmd.Flags().StringP(scorecard.OutputFormatOpt, "o", "human-readable", "Output format for results. Valid values: human-readable, json")
scorecardCmd.Flags().Bool(scorecard.VerboseOpt, false, "Enable verbose logging")

if err := viper.BindPFlags(scorecardCmd.Flags()); err != nil {
log.Fatalf("Failed to bind scorecard flags to viper: %v", err)
Expand Down
4 changes: 4 additions & 0 deletions doc/sdk-cli-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ Usage:
operator-sdk [command]
```

### Global Flags

* `--verbose` - enable debug logging

## build

### Args
Expand Down
20 changes: 20 additions & 0 deletions internal/pkg/flags/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2019 The Operator-SDK Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package flags

// global command-line flags
const (
VerboseOpt = "verbose"
)
6 changes: 1 addition & 5 deletions internal/pkg/scorecard/scorecard.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
"github.com/ghodss/yaml"
olmapiv1alpha1 "github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators/v1alpha1"
olminstall "github.com/operator-framework/operator-lifecycle-manager/pkg/controller/install"
logrus "github.com/sirupsen/logrus"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
v1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -65,7 +65,6 @@ const (
ProxyImageOpt = "proxy-image"
ProxyPullPolicyOpt = "proxy-pull-policy"
CRDsDirOpt = "crds-dir"
VerboseOpt = "verbose"
OutputFormatOpt = "output"
)

Expand Down Expand Up @@ -322,9 +321,6 @@ func ScorecardTests(cmd *cobra.Command, args []string) error {
return err
}
cmd.SilenceUsage = true
if viper.GetBool(VerboseOpt) {
log.SetLevel(logrus.DebugLevel)
}
suites, err := runTests()
if err != nil {
return err
Expand Down
2 changes: 2 additions & 0 deletions internal/util/projutil/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ import (
"strings"

homedir "github.com/mitchellh/go-homedir"
log "github.com/sirupsen/logrus"
)

func ExecCmd(cmd *exec.Cmd) error {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
log.Debugf("Running %#v", cmd.Args)
if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to exec %#v: %v", cmd.Args, err)
}
Expand Down
14 changes: 10 additions & 4 deletions internal/util/projutil/project_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,15 @@ func MustSetGopath(currentGopath string) string {

var flagRe = regexp.MustCompile("(.* )?-v(.* )?")

// IsGoVerbose returns true if GOFLAGS contains "-v". This function is useful
// when deciding whether to make "go" command output verbose.
func IsGoVerbose() bool {
// SetGoVerbose sets GOFLAGS="${GOFLAGS} -v" if GOFLAGS does not
// already contain "-v" to make "go" command output verbose.
func SetGoVerbose() error {
gf, ok := os.LookupEnv(GoFlagsEnv)
return ok && len(gf) != 0 && flagRe.MatchString(gf)
if !ok || len(gf) == 0 {
return os.Setenv(GoFlagsEnv, "-v")
}
if !flagRe.MatchString(gf) {
return os.Setenv(GoFlagsEnv, gf+" -v")
}
return nil
}

0 comments on commit 3490a56

Please sign in to comment.