From d913283048e931f626290eadbc1f09c3d2c5ca2b Mon Sep 17 00:00:00 2001 From: Matheus Moraes Date: Wed, 17 May 2023 10:14:57 -0300 Subject: [PATCH] fix `params` type in CustomCheckSpec --- api/zora/v1alpha1/customcheck_types.go | 30 ++++++++++++------- api/zora/v1alpha1/zz_generated.deepcopy.go | 7 ++++- .../crds/zora.undistro.io_customchecks.yaml | 3 +- .../bases/zora.undistro.io_customchecks.yaml | 3 +- 4 files changed, 30 insertions(+), 13 deletions(-) diff --git a/api/zora/v1alpha1/customcheck_types.go b/api/zora/v1alpha1/customcheck_types.go index 86df51fe..c1c2d4ad 100644 --- a/api/zora/v1alpha1/customcheck_types.go +++ b/api/zora/v1alpha1/customcheck_types.go @@ -15,28 +15,38 @@ package v1alpha1 import ( + "encoding/json" "fmt" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" - marvin "github.com/undistro/marvin/pkg/types" + apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) // CustomCheckSpec defines the desired state of CustomCheck type CustomCheckSpec struct { - Match Match `json:"match"` - Validations []Validation `json:"validations"` - Params unstructured.Unstructured `json:"params,omitempty"` - Message string `json:"message"` - Category string `json:"category"` - URL string `json:"url,omitempty"` + Match Match `json:"match"` + Validations []Validation `json:"validations"` + Message string `json:"message"` + Category string `json:"category"` + URL string `json:"url,omitempty"` + + // Parameters to be used in validations + Params *apiextensionsv1.JSON `json:"params,omitempty"` //+kubebuilder:validation:Type=string //+kubebuilder:validation:Enum=Low;Medium;High Severity string `json:"severity"` } +func (r *CustomCheck) GetParams() map[string]interface{} { + var values map[string]interface{} + if r.Spec.Params != nil { + _ = json.Unmarshal(r.Spec.Params.Raw, &values) + } + return values +} + type Match marvin.Match type Validation marvin.Validation @@ -75,7 +85,7 @@ func (r *CustomCheck) ToMarvin() *marvin.Check { ID: r.Name, Match: marvin.Match(r.Spec.Match), Validations: mv, - Params: r.Spec.Params.Object, + Params: r.GetParams(), Severity: marvin.ParseSeverity(r.Spec.Severity), Message: r.Spec.Message, Labels: map[string]string{"category": r.Spec.Category, "url": r.Spec.URL}, diff --git a/api/zora/v1alpha1/zz_generated.deepcopy.go b/api/zora/v1alpha1/zz_generated.deepcopy.go index 23ed992a..00c3f71c 100644 --- a/api/zora/v1alpha1/zz_generated.deepcopy.go +++ b/api/zora/v1alpha1/zz_generated.deepcopy.go @@ -23,6 +23,7 @@ import ( "github.com/undistro/marvin/pkg/types" "github.com/undistro/zora/pkg/discovery" "k8s.io/api/core/v1" + apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" runtime "k8s.io/apimachinery/pkg/runtime" ) @@ -452,7 +453,11 @@ func (in *CustomCheckSpec) DeepCopyInto(out *CustomCheckSpec) { *out = make([]Validation, len(*in)) copy(*out, *in) } - in.Params.DeepCopyInto(&out.Params) + if in.Params != nil { + in, out := &in.Params, &out.Params + *out = new(apiextensionsv1.JSON) + (*in).DeepCopyInto(*out) + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new CustomCheckSpec. diff --git a/charts/zora/crds/zora.undistro.io_customchecks.yaml b/charts/zora/crds/zora.undistro.io_customchecks.yaml index 9ceb2b5b..1a94916f 100644 --- a/charts/zora/crds/zora.undistro.io_customchecks.yaml +++ b/charts/zora/crds/zora.undistro.io_customchecks.yaml @@ -85,7 +85,8 @@ spec: message: type: string params: - type: object + description: Parameters to be used in validations + x-kubernetes-preserve-unknown-fields: true severity: enum: - Low diff --git a/config/crd/bases/zora.undistro.io_customchecks.yaml b/config/crd/bases/zora.undistro.io_customchecks.yaml index a50ab37d..d8f2bdfd 100644 --- a/config/crd/bases/zora.undistro.io_customchecks.yaml +++ b/config/crd/bases/zora.undistro.io_customchecks.yaml @@ -71,7 +71,8 @@ spec: message: type: string params: - type: object + description: Parameters to be used in validations + x-kubernetes-preserve-unknown-fields: true severity: enum: - Low