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

srlinux: Use controller-runtime dynamic client instead of a generated clientset #326

Merged
merged 17 commits into from
Mar 16, 2023

Conversation

n0shut
Copy link
Contributor

@n0shut n0shut commented Mar 10, 2023

Hi all,

It seems it is considered to be a common (good?) practice nowadays to use an API client provided by the controller-runtime package that allows users to work with both structured and unstructured objects while removing the need to generate clientsets [1] [2] [3] [4].

In this PR I am removing srl-controller clientset in favor of the controller-runtime client. Currently I put controller-runtime client under the Node struct, but if this approach will be considered good by the kne team it definitely makes sense to initialize a controller once for all node implementations. Each node implementation just needs to register its own schema with the client to make it all work.

This PR works with the tip of the branch that nokia works on in parallel - srl-labs/srl-controller#31.

Looking forward to your review comments about this approach.

/cc @marcushines

[1] -- https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.14.5/pkg/client
[2] -- https://hackernoon.com/platforms-on-k8s-with-golang-watch-any-crd-0v2o3z1q#h-interacting-with-cr-ds-the-new-way
[3] -- kubernetes-sigs/kubebuilder#1152 (comment)
[4] -- https://medium.com/@timebertt/kubernetes-controllers-at-scale-clients-caches-conflicts-patches-explained-aa0f7a8b4332#053d

@n0shut
Copy link
Contributor Author

n0shut commented Mar 11, 2023

A fake client exists from controller-runtime client -- https://pkg.go.dev/sigs.k8s.io/controller-runtime/pkg/client/fake?utm_source=godoc

This allows to write unit tests for the CRs in question. Example - https://github.com/elastic/cloud-on-k8s/blob/main/pkg/controller/apmserver/controller_test.go

@coveralls
Copy link

coveralls commented Mar 11, 2023

Pull Request Test Coverage Report for Build 4441135746

  • 5 of 25 (20.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.01%) to 62.065%

Changes Missing Coverage Covered Lines Changed/Added Lines %
topo/node/srl/srl.go 5 25 20.0%
Totals Coverage Status
Change from base Build 4420828528: -0.01%
Covered Lines: 2837
Relevant Lines: 4571

💛 - Coveralls

@n0shut n0shut changed the title Use controller-runtime dynamic client instead of a generated clientset srlinux: Use controller-runtime dynamic client instead of a generated clientset Mar 11, 2023
@n0shut n0shut marked this pull request as ready for review March 11, 2023 10:52
@n0shut n0shut marked this pull request as draft March 11, 2023 10:52
@n0shut
Copy link
Contributor Author

n0shut commented Mar 12, 2023

Hi @alexmasi @marcushines
I have updated this PR to pass all KNE tests using the proposed change to srl-controller in srl-labs/srl-controller#31.

I have also updated the srlinux example topology and startup file to feature srlinux 22.11.2 version and recent kind/kne versions.
For testing purposes I have released srl-controller:0.5.0-rc1 controller container and matching manifests. I have installed it on a kind cluster with

kubectl apply -k https://github.com/srl-labs/srl-controller/config/default

Additionally, I verified that all OC services work based on the updated example topology.

I would appreciate your comments and presubmit testing (granted you align the manifests to the new versions)

@@ -62,6 +64,11 @@ func (f *fakeWatch) ResultChan() <-chan watch.Event {
}

func TestNew(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

func setUp() func() {
origSrlinuxClient := newSrlinuxClient
newSrlinuxClient = func(_ *rest.Config) (ctrlclient.Client, error) {
return nil, nil
}
return func() {
newSrlLinuxClient = origSrLinuxClient
}

then your tests

defer setUp()()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@marcushines I used a different name, but kept the logic. Interesting way of doing setup/teardown, huh.

cde31b6

@n0shut n0shut force-pushed the update-srl-controller-1.25 branch from 3cdf0c1 to cbde803 Compare March 15, 2023 12:03
@n0shut
Copy link
Contributor Author

n0shut commented Mar 15, 2023

Hi @alexmasi I updated the manifest, would be cool to see if we can run a presubmit using this branch

@@ -2361,9 +2401,6 @@
}
},
"srl_nokia-network-instance:network-instance": [
{
"name": "DEFAULT"
},
Copy link
Contributor

Choose a reason for hiding this comment

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

I would like this line to be reverted, helps align this topo with the featureprofiles defaults

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done a5ecb13

if err != nil {
return err
}
n.ControllerClient.Delete(ctx, &srlinuxv1.Srlinux{ObjectMeta: metav1.ObjectMeta{Name: n.Name()}})
Copy link
Contributor

Choose a reason for hiding this comment

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

check err?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done 3190719

go.mod Outdated
@@ -21,7 +21,7 @@ require (
github.com/scrapli/scrapligocfg v1.0.0
github.com/spf13/cobra v1.6.1
github.com/spf13/pflag v1.0.5
github.com/srl-labs/srl-controller v0.4.6
github.com/srl-labs/srl-controller v0.4.7-0.20230311230716-e1c96973d463
Copy link
Contributor

Choose a reason for hiding this comment

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

the container is v0.5.0 and the corresponding lib is v0.4.7, is that intended?

Copy link
Contributor

Choose a reason for hiding this comment

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

yes, just for testing inside this PR.
If tests are ok I will take both as 0.5.0

I spoke to Marcus and I will regenerate the manifests to use :latest container tag, so that manifest stays more-or-less static

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah I tested on my machine, good to switch to 0.5.0

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@alexmasi
Copy link
Contributor

/gcbrun

@n0shut n0shut marked this pull request as ready for review March 15, 2023 19:44
@n0shut n0shut requested a review from alexmasi March 15, 2023 19:44
- args:
- --health-probe-bind-address=:8081
- --metrics-bind-address=127.0.0.1:8080
- --leader-elect
command:
- /manager
image: ghcr.io/srl-labs/srl-controller:0.4.6
image: ghcr.io/srl-labs/srl-controller:latest
Copy link
Contributor

Choose a reason for hiding this comment

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

move this to 0.5.0

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

go.mod Outdated
@@ -21,7 +21,7 @@ require (
github.com/scrapli/scrapligocfg v1.0.0
github.com/spf13/cobra v1.6.1
github.com/spf13/pflag v1.0.5
github.com/srl-labs/srl-controller v0.4.6
github.com/srl-labs/srl-controller v0.4.7-0.20230311230716-e1c96973d463
Copy link
Contributor

Choose a reason for hiding this comment

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

yeah I tested on my machine, good to switch to 0.5.0

@@ -10,7 +10,7 @@ nodes: {
vendor: NOKIA
model: "ixr6e"
config:{
image: "ghcr.io/nokia/srlinux:22.6.4"
image: "ghcr.io/nokia/srlinux:22.11.2"
Copy link
Contributor

Choose a reason for hiding this comment

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

just remove this line so it defaults to the :latest tag (also in the other nokia examples as well)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done, I added a comment about this behavior if that's ok. 6443adf

@alexmasi
Copy link
Contributor

Okay testing 0.4.7-xxxxxx commit with 0.5.0-rc worked on my machine. The presubmit in its current state will not pass, I am working on a better presubmit to test things like this

Once the versions are 0.5.0 and the comments are addressed I will get this merged

@n0shut n0shut requested a review from alexmasi March 16, 2023 17:14
marcushines
marcushines previously approved these changes Mar 16, 2023
@marcushines
Copy link
Contributor

/gcbrun

@alexmasi alexmasi merged commit 8c1e29f into openconfig:main Mar 16, 2023
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.

5 participants