Skip to content

Commit

Permalink
change events back to blocked and pull out filter logic for expiration
Browse files Browse the repository at this point in the history
  • Loading branch information
njtran committed Mar 22, 2023
1 parent 8c6783b commit 136823f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
4 changes: 2 additions & 2 deletions pkg/controllers/deprovisioning/drift.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
11 changes: 7 additions & 4 deletions pkg/controllers/deprovisioning/expiration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions pkg/controllers/deprovisioning/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 136823f

Please sign in to comment.