Skip to content

Commit

Permalink
fix: don't add suspend annotations when replica is set to 0
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe committed Oct 16, 2024
1 parent 156b099 commit 782411c
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 38 deletions.
8 changes: 0 additions & 8 deletions pkg/controllers/canary_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,7 @@ func (r *CanaryReconciler) Reconcile(parentCtx gocontext.Context, req ctrl.Reque
if canary.Spec.Replicas != nil && canaryForStatus.Status.Replicas != *canary.Spec.Replicas {
if *canary.Spec.Replicas == 0 {
canaryJobs.Unschedule(canary.GetPersistedID())

if err := db.SuspendCanary(ctx, canary.GetPersistedID(), true); err != nil {
return ctrl.Result{Requeue: true, RequeueAfter: 2 * time.Minute}, err
}
} else {
if err := db.SuspendCanary(ctx, canary.GetPersistedID(), false); err != nil {
return ctrl.Result{Requeue: true, RequeueAfter: 2 * time.Minute}, err
}

if err := canaryJobs.SyncCanaryJob(ctx, *dbCanary); err != nil {
return ctrl.Result{Requeue: true, RequeueAfter: 2 * time.Minute}, err
}
Expand Down
33 changes: 3 additions & 30 deletions pkg/db/canary.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"errors"
"fmt"

"time"

apiContext "github.com/flanksource/canary-checker/api/context"
Expand All @@ -29,9 +28,7 @@ import (
"gorm.io/gorm/clause"
)

var (
PostgresDuplicateKeyError = &pgconn.PgError{Code: "23505"}
)
var PostgresDuplicateKeyError = &pgconn.PgError{Code: "23505"}

func GetAllCanariesForSync(ctx context.Context, namespace string) ([]pkg.Canary, error) {
query := `
Expand All @@ -50,6 +47,7 @@ func GetAllCanariesForSync(ctx context.Context, namespace string) ([]pkg.Canary,
WHERE
deleted_at IS NULL AND
agent_id = '00000000-0000-0000-0000-000000000000' AND
(spec->>'replicas' != '0' OR spec->'replicas' IS NULL) AND
(annotations->>'suspend' != 'true' OR annotations->>'suspend' IS NULL)
`

Expand Down Expand Up @@ -430,7 +428,6 @@ func PersistCanaryModel(ctx context.Context, model pkg.Canary) (*pkg.Canary, boo
models.Canary{}.ConflictClause(),
clause.Returning{},
).Create(&model).Error

// Duplicate key happens when an already created canary is persisted
// We will ignore this error but act on other errors
// In this scenario PostgresDuplicateKeyError is checked primarily and
Expand Down Expand Up @@ -477,7 +474,7 @@ func PersistCanaryModel(ctx context.Context, model pkg.Canary) (*pkg.Canary, boo
return nil, false, err
}

var checks = make(map[string]string)
checks := make(map[string]string)
var newCheckIDs []string
for _, config := range spec.GetAllChecks() {
check := pkg.FromExternalCheck(model, config)
Expand Down Expand Up @@ -517,27 +514,3 @@ func PersistCanary(ctx context.Context, canary v1.Canary, source string) (*pkg.C

return PersistCanaryModel(ctx, model)
}

// SuspendCanary sets the suspend annotation on the canary table.
func SuspendCanary(ctx context.Context, id string, suspend bool) error {
query := `
UPDATE canaries
SET annotations =
CASE
WHEN annotations IS NULL THEN '{"suspend": "true"}'::jsonb
ELSE jsonb_set(annotations, '{suspend}', '"true"')
END
WHERE id = ?;
`

if !suspend {
query = `
UPDATE canaries
SET annotations =
CASE WHEN annotations IS NOT NULL THEN annotations - 'suspend' END
WHERE id = ?;
`
}

return ctx.DB().Exec(query, id).Error
}

0 comments on commit 782411c

Please sign in to comment.