diff --git a/README.md b/README.md index 0b216f3..1df3ff4 100644 --- a/README.md +++ b/README.md @@ -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 + diff --git a/cmd/kubectl-who-can/main.go b/cmd/kubectl-who-can/main.go index b4da0f9..809fcc9 100644 --- a/cmd/kubectl-who-can/main.go +++ b/cmd/kubectl-who-can/main.go @@ -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) diff --git a/go.mod b/go.mod index cf837e2..1122b53 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 ) diff --git a/pkg/cmd/list.go b/pkg/cmd/list.go index 4371b54..b0acceb 100644 --- a/pkg/cmd/list.go +++ b/pkg/cmd/list.go @@ -4,7 +4,6 @@ import ( "errors" "flag" "fmt" - "github.com/golang/glog" "github.com/spf13/cobra" "github.com/spf13/pflag" "io" @@ -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" ) @@ -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) } } @@ -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 } @@ -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 } @@ -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 } @@ -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 diff --git a/pkg/cmd/policy_rule_matcher.go b/pkg/cmd/policy_rule_matcher.go index 9b3a7ea..c6f5649 100644 --- a/pkg/cmd/policy_rule_matcher.go +++ b/pkg/cmd/policy_rule_matcher.go @@ -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. @@ -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 } @@ -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 } diff --git a/pkg/cmd/resource_resolver.go b/pkg/cmd/resource_resolver.go index a2aa9b9..671c4e6 100644 --- a/pkg/cmd/resource_resolver.go +++ b/pkg/cmd/resource_resolver.go @@ -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" ) @@ -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) }