Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add folderUID and folderRef in GrafanaDashboard CRD #1601

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions api/v1beta1/grafanadashboard_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ type GrafanaDashboardDatasource struct {
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.

// GrafanaDashboardSpec defines the desired state of GrafanaDashboard
// +kubebuilder:validation:XValidation:rule="(has(self.folderUID) && !(has(self.folderRef))) || (has(self.folderRef) && !(has(self.folderUID))) || !(has(self.folderRef) && (has(self.folderUID)))", message="Only one of folderUID or folderRef can be declared at the same time"
// +kubebuilder:validation:XValidation:rule="(has(self.folder) && !(has(self.folderRef) || has(self.folderUID))) || !(has(self.folder))", message="folder field cannot be set when folderUID or folderRef is already declared"
type GrafanaDashboardSpec struct {
// dashboard json
// +optional
Expand Down Expand Up @@ -85,6 +87,14 @@ type GrafanaDashboardSpec struct {
// +optional
FolderTitle string `json:"folder,omitempty"`

// find parent folder for dashboard
theSuess marked this conversation as resolved.
Show resolved Hide resolved
// +optional
FolderUID string `json:"folderUID" validate:"omitempty,uuid4"`
theSuess marked this conversation as resolved.
Show resolved Hide resolved

// find parent folder using GrafanaFolder CR reference
theSuess marked this conversation as resolved.
Show resolved Hide resolved
// +optional
FolderRef string `json:"folderRef,omitempty"`

// plugins
// +optional
Plugins PluginList `json:"plugins,omitempty"`
Expand Down Expand Up @@ -160,6 +170,8 @@ type GrafanaDashboardStatus struct {
// Last time the dashboard was resynced
LastResync metav1.Time `json:"lastResync,omitempty"`
UID string `json:"uid,omitempty"`

Conditions []metav1.Condition `json:"conditions,omitempty"`
}

//+kubebuilder:object:root=true
Expand All @@ -186,6 +198,31 @@ type GrafanaDashboardList struct {
Items []GrafanaDashboard `json:"items"`
}

// FolderRef implements FolderReferencer.
func (in *GrafanaDashboard) FolderRef() string {
return in.Spec.FolderRef
}

// FolderUID implements FolderReferencer.
func (in *GrafanaDashboard) FolderUID() string {
return in.Spec.FolderUID
}

// FolderNamespace implements FolderReferencer.
func (in *GrafanaDashboard) FolderNamespace() string {
return in.Namespace
}

// Conditions implements FolderReferencer.
func (in *GrafanaDashboard) Conditions() *[]metav1.Condition {
return &in.Status.Conditions
}

// CurrentGeneration implements FolderReferencer.
func (in *GrafanaDashboard) CurrentGeneration() int64 {
return in.Generation
}

func (in *GrafanaDashboard) Unchanged(hash string) bool {
return in.Status.Hash == hash
}
Expand Down
7 changes: 7 additions & 0 deletions api/v1beta1/zz_generated.deepcopy.go

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

84 changes: 84 additions & 0 deletions config/crd/bases/grafana.integreatly.org_grafanadashboards.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,12 @@ spec:
folder:
description: folder assignment for dashboard
type: string
folderRef:
description: find parent folder using GrafanaFolder CR reference
type: string
folderUID:
description: find parent folder for dashboard
type: string
grafanaCom:
description: grafana.com/dashboards
properties:
Expand Down Expand Up @@ -336,13 +342,91 @@ spec:
required:
- instanceSelector
type: object
x-kubernetes-validations:
- message: Only one of folderUID or folderRef can be declared at the same
time
rule: (has(self.folderUID) && !(has(self.folderRef))) || (has(self.folderRef)
&& !(has(self.folderUID))) || !(has(self.folderRef) && (has(self.folderUID)))
- message: folder field cannot be set when folderUID or folderRef is already
declared
rule: (has(self.folder) && !(has(self.folderRef) || has(self.folderUID)))
|| !(has(self.folder))
status:
description: GrafanaDashboardStatus defines the observed state of GrafanaDashboard
properties:
NoMatchingInstances:
description: The dashboard instanceSelector can't find matching grafana
instances
type: boolean
conditions:
items:
description: "Condition contains details for one aspect of the current
state of this API Resource.\n---\nThis struct is intended for
direct use as an array at the field path .status.conditions. For
example,\n\n\n\ttype FooStatus struct{\n\t // Represents the
observations of a foo's current state.\n\t // Known .status.conditions.type
are: \"Available\", \"Progressing\", and \"Degraded\"\n\t //
+patchMergeKey=type\n\t // +patchStrategy=merge\n\t // +listType=map\n\t
\ // +listMapKey=type\n\t Conditions []metav1.Condition `json:\"conditions,omitempty\"
patchStrategy:\"merge\" patchMergeKey:\"type\" protobuf:\"bytes,1,rep,name=conditions\"`\n\n\n\t
\ // other fields\n\t}"
properties:
lastTransitionTime:
description: |-
lastTransitionTime is the last time the condition transitioned from one status to another.
This should be when the underlying condition changed. If that is not known, then using the time when the API field changed is acceptable.
format: date-time
type: string
message:
description: |-
message is a human readable message indicating details about the transition.
This may be an empty string.
maxLength: 32768
type: string
observedGeneration:
description: |-
observedGeneration represents the .metadata.generation that the condition was set based upon.
For instance, if .metadata.generation is currently 12, but the .status.conditions[x].observedGeneration is 9, the condition is out of date
with respect to the current state of the instance.
format: int64
minimum: 0
type: integer
reason:
description: |-
reason contains a programmatic identifier indicating the reason for the condition's last transition.
Producers of specific condition types may define expected values and meanings for this field,
and whether the values are considered a guaranteed API.
The value should be a CamelCase string.
This field may not be empty.
maxLength: 1024
minLength: 1
pattern: ^[A-Za-z]([A-Za-z0-9_,:]*[A-Za-z0-9_])?$
type: string
status:
description: status of the condition, one of True, False, Unknown.
enum:
- "True"
- "False"
- Unknown
type: string
type:
description: |-
type of condition in CamelCase or in foo.example.com/CamelCase.
---
Many .condition.type values are consistent across resources like Available, but because arbitrary conditions can be
useful (see .node.status.conditions), the ability to deconflict is important.
The regex it matches is (dns1123SubdomainFmt/)?(qualifiedNameFmt)
maxLength: 316
pattern: ^([a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*/)?(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])$
type: string
required:
- lastTransitionTime
- message
- reason
- status
- type
type: object
type: array
contentCache:
format: byte
type: string
Expand Down
26 changes: 22 additions & 4 deletions controllers/dashboard_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import (
"github.com/grafana/grafana-operator/v5/controllers/fetchers"
"github.com/grafana/grafana-operator/v5/controllers/metrics"
kuberr "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"

"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/discovery"
Expand All @@ -56,8 +57,10 @@ import (
)

const (
initialSyncDelay = "10s"
syncBatchSize = 100
initialSyncDelay = "10s"
syncBatchSize = 100
conditionDashboardSynchronized = "DashboardSynchronized"
conditionErrorFetchingInstance = "ErrFetchingInstances"
theSuess marked this conversation as resolved.
Show resolved Hide resolved
)

// GrafanaDashboardReconciler reconciles a GrafanaDashboard object
Expand Down Expand Up @@ -192,10 +195,13 @@ func (r *GrafanaDashboardReconciler) Reconcile(ctx context.Context, req ctrl.Req

instances, err := r.GetMatchingDashboardInstances(ctx, cr, r.Client)
if err != nil {
setNoMatchingInstance(&cr.Status.Conditions, cr.Generation, conditionErrorFetchingInstance, fmt.Sprintf("error occurred during fetching of instances: %s", err.Error()))
meta.RemoveStatusCondition(&cr.Status.Conditions, conditionDashboardSynchronized)
controllerLog.Error(err, "could not find matching instances", "name", cr.Name, "namespace", cr.Namespace)
return ctrl.Result{RequeueAfter: RequeueDelay}, err
}

removeNoMatchingInstance(&cr.Status.Conditions)
controllerLog.Info("found matching Grafana instances for dashboard", "count", len(instances.Items))

dashboardJson, err := r.fetchDashboardJson(ctx, cr)
Expand Down Expand Up @@ -232,6 +238,7 @@ func (r *GrafanaDashboardReconciler) Reconcile(ctx context.Context, req ctrl.Req
}

success := true
applyErrors := make(map[string]string)
for _, grafana := range instances.Items {
// check if this is a cross namespace import
if grafana.Namespace != cr.Namespace && !cr.IsAllowCrossNamespaceImport() {
Expand Down Expand Up @@ -262,9 +269,13 @@ func (r *GrafanaDashboardReconciler) Reconcile(ctx context.Context, req ctrl.Req
err = r.onDashboardCreated(ctx, &grafana, cr, dashboardModel, hash)
if err != nil {
controllerLog.Error(err, "error reconciling dashboard", "dashboard", cr.Name, "grafana", grafana.Name)
applyErrors[fmt.Sprintf("%s/%s", grafana.Namespace, grafana.Name)] = err.Error()
success = false
}

condition := buildSynchronizedCondition("Dashboard", conditionDashboardSynchronized, cr.Generation, applyErrors, len(instances.Items))
meta.SetStatusCondition(&cr.Status.Conditions, condition)

if grafana.Spec.Preferences != nil && uid == grafana.Spec.Preferences.HomeDashboardUID {
err = r.UpdateHomeDashboard(ctx, grafana, uid, cr)
if err != nil {
Expand Down Expand Up @@ -370,9 +381,16 @@ func (r *GrafanaDashboardReconciler) onDashboardCreated(ctx context.Context, gra
return err
}

folderUID, err := r.GetOrCreateFolder(grafanaClient, cr)
folderUID, err := getFolderUID(ctx, r.Client, cr)
if err != nil {
return kuberr.NewInternalError(err)
return err
}

if folderUID == "" {
folderUID, err = r.GetOrCreateFolder(grafanaClient, cr)
if err != nil {
return err
}
}

uid := fmt.Sprintf("%s", dashboardModel["uid"])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,12 @@ spec:
folder:
description: folder assignment for dashboard
type: string
folderRef:
description: find parent folder using GrafanaFolder CR reference
type: string
folderUID:
description: find parent folder for dashboard
type: string
grafanaCom:
description: grafana.com/dashboards
properties:
Expand Down Expand Up @@ -336,13 +342,91 @@ spec:
required:
- instanceSelector
type: object
x-kubernetes-validations:
- message: Only one of folderUID or folderRef can be declared at the same
time
rule: (has(self.folderUID) && !(has(self.folderRef))) || (has(self.folderRef)
&& !(has(self.folderUID))) || !(has(self.folderRef) && (has(self.folderUID)))
- message: folder field cannot be set when folderUID or folderRef is already
declared
rule: (has(self.folder) && !(has(self.folderRef) || has(self.folderUID)))
|| !(has(self.folder))
status:
description: GrafanaDashboardStatus defines the observed state of GrafanaDashboard
properties:
NoMatchingInstances:
description: The dashboard instanceSelector can't find matching grafana
instances
type: boolean
conditions:
items:
description: "Condition contains details for one aspect of the current
state of this API Resource.\n---\nThis struct is intended for
direct use as an array at the field path .status.conditions. For
example,\n\n\n\ttype FooStatus struct{\n\t // Represents the
observations of a foo's current state.\n\t // Known .status.conditions.type
are: \"Available\", \"Progressing\", and \"Degraded\"\n\t //
+patchMergeKey=type\n\t // +patchStrategy=merge\n\t // +listType=map\n\t
\ // +listMapKey=type\n\t Conditions []metav1.Condition `json:\"conditions,omitempty\"
patchStrategy:\"merge\" patchMergeKey:\"type\" protobuf:\"bytes,1,rep,name=conditions\"`\n\n\n\t
\ // other fields\n\t}"
properties:
lastTransitionTime:
description: |-
lastTransitionTime is the last time the condition transitioned from one status to another.
This should be when the underlying condition changed. If that is not known, then using the time when the API field changed is acceptable.
format: date-time
type: string
message:
description: |-
message is a human readable message indicating details about the transition.
This may be an empty string.
maxLength: 32768
type: string
observedGeneration:
description: |-
observedGeneration represents the .metadata.generation that the condition was set based upon.
For instance, if .metadata.generation is currently 12, but the .status.conditions[x].observedGeneration is 9, the condition is out of date
with respect to the current state of the instance.
format: int64
minimum: 0
type: integer
reason:
description: |-
reason contains a programmatic identifier indicating the reason for the condition's last transition.
Producers of specific condition types may define expected values and meanings for this field,
and whether the values are considered a guaranteed API.
The value should be a CamelCase string.
This field may not be empty.
maxLength: 1024
minLength: 1
pattern: ^[A-Za-z]([A-Za-z0-9_,:]*[A-Za-z0-9_])?$
type: string
status:
description: status of the condition, one of True, False, Unknown.
enum:
- "True"
- "False"
- Unknown
type: string
type:
description: |-
type of condition in CamelCase or in foo.example.com/CamelCase.
---
Many .condition.type values are consistent across resources like Available, but because arbitrary conditions can be
useful (see .node.status.conditions), the ability to deconflict is important.
The regex it matches is (dns1123SubdomainFmt/)?(qualifiedNameFmt)
maxLength: 316
pattern: ^([a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*/)?(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])$
type: string
required:
- lastTransitionTime
- message
- reason
- status
- type
type: object
type: array
contentCache:
format: byte
type: string
Expand Down
Loading
Loading