From fff2d40292b0a0417f7121c9a1053e5e823940e3 Mon Sep 17 00:00:00 2001 From: David van der Spek Date: Fri, 14 Jul 2023 12:30:09 +0200 Subject: [PATCH] fix: use enum for BlockedQueryType Signed-off-by: David van der Spek --- .../v1alpha1/loki_limit_types.go | 75 ++++++++++++++++++- .../v1alpha1/zz_generated.deepcopy.go | 27 ++++++- .../observability.traceshield.io_tenants.yaml | 10 ++- 3 files changed, 105 insertions(+), 7 deletions(-) diff --git a/api/observability/v1alpha1/loki_limit_types.go b/api/observability/v1alpha1/loki_limit_types.go index 8e92de8..c35f116 100644 --- a/api/observability/v1alpha1/loki_limit_types.go +++ b/api/observability/v1alpha1/loki_limit_types.go @@ -1,6 +1,11 @@ package v1alpha1 import ( + "fmt" + "io" + "strconv" + "strings" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -212,8 +217,74 @@ type BlockedQuery struct { // +kubebuilder:validation:Optional Hash *uint32 `yaml:"hash,omitempty" json:"hash,omitempty"` // +kubebuilder:validation:Optional - // +kubebuilder:validation:Type=string - Types *string `yaml:"types,omitempty" json:"types,omitempty"` // TODO: add validation that the string is a comma separated list of the types metric, filter and limited + Types BlockedQueryTypes `yaml:"types,omitempty" json:"types,omitempty"` +} + +type BlockedQueryTypes []BlockedQueryType + +// BlockedQueryType is the type of blocked query +// +kubebuilder:validation:Enum=metric;filter;limited +type BlockedQueryType string + +const ( + BlockedQueryTypeMetric BlockedQueryType = "metric" + BlockedQueryTypeFilter BlockedQueryType = "filter" + BlockedQueryTypeLimited BlockedQueryType = "limited" +) + +func (e BlockedQueryType) IsValid() bool { + switch e { + case BlockedQueryTypeMetric, BlockedQueryTypeFilter, BlockedQueryTypeLimited: + return true + } + return false +} + +func (e BlockedQueryType) String() string { + return string(e) +} + +func (e *BlockedQueryType) UnmarshalGQL(v interface{}) error { + str, ok := v.(string) + if !ok { + return fmt.Errorf("enums must be strings") + } + + *e = BlockedQueryType(str) + if !e.IsValid() { + return fmt.Errorf("%s is not a valid RelabelAction", str) + } + return nil +} + +func (e BlockedQueryType) MarshalGQL(w io.Writer) { + fmt.Fprint(w, strconv.Quote(e.String())) +} + +// String implements the fmt.Stringer interface. +func (t BlockedQueryTypes) String() string { + var s []string + for _, v := range t { + s = append(s, string(v)) + } + return strings.Join(s, ",") +} + +// MarshalYAML implements the yaml.Marshaler interface. +func (t BlockedQueryTypes) MarshalYAML() (interface{}, error) { + return t.String(), nil +} + +// UnmarshalYAML implements the yaml.Unmarshaler interface. +func (t *BlockedQueryTypes) UnmarshalYAML(unmarshal func(interface{}) error) error { + var s string + if err := unmarshal(&s); err != nil { + return err + } + for _, v := range strings.Split(s, ",") { + *t = append(*t, BlockedQueryType(v)) + } + return nil } type BlockedQueryInput BlockedQuery diff --git a/api/observability/v1alpha1/zz_generated.deepcopy.go b/api/observability/v1alpha1/zz_generated.deepcopy.go index a1a2544..22f397d 100644 --- a/api/observability/v1alpha1/zz_generated.deepcopy.go +++ b/api/observability/v1alpha1/zz_generated.deepcopy.go @@ -48,8 +48,8 @@ func (in *BlockedQuery) DeepCopyInto(out *BlockedQuery) { } if in.Types != nil { in, out := &in.Types, &out.Types - *out = new(string) - **out = **in + *out = make(BlockedQueryTypes, len(*in)) + copy(*out, *in) } } @@ -83,8 +83,8 @@ func (in *BlockedQueryInput) DeepCopyInto(out *BlockedQueryInput) { } if in.Types != nil { in, out := &in.Types, &out.Types - *out = new(string) - **out = **in + *out = make(BlockedQueryTypes, len(*in)) + copy(*out, *in) } } @@ -98,6 +98,25 @@ func (in *BlockedQueryInput) DeepCopy() *BlockedQueryInput { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in BlockedQueryTypes) DeepCopyInto(out *BlockedQueryTypes) { + { + in := &in + *out = make(BlockedQueryTypes, len(*in)) + copy(*out, *in) + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new BlockedQueryTypes. +func (in BlockedQueryTypes) DeepCopy() BlockedQueryTypes { + if in == nil { + return nil + } + out := new(BlockedQueryTypes) + in.DeepCopyInto(out) + return *out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *Config) DeepCopyInto(out *Config) { *out = *in diff --git a/config/crd/bases/observability.traceshield.io_tenants.yaml b/config/crd/bases/observability.traceshield.io_tenants.yaml index 9da243c..216961c 100644 --- a/config/crd/bases/observability.traceshield.io_tenants.yaml +++ b/config/crd/bases/observability.traceshield.io_tenants.yaml @@ -53,7 +53,15 @@ spec: regex: type: boolean types: - type: string + items: + description: BlockedQueryType is the type of blocked + query + enum: + - metric + - filter + - limited + type: string + type: array type: object type: array cardinality_limit: