Skip to content

Commit

Permalink
feat: only run sync jobs for canaries of specified namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe committed Oct 4, 2023
1 parent 8fbe884 commit 5931e45
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
2 changes: 2 additions & 0 deletions cmd/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/flanksource/canary-checker/pkg/cache"
"github.com/flanksource/canary-checker/pkg/db"
"github.com/flanksource/canary-checker/pkg/jobs"
"github.com/flanksource/canary-checker/pkg/jobs/canary"
canaryJobs "github.com/flanksource/canary-checker/pkg/jobs/canary"
"github.com/flanksource/canary-checker/pkg/runner"
"github.com/flanksource/canary-checker/pkg/utils"
Expand Down Expand Up @@ -110,6 +111,7 @@ func run(cmd *cobra.Command, args []string) {
includeNamespaces := []string{}
if operatorNamespace != "" {
includeNamespaces = strings.Split(operatorNamespace, ",")
canary.SyncCanaryNamspaces = includeNamespaces
}
runner.RunnerLabels = labels.LoadFromFile("/etc/podinfo/labels")

Expand Down
13 changes: 11 additions & 2 deletions pkg/db/canary.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ import (
"github.com/flanksource/duty/models"
dutyTypes "github.com/flanksource/duty/types"
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)

func GetAllCanariesForSync() ([]pkg.Canary, error) {
func GetAllCanariesForSync(namespaces ...string) ([]pkg.Canary, error) {
query := `
SELECT json_agg(
jsonb_set_lax(to_jsonb(canaries),'{checks}', (
Expand All @@ -41,7 +42,14 @@ func GetAllCanariesForSync() ([]pkg.Canary, error) {
agent_id = '00000000-0000-0000-0000-000000000000'
`

rows, err := Pool.Query(context.Background(), query)
args := make(pgx.NamedArgs)

if namespaces != nil {
query += " AND namespace = ANY(@namespaces)"
args["namespaces"] = namespaces
}

rows, err := Pool.Query(context.Background(), query, args)
if err != nil {
return nil, err
}
Expand All @@ -56,6 +64,7 @@ func GetAllCanariesForSync() ([]pkg.Canary, error) {
return nil, fmt.Errorf("failed to unmarshal canaries:%w for %s", err, rows.RawValues()[0])
}
}

return _canaries, nil
}

Expand Down
6 changes: 5 additions & 1 deletion pkg/jobs/canary/canary_jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ var DataFile string
var Executor bool
var LogPass, LogFail bool

// List of namespaces whose canary specs should be synced.
// If empty, all namespaces will be synced
var SyncCanaryNamspaces []string

var Kommons *kommons.Client
var Kubernetes kubernetes.Interface
var FuncScheduler = cron.New()
Expand Down Expand Up @@ -346,7 +350,7 @@ func SyncCanaryJob(dbCanary pkg.Canary) error {
func SyncCanaryJobs() {
logger.Debugf("Syncing canary jobs")

canaries, err := db.GetAllCanariesForSync()
canaries, err := db.GetAllCanariesForSync(SyncCanaryNamspaces...)
if err != nil {
logger.Errorf("Failed to get canaries: %v", err)

Expand Down

0 comments on commit 5931e45

Please sign in to comment.