From ac96e788b77fcec3eff8f8c016fee4506b9656bc Mon Sep 17 00:00:00 2001 From: Christoph Barbian Date: Fri, 20 Dec 2024 14:56:02 +0100 Subject: [PATCH] refactor force-reconcile --- .gitignore | 2 +- pkg/reconciler/reconciler.go | 10 ++++++++-- pkg/reconciler/util.go | 24 ++---------------------- 3 files changed, 11 insertions(+), 25 deletions(-) diff --git a/.gitignore b/.gitignore index 19e7e8d..f276c03 100644 --- a/.gitignore +++ b/.gitignore @@ -6,7 +6,7 @@ *.dylib bin testbin/* -__debug_bin +*__debug_bin* # Test binary, build with `go test -c` *.test diff --git a/pkg/reconciler/reconciler.go b/pkg/reconciler/reconciler.go index e416760..ac21871 100644 --- a/pkg/reconciler/reconciler.go +++ b/pkg/reconciler/reconciler.go @@ -11,6 +11,7 @@ import ( "fmt" "math" "strconv" + "time" "github.com/iancoleman/strcase" "github.com/pkg/errors" @@ -387,7 +388,7 @@ func (r *Reconciler) Apply(ctx context.Context, inventory *[]*InventoryItem, obj // calculate object digest // note: if the effective reconcile policy of an object changes, it will always be reconciled at least one more time; // this is in particular the case if the policy changes from or to ReconcilePolicyOnce. - digest, err := calculateObjectDigest(object, item, componentRevision, getReconcilePolicy(object)) + digest, err := calculateObjectDigest(object, componentRevision, getReconcilePolicy(object)) if err != nil { return false, errors.Wrapf(err, "error calculating digest for object %s", types.ObjectKeyToString(object)) } @@ -632,14 +633,18 @@ func (r *Reconciler) Apply(ctx context.Context, inventory *[]*InventoryItem, obj setAnnotation(object, r.annotationKeyDigest, item.Digest) updatePolicy := getUpdatePolicy(object) + now := time.Now() if existingObject == nil { if err := r.createObject(ctx, object, nil, updatePolicy); err != nil { return false, errors.Wrapf(err, "error creating object %s", item) } item.Phase = PhaseCreating item.Status = status.InProgressStatus + item.LastAppliedAt = &metav1.Time{Time: now} numUnready++ - } else if existingObject.GetDeletionTimestamp().IsZero() && existingObject.GetAnnotations()[r.annotationKeyDigest] != item.Digest { + } else if existingObject.GetDeletionTimestamp().IsZero() && + // TODO: make force-reconcile period (60 minutes as of now) configurable + (existingObject.GetAnnotations()[r.annotationKeyDigest] != item.Digest || item.LastAppliedAt != nil && item.LastAppliedAt.Time.Before(now.Add(-60*time.Minute))) { switch updatePolicy { case UpdatePolicyRecreate: // TODO: perform an additional owner id check @@ -654,6 +659,7 @@ func (r *Reconciler) Apply(ctx context.Context, inventory *[]*InventoryItem, obj } item.Phase = PhaseUpdating item.Status = status.InProgressStatus + item.LastAppliedAt = &metav1.Time{Time: now} numUnready++ } else { existingStatus, err := r.statusAnalyzer.ComputeStatus(existingObject) diff --git a/pkg/reconciler/util.go b/pkg/reconciler/util.go index e35ac58..72e204b 100644 --- a/pkg/reconciler/util.go +++ b/pkg/reconciler/util.go @@ -11,10 +11,7 @@ import ( "encoding/hex" "encoding/json" "fmt" - "regexp" - "strconv" "strings" - "time" "github.com/pkg/errors" "github.com/sap/go-generics/slices" @@ -61,7 +58,7 @@ func checkRange(x int, min int, max int) error { return nil } -func calculateObjectDigest(obj client.Object, item *InventoryItem, revision int64, reconcilePolicy ReconcilePolicy) (string, error) { +func calculateObjectDigest(obj client.Object, revision int64, reconcilePolicy ReconcilePolicy) (string, error) { if reconcilePolicy == ReconcilePolicyOnce { return "__once__", nil } @@ -85,24 +82,7 @@ func calculateObjectDigest(obj client.Object, item *InventoryItem, revision int6 digest = fmt.Sprintf("%s@%d", digest, revision) } - previousDigest := "" - previousTimestamp := int64(0) - if item != nil { - if m := regexp.MustCompile(`^([0-9a-f@]+):(\d{10})$`).FindStringSubmatch(item.Digest); m != nil { - previousDigest = m[1] - previousTimestamp = must(strconv.ParseInt(m[2], 10, 64)) - } - } - now := time.Now().Unix() - timestamp := int64(0) - // TODO: make force-reconcile period configurable (globally, per object, ...?) - if previousDigest == digest && now-previousTimestamp <= 3600 { - timestamp = previousTimestamp - } else { - timestamp = now - } - - return fmt.Sprintf("%s:%d", digest, timestamp), nil + return digest, nil } func setLabel(obj client.Object, key string, value string) {