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

feat: only run sync jobs for canaries of specified namespaces #1291

Merged
merged 2 commits into from
Oct 4, 2023
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
1 change: 1 addition & 0 deletions cmd/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ func run(cmd *cobra.Command, args []string) {
includeNamespaces := []string{}
if operatorNamespace != "" {
includeNamespaces = strings.Split(operatorNamespace, ",")
canaryJobs.CanaryNamespaces = 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

// CanaryNamespaces is a list of namespaces whose canary specs should be synced.
// If empty, all namespaces will be synced
var CanaryNamespaces []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(CanaryNamespaces...)
if err != nil {
logger.Errorf("Failed to get canaries: %v", err)

Expand Down
Loading