From d3e4fede02cdc1bd635e1b92312c3f2903b6ad85 Mon Sep 17 00:00:00 2001 From: Pooja Singh <36136335+NoSkillGirl@users.noreply.github.com> Date: Thu, 29 Apr 2021 23:11:15 +0530 Subject: [PATCH] Fix for commented yaml files in Kyverno CLI (#1849) * fix for commented policy yaml file Signed-off-by: NoSkillGirl * fix for commented resource yaml file Signed-off-by: NoSkillGirl --- pkg/kyverno/common/fetch.go | 4 ++++ pkg/utils/loadpolicy.go | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/pkg/kyverno/common/fetch.go b/pkg/kyverno/common/fetch.go index 5e650bbfb7..c67e7aca89 100644 --- a/pkg/kyverno/common/fetch.go +++ b/pkg/kyverno/common/fetch.go @@ -160,6 +160,10 @@ func GetResource(resourceBytes []byte) ([]*unstructured.Unstructured, error) { for _, resourceYaml := range files { resource, err := convertResourceToUnstructured(resourceYaml) if err != nil { + if strings.Contains(err.Error(), "Object 'Kind' is missing") { + log.Log.V(3).Info("skipping resource as kind not found") + continue + } getErrString = getErrString + err.Error() + "\n" } resources = append(resources, resource) diff --git a/pkg/utils/loadpolicy.go b/pkg/utils/loadpolicy.go index 1cb7003da9..58f39934e8 100644 --- a/pkg/utils/loadpolicy.go +++ b/pkg/utils/loadpolicy.go @@ -9,6 +9,7 @@ import ( v1 "github.com/kyverno/kyverno/pkg/api/kyverno/v1" "k8s.io/apimachinery/pkg/util/yaml" + "sigs.k8s.io/controller-runtime/pkg/log" ) // GetPolicy - extracts policies from YAML bytes @@ -29,6 +30,11 @@ func GetPolicy(bytes []byte) (clusterPolicies []*v1.ClusterPolicy, err error) { return nil, fmt.Errorf("failed to decode policy: %v", err) } + if policy.TypeMeta.Kind == "" { + log.Log.V(3).Info("skipping file as policy.TypeMeta.Kind not found") + continue + } + if !(policy.TypeMeta.Kind == "ClusterPolicy" || policy.TypeMeta.Kind == "Policy") { msg := fmt.Sprintf("resource %s/%s is not a Policy or a ClusterPolicy", policy.Kind, policy.Name) return nil, fmt.Errorf(msg)