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

[INCOMPTIBLE CHANGE] Refactor force-reconcile #192

Merged
merged 2 commits into from
Jan 1, 2025
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
10 changes: 8 additions & 2 deletions pkg/reconciler/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"fmt"
"math"
"strconv"
"time"

"github.com/iancoleman/strcase"
"github.com/pkg/errors"
Expand Down Expand Up @@ -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))
}
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
24 changes: 2 additions & 22 deletions pkg/reconciler/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"regexp"
"strconv"
"strings"
"time"

"github.com/pkg/errors"
"github.com/sap/go-generics/slices"
Expand Down Expand Up @@ -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
}
Expand All @@ -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) {
Expand Down
Loading