1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-31 03:45:17 +00:00

fix logs api

This commit is contained in:
shivkumar dudhani 2020-03-20 11:43:21 -07:00
parent 2638e1002a
commit 4320111c5c
10 changed files with 25 additions and 31 deletions

View file

@ -57,7 +57,6 @@ func main() {
if err := flag.Set("v", "2"); err != nil { if err := flag.Set("v", "2"); err != nil {
setupLog.Error(err, "failed to set log level") setupLog.Error(err, "failed to set log level")
os.Exit(1) os.Exit(1)
// klog.Fatalf("failed to set log level: %v", err)
} }
// Generate CSR with CN as FQDN due to https://github.com/nirmata/kyverno/issues/542 // Generate CSR with CN as FQDN due to https://github.com/nirmata/kyverno/issues/542

View file

@ -233,7 +233,7 @@ func validatePatterns(log logr.Logger, ctx context.EvalInterface, resource unstr
errorStr = append(errorStr, err.Error()) errorStr = append(errorStr, err.Error())
} }
resp.Success = false resp.Success = false
glog.V(4).Infof("Validation rule '%s' failed. %s", rule.Name, errorStr) log.V(4).Info(fmt.Sprintf("Validation rule '%s' failed. %s", rule.Name, errorStr))
if rule.Validation.Message == "" { if rule.Validation.Message == "" {
resp.Message = fmt.Sprintf("Validation rule '%s' has failed", rule.Name) resp.Message = fmt.Sprintf("Validation rule '%s' has failed", rule.Name)
} else { } else {

View file

@ -11,8 +11,6 @@ import (
policy2 "github.com/nirmata/kyverno/pkg/policy" policy2 "github.com/nirmata/kyverno/pkg/policy"
"github.com/golang/glog"
"k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/discovery" "k8s.io/client-go/discovery"
@ -32,6 +30,7 @@ import (
yamlv2 "gopkg.in/yaml.v2" yamlv2 "gopkg.in/yaml.v2"
"k8s.io/cli-runtime/pkg/genericclioptions" "k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/kubernetes/scheme" "k8s.io/client-go/kubernetes/scheme"
log "sigs.k8s.io/controller-runtime/pkg/log"
) )
func Command() *cobra.Command { func Command() *cobra.Command {
@ -49,7 +48,7 @@ func Command() *cobra.Command {
defer func() { defer func() {
if err != nil { if err != nil {
if !sanitizedError.IsErrorSanitized(err) { if !sanitizedError.IsErrorSanitized(err) {
glog.V(4).Info(err) log.Log.Error(err, "failed to sanitize")
err = fmt.Errorf("Internal error") err = fmt.Errorf("Internal error")
} }
} }

View file

@ -9,6 +9,9 @@ import (
"github.com/nirmata/kyverno/pkg/kyverno/apply" "github.com/nirmata/kyverno/pkg/kyverno/apply"
"github.com/nirmata/kyverno/pkg/kyverno/version" "github.com/nirmata/kyverno/pkg/kyverno/version"
"k8s.io/klog"
"k8s.io/klog/klogr"
log "sigs.k8s.io/controller-runtime/pkg/log"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -19,7 +22,7 @@ func CLI() {
Short: "kyverno manages native policies of Kubernetes", Short: "kyverno manages native policies of Kubernetes",
} }
configureGlog(cli) configurelog(cli)
commands := []*cobra.Command{ commands := []*cobra.Command{
version.Command(), version.Command(),
@ -36,9 +39,9 @@ func CLI() {
} }
} }
func configureGlog(cli *cobra.Command) { func configurelog(cli *cobra.Command) {
flag.Parse() klog.InitFlags(nil)
_ = flag.Set("logtostderr", "true") log.SetLogger(klogr.New())
cli.PersistentFlags().AddGoFlagSet(flag.CommandLine) cli.PersistentFlags().AddGoFlagSet(flag.CommandLine)
_ = cli.PersistentFlags().MarkHidden("alsologtostderr") _ = cli.PersistentFlags().MarkHidden("alsologtostderr")

View file

@ -9,13 +9,12 @@ import (
"github.com/nirmata/kyverno/pkg/kyverno/sanitizedError" "github.com/nirmata/kyverno/pkg/kyverno/sanitizedError"
"github.com/golang/glog"
policyvalidate "github.com/nirmata/kyverno/pkg/policy" policyvalidate "github.com/nirmata/kyverno/pkg/policy"
v1 "github.com/nirmata/kyverno/pkg/api/kyverno/v1" v1 "github.com/nirmata/kyverno/pkg/api/kyverno/v1"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/util/yaml" "k8s.io/apimachinery/pkg/util/yaml"
log "sigs.k8s.io/controller-runtime/pkg/log"
) )
func Command() *cobra.Command { func Command() *cobra.Command {
@ -27,7 +26,7 @@ func Command() *cobra.Command {
defer func() { defer func() {
if err != nil { if err != nil {
if !sanitizedError.IsErrorSanitized(err) { if !sanitizedError.IsErrorSanitized(err) {
glog.V(4).Info(err) log.Log.Error(err, "failed to sanitize")
err = fmt.Errorf("Internal error") err = fmt.Errorf("Internal error")
} }
} }

View file

@ -6,13 +6,12 @@ import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"github.com/golang/glog"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
"github.com/googleapis/gnostic/compiler" "github.com/googleapis/gnostic/compiler"
openapi_v2 "github.com/googleapis/gnostic/OpenAPIv2" openapi_v2 "github.com/googleapis/gnostic/OpenAPIv2"
log "sigs.k8s.io/controller-runtime/pkg/log"
client "github.com/nirmata/kyverno/pkg/dclient" client "github.com/nirmata/kyverno/pkg/dclient"
"k8s.io/apimachinery/pkg/util/wait" "k8s.io/apimachinery/pkg/util/wait"
@ -44,12 +43,12 @@ func NewCRDSync(client *client.Client) *crdSync {
func (c *crdSync) Run(workers int, stopCh <-chan struct{}) { func (c *crdSync) Run(workers int, stopCh <-chan struct{}) {
newDoc, err := c.client.DiscoveryClient.OpenAPISchema() newDoc, err := c.client.DiscoveryClient.OpenAPISchema()
if err != nil { if err != nil {
glog.V(4).Infof("cannot get openapi schema: %v", err) log.Log.Error(err, "cannot get openapi schema")
} }
err = useOpenApiDocument(newDoc) err = useOpenApiDocument(newDoc)
if err != nil { if err != nil {
glog.V(4).Infof("Could not set custom OpenApi document: %v\n", err) log.Log.Error(err, "Could not set custom OpenApi document")
} }
for i := 0; i < workers; i++ { for i := 0; i < workers; i++ {
@ -64,7 +63,7 @@ func (c *crdSync) sync() {
crds, err := c.client.ListResource("CustomResourceDefinition", "", nil) crds, err := c.client.ListResource("CustomResourceDefinition", "", nil)
if err != nil { if err != nil {
glog.V(4).Infof("could not fetch crd's from server: %v", err) log.Log.Error(err, "could not fetch crd's from server")
return return
} }
@ -91,7 +90,7 @@ func parseCRD(crd unstructured.Unstructured) {
crdName := crdDefinition.Spec.Names.Kind crdName := crdDefinition.Spec.Names.Kind
if len(crdDefinition.Spec.Versions) < 1 { if len(crdDefinition.Spec.Versions) < 1 {
glog.V(4).Infof("could not parse crd schema, no versions present") log.Log.V(4).Info("could not parse crd schema, no versions present")
return return
} }
@ -101,7 +100,7 @@ func parseCRD(crd unstructured.Unstructured) {
parsedSchema, err := openapi_v2.NewSchema(schema, compiler.NewContext("schema", nil)) parsedSchema, err := openapi_v2.NewSchema(schema, compiler.NewContext("schema", nil))
if err != nil { if err != nil {
glog.V(4).Infof("could not parse crd schema:%v", err) log.Log.Error(err, "could not parse crd schema:")
return return
} }

View file

@ -8,8 +8,6 @@ import (
"github.com/nirmata/kyverno/data" "github.com/nirmata/kyverno/data"
"github.com/golang/glog"
"github.com/nirmata/kyverno/pkg/engine" "github.com/nirmata/kyverno/pkg/engine"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@ -19,6 +17,7 @@ import (
"github.com/googleapis/gnostic/compiler" "github.com/googleapis/gnostic/compiler"
"k8s.io/kube-openapi/pkg/util/proto" "k8s.io/kube-openapi/pkg/util/proto"
"k8s.io/kube-openapi/pkg/util/proto/validation" "k8s.io/kube-openapi/pkg/util/proto/validation"
log "sigs.k8s.io/controller-runtime/pkg/log"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
) )
@ -69,7 +68,7 @@ func ValidatePolicyMutation(policy v1.ClusterPolicy) error {
newPolicy.Spec.Rules = rules newPolicy.Spec.Rules = rules
resource, _ := generateEmptyResource(openApiGlobalState.definitions[openApiGlobalState.kindToDefinitionName[kind]]).(map[string]interface{}) resource, _ := generateEmptyResource(openApiGlobalState.definitions[openApiGlobalState.kindToDefinitionName[kind]]).(map[string]interface{})
if resource == nil { if resource == nil {
glog.V(4).Infof("Cannot Validate policy: openApi definition now found for %v", kind) log.Log.V(4).Info(fmt.Sprintf("Cannot Validate policy: openApi definition now found for %v", kind))
return nil return nil
} }
newResource := unstructured.Unstructured{Object: resource} newResource := unstructured.Unstructured{Object: resource}

View file

@ -6,7 +6,6 @@ import (
"time" "time"
"github.com/go-logr/logr" "github.com/go-logr/logr"
"github.com/golang/glog"
"github.com/minio/minio/pkg/wildcard" "github.com/minio/minio/pkg/wildcard"
kyverno "github.com/nirmata/kyverno/pkg/api/kyverno/v1" kyverno "github.com/nirmata/kyverno/pkg/api/kyverno/v1"
"github.com/nirmata/kyverno/pkg/config" "github.com/nirmata/kyverno/pkg/config"
@ -40,7 +39,6 @@ func (pc *PolicyController) processExistingResources(policy kyverno.ClusterPolic
} }
// apply the policy on each // apply the policy on each
glog.V(4).Infof("apply policy %s with resource version %s on resource %s/%s/%s with resource version %s", policy.Name, policy.ResourceVersion, resource.GetKind(), resource.GetNamespace(), resource.GetName(), resource.GetResourceVersion())
engineResponse := applyPolicy(policy, resource, logger) engineResponse := applyPolicy(policy, resource, logger)
// get engine response for mutation & validation independently // get engine response for mutation & validation independently
engineResponses = append(engineResponses, engineResponse...) engineResponses = append(engineResponses, engineResponse...)

View file

@ -2,16 +2,16 @@ package policystatus
import ( import (
"encoding/json" "encoding/json"
"fmt"
"sync" "sync"
"time" "time"
"github.com/golang/glog"
"k8s.io/apimachinery/pkg/util/wait" "k8s.io/apimachinery/pkg/util/wait"
"github.com/nirmata/kyverno/pkg/client/clientset/versioned" "github.com/nirmata/kyverno/pkg/client/clientset/versioned"
v1 "github.com/nirmata/kyverno/pkg/api/kyverno/v1" v1 "github.com/nirmata/kyverno/pkg/api/kyverno/v1"
log "sigs.k8s.io/controller-runtime/pkg/log"
) )
// Policy status implementation works in the following way, // Policy status implementation works in the following way,
@ -111,8 +111,7 @@ func (s *Sync) updateStatusCache(stopCh <-chan struct{}) {
s.cache.keyToMutex.Get(statusUpdater.PolicyName()).Unlock() s.cache.keyToMutex.Get(statusUpdater.PolicyName()).Unlock()
oldStatus, _ := json.Marshal(status) oldStatus, _ := json.Marshal(status)
newStatus, _ := json.Marshal(updatedStatus) newStatus, _ := json.Marshal(updatedStatus)
log.Log.V(4).Info(fmt.Sprintf("\nupdated status of policy - %v\noldStatus:\n%v\nnewStatus:\n%v\n", statusUpdater.PolicyName(), string(oldStatus), string(newStatus)))
glog.V(4).Infof("\nupdated status of policy - %v\noldStatus:\n%v\nnewStatus:\n%v\n", statusUpdater.PolicyName(), string(oldStatus), string(newStatus))
case <-stopCh: case <-stopCh:
return return
} }
@ -140,7 +139,7 @@ func (s *Sync) updatePolicyStatus() {
s.cache.dataMu.Lock() s.cache.dataMu.Lock()
delete(s.cache.data, policyName) delete(s.cache.data, policyName)
s.cache.dataMu.Unlock() s.cache.dataMu.Unlock()
glog.V(4).Info(err) log.Log.Error(err, "failed to update policy status")
} }
} }
} }

View file

@ -7,7 +7,6 @@ import (
"github.com/nirmata/kyverno/pkg/openapi" "github.com/nirmata/kyverno/pkg/openapi"
"github.com/golang/glog"
kyverno "github.com/nirmata/kyverno/pkg/api/kyverno/v1" kyverno "github.com/nirmata/kyverno/pkg/api/kyverno/v1"
v1 "github.com/nirmata/kyverno/pkg/api/kyverno/v1" v1 "github.com/nirmata/kyverno/pkg/api/kyverno/v1"
"github.com/nirmata/kyverno/pkg/engine" "github.com/nirmata/kyverno/pkg/engine"
@ -70,7 +69,7 @@ func (ws *WebhookServer) HandleMutation(request *v1beta1.AdmissionRequest, resou
} }
err := openapi.ValidateResource(*engineResponse.PatchedResource.DeepCopy(), engineResponse.PatchedResource.GetKind()) err := openapi.ValidateResource(*engineResponse.PatchedResource.DeepCopy(), engineResponse.PatchedResource.GetKind())
if err != nil { if err != nil {
glog.V(4).Infoln(err) logger.Error(err, "failed to validate resource")
continue continue
} }
// gather patches // gather patches