From 136823f0164b16ff03ee548e894e4ab8bfed3734 Mon Sep 17 00:00:00 2001 From: njtran Date: Wed, 22 Mar 2023 10:58:50 -0700 Subject: [PATCH] change events back to blocked and pull out filter logic for expiration --- pkg/controllers/deprovisioning/drift.go | 4 ++-- pkg/controllers/deprovisioning/expiration.go | 11 +++++++---- pkg/controllers/deprovisioning/helpers.go | 6 +++--- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/pkg/controllers/deprovisioning/drift.go b/pkg/controllers/deprovisioning/drift.go index b5a94fb310..bf52b81a5e 100644 --- a/pkg/controllers/deprovisioning/drift.go +++ b/pkg/controllers/deprovisioning/drift.go @@ -57,8 +57,8 @@ func (d *Drift) ShouldDeprovision(ctx context.Context, c *Candidate) bool { } // ComputeCommand generates a deprovisioning command given deprovisionable machines -func (d *Drift) ComputeCommand(ctx context.Context, candidates ...*Candidate) (Command, error) { - candidates, err := filterCandidates(ctx, d.kubeClient, d.recorder, candidates) +func (d *Drift) ComputeCommand(ctx context.Context, nodes ...*Candidate) (Command, error) { + candidates, err := filterCandidates(ctx, d.kubeClient, d.recorder, nodes) if err != nil { return Command{}, fmt.Errorf("filtering candidates, %w", err) } diff --git a/pkg/controllers/deprovisioning/expiration.go b/pkg/controllers/deprovisioning/expiration.go index 8da5a24739..8ba8a096d0 100644 --- a/pkg/controllers/deprovisioning/expiration.go +++ b/pkg/controllers/deprovisioning/expiration.go @@ -61,20 +61,23 @@ func (e *Expiration) ShouldDeprovision(ctx context.Context, c *Candidate) bool { } // SortCandidates orders expired nodes by when they've expired -func (e *Expiration) SortCandidates(candidates []*Candidate) []*Candidate { +func (e *Expiration) filterAndSortCandidates(ctx context.Context, nodes []*Candidate) ([]*Candidate, error) { + candidates, err := filterCandidates(ctx, e.kubeClient, e.recorder, nodes) + if err != nil { + return nil, fmt.Errorf("filtering candidates, %w", err) + } sort.Slice(candidates, func(i int, j int) bool { return getExpirationTime(candidates[i].Node, candidates[i].provisioner).Before(getExpirationTime(candidates[j].Node, candidates[j].provisioner)) }) - return candidates + return candidates, nil } // ComputeCommand generates a deprovisioning command given deprovisionable nodes func (e *Expiration) ComputeCommand(ctx context.Context, nodes ...*Candidate) (Command, error) { - candidates, err := filterCandidates(ctx, e.kubeClient, e.recorder, nodes) + candidates, err := e.filterAndSortCandidates(ctx, nodes) if err != nil { return Command{}, fmt.Errorf("filtering candidates, %w", err) } - candidates = e.SortCandidates(candidates) for _, candidate := range candidates { // Check if we need to create any nodes. diff --git a/pkg/controllers/deprovisioning/helpers.go b/pkg/controllers/deprovisioning/helpers.go index 90a732d950..4ec13a88c7 100644 --- a/pkg/controllers/deprovisioning/helpers.go +++ b/pkg/controllers/deprovisioning/helpers.go @@ -48,15 +48,15 @@ func filterCandidates(ctx context.Context, kubeClient client.Client, recorder ev // filter out nodes that can't be terminated nodes = lo.Filter(nodes, func(cn *Candidate, _ int) bool { if !cn.Node.DeletionTimestamp.IsZero() { - recorder.Publish(deprovisioningevents.Unconsolidatable(cn.Node, "in the process of deletion")...) + recorder.Publish(deprovisioningevents.Blocked(cn.Node, "in the process of deletion")...) return false } if pdb, ok := pdbs.CanEvictPods(cn.pods); !ok { - recorder.Publish(deprovisioningevents.Unconsolidatable(cn.Node, fmt.Sprintf("pdb %s prevents pod evictions", pdb))...) + recorder.Publish(deprovisioningevents.Blocked(cn.Node, fmt.Sprintf("pdb %s prevents pod evictions", pdb))...) return false } if p, ok := hasDoNotEvictPod(cn); ok { - recorder.Publish(deprovisioningevents.Unconsolidatable(cn.Node, fmt.Sprintf("pod %s/%s has do not evict annotation", p.Namespace, p.Name))...) + recorder.Publish(deprovisioningevents.Blocked(cn.Node, fmt.Sprintf("pod %s/%s has do not evict annotation", p.Namespace, p.Name))...) return false } return true