1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-28 10:28:36 +00:00

fix: cli dependency to controller-runtime logger (#8280)

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
This commit is contained in:
Charles-Edouard Brétéché 2023-09-06 02:06:44 +02:00 committed by GitHub
parent cbeb5157d6
commit 04e21da671
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 38 additions and 26 deletions

View file

@ -13,6 +13,7 @@ import (
kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1"
"github.com/kyverno/kyverno/api/kyverno/v1beta1"
valuesapi "github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/apis/values"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/log"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/output/color"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/policy"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/source"
@ -34,7 +35,6 @@ import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/yaml"
)

View file

@ -13,6 +13,7 @@ import (
"github.com/google/go-containerregistry/pkg/v1/static"
"github.com/google/go-containerregistry/pkg/v1/types"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/commands/oci/internal"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/log"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/policy"
cobrautils "github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/utils/cobra"
"github.com/kyverno/kyverno/pkg/config"
@ -20,7 +21,6 @@ import (
policyutils "github.com/kyverno/kyverno/pkg/utils/policy"
policyvalidation "github.com/kyverno/kyverno/pkg/validation/policy"
"github.com/spf13/cobra"
"sigs.k8s.io/controller-runtime/pkg/log"
)
func Command(keychain authn.Keychain) *cobra.Command {

View file

@ -6,6 +6,7 @@ import (
"github.com/go-git/go-billy/v5"
testapi "github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/apis/test"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/log"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/output/color"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/output/table"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/test/filter"
@ -16,7 +17,6 @@ import (
engineapi "github.com/kyverno/kyverno/pkg/engine/api"
"github.com/kyverno/kyverno/pkg/openapi"
"github.com/spf13/cobra"
"sigs.k8s.io/controller-runtime/pkg/log"
)
func Command() *cobra.Command {

View file

@ -4,6 +4,7 @@ import (
"fmt"
"github.com/kyverno/kyverno/api/kyverno/v1beta1"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/log"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/output/pluralize"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/policy"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/resource"
@ -21,7 +22,6 @@ import (
"github.com/kyverno/kyverno/pkg/openapi"
policyvalidation "github.com/kyverno/kyverno/pkg/validation/policy"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"sigs.k8s.io/controller-runtime/pkg/log"
)
func runTest(openApiManager openapi.Manager, testCase test.TestCase, auditWarn bool) ([]engineapi.EngineResponse, error) {

View file

@ -0,0 +1,12 @@
package log
import "github.com/kyverno/kyverno/pkg/logging"
const loggerName = "kubectl-kyverno"
var Log = logging.WithName(loggerName)
func Configure() error {
logging.InitFlags(nil)
return logging.Setup(logging.TextFormat, 0)
}

View file

@ -6,7 +6,7 @@ import (
"os"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/commands"
"github.com/kyverno/kyverno/pkg/logging"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/log"
"github.com/spf13/cobra"
)
@ -29,8 +29,7 @@ func run() error {
}
func configureLogs(cli *cobra.Command) error {
logging.InitFlags(nil)
if err := logging.Setup(logging.TextFormat, 0); err != nil {
if err := log.Configure(); err != nil {
return err
}
cli.PersistentFlags().AddGoFlagSet(flag.CommandLine)

View file

@ -11,6 +11,7 @@ import (
kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1"
kyvernov1beta1 "github.com/kyverno/kyverno/api/kyverno/v1beta1"
valuesapi "github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/apis/values"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/log"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/policy/annotations"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/resource"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/source"
@ -25,7 +26,6 @@ import (
engineapi "github.com/kyverno/kyverno/pkg/engine/api"
"github.com/kyverno/kyverno/pkg/engine/jmespath"
"github.com/kyverno/kyverno/pkg/imageverifycache"
"github.com/kyverno/kyverno/pkg/logging"
kubeutils "github.com/kyverno/kyverno/pkg/utils/kube"
yamlv2 "gopkg.in/yaml.v2"
"k8s.io/api/admissionregistration/v1alpha1"
@ -35,8 +35,6 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema"
)
var log = logging.WithName("kubectl-kyverno")
type ResultCounts struct {
Pass int
Fail int
@ -100,7 +98,7 @@ func PrintMutatedOutput(mutateLogPath string, mutateLogPathIsDir bool, yaml stri
if _, err := f.Write([]byte(yaml)); err != nil {
closeErr := f.Close()
if closeErr != nil {
log.Error(closeErr, "failed to close file")
log.Log.Error(closeErr, "failed to close file")
}
return err
}
@ -398,7 +396,7 @@ func handleGeneratePolicy(generateResponse *engineapi.EngineResponse, policyCont
var newRuleResponse []engineapi.RuleResponse
for _, rule := range generateResponse.PolicyResponse.Rules {
genResource, err := c.ApplyGeneratePolicy(log.V(2), &policyContext, gr, []string{rule.Name()})
genResource, err := c.ApplyGeneratePolicy(log.Log.V(2), &policyContext, gr, []string{rule.Name()})
if err != nil {
return nil, err
}

View file

@ -10,6 +10,7 @@ import (
"github.com/go-git/go-billy/v5"
kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1"
valuesapi "github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/apis/values"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/log"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/resource"
"github.com/kyverno/kyverno/pkg/autogen"
"github.com/kyverno/kyverno/pkg/clients/dclient"
@ -84,7 +85,7 @@ func whenClusterIsTrue(resourceTypes []schema.GroupVersionKind, subresourceMap m
}
if lenOfResource >= len(resources) {
if policyReport {
log.V(3).Info(fmt.Sprintf("%s not found in cluster", resourcePath))
log.Log.V(3).Info(fmt.Sprintf("%s not found in cluster", resourcePath))
} else {
fmt.Printf("\n----------------------------------------------------------------------\nresource %s not found in cluster\n----------------------------------------------------------------------\n", resourcePath)
}
@ -101,7 +102,7 @@ func whenClusterIsFalse(resourcePaths []string, policyReport bool) ([]*unstructu
resourceBytes, err := resource.GetFileBytes(resourcePath)
if err != nil {
if policyReport {
log.V(3).Info(fmt.Sprintf("failed to load resources: %s.", resourcePath), "error", err)
log.Log.V(3).Info(fmt.Sprintf("failed to load resources: %s.", resourcePath), "error", err)
} else {
fmt.Printf("\n----------------------------------------------------------------------\nfailed to load resources: %s. \nerror: %s\n----------------------------------------------------------------------\n", resourcePath, err)
}
@ -259,7 +260,7 @@ func addGVKToResourceTypesMap(kind string, resourceTypesMap map[schema.GroupVers
group, version, kind, subresource := kubeutils.ParseKindSelector(kind)
gvrss, err := client.Discovery().FindResources(group, version, kind, subresource)
if err != nil {
log.Info("failed to find resource", "kind", kind, "error", err)
log.Log.Info("failed to find resource", "kind", kind, "error", err)
return
}
for parent, child := range gvrss {

View file

@ -6,6 +6,7 @@ import (
"strings"
kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/log"
sanitizederror "github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/utils/sanitizedError"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/utils/store"
"github.com/kyverno/kyverno/pkg/autogen"
@ -76,20 +77,20 @@ OuterLoop:
}
resPath := fmt.Sprintf("%s/%s/%s", c.Resource.GetNamespace(), c.Resource.GetKind(), c.Resource.GetName())
log.V(3).Info("applying policy on resource", "policy", c.Policy.GetName(), "resource", resPath)
log.Log.V(3).Info("applying policy on resource", "policy", c.Policy.GetName(), "resource", resPath)
resourceRaw, err := c.Resource.MarshalJSON()
if err != nil {
log.Error(err, "failed to marshal resource")
log.Log.Error(err, "failed to marshal resource")
}
updatedResource, err := kubeutils.BytesToUnstructured(resourceRaw)
if err != nil {
log.Error(err, "unable to convert raw resource to unstructured")
log.Log.Error(err, "unable to convert raw resource to unstructured")
}
if err != nil {
log.Error(err, "failed to load resource in context")
log.Log.Error(err, "failed to load resource in context")
}
cfg := config.NewDefaultConfiguration(false)
@ -137,7 +138,7 @@ OuterLoop:
cfg,
)
if err != nil {
log.Error(err, "failed to create policy context")
log.Log.Error(err, "failed to create policy context")
}
policyContext = policyContext.
@ -148,7 +149,7 @@ OuterLoop:
for key, value := range c.Variables {
err = policyContext.JSONContext().AddVariable(key, value)
if err != nil {
log.Error(err, "failed to add variable to context")
log.Log.Error(err, "failed to add variable to context")
}
}
@ -200,7 +201,7 @@ OuterLoop:
if !generateResponse.IsEmpty() {
newRuleResponse, err := handleGeneratePolicy(&generateResponse, *policyContext, c.RuleToCloneSourceResource)
if err != nil {
log.Error(err, "failed to apply generate policy")
log.Log.Error(err, "failed to apply generate policy")
} else {
generateResponse.PolicyResponse.Rules = newRuleResponse
}

View file

@ -9,6 +9,7 @@ import (
"github.com/go-git/go-billy/v5"
kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1"
valuesapi "github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/apis/values"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/log"
sanitizederror "github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/utils/sanitizedError"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/utils/store"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/values"
@ -45,7 +46,7 @@ func GetVariable(
if globalValMap != nil {
if _, ok := globalValMap["request.operation"]; !ok {
globalValMap["request.operation"] = "CREATE"
log.V(3).Info("Defaulting request.operation to CREATE")
log.Log.V(3).Info("Defaulting request.operation to CREATE")
}
}
@ -96,12 +97,12 @@ func getVariable(
if vals.GlobalValues == nil {
vals.GlobalValues = make(map[string]string)
vals.GlobalValues["request.operation"] = "CREATE"
log.V(3).Info("Defaulting request.operation to CREATE")
log.Log.V(3).Info("Defaulting request.operation to CREATE")
} else {
if val, ok := vals.GlobalValues["request.operation"]; ok {
if val == "" {
vals.GlobalValues["request.operation"] = "CREATE"
log.V(3).Info("Globally request.operation value provided by the user is empty, defaulting it to CREATE", "request.opearation: ", vals.GlobalValues)
log.Log.V(3).Info("Globally request.operation value provided by the user is empty, defaulting it to CREATE", "request.opearation: ", vals.GlobalValues)
}
}
}
@ -114,7 +115,7 @@ func getVariable(
if val, ok := r.Values["request.operation"]; ok {
if val == "" {
r.Values["request.operation"] = "CREATE"
log.V(3).Info("No request.operation found, defaulting it to CREATE", "policy", p.Name)
log.Log.V(3).Info("No request.operation found, defaulting it to CREATE", "policy", p.Name)
}
}
for variableInFile := range r.Values {