Skip to content

Commit

Permalink
chore: Replace google/glog with kubernetes/klog (#71)
Browse files Browse the repository at this point in the history
Resolves: #70
  • Loading branch information
thanasisk authored Apr 10, 2020
1 parent f78ee94 commit 5925f66
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 16 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,4 @@ This repository is available under the [Apache License 2.0](https://github.com/a

[asciicast-img]: https://asciinema.org/a/ccqqYwA5L5rMV9kd1tgzyZJ2j.svg
[asciicast]: https://asciinema.org/a/ccqqYwA5L5rMV9kd1tgzyZJ2j

18 changes: 18 additions & 0 deletions cmd/kubectl-who-can/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,29 @@ import (
"github.com/aquasecurity/kubectl-who-can/pkg/cmd"
clioptions "k8s.io/cli-runtime/pkg/genericclioptions"
// Load all known auth plugins
"flag"
"github.com/spf13/pflag"
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/klog"
"os"
)

func initFlags() {
klog.InitFlags(nil)
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)

// Hide all klog flags except for -v
flag.CommandLine.VisitAll(func(f *flag.Flag) {
if f.Name != "v" {
pflag.Lookup(f.Name).Hidden = true
}
})
}

func main() {
defer klog.Flush()

initFlags()
root, err := cmd.NewWhoCanCommand(clioptions.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr})
if err != nil {
fmt.Printf("Error: %v\n", err)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module github.com/aquasecurity/kubectl-who-can
go 1.12

require (
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b
github.com/spf13/cobra v0.0.4
github.com/spf13/pflag v1.0.3
github.com/stretchr/testify v1.3.0
Expand All @@ -12,4 +11,5 @@ require (
k8s.io/apimachinery v0.0.0-20190703205208-4cfb76a8bf76
k8s.io/cli-runtime v0.0.0-20190612131021-ced92c4c4749
k8s.io/client-go v0.0.0-20190704045512-07281898b0f0
k8s.io/klog v0.3.1
)
14 changes: 7 additions & 7 deletions pkg/cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"flag"
"fmt"
"github.com/golang/glog"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"io"
Expand All @@ -19,6 +18,7 @@ import (
clientrbac "k8s.io/client-go/kubernetes/typed/rbac/v1"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/klog"
"strings"
"text/tabwriter"
)
Expand Down Expand Up @@ -195,13 +195,13 @@ func ActionFrom(clientConfig clientcmd.ClientConfig, flags *pflag.FlagSet, args
action.Verb = args[0]
if strings.HasPrefix(args[1], "/") {
action.NonResourceURL = args[1]
glog.V(3).Infof("Resolved nonResourceURL `%s`", action.NonResourceURL)
klog.V(3).Infof("Resolved nonResourceURL `%s`", action.NonResourceURL)
} else {
resourceTokens := strings.SplitN(args[1], "/", 2)
action.Resource = resourceTokens[0]
if len(resourceTokens) > 1 {
action.ResourceName = resourceTokens[1]
glog.V(3).Infof("Resolved resourceName `%s`", action.ResourceName)
klog.V(3).Infof("Resolved resourceName `%s`", action.ResourceName)
}
}

Expand All @@ -217,7 +217,7 @@ func ActionFrom(clientConfig clientcmd.ClientConfig, flags *pflag.FlagSet, args

if action.AllNamespaces {
action.Namespace = core.NamespaceAll
glog.V(3).Infof("Resolved namespace `%s` from --all-namespaces flag", action.Namespace)
klog.V(3).Infof("Resolved namespace `%s` from --all-namespaces flag", action.Namespace)
return
}

Expand All @@ -227,7 +227,7 @@ func ActionFrom(clientConfig clientcmd.ClientConfig, flags *pflag.FlagSet, args
}

if action.Namespace != "" {
glog.V(3).Infof("Resolved namespace `%s` from --namespace flag", action.Namespace)
klog.V(3).Infof("Resolved namespace `%s` from --namespace flag", action.Namespace)
return
}

Expand All @@ -236,7 +236,7 @@ func ActionFrom(clientConfig clientcmd.ClientConfig, flags *pflag.FlagSet, args
if err != nil {
err = fmt.Errorf("getting namespace from current context: %v", err)
}
glog.V(3).Infof("Resolved namespace `%s` from current context", action.Namespace)
klog.V(3).Infof("Resolved namespace `%s` from current context", action.Namespace)
return
}

Expand Down Expand Up @@ -271,7 +271,7 @@ func (w *WhoCan) Check(action Action) (roleBindings []rbac.RoleBinding, clusterR
err = fmt.Errorf("resolving resource: %v", err)
return
}
glog.V(3).Infof("Resolved resource `%s`", resolvedAction.gr.String())
klog.V(3).Infof("Resolved resource `%s`", resolvedAction.gr.String())
}

// Get the Roles that relate to the Verbs and Resources we are interested in
Expand Down
10 changes: 5 additions & 5 deletions pkg/cmd/policy_rule_matcher.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package cmd

import (
"github.com/golang/glog"
rbac "k8s.io/api/rbac/v1"
"k8s.io/klog"
)

// PolicyRuleMatcher wraps the Matches* methods.
Expand All @@ -28,10 +28,10 @@ func (m *matcher) MatchesRole(role rbac.Role, action resolvedAction) bool {
if !m.matches(rule, action) {
continue
}
glog.V(4).Infof("Role [%s] matches action filter? YES", role.Name)
klog.V(4).Infof("Role [%s] matches action filter? YES", role.Name)
return true
}
glog.V(4).Infof("Role [%s] matches action filter? NO", role.Name)
klog.V(4).Infof("Role [%s] matches action filter? NO", role.Name)
return false
}

Expand All @@ -41,10 +41,10 @@ func (m *matcher) MatchesClusterRole(role rbac.ClusterRole, action resolvedActio
continue
}

glog.V(4).Infof("ClusterRole [%s] matches action filter? YES", role.Name)
klog.V(4).Infof("ClusterRole [%s] matches action filter? YES", role.Name)
return true
}
glog.V(4).Infof("ClusterRole [%s] matches action filter? NO", role.Name)
klog.V(4).Infof("ClusterRole [%s] matches action filter? NO", role.Name)
return false
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/cmd/resource_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package cmd

import (
"fmt"
"github.com/golang/glog"
rbac "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/api/meta"
apismeta "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/discovery"
"k8s.io/klog"
"strings"
)

Expand Down Expand Up @@ -44,13 +44,13 @@ func (rv *resourceResolver) Resolve(verb, resource, subResource string) (schema.

gvr, err := rv.resolveGVR(resource)
if err != nil {
glog.V(3).Infof("Error while resolving GVR for resource %s: %v", resource, err)
klog.V(3).Infof("Error while resolving GVR for resource %s: %v", resource, err)
return schema.GroupResource{}, fmt.Errorf("the server doesn't have a resource type \"%s\"", name)
}

apiResource, err := rv.resolveAPIResource(gvr, subResource)
if err != nil {
glog.V(3).Infof("Error while resolving APIResource for GVR %v and subResource %s: %v", gvr, subResource, err)
klog.V(3).Infof("Error while resolving APIResource for GVR %v and subResource %s: %v", gvr, subResource, err)
return schema.GroupResource{}, fmt.Errorf("the server doesn't have a resource type \"%s\"", name)
}

Expand Down

0 comments on commit 5925f66

Please sign in to comment.