Skip to content
Permalink
Browse files

Migrate from glog to klog (#390)

Kubernetes ecosystem has moved to klog. This is more actively maintained
than glog. We might find caveats and gotchas along the way, but seems to be
working fine locally.

Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
  • Loading branch information
ahmetb authored and k8s-ci-robot committed Nov 16, 2019
1 parent 46a5a53 commit 51122e9d5b3efe0c9e858b0615f70035556ab515
@@ -4,5 +4,5 @@ go 1.12

require (
github.com/pkg/errors v0.8.1 // indirect
sigs.k8s.io/krew v0.2.2-0.20190724210953-8867a94acd6d
sigs.k8s.io/krew v0.3.2
)
@@ -9,6 +9,7 @@ github.com/docker/spdystream v0.0.0-20160310174837-449fdfce4d96/go.mod h1:Qh8CwZ
github.com/elazarl/goproxy v0.0.0-20170405201442-c4fc26588b6e/go.mod h1:/Zj4wYkgs4iZTTu3o/KG3Itv/qCCa8VVMlb3i9OVuzc=
github.com/emicklei/go-restful v0.0.0-20170410110728-ff4f55a20633/go.mod h1:otzb+WCGbkyDHkqmQmT5YD2WR4BBwUdeQoFo8l/7tVs=
github.com/evanphx/json-patch v4.2.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/ghodss/yaml v0.0.0-20150909031657-73d445a93680/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/go-openapi/jsonpointer v0.0.0-20160704185906-46af16f9f7b1/go.mod h1:+35s3my2LFTysnkMfxsJBAMHj/DoqoB9knIWoYG/Vk0=
@@ -46,7 +47,9 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/mailru/easyjson v0.0.0-20160728113105-d5b7844b561a/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180320133207-05fbef0ca5da/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
@@ -85,6 +88,7 @@ golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sys v0.0.0-20170830134202-bb24a47a89ea/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190616124812-15dcb6c0061f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190712062909-fae7ac547cb7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.0.0-20160726164857-2910a502d2bf/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
@@ -116,6 +120,8 @@ k8s.io/klog v0.3.3/go.mod h1:Gq+BEi5rUBO/HRz0bTSXDUcqjScdoY3a9IHpCEIOOfk=
k8s.io/kube-openapi v0.0.0-20190709113604-33be087ad058/go.mod h1:nfDlWeOsu3pUf4yWGL+ERqohP4YsZcBJXWMK+gkzOA4=
sigs.k8s.io/krew v0.2.2-0.20190724210953-8867a94acd6d h1:ftkzey0SeGithDvmwEsUnlDwV1PKn8i+rNEfabj1W8w=
sigs.k8s.io/krew v0.2.2-0.20190724210953-8867a94acd6d/go.mod h1:wsheYhp5znrUApov8QcNjBCwXu85fIGjRJvjSkE8pI8=
sigs.k8s.io/krew v0.3.2 h1:FKBD9xy9GtO5A8FeVk/cbgG6vzPbUg4SwUziZj/TGoU=
sigs.k8s.io/krew v0.3.2/go.mod h1:G0cvdJtBb0gHBQZtCBumtllvhU6H+ggDr2oLznaLglc=
sigs.k8s.io/structured-merge-diff v0.0.0-20190525122527-15d366b2352e/go.mod h1:wWxsB5ozmmv/SG7nM11ayaAW51xMvak/t1r0CSlcokI=
sigs.k8s.io/yaml v1.1.0 h1:4A07+ZFc2wgJwo8YNlQpr1rVlgUDlxXHhPJciaPY5gs=
sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=
@@ -19,9 +19,9 @@ import (
"fmt"
"os"

"github.com/golang/glog"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"k8s.io/klog"

"sigs.k8s.io/krew/cmd/krew/cmd/internal"
"sigs.k8s.io/krew/pkg/index"
@@ -110,7 +110,7 @@ Remarks:
}

for _, plugin := range install {
glog.V(2).Infof("Will install plugin: %s\n", plugin.Name)
klog.V(2).Infof("Will install plugin: %s\n", plugin.Name)
}

var failed []string
@@ -121,11 +121,11 @@ Remarks:
ArchiveFileOverride: *archiveFileOverride,
})
if err == installation.ErrIsAlreadyInstalled {
glog.Warningf("Skipping plugin %q, it is already installed", plugin.Name)
klog.Warningf("Skipping plugin %q, it is already installed", plugin.Name)
continue
}
if err != nil {
glog.Warningf("failed to install plugin %q: %v", plugin.Name, err)
klog.Warningf("failed to install plugin %q: %v", plugin.Name, err)
if returnErr == nil {
returnErr = err
}
@@ -145,11 +145,11 @@ Remarks:
},
PreRunE: func(cmd *cobra.Command, args []string) error {
if *manifest != "" {
glog.V(4).Infof("--manifest specified, not ensuring plugin index")
klog.V(4).Infof("--manifest specified, not ensuring plugin index")
return nil
}
if *noUpdateIndex {
glog.V(4).Infof("--no-update-index specified, skipping updating local copy of plugin index")
klog.V(4).Infof("--no-update-index specified, skipping updating local copy of plugin index")
return nil
}
return ensureIndexUpdated(cmd, args)
@@ -19,11 +19,11 @@ import (
"fmt"
"os"

"github.com/golang/glog"
"github.com/mattn/go-isatty"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"k8s.io/klog"

"sigs.k8s.io/krew/pkg/constants"
"sigs.k8s.io/krew/pkg/environment"
@@ -52,17 +52,20 @@ You can invoke krew through kubectl: "kubectl krew [command]..."`,
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := rootCmd.Execute(); err != nil {
if glog.V(1) {
glog.Fatalf("%+v", err) // with stack trace
if klog.V(1) {
klog.Fatalf("%+v", err) // with stack trace
} else {
glog.Fatal(err) // just error message
klog.Fatal(err) // just error message
}
}
}

func init() {
klog.InitFlags(nil)

pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
_ = flag.CommandLine.Parse([]string{}) // convince pkg/flag we parsed the flags

flag.CommandLine.VisitAll(func(f *flag.Flag) {
if f.Name != "v" { // hide all glog flags except for -v
pflag.Lookup(f.Name).Hidden = true
@@ -79,7 +82,7 @@ func init() {
paths.InstallPath(),
paths.BinPath(),
paths.InstallReceiptsPath()); err != nil {
glog.Fatal(err)
klog.Fatal(err)
}
}

@@ -95,11 +98,11 @@ func preRun(cmd *cobra.Command, _ []string) error {
}

if installation.IsWindows() {
glog.V(4).Infof("detected windows, will check for old krew installations to clean up")
klog.V(4).Infof("detected windows, will check for old krew installations to clean up")
err := cleanupStaleKrewInstallations()
if err != nil {
glog.Warningf("Failed to clean up old installations of krew (on windows).")
glog.Warningf("You may need to clean them up manually. Error: %v", err)
klog.Warningf("Failed to clean up old installations of krew (on windows).")
klog.Warningf("You may need to clean them up manually. Error: %v", err)
}
}
return nil
@@ -108,14 +111,14 @@ func preRun(cmd *cobra.Command, _ []string) error {
func cleanupStaleKrewInstallations() error {
r, err := receipt.Load(paths.PluginInstallReceiptPath(constants.KrewPluginName))
if os.IsNotExist(err) {
glog.V(1).Infof("could not find krew's own plugin receipt, skipping cleanup of stale krew installations")
klog.V(1).Infof("could not find krew's own plugin receipt, skipping cleanup of stale krew installations")
return nil
} else if err != nil {
return errors.Wrap(err, "cannot load krew's own plugin receipt")
}
v := r.Spec.Version

glog.V(1).Infof("Clean up krew stale installations, current=%s", v)
klog.V(1).Infof("Clean up krew stale installations, current=%s", v)
return installation.CleanupStaleKrewInstallations(paths.PluginInstallPath(constants.KrewPluginName), v)
}

@@ -130,7 +133,7 @@ func checkIndex(_ *cobra.Command, _ []string) error {

func ensureDirs(paths ...string) error {
for _, p := range paths {
glog.V(4).Infof("Ensure creating dir: %q", p)
klog.V(4).Infof("Ensure creating dir: %q", p)
if err := os.MkdirAll(p, 0755); err != nil {
return errors.Wrapf(err, "failed to ensure create directory %q", p)
}
@@ -18,9 +18,9 @@ import (
"fmt"
"os"

"github.com/golang/glog"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"k8s.io/klog"

"sigs.k8s.io/krew/pkg/installation"
)
@@ -38,7 +38,7 @@ Remarks:
Failure to uninstall a plugin will result in an error and exit immediately.`,
RunE: func(cmd *cobra.Command, args []string) error {
for _, name := range args {
glog.V(4).Infof("Going to uninstall plugin %s\n", name)
klog.V(4).Infof("Going to uninstall plugin %s\n", name)
if err := installation.Uninstall(paths, name); err != nil {
return errors.Wrapf(err, "failed to uninstall plugin %s", name)
}
@@ -18,9 +18,9 @@ import (
"fmt"
"os"

"github.com/golang/glog"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"k8s.io/klog"

"sigs.k8s.io/krew/pkg/constants"
"sigs.k8s.io/krew/pkg/gitutil"
@@ -42,7 +42,7 @@ Remarks:
}

func ensureIndexUpdated(_ *cobra.Command, _ []string) error {
glog.V(1).Infof("Updating the local copy of plugin index (%s)", paths.IndexPath())
klog.V(1).Infof("Updating the local copy of plugin index (%s)", paths.IndexPath())
if err := gitutil.EnsureUpdated(constants.IndexURI, paths.IndexPath()); err != nil {
return errors.Wrap(err, "failed to update the local index")
}
@@ -18,9 +18,9 @@ import (
"fmt"
"os"

"github.com/golang/glog"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"k8s.io/klog"

"sigs.k8s.io/krew/cmd/krew/cmd/internal"
"sigs.k8s.io/krew/pkg/index/indexscanner"
@@ -62,7 +62,7 @@ kubectl krew upgrade foo bar"`,
return errors.Wrapf(err, "failed to load the plugin manifest for plugin %s", name)
}

glog.V(2).Infof("Upgrading plugin: %s\n", plugin.Name)
klog.V(2).Infof("Upgrading plugin: %s\n", plugin.Name)
err = installation.Upgrade(paths, plugin)
if ignoreUpgraded && err == installation.ErrIsAlreadyUpgraded {
fmt.Fprintf(os.Stderr, "Skipping plugin %s, it is already on the newest version\n", plugin.Name)
@@ -78,7 +78,7 @@ kubectl krew upgrade foo bar"`,
},
PreRunE: func(cmd *cobra.Command, args []string) error {
if *noUpdateIndex {
glog.V(4).Infof("--no-update-index specified, skipping updating local copy of plugin index")
klog.V(4).Infof("--no-update-index specified, skipping updating local copy of plugin index")
return nil
}
return ensureIndexUpdated(cmd, args)
@@ -15,12 +15,12 @@
package main

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

"sigs.k8s.io/krew/cmd/krew/cmd"
)

func main() {
cmd.Execute()
defer glog.Flush()
defer klog.Flush()
}
@@ -25,10 +25,10 @@ import (
"path/filepath"
"strings"

"github.com/golang/glog"
"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/klog"

"sigs.k8s.io/krew/pkg/constants"
"sigs.k8s.io/krew/pkg/index"
@@ -40,29 +40,29 @@ var flManifest string

func init() {
flag.StringVar(&flManifest, "manifest", "", "path to plugin manifest file")
if err := flag.Set("logtostderr", "true"); err != nil {
fmt.Printf("can't set log to stderr %+v", err)
os.Exit(1)
}
}

func main() {
// TODO(ahmetb) iterate over glog flags and hide them (not sure if possible without using pflag)
klog.InitFlags(nil)
if err := flag.Set("logtostderr", "true"); err != nil {
fmt.Printf("can't set log to stderr %+v", err)
os.Exit(1)
}
flag.Parse()

defer glog.Flush()
defer klog.Flush()

if flManifest == "" {
glog.Fatal("-manifest must be specified")
klog.Fatal("-manifest must be specified")
}

if err := validateManifestFile(flManifest); err != nil {
glog.Fatalf("%v", err) // with stack trace
klog.Fatalf("%v", err) // with stack trace
}
}

func validateManifestFile(path string) error {
glog.V(4).Infof("reading file %s", path)
klog.V(4).Infof("reading file %s", path)
p, err := indexscanner.ReadPluginFile(path)
if err != nil {
return errors.Wrap(err, "failed to read plugin file")
@@ -73,35 +73,35 @@ func validateManifestFile(path string) error {
return fmt.Errorf("expected manifest extension %q but found %q", constants.ManifestExtension, manifestExtension)
}
pluginNameFromFileName := strings.TrimSuffix(filename, manifestExtension)
glog.V(4).Infof("inferred plugin name as %s", pluginNameFromFileName)
klog.V(4).Infof("inferred plugin name as %s", pluginNameFromFileName)

// validate plugin manifest
if err := validation.ValidatePlugin(pluginNameFromFileName, p); err != nil {
return errors.Wrap(err, "plugin validation error")
}
glog.Infof("structural validation OK")
klog.Infof("structural validation OK")

// make sure each platform matches a supported platform
for i, p := range p.Spec.Platforms {
if os, arch := findAnyMatchingPlatform(p.Selector); os == "" || arch == "" {
return errors.Errorf("spec.platform[%d]'s selector (%v) doesn't match any supported platforms", i, p.Selector)
}
}
glog.Infof("all spec.platform[] items are used")
klog.Infof("all spec.platform[] items are used")

// validate no supported <os,arch> is matching multiple platform specs
if err := isOverlappingPlatformSelectors(p.Spec.Platforms); err != nil {
return errors.Wrap(err, "overlapping platform selectors found")
}
glog.Infof("no overlapping spec.platform[].selector")
klog.Infof("no overlapping spec.platform[].selector")

// exercise "install" for all platforms
for i, p := range p.Spec.Platforms {
glog.Infof("installing spec.platform[%d]", i)
klog.Infof("installing spec.platform[%d]", i)
if err := installPlatformSpec(path, p); err != nil {
return errors.Wrapf(err, "spec.platforms[%d] failed to install", i)
}
glog.Infof("installed spec.platforms[%d]", i)
klog.Infof("installed spec.platforms[%d]", i)
}
log.Printf("all %d spec.platforms installed fine", len(p.Spec.Platforms))
return nil
@@ -141,7 +141,7 @@ func installPlatformSpec(manifestFile string, p index.Platform) error {
}
defer func() {
if err := os.RemoveAll(tmpDir); err != nil {
glog.Warningf("failed to remove temp dir: %s", tmpDir)
klog.Warningf("failed to remove temp dir: %s", tmpDir)
}
}()

@@ -152,7 +152,7 @@ func installPlatformSpec(manifestFile string, p index.Platform) error {
"KREW_OS=" + goos,
"KREW_ARCH=" + goarch,
}
glog.V(2).Infof("installing plugin with: %+v", cmd.Env)
klog.V(2).Infof("installing plugin with: %+v", cmd.Env)
cmd.Env = append(cmd.Env, "PATH="+os.Getenv("PATH"))

b, err := cmd.CombinedOutput()
@@ -167,10 +167,10 @@ func installPlatformSpec(manifestFile string, p index.Platform) error {
func findAnyMatchingPlatform(selector *metav1.LabelSelector) (string, string) {
for _, p := range allPlatforms() {
if selectorMatchesOSArch(selector, p[0], p[1]) {
glog.V(4).Infof("%s MATCHED <%s,%s>", selector, p[0], p[1])
klog.V(4).Infof("%s MATCHED <%s,%s>", selector, p[0], p[1])
return p[0], p[1]
}
glog.V(4).Infof("%s didn't match <%s,%s>", selector, p[0], p[1])
klog.V(4).Infof("%s didn't match <%s,%s>", selector, p[0], p[1])
}
return "", ""
}
@@ -179,7 +179,7 @@ func selectorMatchesOSArch(selector *metav1.LabelSelector, os, arch string) bool
sel, err := metav1.LabelSelectorAsSelector(selector)
if err != nil {
// this should've been caught by validation.ValidatePlatform() earlier
glog.Warningf("Failed to convert label selector: %+v", selector)
klog.Warningf("Failed to convert label selector: %+v", selector)
return false
}
return sel.Matches(labels.Set{

0 comments on commit 51122e9

Please sign in to comment.
You can’t perform that action at this time.