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

k8sclient.client: refactored to create singleton pattern, remove init #340

Merged
merged 3 commits into from
Jul 9, 2018
Merged

k8sclient.client: refactored to create singleton pattern, remove init #340

merged 3 commits into from
Jul 9, 2018

Conversation

CSdread
Copy link
Contributor

@CSdread CSdread commented Jul 6, 2018

ref: #292

k8sclient.client: refactored to create singleton pattern, remove init

At this time it is possible to abstract away the api for unit testing, the init
method blocks the ability to do the unit tests because it tries to connect to
the cluster. This refactor though not fully the direction you guys are going, is a temporary fix to allow unit testing of downstream code in operators using the sdk. In addition, this does not break the existing API and allows for no other code to change.

@CSdread
Copy link
Contributor Author

CSdread commented Jul 6, 2018

This also passes the run through the readme test. Ran that with master and this change as of tonight.

runBackgroundCacheReset(1 * time.Minute)
// GetResourceClient returns the resource client using a singleton factory
func GetResourceClient(apiVersion, kind, namespace string) (dynamic.ResourceInterface, string, error) {
if singletonFactory == nil {
Copy link

@kf6nux kf6nux Jul 6, 2018

Choose a reason for hiding this comment

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

This isn't goroutine safe. If it's possible to be called concurrently, consider using a mutex or https://golang.org/pkg/sync/#Once.Do

kubeConfig.ContentConfig = dynamic.ContentConfig()
clientPool := dynamic.NewClientPool(kubeConfig, restMapper, dynamic.LegacyAPIPathResolverFunc)

singletonFactory := &resourceClientFactory{
Copy link

Choose a reason for hiding this comment

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

Unintentional variable shadowing?

@spahl spahl requested a review from hasbro17 July 9, 2018 15:55
@@ -77,8 +97,8 @@ func GetResourceClient(apiVersion, kind, namespace string) (dynamic.ResourceInte
}

// GetKubeClient returns the kubernetes client used to create the dynamic client
func GetKubeClient() kubernetes.Interface {
return kubeClient
func (c *resourceClientFactory) GetKubeClient() kubernetes.Interface {
Copy link
Contributor

Choose a reason for hiding this comment

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

This needs to remain a global function. We had to expose the k8s clientset upon multiple requests #190 #295
So this should be:

func GetKubeClient() kubernetes.Interface {
  once.Do(newSingletonFactory)
  return singletonFactory.kubeClient
}

@hasbro17
Copy link
Contributor

hasbro17 commented Jul 9, 2018

@CSdread I'm fine with this intermediate fix for now if it unblocks people to write unit tests for pkg/stub. Can you please rebase with the master.
Just wanted to note that the unit tests will still be limited to not being able to use the SDK APIs since you can't mock the singleton object. But at least this won't outright fail for unit tests that don't go through the APIs.

And just for more context on the long term plan for global state: we are currently looking at using the controller-runtime library in the SDK to help us remove the global state and make unit testing easier by letting us inject a fake client.

@CSdread
Copy link
Contributor Author

CSdread commented Jul 9, 2018

@hasbro17 that is an awesome plan! I have abstracted out the use of the sdk functions to a class that is also a singleton, that enables me to inject a repository of "queries" to the sdk as a class into my controller. This will enable me to test because i can mock out those methods.

@CSdread
Copy link
Contributor Author

CSdread commented Jul 9, 2018

@hasbro17 fixed this up as specified, and re-based. Ready for your approval and merging. Thank you!

Gopkg.lock Outdated
revision = "c12348ce28de40eed0136aa2b644d0ee0650e56c"
version = "v1.0.1"
branch = "master"
name = "github.com/operator-framework/operator-sdk"
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you need to run dep ensure again to update the lock file with the changes from the master.
You're missing the prometheus dependencies that got introduced in #323
https://github.com/operator-framework/operator-sdk/blob/master/Gopkg.lock#L193-L199

Plus I don't know why the SDK is vendoring itself here. This should not be here.
Can you remove your vendor directory and Gopkg.lock file and try dep ensure again.

I've tried your PR locally and it does not need to vendor the SDK.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

not sure either, will do, thanks @hasbro17

@CSdread
Copy link
Contributor Author

CSdread commented Jul 9, 2018

@hasbro17 fixed

Gopkg.lock Outdated

[[projects]]
branch = "master"
name = "github.com/operator-framework/operator-sdk"
Copy link
Contributor

Choose a reason for hiding this comment

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

This still showing up here.
I think it might be better if you can exclude Gopkg.lock from your PR.
Since no new dependencies were introduced by your changes, I don't think Gopkg.lock needs to be updated.
The SDK dependency might be showing up due to some other local state that's not included in this PR.

After this is merged I'll update the lock file myself separately.

@CSdread
Copy link
Contributor Author

CSdread commented Jul 9, 2018

done @hasbro17 and for future ill destroy my fork and recreate, that way i can stop that

@hasbro17
Copy link
Contributor

hasbro17 commented Jul 9, 2018

LGTM

@hasbro17 hasbro17 merged commit 8be7ce0 into operator-framework:master Jul 9, 2018
@CSdread CSdread deleted the unit-test-unblock branch July 9, 2018 22:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants