diff --git a/cmd/kyverno/kyverno.go b/cmd/kyverno/kyverno.go index 8cfba43fb0..33bc524e9c 100644 --- a/cmd/kyverno/kyverno.go +++ b/cmd/kyverno/kyverno.go @@ -10,16 +10,16 @@ import ( flag "github.com/spf13/pflag" ) -func init() { - flag.CommandLine.AddGoFlagSet(goflag.CommandLine) - config.LogDefaultFlags() - flag.Parse() -} func main() { cmd := kyverno.NewDefaultKyvernoCommand() - if err := cmd.Execute(); err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) os.Exit(1) } } + +func init() { + flag.CommandLine.AddGoFlagSet(goflag.CommandLine) + config.LogDefaultFlags() + flag.Parse() +} diff --git a/pkg/kyverno/apply/apply.go b/pkg/kyverno/apply/apply.go index 4b930bfa93..aa1f5f025b 100644 --- a/pkg/kyverno/apply/apply.go +++ b/pkg/kyverno/apply/apply.go @@ -5,9 +5,9 @@ import ( "encoding/json" "fmt" "io" - "log" "os" + "github.com/golang/glog" kubepolicy "github.com/nirmata/kyverno/pkg/apis/policy/v1alpha1" "github.com/nirmata/kyverno/pkg/engine" "github.com/spf13/cobra" @@ -54,21 +54,21 @@ func complete(kubeconfig string, args []string) (*kubepolicy.Policy, []*resource policyDir, resourceDir, err := validateDir(args) if err != nil { - fmt.Printf("Failed to parse file path, err: %v\n", err) + glog.Errorf("Failed to parse file path, err: %v\n", err) os.Exit(1) } // extract policy policy, err := extractPolicy(policyDir) if err != nil { - log.Printf("failed to extract policy: %v", err) + glog.Errorf("failed to extract policy: %v\n", err) os.Exit(1) } // extract rawResource resources, err := extractResource(resourceDir, kubeconfig) if err != nil { - log.Printf("failed to parse resource: %v", err) + glog.Errorf("failed to parse resource: %v", err) os.Exit(1) } @@ -79,13 +79,13 @@ func applyPolicy(policy *kubepolicy.Policy, resources []*resourceInfo) (output s for _, resource := range resources { patchedDocument, err := applyPolicyOnRaw(policy, resource.rawResource, resource.gvk) if err != nil { - fmt.Printf("Error applying policy on resource %s, err: %v\n", resource.gvk.Kind, err) + glog.Errorf("Error applying policy on resource %s, err: %v\n", resource.gvk.Kind, err) continue } out, err := prettyPrint(patchedDocument) if err != nil { - fmt.Printf("JSON parse error: %v\n", err) + glog.Errorf("JSON parse error: %v\n", err) continue } @@ -143,7 +143,7 @@ func extractResource(fileDir, kubeconfig string) ([]*resourceInfo, error) { } if isDir { - files, err = ScanDir(fileDir) + files, err = scanDir(fileDir) if err != nil { return nil, err } @@ -154,7 +154,7 @@ func extractResource(fileDir, kubeconfig string) ([]*resourceInfo, error) { for _, dir := range files { data, err := loadFile(dir) if err != nil { - fmt.Printf("Warning: errpr while loading file: %v\n", err) + glog.Warningf("Error while loading file: %v\n", err) continue } @@ -164,19 +164,19 @@ func extractResource(fileDir, kubeconfig string) ([]*resourceInfo, error) { decode := scheme.Codecs.UniversalDeserializer().Decode obj, gvk, err := decode([]byte(d), nil, nil) if err != nil { - fmt.Printf("Warning: error while decoding YAML object, err: %s\n", err) + glog.Warningf("Error while decoding YAML object, err: %s\n", err) continue } actualObj, err := convertToActualObject(kubeconfig, gvk, obj) if err != nil { - fmt.Printf("Warning: error while converting resource %s to actual k8s object, err: %v\n", gvk.Kind, err) - fmt.Printf("Apply policy on raw resource.\n") + glog.V(3).Infof("Failed to convert resource %s to actual k8s object: %v\n", gvk.Kind, err) + glog.V(3).Infof("Apply policy on raw resource.\n") } raw, err := json.Marshal(actualObj) if err != nil { - fmt.Printf("Warning: error while marshalling manifest, err: %v\n", err) + glog.Warningf("Error while marshalling manifest, err: %v\n", err) continue } diff --git a/pkg/kyverno/apply/util.go b/pkg/kyverno/apply/util.go index e20514aa74..f46a364961 100644 --- a/pkg/kyverno/apply/util.go +++ b/pkg/kyverno/apply/util.go @@ -3,11 +3,11 @@ package apply import ( "fmt" "io/ioutil" - "log" "os" "path/filepath" "strings" + "github.com/golang/glog" yamlv2 "gopkg.in/yaml.v2" rest "k8s.io/client-go/rest" clientcmd "k8s.io/client-go/tools/clientcmd" @@ -27,7 +27,7 @@ func createClientConfig(kubeconfig string) (*rest.Config, error) { func defaultKubeconfigPath() string { home, err := os.UserHomeDir() if err != nil { - log.Printf("Warning: failed to get home dir: %v\n", err) + glog.Warningf("Warning: failed to get home dir: %v\n", err) return "" } @@ -75,17 +75,14 @@ func isDir(dir string) (bool, error) { return fi.IsDir(), nil } -func ScanDir(dir string) ([]string, error) { +func scanDir(dir string) ([]string, error) { var res []string err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { if err != nil { - fmt.Printf("prevent panic by handling failure accessing a path %q: %v\n", dir, err) - return err + return fmt.Errorf("prevent panic by handling failure accessing a path %q: %v\n", dir, err) } - /* if len(strings.Split(path, "/")) == 4 { - fmt.Println(path) - } */ + res = append(res, path) return nil })