-
Notifications
You must be signed in to change notification settings - Fork 38
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
Address review comments for operator conditions helpers #42
Address review comments for operator conditions helpers #42
Conversation
Pull Request Test Coverage Report for Build 395031807
💛 - Coveralls |
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.
Just a few questions
62d772c
to
d4733ce
Compare
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.
Not to throw a wrench into things, but another potential way to go with this library is to make an interface like this:
type Upgradabler interface {
// GetUpgradable gets the Upgradable condition on the operator's
// OperatorCondition. It returns an error if there are problems getting
// the OperatorCondition object or if the Upgradable condition does not
// exist.
GetUpgradable(context.Context) (*metav1.Condition, error)
// SetUpgradable sets the Upgradable condition on the operator's
// OperatorCondition to the status s. It returns an error if there
// are problems getting or updating the OperatorCondition object.
SetUpgradable(context.Context, metav1.ConditionStatus, ...Option) error
}
// Option is a function that applies a change to a condition
// This can be used to set optional condition fields, like reasons
// and messages.
type Option func(*metav1.Condition)
func WithReason(reason string) Option {
return func(c *metav1.Condition) {
c.Reason = reason
}
}
func WithMessage(message string) Option {
return func(c *metav1.Condition) {
c.Message = message
}
}
And then we could have an implementation that internally looks up (and caches) the namespaced name, and then fetches the current OperatorCondition, sets (or gets) the condition value, and then performs an update (if the value changed).
This way, a user wouldn't have to import operator-framework/api
or really even know about the OperatorCondition
type, and it would make it much easier for operator authors to mock an implementation of this and write tests around their upgrade logic.
d31f2c3
to
1cf5bcd
Compare
* Move getOperatorNamespace() to a seprate utils package. * Allow namespace to be set locally with an env variable. * Modify the order of tests and add godoc to env variables. * Remove redundant code.
1cf5bcd
to
ddc803b
Compare
internal/utils/utils.go
Outdated
var ErrNoNamespace = fmt.Errorf("namespace not found for current environment") | ||
|
||
// GetOperatorNamespace returns the namespace the operator should be running in. | ||
func GetOperatorNamespace(readNamespace func() ([]byte, error)) (string, error) { |
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.
Alternative approach:
Instead of doing
func GetOperatorNamespace(readNamespace func() ([]byte, error)) (string, error) {
...
}
var readNamespace = func() ([]byte, error) {
return ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace")
}
func foo() {
_, _ = GetOperatorNamespace(readNamespace)
}
you could do
func GetOperatorNamespace() (string, error) {
...
}
var readNamespace = GetOperatorNamespace()
func foo() {
_, _ = readNamespace()
}
which is a little less confusing and still mockable in tests in a similar way as to how you have it now:
var _ = Describe("test namespace", func() {
// This
readNamespace = func() (string, error) {
return "some-ns"
}
// Or this
f := func(s string) (func() (string, error)) {
return func() (string, error) { return s, nil }
}
readNamespace = f("some-ns")
}
|
||
// GetOperatorNamespace returns the namespace the operator should be running in from | ||
// the associated service account secret. | ||
var GetOperatorNamespace = func() (string, error) { |
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.
Since go would not allow var readNamespace = GetOperatorNamespace()
when GetOperatorNamespace()
is not an anonymous function, have declared it as a variable here. We would still need readSAFile
to test this function.
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.
/lgtm
/approve |
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: estroz The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
Description of the change:
This PR does the following:
Motivation for the change:
Follow-up #41