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 parentFolderUID field in GrafanaFolder CRD #1566

Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 11 additions & 0 deletions api/v1beta1/grafanafolder_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ type GrafanaFolderSpec struct {
// +optional
AllowCrossNamespaceImport *bool `json:"allowCrossNamespaceImport,omitempty"`

// UID of the folder in which the current folder should be created
// +optional
ParentFolderUID string `json:"parentFolderUID,omitempty"`

// how often the folder is synced, defaults to 5m if not set
// +optional
// +kubebuilder:validation:Type=string
Expand All @@ -62,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
Expand Down Expand Up @@ -111,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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ spec:
x-kubernetes-validations:
- message: Value is immutable
rule: self == oldSelf
parentFolderUID:
description: UID of the folder in which the current folder should
be created
type: string
permissions:
description: raw json with folder permissions
type: string
Expand Down Expand Up @@ -130,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
Expand Down
26 changes: 20 additions & 6 deletions controllers/grafanafolder_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -327,11 +327,23 @@ 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 {
folderResp, err := grafanaClient.Folders.CreateFolder(&models.CreateFolderCommand{
Title: title,
UID: uid,
})
body := &models.CreateFolderCommand{
Title: title,
UID: uid,
ParentUID: cr.Spec.ParentFolderUID,
}

folderResp, err := grafanaClient.Folders.CreateFolder(body)
theSuess marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}
Expand Down Expand Up @@ -362,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)
}

Expand All @@ -372,7 +385,8 @@ func (r *GrafanaFolderReconciler) Exists(client *genapi.GrafanaHTTPAPI, cr *graf
page := int64(1)
limit := int64(10000)
for {
params := folders.NewGetFoldersParams().WithPage(&page).WithLimit(&limit)
params := folders.NewGetFoldersParams().WithPage(&page).WithLimit(&limit).WithParentUID(&cr.Status.ParentFolderUID)

foldersResp, err := client.Folders.GetFolders(params)
if err != nil {
return false, "", err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ spec:
x-kubernetes-validations:
- message: Value is immutable
rule: self == oldSelf
parentFolderUID:
description: UID of the folder in which the current folder should
be created
type: string
permissions:
description: raw json with folder permissions
type: string
Expand Down Expand Up @@ -130,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
Expand Down
9 changes: 9 additions & 0 deletions deploy/kustomize/base/crds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1244,6 +1244,10 @@ spec:
x-kubernetes-validations:
- message: Value is immutable
rule: self == oldSelf
parentFolderUID:
description: UID of the folder in which the current folder should
be created
type: string
permissions:
description: raw json with folder permissions
type: string
Expand Down Expand Up @@ -1275,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
Expand Down
15 changes: 15 additions & 0 deletions docs/docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -2472,6 +2472,13 @@ GrafanaFolderSpec defines the desired state of GrafanaFolder
allow to import this resources from an operator in a different namespace<br/>
</td>
<td>false</td>
</tr><tr>
<td><b>parentFolderUID</b></td>
<td>string</td>
<td>
UID of the folder in which the current folder should be created<br/>
</td>
<td>false</td>
</tr><tr>
<td><b>permissions</b></td>
<td>string</td>
Expand Down Expand Up @@ -2622,6 +2629,14 @@ Important: Run "make" to regenerate code after modifying this file<br/>
<i>Format</i>: date-time<br/>
</td>
<td>false</td>
</tr><tr>
<td><b>parentFolderUID</b></td>
<td>string</td>
<td>
UID of the parent folder where the folder is created.
Will be empty if the folder is deployed at the root level<br/>
</td>
<td>false</td>
</tr></tbody>
</table>

Expand Down
2 changes: 2 additions & 0 deletions hack/kind/resources/default/grafana.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ spec:
config:
log:
mode: "console"
feature_toggles:
enable: nestedFolders
auth:
disable_login_form: "false"
security:
Expand Down
Loading