-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Add mechanism to override drainability status #6196
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/* | ||
Copyright 2023 The Kubernetes Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package rules | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
apiv1 "k8s.io/api/core/v1" | ||
"k8s.io/autoscaler/cluster-autoscaler/simulator/drainability" | ||
"k8s.io/autoscaler/cluster-autoscaler/utils/drain" | ||
) | ||
|
||
func TestDrainable(t *testing.T) { | ||
for desc, tc := range map[string]struct { | ||
rules Rules | ||
want drainability.Status | ||
}{ | ||
"no rules": { | ||
want: drainability.NewUndefinedStatus(), | ||
}, | ||
"first non-undefined rule returned": { | ||
rules: Rules{ | ||
fakeRule{drainability.NewUndefinedStatus()}, | ||
fakeRule{drainability.NewDrainableStatus()}, | ||
fakeRule{drainability.NewSkipStatus()}, | ||
}, | ||
want: drainability.NewDrainableStatus(), | ||
}, | ||
"override match": { | ||
rules: Rules{ | ||
fakeRule{drainability.Status{ | ||
Outcome: drainability.DrainOk, | ||
Overrides: []drainability.OutcomeType{drainability.BlockDrain}, | ||
}}, | ||
fakeRule{drainability.NewBlockedStatus(drain.NotEnoughPdb, nil)}, | ||
}, | ||
want: drainability.Status{ | ||
Outcome: drainability.DrainOk, | ||
Overrides: []drainability.OutcomeType{drainability.BlockDrain}, | ||
}, | ||
}, | ||
"override no match": { | ||
rules: Rules{ | ||
fakeRule{drainability.Status{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: How about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think they're both quite readable. I would argue the opposite and say that the constructors in the drainability package are unnecessary boilerplate. Since
The latter is better because it's explicit about parameters and requires no additional boilerplate when new fields are added to the struct. |
||
Outcome: drainability.DrainOk, | ||
Overrides: []drainability.OutcomeType{drainability.SkipDrain}, | ||
}}, | ||
fakeRule{drainability.NewBlockedStatus(drain.NotEnoughPdb, nil)}, | ||
}, | ||
want: drainability.NewBlockedStatus(drain.NotEnoughPdb, nil), | ||
}, | ||
"override unreachable": { | ||
rules: Rules{ | ||
fakeRule{drainability.NewSkipStatus()}, | ||
fakeRule{drainability.Status{ | ||
Outcome: drainability.DrainOk, | ||
Overrides: []drainability.OutcomeType{drainability.BlockDrain}, | ||
}}, | ||
fakeRule{drainability.NewBlockedStatus(drain.NotEnoughPdb, nil)}, | ||
}, | ||
want: drainability.NewSkipStatus(), | ||
}, | ||
"multiple overrides all run": { | ||
rules: Rules{ | ||
fakeRule{drainability.Status{ | ||
Outcome: drainability.DrainOk, | ||
Overrides: []drainability.OutcomeType{drainability.SkipDrain}, | ||
}}, | ||
fakeRule{drainability.Status{ | ||
Outcome: drainability.SkipDrain, | ||
Overrides: []drainability.OutcomeType{drainability.BlockDrain}, | ||
}}, | ||
fakeRule{drainability.NewBlockedStatus(drain.NotEnoughPdb, nil)}, | ||
}, | ||
want: drainability.Status{ | ||
Outcome: drainability.SkipDrain, | ||
Overrides: []drainability.OutcomeType{drainability.BlockDrain}, | ||
}, | ||
}, | ||
"multiple overrides respects order": { | ||
rules: Rules{ | ||
fakeRule{drainability.Status{ | ||
Outcome: drainability.SkipDrain, | ||
Overrides: []drainability.OutcomeType{drainability.BlockDrain}, | ||
}}, | ||
fakeRule{drainability.Status{ | ||
Outcome: drainability.DrainOk, | ||
Overrides: []drainability.OutcomeType{drainability.BlockDrain}, | ||
}}, | ||
fakeRule{drainability.NewBlockedStatus(drain.NotEnoughPdb, nil)}, | ||
}, | ||
want: drainability.Status{ | ||
Outcome: drainability.SkipDrain, | ||
Overrides: []drainability.OutcomeType{drainability.BlockDrain}, | ||
}, | ||
}, | ||
} { | ||
t.Run(desc, func(t *testing.T) { | ||
got := tc.rules.Drainable(nil, &apiv1.Pod{}) | ||
if diff := cmp.Diff(tc.want, got); diff != "" { | ||
t.Errorf("Drainable(): got status diff (-want +got):\n%s", diff) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
type fakeRule struct { | ||
status drainability.Status | ||
} | ||
|
||
func (r fakeRule) Name() string { | ||
return "FakeRule" | ||
} | ||
|
||
func (r fakeRule) Drainable(*drainability.DrainContext, *apiv1.Pod) drainability.Status { | ||
return r.status | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe worth logging the fact that override happened? At least at
V(5)
? This logic becomes non-trivial with this change, so some observability wouldn't hurt.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great idea. Added a Name() to the Rule interface to make this log more useful.