Skip to content

Commit

Permalink
implemented glog wrapper for use in delegated commands
Browse files Browse the repository at this point in the history
Signed-off-by: Steve Kuznetsov <[email protected]>
  • Loading branch information
stevekuznetsov committed Apr 20, 2016
1 parent 433ceb1 commit 9d0dd6d
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 13 deletions.
7 changes: 3 additions & 4 deletions pkg/cmd/server/admin/create_keypair.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"os"
"path/filepath"

"github.com/golang/glog"
"github.com/spf13/cobra"

kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
Expand Down Expand Up @@ -86,15 +85,15 @@ func (o CreateKeyPairOptions) Validate(args []string) error {
}

func (o CreateKeyPairOptions) CreateKeyPair() error {
glog.V(4).Infof("Creating a key pair with: %#v", o)
fmt.Fprintf(o.Output, "Creating a key pair with: %#v", o)

if !o.Overwrite {
if _, err := os.Stat(o.PrivateKeyFile); err == nil {
glog.V(3).Infof("Keeping existing private key file %s\n", o.PrivateKeyFile)
fmt.Fprintf(o.Output, "Keeping existing private key file %s\n", o.PrivateKeyFile)
return nil
}
if _, err := os.Stat(o.PublicKeyFile); err == nil {
glog.V(3).Infof("Keeping existing public key file %s\n", o.PublicKeyFile)
fmt.Fprintf(o.Output, "Keeping existing public key file %s\n", o.PublicKeyFile)
return nil
}
}
Expand Down
22 changes: 15 additions & 7 deletions pkg/cmd/server/start/start_allinone.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type AllInOneOptions struct {
NodeConfigFile string
PrintIP bool
ServiceNetworkCIDR string
Output io.Writer
}

const allInOneLong = `
Expand All @@ -59,7 +60,14 @@ You may also pass --kubeconfig=<path> to connect to an external Kubernetes clust

// NewCommandStartAllInOne provides a CLI handler for 'start' command
func NewCommandStartAllInOne(basename string, out io.Writer) (*cobra.Command, *AllInOneOptions) {
options := &AllInOneOptions{MasterOptions: &MasterOptions{Output: out}}
// delegated commands should use glog instead of writing to the output stream
delegatedOutput := cmdutil.NewGLogWriterV(3)
options := &AllInOneOptions{
Output: out,
MasterOptions: &MasterOptions{
Output: delegatedOutput,
},
}
options.MasterOptions.DefaultsFromName(basename)

cmds := &cobra.Command{
Expand Down Expand Up @@ -105,16 +113,16 @@ func NewCommandStartAllInOne(basename string, out io.Writer) (*cobra.Command, *A
BindListenArg(listenArg, flags, "")
BindImageFormatArgs(imageFormatArgs, flags, "")

startMaster, _ := NewCommandStartMaster(basename, out)
startNode, _ := NewCommandStartNode(basename, out)
startNodeNetwork, _ := NewCommandStartNetwork(basename, out)
startEtcdServer, _ := NewCommandStartEtcdServer(RecommendedStartEtcdServerName, basename, out)
startMaster, _ := NewCommandStartMaster(basename, delegatedOutput)
startNode, _ := NewCommandStartNode(basename, delegatedOutput)
startNodeNetwork, _ := NewCommandStartNetwork(basename, delegatedOutput)
startEtcdServer, _ := NewCommandStartEtcdServer(RecommendedStartEtcdServerName, basename, delegatedOutput)
cmds.AddCommand(startMaster)
cmds.AddCommand(startNode)
cmds.AddCommand(startNodeNetwork)
cmds.AddCommand(startEtcdServer)

startKube := kubernetes.NewCommand("kubernetes", basename, out)
startKube := kubernetes.NewCommand("kubernetes", basename, delegatedOutput)
cmds.AddCommand(startKube)

// autocompletion hints
Expand Down Expand Up @@ -227,7 +235,7 @@ func (o AllInOneOptions) StartAllInOne() error {
if err != nil {
return err
}
fmt.Fprintf(o.MasterOptions.Output, "%s\n", host)
fmt.Fprintf(o.Output, "%s\n", host)
return nil
}
masterOptions := *o.MasterOptions
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/server/start/start_master.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ func (o MasterOptions) CreateCerts() error {
APIServerCAFiles: o.MasterArgs.APIServerCAFiles,
CABundleFile: admin.DefaultCABundleFile(o.MasterArgs.ConfigDir.Value()),
PublicAPIServerURL: publicMasterAddr.String(),
Output: o.Output,
Output: cmdutil.NewGLogWriterV(3),
}
if err := mintAllCertsOptions.Validate(nil); err != nil {
return err
Expand Down
26 changes: 25 additions & 1 deletion pkg/cmd/util/log.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package util

import "github.com/golang/glog"
import (
"io"

"github.com/golang/glog"
)

// GetLogLevel returns the current glog log level
func GetLogLevel() (level int) {
Expand All @@ -11,3 +15,23 @@ func GetLogLevel() (level int) {
}
return
}

// NewGLogWriterV returns a new Writer that delegates to `glog.Info` at the
// desired level of verbosity
func NewGLogWriterV(level int) io.Writer {
return &gLogWriter{
level: glog.Level(level),
}
}

// gLogWriter is a Writer that writes by delegating to `glog.Info`
type gLogWriter struct {
// level is the default level to log at
level glog.Level
}

func (w *gLogWriter) Write(p []byte) (n int, err error) {
glog.V(w.level).InfoDepth(2, string(p))

return len(p), nil
}

0 comments on commit 9d0dd6d

Please sign in to comment.