Skip to content

Commit

Permalink
fix: lint different versions (#1924) (#1925)
Browse files Browse the repository at this point in the history
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
Co-authored-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
  • Loading branch information
gcp-cherry-pick-bot[bot] and eddycharly authored Sep 6, 2024
1 parent 0c73b67 commit 6fce610
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 25 deletions.
18 changes: 14 additions & 4 deletions pkg/commands/lint/command.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package lint

import (
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"

"github.com/spf13/cobra"
"github.com/xeipuuv/gojsonschema"
"k8s.io/apimachinery/pkg/runtime/schema"
)

func Command() *cobra.Command {
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down
23 changes: 2 additions & 21 deletions pkg/commands/lint/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

0 comments on commit 6fce610

Please sign in to comment.