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

fix: ensure folder permisisions are updated on resync #1169

Merged
merged 3 commits into from
Jul 21, 2023
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
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.

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