Skip to content

Commit

Permalink
feat: add lastSyncTime annotation to CRs (#49)
Browse files Browse the repository at this point in the history
Every time a controller attempts to reconcile a CR, the last-sync-time
annotation gets updated.


![image](https://github.com/mercedes-benz/garm-operator/assets/48678421/821f2a75-d53f-412b-92bb-879a04e3150f)

Co-authored-by: Mario Constanti <github@constanti.de>
  • Loading branch information
rafalgalaw and bavarianbidi authored Jan 8, 2024
1 parent 7ae0d42 commit 0021abc
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 1 deletion.
7 changes: 7 additions & 0 deletions internal/controller/enterprise_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ func (r *EnterpriseReconciler) Reconcile(ctx context.Context, req ctrl.Request)
return ctrl.Result{}, nil
}

annotations.SetLastSyncTime(enterprise)
err = r.Update(ctx, enterprise)
if err != nil {
log.Error(err, "can not set annotation")
return ctrl.Result{}, err
}

enterpriseClient := garmClient.NewEnterpriseClient()
err = enterpriseClient.Login(garmClient.GarmScopeParams{
BaseURL: r.BaseURL,
Expand Down
7 changes: 7 additions & 0 deletions internal/controller/organization_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ func (r *OrganizationReconciler) Reconcile(ctx context.Context, req ctrl.Request
return ctrl.Result{}, nil
}

annotations.SetLastSyncTime(organization)
err = r.Update(ctx, organization)
if err != nil {
log.Error(err, "can not set annotation")
return ctrl.Result{}, err
}

organizationClient := garmClient.NewOrganizationClient()
err = organizationClient.Login(garmClient.GarmScopeParams{
BaseURL: r.BaseURL,
Expand Down
9 changes: 8 additions & 1 deletion internal/controller/pool_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,15 @@ func (r *PoolReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.
return ctrl.Result{}, nil
}

annotations.SetLastSyncTime(pool)
err := r.Update(ctx, pool)
if err != nil {
log.Error(err, "can not set annotation")
return ctrl.Result{}, err
}

poolClient := garmClient.NewPoolClient()
err := poolClient.Login(garmClient.GarmScopeParams{
err = poolClient.Login(garmClient.GarmScopeParams{
BaseURL: r.BaseURL,
Username: r.Username,
Password: r.Password,
Expand Down
7 changes: 7 additions & 0 deletions internal/controller/repository_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ func (r *RepositoryReconciler) Reconcile(ctx context.Context, req ctrl.Request)
return ctrl.Result{}, nil
}

annotations.SetLastSyncTime(repository)
err = r.Update(ctx, repository)
if err != nil {
log.Error(err, "can not set annotation")
return ctrl.Result{}, err
}

repositoryClient := garmClient.NewRepositoryClient()
err = repositoryClient.Login(garmClient.GarmScopeParams{
BaseURL: r.BaseURL,
Expand Down
9 changes: 9 additions & 0 deletions internal/controller/runner_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/mercedes-benz/garm-operator/pkg/config"
"github.com/mercedes-benz/garm-operator/pkg/filter"
instancefilter "github.com/mercedes-benz/garm-operator/pkg/filter/instance"
"github.com/mercedes-benz/garm-operator/pkg/util/annotations"
)

// RunnerReconciler reconciles a Runner object
Expand Down Expand Up @@ -59,6 +60,7 @@ func (r *RunnerReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
}

func (r *RunnerReconciler) reconcile(ctx context.Context, req ctrl.Request, instanceClient garmClient.InstanceClient) (ctrl.Result, error) {
log := log.FromContext(ctx)
// try fetch runner instance in garm db with events coming from reconcile loop events of RunnerCR or from manually enqueued events of garm api.
garmRunner, err := r.getGarmRunnerInstance(instanceClient, req.Name)
if err != nil {
Expand All @@ -71,6 +73,13 @@ func (r *RunnerReconciler) reconcile(ctx context.Context, req ctrl.Request, inst
return r.handleCreateRunnerCR(ctx, req, err, garmRunner)
}

annotations.SetLastSyncTime(runner)
err = r.Update(ctx, runner)
if err != nil {
log.Error(err, "can not set annotation")
return ctrl.Result{}, err
}

// delete runner in garm db
if !runner.ObjectMeta.DeletionTimestamp.IsZero() {
return r.reconcileDelete(ctx, instanceClient, garmRunner)
Expand Down
1 change: 1 addition & 0 deletions pkg/client/key/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ const (
PoolFinalizerName = groupName + "/pool"
RunnerFinalizerName = groupName + "/runner"
PausedAnnotation = groupName + "/paused"
LastSyncTimeAnnotation = groupName + "/last-sync-time"
)
21 changes: 21 additions & 0 deletions pkg/util/annotations/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
package annotations

import (
"time"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/mercedes-benz/garm-operator/pkg/client/key"
Expand All @@ -22,3 +24,22 @@ func hasAnnotation(o metav1.Object, annotation string) bool {
_, ok := annotations[annotation]
return ok
}

func SetLastSyncTime(o metav1.Object) {
now := time.Now().UTC()
newAnnotations := appendAnnotations(o, key.LastSyncTimeAnnotation, now.Format(time.RFC3339))
o.SetAnnotations(newAnnotations)
}

func appendAnnotations(o metav1.Object, kayValuePair ...string) map[string]string {
newAnnotations := map[string]string{}
for k, v := range o.GetAnnotations() {
newAnnotations[k] = v
}
for i := 0; i < len(kayValuePair)-1; i += 2 {
k := kayValuePair[i]
v := kayValuePair[i+1]
newAnnotations[k] = v
}
return newAnnotations
}

0 comments on commit 0021abc

Please sign in to comment.