Skip to content
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

Conversation

varshaprasad96
Copy link
Member

Description of the change:
This PR does the following:

  • 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.

Motivation for the change:
Follow-up #41

@coveralls
Copy link

coveralls commented Nov 21, 2020

Pull Request Test Coverage Report for Build 395031807

  • 12 of 16 (75.0%) changed or added relevant lines in 3 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.3%) to 77.363%

Changes Missing Coverage Covered Lines Changed/Added Lines %
internal/utils/utils.go 8 12 66.67%
Totals Coverage Status
Change from base Build 373437473: 0.3%
Covered Lines: 311
Relevant Lines: 402

💛 - Coveralls

Copy link
Member

@jmrodri jmrodri left a 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

conditions/operator_conditions.go Outdated Show resolved Hide resolved
leader/leader_test.go Outdated Show resolved Hide resolved
leader/leader.go Outdated Show resolved Hide resolved
Copy link
Member

@joelanford joelanford left a 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.

@varshaprasad96 varshaprasad96 force-pushed the op-condition/address-sug branch 2 times, most recently from d31f2c3 to 1cf5bcd Compare November 24, 2020 00:54
* 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.
@varshaprasad96
Copy link
Member Author

@estroz @jmrodri : Made the suggested changes, please have a look

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) {
Copy link
Member

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) {
Copy link
Member Author

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.

Copy link
Member

@estroz estroz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm

@openshift-ci-robot openshift-ci-robot added the lgtm Indicates that a PR is ready to be merged. label Dec 2, 2020
@varshaprasad96 varshaprasad96 changed the title Address review comments for opertor conditions helpers Address review comments for operator conditions helpers Dec 2, 2020
@estroz
Copy link
Member

estroz commented Dec 2, 2020

/approve

@openshift-ci-robot
Copy link

[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 /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@openshift-ci-robot openshift-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Dec 2, 2020
@openshift-merge-robot openshift-merge-robot merged commit 94fc682 into operator-framework:main Dec 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. lgtm Indicates that a PR is ready to be merged.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants