From c234a9a71590bfe5a146393d7f13ef57f450a4b9 Mon Sep 17 00:00:00 2001 From: "christophe.boucharlat" Date: Thu, 14 Nov 2024 10:56:38 +0100 Subject: [PATCH] fix(grafana.spec.client): changed headers format to a map of key value to simplify syntax Signed-off-by: christophe.boucharlat --- api/v1beta1/grafana_types.go | 12 +----- api/v1beta1/zz_generated.deepcopy.go | 23 +++-------- .../grafana.integreatly.org_grafanas.yaml | 17 ++------ controllers/client/grafana_client.go | 15 +------ controllers/client/http_client.go | 2 +- .../grafana.integreatly.org_grafanas.yaml | 17 ++------ deploy/kustomize/base/crds.yaml | 17 ++------ docs/docs/api.md | 40 ++----------------- 8 files changed, 24 insertions(+), 119 deletions(-) diff --git a/api/v1beta1/grafana_types.go b/api/v1beta1/grafana_types.go index 4927eed72..9d0155774 100644 --- a/api/v1beta1/grafana_types.go +++ b/api/v1beta1/grafana_types.go @@ -119,17 +119,9 @@ type GrafanaClient struct { // TLS Configuration used to talk with the grafana instance. // +optional TLS *TLSConfig `json:"tls,omitempty"` - // Optional list of Header configuration objects that specify headers key and their value. + // Optional map of header key and value. // +optional - Headers *[]Header `json:"headers,omitempty"` -} - -// Header specifies http.Header key and value -type Header struct { - // Header name - Key string `json:"key,omitempty"` - // Header value - Value string `json:"value,omitempty"` + Headers *map[string]string `json:"headers,omitempty"` } // GrafanaPreferences holds Grafana preferences API settings diff --git a/api/v1beta1/zz_generated.deepcopy.go b/api/v1beta1/zz_generated.deepcopy.go index d461cc650..e1f2ff625 100644 --- a/api/v1beta1/zz_generated.deepcopy.go +++ b/api/v1beta1/zz_generated.deepcopy.go @@ -555,11 +555,13 @@ func (in *GrafanaClient) DeepCopyInto(out *GrafanaClient) { } if in.Headers != nil { in, out := &in.Headers, &out.Headers - *out = new([]Header) + *out = new(map[string]string) if **in != nil { in, out := *in, *out - *out = make([]Header, len(*in)) - copy(*out, *in) + *out = make(map[string]string, len(*in)) + for key, val := range *in { + (*out)[key] = val + } } } } @@ -1533,21 +1535,6 @@ func (in *GrafanaStatus) DeepCopy() *GrafanaStatus { return out } -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *Header) DeepCopyInto(out *Header) { - *out = *in -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Header. -func (in *Header) DeepCopy() *Header { - if in == nil { - return nil - } - out := new(Header) - in.DeepCopyInto(out) - return out -} - // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *IngressNetworkingV1) DeepCopyInto(out *IngressNetworkingV1) { *out = *in diff --git a/config/crd/bases/grafana.integreatly.org_grafanas.yaml b/config/crd/bases/grafana.integreatly.org_grafanas.yaml index bdcfa2e7d..874c87c98 100644 --- a/config/crd/bases/grafana.integreatly.org_grafanas.yaml +++ b/config/crd/bases/grafana.integreatly.org_grafanas.yaml @@ -59,19 +59,10 @@ spec: grafana instance. properties: headers: - description: Optional list of Header configuration objects that - specify headers key and their value. - items: - description: Header specifies http.Header key and value - properties: - key: - description: Header name - type: string - value: - description: Header value - type: string - type: object - type: array + additionalProperties: + type: string + description: Optional map of header key and value. + type: object preferIngress: description: If the operator should send it's request through the grafana instances ingress object instead of through the diff --git a/controllers/client/grafana_client.go b/controllers/client/grafana_client.go index 563e9a7b6..0ef47e556 100644 --- a/controllers/client/grafana_client.go +++ b/controllers/client/grafana_client.go @@ -142,7 +142,7 @@ func NewGeneratedGrafanaClient(ctx context.Context, c client.Client, grafana *v1 transport := NewInstrumentedRoundTripper(grafana.Name, metrics.GrafanaApiRequests, grafana.IsExternal(), tlsConfig) if grafana.Spec.Client.Headers != nil { - transport.(*instrumentedRoundTripper).addHeaders(extractClientHeaders(grafana)) + transport.(*instrumentedRoundTripper).addHeaders(*grafana.Spec.Client.Headers) } client := &http.Client{ @@ -169,16 +169,3 @@ func NewGeneratedGrafanaClient(ctx context.Context, c client.Client, grafana *v1 return cl, nil } - -func extractClientHeaders(grafana *v1beta1.Grafana) map[string]string { - if grafana.Spec.Client.Headers == nil { - return nil - } - - headers := make(map[string]string, len(*grafana.Spec.Client.Headers)) - for _, h := range *grafana.Spec.Client.Headers { - headers[h.Key] = h.Value - } - - return headers -} diff --git a/controllers/client/http_client.go b/controllers/client/http_client.go index 20f61b929..c5a8b7aa7 100644 --- a/controllers/client/http_client.go +++ b/controllers/client/http_client.go @@ -28,7 +28,7 @@ func NewHTTPClient(ctx context.Context, c client.Client, grafana *v1beta1.Grafan transport := NewInstrumentedRoundTripper(grafana.Name, metrics.GrafanaApiRequests, grafana.IsExternal(), tlsConfig) if grafana.Spec.Client.Headers != nil { - transport.(*instrumentedRoundTripper).addHeaders(extractClientHeaders(grafana)) + transport.(*instrumentedRoundTripper).addHeaders(*grafana.Spec.Client.Headers) } return &http.Client{ diff --git a/deploy/helm/grafana-operator/crds/grafana.integreatly.org_grafanas.yaml b/deploy/helm/grafana-operator/crds/grafana.integreatly.org_grafanas.yaml index bdcfa2e7d..874c87c98 100644 --- a/deploy/helm/grafana-operator/crds/grafana.integreatly.org_grafanas.yaml +++ b/deploy/helm/grafana-operator/crds/grafana.integreatly.org_grafanas.yaml @@ -59,19 +59,10 @@ spec: grafana instance. properties: headers: - description: Optional list of Header configuration objects that - specify headers key and their value. - items: - description: Header specifies http.Header key and value - properties: - key: - description: Header name - type: string - value: - description: Header value - type: string - type: object - type: array + additionalProperties: + type: string + description: Optional map of header key and value. + type: object preferIngress: description: If the operator should send it's request through the grafana instances ingress object instead of through the diff --git a/deploy/kustomize/base/crds.yaml b/deploy/kustomize/base/crds.yaml index 1bfe9e018..083886bdb 100644 --- a/deploy/kustomize/base/crds.yaml +++ b/deploy/kustomize/base/crds.yaml @@ -1883,19 +1883,10 @@ spec: grafana instance. properties: headers: - description: Optional list of Header configuration objects that - specify headers key and their value. - items: - description: Header specifies http.Header key and value - properties: - key: - description: Header name - type: string - value: - description: Header value - type: string - type: object - type: array + additionalProperties: + type: string + description: Optional map of header key and value. + type: object preferIngress: description: If the operator should send it's request through the grafana instances ingress object instead of through the diff --git a/docs/docs/api.md b/docs/docs/api.md index 3fc815d09..03bc3ca44 100644 --- a/docs/docs/api.md +++ b/docs/docs/api.md @@ -3785,10 +3785,10 @@ Client defines how the grafana-operator talks to the grafana instance. - headers - []object + headers + map[string]string - Optional list of Header configuration objects that specify headers key and their value.
+ Optional map of header key and value.
false @@ -3818,40 +3818,6 @@ Client defines how the grafana-operator talks to the grafana instance. -### Grafana.spec.client.headers[index] -[↩ Parent](#grafanaspecclient) - - - -Header specifies http.Header key and value - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescriptionRequired
keystring - Header name
-
false
valuestring - Header value
-
false
- - ### Grafana.spec.client.tls [↩ Parent](#grafanaspecclient)