Skip to content

Commit

Permalink
Merge pull request #1169 from JameelB/fix/ensure-folders-are-updated-…
Browse files Browse the repository at this point in the history
…on-resync

fix: ensure folder permisisions are updated on resync
  • Loading branch information
pb82 committed Jul 21, 2023
2 parents ffd4174 + 159ab76 commit a51ee18
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 4 deletions.
7 changes: 7 additions & 0 deletions api/v1beta1/grafanafolder_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ type GrafanaFolderStatus struct {
Hash string `json:"hash,omitempty"`
// The folder instanceSelector can't find matching grafana instances
NoMatchingInstances bool `json:"NoMatchingInstances,omitempty"`
// Last time the folder was resynced
LastResync metav1.Time `json:"lastResync,omitempty"`
}

//+kubebuilder:object:root=true
Expand Down Expand Up @@ -133,3 +135,8 @@ func (in *GrafanaFolder) GetResyncPeriod() time.Duration {

return duration
}

func (in *GrafanaFolder) ResyncPeriodHasElapsed() bool {
deadline := in.Status.LastResync.Add(in.GetResyncPeriod())
return time.Now().After(deadline)
}
3 changes: 2 additions & 1 deletion api/v1beta1/zz_generated.deepcopy.go

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

3 changes: 3 additions & 0 deletions config/crd/bases/grafana.integreatly.org_grafanafolders.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ spec:
type: boolean
hash:
type: string
lastResync:
format: date-time
type: string
type: object
type: object
served: true
Expand Down
4 changes: 4 additions & 0 deletions config/grafana.integreatly.org_grafanafolders.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ spec:
of cluster Important: Run "make" to regenerate code after modifying
this file'
type: string
lastResync:
description: Last time the folder was resynced
format: date-time
type: string
type: object
type: object
served: true
Expand Down
15 changes: 12 additions & 3 deletions controllers/grafanafolder_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/grafana-operator/grafana-operator/v5/controllers/metrics"
grapi "github.com/grafana/grafana-api-golang-client"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/grafana-operator/grafana-operator/v5/api/v1beta1"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -202,7 +203,10 @@ func (r *GrafanaFolderReconciler) Reconcile(ctx context.Context, req ctrl.Reques
return ctrl.Result{RequeueAfter: RequeueDelay}, nil
}

return ctrl.Result{RequeueAfter: folder.GetResyncPeriod()}, nil
if folder.ResyncPeriodHasElapsed() {
folder.Status.LastResync = metav1.Time{Time: time.Now()}
}
return ctrl.Result{RequeueAfter: folder.GetResyncPeriod()}, r.UpdateStatus(ctx, folder)
}

// SetupWithManager sets up the controller with the Manager.
Expand Down Expand Up @@ -289,6 +293,11 @@ func (r *GrafanaFolderReconciler) onFolderCreated(ctx context.Context, grafana *
return err
}

// always update after resync period has elapsed even if cr is unchanged.
if exists && cr.Unchanged() && !cr.ResyncPeriodHasElapsed() {
return nil
}

if exists {
// Add to status to cover cases:
// - operator have previously failed to update status
Expand Down Expand Up @@ -330,7 +339,7 @@ func (r *GrafanaFolderReconciler) onFolderCreated(ctx context.Context, grafana *
}

// NOTE: it's up to a user to reset permissions with correct json
if !cr.Unchanged() && cr.Spec.Permissions != "" {
if cr.Spec.Permissions != "" {
permissions := grapi.PermissionItems{}
err = json.Unmarshal([]byte(cr.Spec.Permissions), &permissions)
if err != nil {
Expand All @@ -343,7 +352,7 @@ func (r *GrafanaFolderReconciler) onFolderCreated(ctx context.Context, grafana *
}
}

return r.UpdateStatus(ctx, cr)
return nil
}

func (r *GrafanaFolderReconciler) UpdateStatus(ctx context.Context, cr *v1beta1.GrafanaFolder) error {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ spec:
type: boolean
hash:
type: string
lastResync:
format: date-time
type: string
type: object
type: object
served: true
Expand Down
4 changes: 4 additions & 0 deletions deploy/kustomize/base/crds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,10 @@ spec:
of cluster Important: Run "make" to regenerate code after modifying
this file'
type: string
lastResync:
description: Last time the folder was resynced
format: date-time
type: string
type: object
type: object
served: true
Expand Down
9 changes: 9 additions & 0 deletions docs/docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1506,6 +1506,15 @@ GrafanaFolderStatus defines the observed state of GrafanaFolder
INSERT ADDITIONAL STATUS FIELD - define observed state of cluster Important: Run "make" to regenerate code after modifying this file<br/>
</td>
<td>false</td>
</tr><tr>
<td><b>lastResync</b></td>
<td>string</td>
<td>
Last time the folder was resynced<br/>
<br/>
<i>Format</i>: date-time<br/>
</td>
<td>false</td>
</tr></tbody>
</table>

Expand Down

0 comments on commit a51ee18

Please sign in to comment.