From 6316e8f9160f9fe748771a7073b19ee52fe707f5 Mon Sep 17 00:00:00 2001 From: Yash Mehrotra Date: Sun, 10 Nov 2024 17:45:35 +0530 Subject: [PATCH] chore: add reconcile job for deleted crds --- pkg/jobs/jobs.go | 7 ++++- pkg/jobs/topology/topology_jobs.go | 45 ++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/pkg/jobs/jobs.go b/pkg/jobs/jobs.go index 860c08d71..e050cc6d6 100644 --- a/pkg/jobs/jobs.go +++ b/pkg/jobs/jobs.go @@ -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 { diff --git a/pkg/jobs/topology/topology_jobs.go b/pkg/jobs/topology/topology_jobs.go index 094ab4a0c..d808371e2 100644 --- a/pkg/jobs/topology/topology_jobs.go +++ b/pkg/jobs/topology/topology_jobs.go @@ -157,3 +157,48 @@ var CleanupDeletedTopologyComponents = &job.Job{ return nil }, } + +var TopologyCRDReconcile = &job.Job{ + Name: "TopologyCRDReconcile", + Schedule: "@every 1h", + 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 + } + + for _, t := range topologies { + obj, err := ctx.Kommons().GetByKind("Topology", t.Namespace, t.Name) + if obj != nil { + if string(obj.GetUID()) == t.ID.String() { + ctx.History.IncrSuccess() + continue + } + } + + // If there is an error with GetByKind func, it indicates a problem with kube-api + // so we report it but do not delete the topology + if err != nil { + ctx.History.AddErrorf("error fetching resource from k8s[Topology/%s/%s]: %v", t.Namespace, t.Name, err) + continue + } + + // 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 + } + ctx.History.IncrSuccess() + DeleteTopologyJob(t.ID.String()) + } + return nil + }, +}