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

chore: add reconcile job for deleted crds #2326

Merged
merged 6 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
7 changes: 6 additions & 1 deletion pkg/jobs/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ func Start() {
}
}

for _, j := range []*job.Job{topologyJobs.CleanupDeletedTopologyComponents, topologyJobs.SyncTopology, canaryJobs.SyncCanaryJobs, canaryJobs.CleanupDeletedCanaryChecks, dutyQuery.SyncComponentCacheJob} {
miscJobs := []*job.Job{
topologyJobs.CleanupDeletedTopologyComponents, topologyJobs.SyncTopology,
topologyJobs.TopologyCRDReconcile, canaryJobs.SyncCanaryJobs,
canaryJobs.CleanupDeletedCanaryChecks, dutyQuery.SyncComponentCacheJob,
}
for _, j := range miscJobs {
job := j
job.Context = context.DefaultContext
if err := job.AddToScheduler(FuncScheduler); err != nil {
Expand Down
47 changes: 47 additions & 0 deletions pkg/jobs/topology/topology_jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package topology
import (
"fmt"
"reflect"
"slices"
"sync"

canaryCtx "github.com/flanksource/canary-checker/api/context"
Expand All @@ -17,6 +18,9 @@ import (
"github.com/flanksource/duty/job"
"github.com/flanksource/duty/models"
"github.com/robfig/cron/v3"
"github.com/samber/lo"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

var TopologyScheduler = cron.New()
Expand Down Expand Up @@ -157,3 +161,46 @@ var CleanupDeletedTopologyComponents = &job.Job{
return nil
},
}

var TopologyCRDReconcile = &job.Job{
Name: "TopologyCRDReconcile",
Schedule: "@every 1h",
yashmehrotra marked this conversation as resolved.
Show resolved Hide resolved
Singleton: true,
JobHistory: true,
Retention: job.RetentionBalanced,
Fn: func(ctx job.JobRuntime) error {
var topologies []models.Topology

if err := ctx.DB().
Select("id", "name", "namespace").
Where("source IN (?, ?)", models.SourceCRD, models.SourceTopology).
Where(duty.LocalFilter).
Find(&topologies).Error; err != nil {
return err
}

client, err := ctx.KubernetesDynamicClient().GetClientByKind("Topology")
if err != nil {
return fmt.Errorf("error creating dynamic client for Topology: %w", err)
}

objs, err := client.List(ctx, metav1.ListOptions{})
if err != nil {
return fmt.Errorf("error listing Topology kind: %w", err)
}
k8sIDs := lo.Map(objs.Items, func(obj unstructured.Unstructured, _ int) string { return string(obj.GetUID()) })
for _, t := range topologies {
yashmehrotra marked this conversation as resolved.
Show resolved Hide resolved
yashmehrotra marked this conversation as resolved.
Show resolved Hide resolved
if !slices.Contains(k8sIDs, t.ID.String()) {
// id mismatch or object not found in k8s, delete current topology
if err := db.DeleteTopology(ctx.DB(), t.ID.String()); err != nil {
ctx.History.AddErrorf("error deleting topology[%s]: %v", t.ID, err)
continue
}
DeleteTopologyJob(t.ID.String())
}

ctx.History.IncrSuccess()
}
return nil
},
}
Loading