Skip to content

Commit

Permalink
Merge pull request #1055 from grafana-operator/datasource-env-vars-re…
Browse files Browse the repository at this point in the history
…write

feat: advanced secret and configmap refs in data sources
  • Loading branch information
pb82 committed May 19, 2023
2 parents c07c0ef + d4158e3 commit 9435e83
Show file tree
Hide file tree
Showing 13 changed files with 512 additions and 64 deletions.
32 changes: 28 additions & 4 deletions api/v1beta1/grafanadatasource_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"fmt"
"time"

v1 "k8s.io/api/core/v1"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down Expand Up @@ -65,9 +67,9 @@ type GrafanaDatasourceSpec struct {
// +optional
Plugins PluginList `json:"plugins,omitempty"`

// secrets used for variable expansion
// environments variables from secrets or config maps
// +optional
Secrets []string `json:"secrets,omitempty"`
ValuesFrom []GrafanaDatasourceValueFrom `json:"valuesFrom,omitempty"`

// how often the datasource is refreshed, defaults to 24h if not set
// +optional
Expand All @@ -78,6 +80,20 @@ type GrafanaDatasourceSpec struct {
AllowCrossNamespaceImport *bool `json:"allowCrossNamespaceImport,omitempty"`
}

type GrafanaDatasourceValueFrom struct {
TargetPath string `json:"targetPath"`
ValueFrom GrafanaDatasourceValueFromSource `json:"valueFrom"`
}

type GrafanaDatasourceValueFromSource struct {
// Selects a key of a ConfigMap.
// +optional
ConfigMapKeyRef *v1.ConfigMapKeySelector `json:"configMapKeyRef,omitempty"`
// Selects a key of a Secret.
// +optional
SecretKeyRef *v1.SecretKeySelector `json:"secretKeyRef,omitempty"`
}

// GrafanaDatasourceStatus defines the observed state of GrafanaDatasource
type GrafanaDatasourceStatus struct {
Hash string `json:"hash,omitempty"`
Expand Down Expand Up @@ -121,8 +137,16 @@ func (in *GrafanaDatasource) Hash() string {
hash.Write([]byte(in.Spec.Datasource.User))
hash.Write([]byte(in.Spec.Datasource.URL))

for _, secret := range in.Spec.Secrets {
hash.Write([]byte(secret))
for _, valueRef := range in.Spec.ValuesFrom {
hash.Write([]byte(valueRef.TargetPath))
if valueRef.ValueFrom.ConfigMapKeyRef != nil {
hash.Write([]byte(valueRef.ValueFrom.ConfigMapKeyRef.Name))
hash.Write([]byte(valueRef.ValueFrom.ConfigMapKeyRef.Key))
}
if valueRef.ValueFrom.SecretKeyRef != nil {
hash.Write([]byte(valueRef.ValueFrom.SecretKeyRef.Name))
hash.Write([]byte(valueRef.ValueFrom.SecretKeyRef.Key))
}
}

if in.Spec.Datasource.BasicAuth != nil && *in.Spec.Datasource.BasicAuth {
Expand Down
51 changes: 47 additions & 4 deletions api/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 35 additions & 2 deletions bundle/manifests/grafana.integreatly.org_grafanadatasources.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,42 @@ spec:
type: array
resyncPeriod:
type: string
secrets:
valuesFrom:
items:
type: string
properties:
targetPath:
type: string
valueFrom:
properties:
configMapKeyRef:
properties:
key:
type: string
name:
type: string
optional:
type: boolean
required:
- key
type: object
x-kubernetes-map-type: atomic
secretKeyRef:
properties:
key:
type: string
name:
type: string
optional:
type: boolean
required:
- key
type: object
x-kubernetes-map-type: atomic
type: object
required:
- targetPath
- valueFrom
type: object
type: array
required:
- instanceSelector
Expand Down
37 changes: 35 additions & 2 deletions config/crd/bases/grafana.integreatly.org_grafanadatasources.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,42 @@ spec:
type: array
resyncPeriod:
type: string
secrets:
valuesFrom:
items:
type: string
properties:
targetPath:
type: string
valueFrom:
properties:
configMapKeyRef:
properties:
key:
type: string
name:
type: string
optional:
type: boolean
required:
- key
type: object
x-kubernetes-map-type: atomic
secretKeyRef:
properties:
key:
type: string
name:
type: string
optional:
type: boolean
required:
- key
type: object
x-kubernetes-map-type: atomic
type: object
required:
- targetPath
- valueFrom
type: object
type: array
required:
- instanceSelector
Expand Down
52 changes: 49 additions & 3 deletions config/grafana.integreatly.org_grafanadatasources.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,56 @@ spec:
description: how often the datasource is refreshed, defaults to 24h
if not set
type: string
secrets:
description: secrets used for variable expansion
valuesFrom:
description: environments variables from secrets or config maps
items:
type: string
properties:
targetPath:
type: string
valueFrom:
properties:
configMapKeyRef:
description: Selects a key of a ConfigMap.
properties:
key:
description: The key to select.
type: string
name:
description: 'Name of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names
TODO: Add other useful fields. apiVersion, kind, uid?'
type: string
optional:
description: Specify whether the ConfigMap or its key
must be defined
type: boolean
required:
- key
type: object
x-kubernetes-map-type: atomic
secretKeyRef:
description: Selects a key of a Secret.
properties:
key:
description: The key of the secret to select from. Must
be a valid secret key.
type: string
name:
description: 'Name of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names
TODO: Add other useful fields. apiVersion, kind, uid?'
type: string
optional:
description: Specify whether the Secret or its key must
be defined
type: boolean
required:
- key
type: object
x-kubernetes-map-type: atomic
type: object
required:
- targetPath
- valueFrom
type: object
type: array
required:
- instanceSelector
Expand Down

0 comments on commit 9435e83

Please sign in to comment.