diff --git a/api/v1beta1/grafanafolder_types.go b/api/v1beta1/grafanafolder_types.go index ebc1c943e..87e5de164 100644 --- a/api/v1beta1/grafanafolder_types.go +++ b/api/v1beta1/grafanafolder_types.go @@ -66,6 +66,9 @@ type GrafanaFolderStatus struct { NoMatchingInstances bool `json:"NoMatchingInstances,omitempty"` // Last time the folder was resynced LastResync metav1.Time `json:"lastResync,omitempty"` + // UID of the parent folder where the folder is created. + // Will be empty if the folder is deployed at the root level + ParentFolderUID string `json:"parentFolderUID,omitempty"` } //+kubebuilder:object:root=true @@ -115,6 +118,10 @@ func (in *GrafanaFolder) Unchanged() bool { return in.Hash() == in.Status.Hash } +func (in *GrafanaFolder) Moved() bool { + return in.Spec.ParentFolderUID != in.Status.ParentFolderUID +} + func (in *GrafanaFolder) IsAllowCrossNamespaceImport() bool { if in.Spec.AllowCrossNamespaceImport != nil { return *in.Spec.AllowCrossNamespaceImport diff --git a/config/crd/bases/grafana.integreatly.org_grafanafolders.yaml b/config/crd/bases/grafana.integreatly.org_grafanafolders.yaml index 6e882b6a1..db63e0320 100644 --- a/config/crd/bases/grafana.integreatly.org_grafanafolders.yaml +++ b/config/crd/bases/grafana.integreatly.org_grafanafolders.yaml @@ -134,6 +134,11 @@ spec: description: Last time the folder was resynced format: date-time type: string + parentFolderUID: + description: |- + UID of the parent folder where the folder is created. + Will be empty if the folder is deployed at the root level + type: string type: object type: object served: true diff --git a/controllers/grafanafolder_controller.go b/controllers/grafanafolder_controller.go index 53e789e42..9b4eca188 100644 --- a/controllers/grafanafolder_controller.go +++ b/controllers/grafanafolder_controller.go @@ -301,7 +301,7 @@ func (r *GrafanaFolderReconciler) onFolderCreated(ctx context.Context, grafana * } // always update after resync period has elapsed even if cr is unchanged. - if exists && cr.Unchanged() && !cr.ResyncPeriodHasElapsed() { + if exists && cr.Unchanged() && !cr.ResyncPeriodHasElapsed() && !cr.Moved() { return nil } @@ -327,6 +327,15 @@ func (r *GrafanaFolderReconciler) onFolderCreated(ctx context.Context, grafana * return err } } + + if cr.Moved() { + _, err = grafanaClient.Folders.MoveFolder(remoteUID, &models.MoveFolderCommand{ //nolint + ParentUID: cr.Spec.ParentFolderUID, + }) + if err != nil { + return err + } + } } else { body := &models.CreateFolderCommand{ Title: title, @@ -365,6 +374,7 @@ func (r *GrafanaFolderReconciler) onFolderCreated(ctx context.Context, grafana * func (r *GrafanaFolderReconciler) UpdateStatus(ctx context.Context, cr *grafanav1beta1.GrafanaFolder) error { cr.Status.Hash = cr.Hash() + cr.Status.ParentFolderUID = cr.Spec.ParentFolderUID return r.Client.Status().Update(ctx, cr) } @@ -375,7 +385,7 @@ func (r *GrafanaFolderReconciler) Exists(client *genapi.GrafanaHTTPAPI, cr *graf page := int64(1) limit := int64(10000) for { - params := folders.NewGetFoldersParams().WithPage(&page).WithLimit(&limit).WithParentUID(&cr.Spec.ParentFolderUID) + params := folders.NewGetFoldersParams().WithPage(&page).WithLimit(&limit).WithParentUID(&cr.Status.ParentFolderUID) foldersResp, err := client.Folders.GetFolders(params) if err != nil { diff --git a/deploy/helm/grafana-operator/crds/grafana.integreatly.org_grafanafolders.yaml b/deploy/helm/grafana-operator/crds/grafana.integreatly.org_grafanafolders.yaml index 6e882b6a1..db63e0320 100644 --- a/deploy/helm/grafana-operator/crds/grafana.integreatly.org_grafanafolders.yaml +++ b/deploy/helm/grafana-operator/crds/grafana.integreatly.org_grafanafolders.yaml @@ -134,6 +134,11 @@ spec: description: Last time the folder was resynced format: date-time type: string + parentFolderUID: + description: |- + UID of the parent folder where the folder is created. + Will be empty if the folder is deployed at the root level + type: string type: object type: object served: true diff --git a/deploy/kustomize/base/crds.yaml b/deploy/kustomize/base/crds.yaml index bd98ee835..2f28a10b5 100644 --- a/deploy/kustomize/base/crds.yaml +++ b/deploy/kustomize/base/crds.yaml @@ -1279,6 +1279,11 @@ spec: description: Last time the folder was resynced format: date-time type: string + parentFolderUID: + description: |- + UID of the parent folder where the folder is created. + Will be empty if the folder is deployed at the root level + type: string type: object type: object served: true diff --git a/docs/docs/api.md b/docs/docs/api.md index b60b1c8a5..3fce0dd94 100644 --- a/docs/docs/api.md +++ b/docs/docs/api.md @@ -2629,6 +2629,14 @@ Important: Run "make" to regenerate code after modifying this file
Format: date-time
false + + parentFolderUID + string + + UID of the parent folder where the folder is created. +Will be empty if the folder is deployed at the root level
+ + false