Skip to content

Commit 51a5911

Browse files
authored
Fix an issue when RunsCollector controller does not remove finalizer (#656)
1 parent 0bf8af1 commit 51a5911

File tree

5 files changed

+27
-6
lines changed

5 files changed

+27
-6
lines changed

api/v1alpha2/runscollector_types.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ type RunsCollectorStatus struct {
3232

3333
//+kubebuilder:object:root=true
3434
//+kubebuilder:subresource:status
35-
//+kubebuilder:printcolumn:name="Pool ID",type=string,JSONPath=`.status.agentPool.ID`
36-
//+kubebuilder:printcolumn:name="Pool Name",type=string,JSONPath=`.status.agentPool.Name`
35+
//+kubebuilder:printcolumn:name="Pool ID",type=string,JSONPath=`.status.agentPool.id`
36+
//+kubebuilder:printcolumn:name="Pool Name",type=string,JSONPath=`.status.agentPool.name`
3737
//+kubebuilder:metadata:labels="app.terraform.io/crd-schema-version=v25.11.0"
3838

3939
// Runs Collector scraptes HCP Terraform Run statuses from a given Agent Pool and exposes them as Prometheus-compatible metrics.

charts/hcp-terraform-operator/crds/app.terraform.io_runscollectors.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ spec:
2020
scope: Namespaced
2121
versions:
2222
- additionalPrinterColumns:
23-
- jsonPath: .status.agentPool.ID
23+
- jsonPath: .status.agentPool.id
2424
name: Pool ID
2525
type: string
26-
- jsonPath: .status.agentPool.Name
26+
- jsonPath: .status.agentPool.name
2727
name: Pool Name
2828
type: string
2929
name: v1alpha2

config/crd/bases/app.terraform.io_runscollectors.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ spec:
1717
scope: Namespaced
1818
versions:
1919
- additionalPrinterColumns:
20-
- jsonPath: .status.agentPool.ID
20+
- jsonPath: .status.agentPool.id
2121
name: Pool ID
2222
type: string
23-
- jsonPath: .status.agentPool.Name
23+
- jsonPath: .status.agentPool.name
2424
name: Pool Name
2525
type: string
2626
name: v1alpha2

internal/controller/predicates.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ func deletionTimestampPredicate(o client.Object) bool {
9797
agentTokenFinalizer,
9898
moduleFinalizer,
9999
projectFinalizer,
100+
runsCollectorFinalizer,
100101
workspaceFinalizer,
101102
}
102103

internal/controller/runscollector_controller.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,18 @@ func (r *RunsCollectorReconciler) SetupWithManager(mgr ctrl.Manager) error {
200200
Complete(r)
201201
}
202202

203+
func (r *RunsCollectorReconciler) removeFinalizer(ctx context.Context, rc *runsCollectorInstance) error {
204+
controllerutil.RemoveFinalizer(&rc.instance, runsCollectorFinalizer)
205+
206+
err := r.Update(ctx, &rc.instance)
207+
if err != nil {
208+
rc.log.Error(err, "Reconcile Runs Collector", "msg", fmt.Sprintf("failed to remove finalizer %s", runsCollectorFinalizer))
209+
r.Recorder.Eventf(&rc.instance, corev1.EventTypeWarning, "RemoveRunsCollector", "Failed to remove finalizer %s", runsCollectorFinalizer)
210+
}
211+
212+
return err
213+
}
214+
203215
func (r *RunsCollectorReconciler) getAgentPoolIDByName(ctx context.Context, rc *runsCollectorInstance) (*tfc.AgentPool, error) {
204216
name := rc.instance.Spec.AgentPool.Name
205217

@@ -254,6 +266,14 @@ func (r *RunsCollectorReconciler) updateStatusAgentPool(ctx context.Context, rc
254266
}
255267

256268
func (r *RunsCollectorReconciler) reconcileRuns(ctx context.Context, rc *runsCollectorInstance) error {
269+
// TODO:
270+
// - Think if we need to reset all metrics to 0 when remove finalizer.
271+
if isDeletionCandidate(&rc.instance, runsCollectorFinalizer) {
272+
rc.log.Info("Reconcile Runs Collector", "msg", "object marked as deleted, need to remove finalizer")
273+
r.Recorder.Event(&rc.instance, corev1.EventTypeNormal, "ReconcileRunsCollector", "Object marked as deleted, need to remove finalizer")
274+
return r.removeFinalizer(ctx, rc)
275+
}
276+
257277
runs := map[tfc.RunStatus]float64{}
258278
var runsTotal float64
259279
// TODO:

0 commit comments

Comments
 (0)