From bcaa04cfaf3b7870f3b0a1864724bccd5c8d2ec5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Charles-Edouard=20Br=C3=A9t=C3=A9ch=C3=A9?= Date: Tue, 16 Jul 2024 11:26:49 +0200 Subject: [PATCH] chore: remove useless config validation (#1642) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Charles-Edouard Brétéché --- pkg/loaders/config/load.go | 15 +------ pkg/loaders/config/load_test.go | 15 +------ pkg/validation/config/cluster.go | 14 ------ pkg/validation/config/cluster_test.go | 41 ------------------ pkg/validation/config/configuration.go | 15 ------- pkg/validation/config/configuration_spec.go | 19 -------- pkg/validation/config/configuration_test.go | 48 --------------------- 7 files changed, 3 insertions(+), 164 deletions(-) delete mode 100644 pkg/validation/config/cluster.go delete mode 100644 pkg/validation/config/cluster_test.go delete mode 100644 pkg/validation/config/configuration.go delete mode 100644 pkg/validation/config/configuration_spec.go delete mode 100644 pkg/validation/config/configuration_test.go diff --git a/pkg/loaders/config/load.go b/pkg/loaders/config/load.go index 6e4e797a6..ed8edfeaa 100644 --- a/pkg/loaders/config/load.go +++ b/pkg/loaders/config/load.go @@ -16,7 +16,6 @@ import ( "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" - "k8s.io/apimachinery/pkg/util/validation/field" "k8s.io/client-go/openapi" "sigs.k8s.io/kubectl-validate/pkg/openapiclient" ) @@ -29,7 +28,6 @@ type ( splitter = func([]byte) ([][]byte, error) loaderFactory = func(openapi.Client) (loader.Loader, error) converter = func(schema.GroupVersionKind, unstructured.Unstructured) (*v1alpha2.Configuration, error) - validator = func(obj *v1alpha2.Configuration) field.ErrorList ) var ( @@ -64,10 +62,10 @@ func LoadBytes(content []byte) (*v1alpha2.Configuration, error) { } func Parse(content []byte) ([]*v1alpha2.Configuration, error) { - return parse(content, nil, nil, nil, nil) + return parse(content, nil, nil, nil) } -func parse(content []byte, splitter splitter, loaderFactory loaderFactory, converter converter, validator validator) ([]*v1alpha2.Configuration, error) { +func parse(content []byte, splitter splitter, loaderFactory loaderFactory, converter converter) ([]*v1alpha2.Configuration, error) { if splitter == nil { splitter = yaml.SplitDocuments } @@ -77,12 +75,6 @@ func parse(content []byte, splitter splitter, loaderFactory loaderFactory, conve if converter == nil { converter = defaultConverter } - if validator == nil { - // TODO: replace with schema validation - validator = func(obj *v1alpha2.Configuration) field.ErrorList { - return nil - } - } documents, err := splitter(content) if err != nil { return nil, err @@ -106,9 +98,6 @@ func parse(content []byte, splitter splitter, loaderFactory loaderFactory, conve if err != nil { return nil, err } - if err := validator(config).ToAggregate(); err != nil { - return nil, err - } configs = append(configs, config) } return configs, nil diff --git a/pkg/loaders/config/load_test.go b/pkg/loaders/config/load_test.go index 4c7490269..91772f6bf 100644 --- a/pkg/loaders/config/load_test.go +++ b/pkg/loaders/config/load_test.go @@ -14,7 +14,6 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime/schema" - "k8s.io/apimachinery/pkg/util/validation/field" "k8s.io/client-go/openapi" "k8s.io/utils/ptr" ) @@ -141,7 +140,6 @@ func Test_parse(t *testing.T) { splitter splitter loaderFactory loaderFactory converter converter - validator validator wantErr bool }{{ name: "default", @@ -185,21 +183,10 @@ func Test_parse(t *testing.T) { return nil, errors.New("converter") }, wantErr: true, - }, { - name: "validator error", - splitter: nil, - loaderFactory: nil, - converter: nil, - validator: func(obj *v1alpha2.Configuration) field.ErrorList { - return field.ErrorList{ - field.Invalid(nil, nil, ""), - } - }, - wantErr: true, }} for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - _, err := parse(content, tt.splitter, tt.loaderFactory, tt.converter, tt.validator) + _, err := parse(content, tt.splitter, tt.loaderFactory, tt.converter) if tt.wantErr { assert.Error(t, err) } else { diff --git a/pkg/validation/config/cluster.go b/pkg/validation/config/cluster.go deleted file mode 100644 index ee230d702..000000000 --- a/pkg/validation/config/cluster.go +++ /dev/null @@ -1,14 +0,0 @@ -package config - -import ( - "github.com/kyverno/chainsaw/pkg/apis/v1alpha1" - "k8s.io/apimachinery/pkg/util/validation/field" -) - -func ValidateCluster(path *field.Path, obj v1alpha1.Cluster) field.ErrorList { - var errs field.ErrorList - if obj.Kubeconfig == "" { - errs = append(errs, field.Required(path.Child("kubeconfig"), "a kubeconfig is required")) - } - return errs -} diff --git a/pkg/validation/config/cluster_test.go b/pkg/validation/config/cluster_test.go deleted file mode 100644 index 063de9a92..000000000 --- a/pkg/validation/config/cluster_test.go +++ /dev/null @@ -1,41 +0,0 @@ -package config - -import ( - "testing" - - "github.com/kyverno/chainsaw/pkg/apis/v1alpha1" - "github.com/stretchr/testify/assert" - "k8s.io/apimachinery/pkg/util/validation/field" -) - -func TestValidateCluster(t *testing.T) { - tests := []struct { - name string - path *field.Path - obj v1alpha1.Cluster - want field.ErrorList - }{{ - name: "empty", - path: field.NewPath("foo"), - want: field.ErrorList{ - &field.Error{ - Type: field.ErrorTypeRequired, - BadValue: "", - Field: "foo.kubeconfig", - Detail: "a kubeconfig is required", - }, - }, - }, { - name: "valid", - path: field.NewPath("foo"), - obj: v1alpha1.Cluster{ - Kubeconfig: "foo", - }, - }} - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got := ValidateCluster(tt.path, tt.obj) - assert.Equal(t, tt.want, got) - }) - } -} diff --git a/pkg/validation/config/configuration.go b/pkg/validation/config/configuration.go deleted file mode 100644 index dfdd82309..000000000 --- a/pkg/validation/config/configuration.go +++ /dev/null @@ -1,15 +0,0 @@ -package config - -import ( - "github.com/kyverno/chainsaw/pkg/apis/v1alpha1" - "k8s.io/apimachinery/pkg/util/validation/field" -) - -func ValidateConfiguration(obj *v1alpha1.Configuration) field.ErrorList { - var errs field.ErrorList - if obj != nil { - var path *field.Path - errs = append(errs, ValidateConfigurationSpec(path.Child("spec"), obj.Spec)...) - } - return errs -} diff --git a/pkg/validation/config/configuration_spec.go b/pkg/validation/config/configuration_spec.go deleted file mode 100644 index 67b608586..000000000 --- a/pkg/validation/config/configuration_spec.go +++ /dev/null @@ -1,19 +0,0 @@ -package config - -import ( - "github.com/kyverno/chainsaw/pkg/apis/v1alpha1" - "github.com/kyverno/chainsaw/pkg/validation/test" - "k8s.io/apimachinery/pkg/util/validation/field" -) - -func ValidateConfigurationSpec(path *field.Path, obj v1alpha1.ConfigurationSpec) field.ErrorList { - var errs field.ErrorList - path = path.Child("clusters") - for name, cluster := range obj.Clusters { - errs = append(errs, ValidateCluster(path.Key(name), cluster)...) - } - for i, catch := range obj.Catch { - errs = append(errs, test.ValidateCatchFinally(path.Child("catch").Index(i), catch)...) - } - return errs -} diff --git a/pkg/validation/config/configuration_test.go b/pkg/validation/config/configuration_test.go deleted file mode 100644 index 2cbc5377f..000000000 --- a/pkg/validation/config/configuration_test.go +++ /dev/null @@ -1,48 +0,0 @@ -package config - -import ( - "testing" - - "github.com/kyverno/chainsaw/pkg/apis/v1alpha1" - "github.com/stretchr/testify/assert" - "k8s.io/apimachinery/pkg/util/validation/field" -) - -func TestValidateConfiguration(t *testing.T) { - tests := []struct { - name string - obj *v1alpha1.Configuration - want field.ErrorList - }{{ - name: "null", - }, { - name: "empty", - obj: &v1alpha1.Configuration{}, - }, { - name: "with cluster", - obj: &v1alpha1.Configuration{ - Spec: v1alpha1.ConfigurationSpec{ - Clusters: map[string]v1alpha1.Cluster{ - "foo": { - Kubeconfig: "foo", - }, - }, - }, - }, - }, { - name: "with catch", - obj: &v1alpha1.Configuration{ - Spec: v1alpha1.ConfigurationSpec{ - Catch: []v1alpha1.CatchFinally{{ - Events: &v1alpha1.Events{}, - }}, - }, - }, - }} - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got := ValidateConfiguration(tt.obj) - assert.Equal(t, tt.want, got) - }) - } -}