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 lastSyncTime annotation to CRs #49

Merged
merged 2 commits into from
Jan 8, 2024
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 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
}