Skip to content

Commit

Permalink
fix: canary suspension
Browse files Browse the repository at this point in the history
[skip ci]
  • Loading branch information
adityathebe committed Oct 7, 2024
1 parent 1a0bfbd commit a519bf1
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 9 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ require (
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
)

// replace github.com/flanksource/duty => ../duty
replace github.com/flanksource/duty => ../duty

// replace github.com/flanksource/artifacts => ../artifacts

Expand Down
9 changes: 8 additions & 1 deletion pkg/controllers/canary_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"time"

"github.com/flanksource/canary-checker/pkg/db"
"github.com/flanksource/canary-checker/pkg/utils"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

Expand Down Expand Up @@ -77,6 +78,7 @@ func (r *CanaryReconciler) Reconcile(parentCtx gocontext.Context, req ctrl.Reque
if runner.IsCanaryIgnored(&canary.ObjectMeta) {
return ctrl.Result{}, nil
}

ctx := r.Context.WithObject(canary.ObjectMeta).WithName(req.NamespacedName.String())

canary.SetRunnerName(r.RunnerName)
Expand Down Expand Up @@ -207,8 +209,13 @@ func (r *CanaryReconciler) updateCanaryInDB(ctx dutyContext.Context, canary *v1.
if err != nil {
return nil, err
}

opts := jsondiff.DefaultJSONOptions()
if diff, _ := jsondiff.Compare(canarySpecJSON, dbCanary.Spec, &opts); diff != jsondiff.FullMatch {
diff, _ := jsondiff.Compare(canarySpecJSON, dbCanary.Spec, &opts)
specChanged := diff != jsondiff.FullMatch

annotationsChanged := !utils.IsMapIdentical(canary.Annotations, dbCanary.Annotations)
if annotationsChanged || specChanged {
dbCanary, err = r.persistAndCacheCanary(ctx, canary)
if err != nil {
return nil, err
Expand Down
12 changes: 8 additions & 4 deletions pkg/db/canary.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,11 +449,15 @@ func PersistCanaryModel(ctx context.Context, model pkg.Canary) (*pkg.Canary, boo

var changed bool
if existing.ID != uuid.Nil {
jsonDiff, err := diff.JSONCompare(string(model.Spec), string(existing.Spec))
if err != nil {
return nil, false, fmt.Errorf("failed to compare old and existing model")
if !utils.IsMapIdentical(model.Annotations, existing.Annotations) {
changed = true
} else {
jsonDiff, err := diff.JSONCompare(string(model.Spec), string(existing.Spec))
if err != nil {
return nil, false, fmt.Errorf("failed to compare old and existing model")
}
changed = jsonDiff != ""
}
changed = jsonDiff != ""
}

var oldCheckIDs []string
Expand Down
3 changes: 2 additions & 1 deletion pkg/jobs/canary/canary_jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ func (j CanaryJob) GetNamespacedName() types.NamespacedName {
}

func (j CanaryJob) Run(ctx dutyjob.JobRuntime) error {
if runner.IsCanaryIgnored(&j.Canary.ObjectMeta) {
if runner.IsCanarySuspended(&j.Canary.ObjectMeta) || runner.IsCanaryIgnored(&j.Canary.ObjectMeta) {
return nil
}

canaryID := j.DBCanary.ID.String()
ctx.History.ResourceID = canaryID
ctx.History.ResourceType = "canary"
Expand Down
5 changes: 3 additions & 2 deletions pkg/jobs/canary/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func SyncCanaryJob(ctx context.Context, dbCanary pkg.Canary) error {
return nil
}

if runner.IsCanaryIgnored(&canary.ObjectMeta) {
if runner.IsCanarySuspended(&canary.ObjectMeta) || runner.IsCanaryIgnored(&canary.ObjectMeta) {
Unschedule(id)
return nil
}
Expand Down Expand Up @@ -208,9 +208,10 @@ func ScanCanaryConfigs(ctx context.Context) {
}

for _, canary := range configs {
if runner.IsCanaryIgnored(&canary.ObjectMeta) {
if runner.IsCanarySuspended(&canary.ObjectMeta) || runner.IsCanaryIgnored(&canary.ObjectMeta) {
continue
}

_, _, err := db.PersistCanary(ctx, canary, path.Base(configfile))
if err != nil {
logger.Errorf("could not persist %s: %v", canary.Name, err)
Expand Down
4 changes: 4 additions & 0 deletions pkg/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,9 @@ func IsCanaryIgnored(canary *metav1.ObjectMeta) bool {
}
}

return false
}

func IsCanarySuspended(canary *metav1.ObjectMeta) bool {
return canary.Annotations != nil && canary.Annotations["suspend"] == "true"
}
14 changes: 14 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,17 @@ func FreePort() int {
address := listener.Addr().(*net.TCPAddr)
return address.Port
}

func IsMapIdentical[K comparable](map1, map2 map[string]K) bool {
if len(map1) != len(map2) {
return false
}

for k, v1 := range map1 {
if v2, exists := map2[k]; !exists || v1 != v2 {
return false
}
}

return true
}

0 comments on commit a519bf1

Please sign in to comment.