From 6fce6107f630097727494b25a4d2305f948119f9 Mon Sep 17 00:00:00 2001 From: "gcp-cherry-pick-bot[bot]" <98988430+gcp-cherry-pick-bot[bot]@users.noreply.github.com> Date: Fri, 6 Sep 2024 13:23:27 +0000 Subject: [PATCH] fix: lint different versions (#1924) (#1925) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Charles-Edouard Brétéché Co-authored-by: Charles-Edouard Brétéché --- pkg/commands/lint/command.go | 18 ++++++++++++++---- pkg/commands/lint/schema.go | 23 ++--------------------- 2 files changed, 16 insertions(+), 25 deletions(-) diff --git a/pkg/commands/lint/command.go b/pkg/commands/lint/command.go index 7b4f0c3e8..d0204e9dc 100644 --- a/pkg/commands/lint/command.go +++ b/pkg/commands/lint/command.go @@ -1,6 +1,7 @@ package lint import ( + "encoding/json" "fmt" "io" "os" @@ -8,6 +9,7 @@ import ( "github.com/spf13/cobra" "github.com/xeipuuv/gojsonschema" + "k8s.io/apimachinery/pkg/runtime/schema" ) func Command() *cobra.Command { @@ -40,16 +42,16 @@ func Command() *cobra.Command { return cmd } -func lintInput(input []byte, schema string, format string, writer io.Writer) error { +func lintInput(input []byte, kind string, format string, writer io.Writer) error { fmt.Fprintln(writer, "Processing input...") - if err := lintSchema(input, schema, format, writer); err != nil { + if err := lintSchema(input, kind, format, writer); err != nil { return err } fmt.Fprintln(writer, "The document is valid") return nil } -func lintSchema(input []byte, schema string, format string, writer io.Writer) error { +func lintSchema(input []byte, kind string, format string, writer io.Writer) error { processor, err := getProcessor(format, input) if err != nil { return err @@ -58,7 +60,15 @@ func lintSchema(input []byte, schema string, format string, writer io.Writer) er if err != nil { return err } - goschema, err := getScheme(schema) + var unstructured map[string]any + if err := json.Unmarshal(jsonInput, &unstructured); err != nil { + return err + } + gv, err := schema.ParseGroupVersion(unstructured["apiVersion"].(string)) + if err != nil { + return err + } + goschema, err := getScheme(kind, gv.Version) if err != nil { return err } diff --git a/pkg/commands/lint/schema.go b/pkg/commands/lint/schema.go index 5589fc1ed..0de5561c5 100644 --- a/pkg/commands/lint/schema.go +++ b/pkg/commands/lint/schema.go @@ -7,29 +7,10 @@ import ( "github.com/kyverno/chainsaw/pkg/data" ) -func getScheme(schema string) ([]byte, error) { - switch schema { - case "test": - return getTestSchema() - case "configuration": - return getConfigurationSchema() - default: - return nil, fmt.Errorf("unknown schema: %s", schema) - } -} - -func getTestSchema() ([]byte, error) { - schemasFs, err := data.Schemas() - if err != nil { - return nil, err - } - return fs.ReadFile(schemasFs, "test-chainsaw-v1alpha1.json") -} - -func getConfigurationSchema() ([]byte, error) { +func getScheme(kind string, version string) ([]byte, error) { schemasFs, err := data.Schemas() if err != nil { return nil, err } - return fs.ReadFile(schemasFs, "configuration-chainsaw-v1alpha1.json") + return fs.ReadFile(schemasFs, fmt.Sprintf("%s-chainsaw-%s.json", kind, version)) }