*: simplify Watch API by using dynamic resource client#34
*: simplify Watch API by using dynamic resource client#34hongchaodeng merged 2 commits intooperator-framework:masterfrom hasbro17:haseeb/use-dynamic-client-for-watch
Conversation
|
Testing out the Informer+dynamic-client Watch implementation separately. Will ping here once it works. |
| "k8s.io/client-go/rest" | ||
| ) | ||
|
|
||
| // MustNewKubeClientAndConfig returns the in-cluster config and kubernetes client |
There was a problem hiding this comment.
Better put these func into k8sutil/client.go
| ) | ||
|
|
||
| // init initializes the restMapper and clientPool needed to create a resource client dynamically | ||
| func init() { |
There was a problem hiding this comment.
We should not put any side effect into util/ package.
Probably have another package called pkg/k8sclient/
| // obj is an instance of the resource type, e.g. &Pod{}. | ||
| // resourcePluralName is the plural name of the resource, e.g. “pods”. | ||
| // resourceClient is the rest client for the resource, e.g. `kubeclient.CoreV1().RESTClient()`. | ||
| // apiVersion is the APIVersion of the resource, e.g "v1" for pods, or "cache.example.com/v1alpha1" for memcached custom resource |
There was a problem hiding this comment.
e.g "v1" for pods, or "cache.example.com/v1alpha1" for memcached custom resource
This is understatement of the specification of kube API.
I would suggest to mention at least 3 things:
- reference upstream kube API doc: https://kubernetes.io/docs/reference/
- describe that apiVersion would be of format "groupName/version" except "core" group. List some examples.
- underscore that "core" group would be only "v1". List some examples.
| // resourcePluralName is the plural name of the resource, e.g. “pods”. | ||
| // resourceClient is the rest client for the resource, e.g. `kubeclient.CoreV1().RESTClient()`. | ||
| // apiVersion is the APIVersion of the resource, e.g "v1" for pods, or "cache.example.com/v1alpha1" for memcached custom resource | ||
| // kind is the Kind of the resource, e.g "Pod" for pods, or "Memcached" for memcached custom resource |
There was a problem hiding this comment.
Give a reference where user can find the kind.
This would be a good reference: https://kubernetes.io/docs/reference/
|
This closes #16 |
I've tested this implementation of informer+dynamic-client separately and verified that it works in watching @hongchaodeng PTAL. |
| // obj is an instance of the resource type, e.g. &Pod{}. | ||
| // resourcePluralName is the plural name of the resource, e.g. “pods”. | ||
| // resourceClient is the rest client for the resource, e.g. `kubeclient.CoreV1().RESTClient()`. | ||
| // apiVersion is the APIVersion of the resource, e.g "v1" for pods, or "cache.example.com/v1alpha1" for memcached custom resource |
There was a problem hiding this comment.
This previous comment wasn't addressed: #34 (comment)
Paste again:
This is understatement of the specification of kube API.
I would suggest to mention at least 3 things:
- reference upstream kube API doc: https://kubernetes.io/docs/reference/
- describe that apiVersion would be of format "groupName/version" except "core" group. List some examples.
- underscore that "core" group would be only "v1". List some examples.
| // resourcePluralName is the plural name of the resource, e.g. “pods”. | ||
| // resourceClient is the rest client for the resource, e.g. `kubeclient.CoreV1().RESTClient()`. | ||
| // apiVersion is the APIVersion of the resource, e.g "v1" for pods, or "cache.example.com/v1alpha1" for memcached custom resource | ||
| // kind is the Kind of the resource, e.g "Pod" for pods, or "Memcached" for memcached custom resource |
There was a problem hiding this comment.
Previous comment wasn't addressed: #34 (comment)
Paste again:
Give a reference where user can find the kind.
This would be a good reference: https://kubernetes.io/docs/reference/
| return resource, nil | ||
| } | ||
|
|
||
| // MustNewKubeClientAndConfig returns the in-cluster config and kubernetes client |
There was a problem hiding this comment.
MustNewKubeClientAndConfig -> mustNewKubeClientAndConfig
| return kubernetes.NewForConfigOrDie(cfg), cfg | ||
| } | ||
|
|
||
| // inClusterConfig returns the in-cluster config accessible to a pod |
There was a problem hiding this comment.
to a pod -> inside a pod
|
|
||
| if !cache.WaitForCacheSync(ctx.Done(), i.sharedIndexInformer.HasSynced) { | ||
| return errors.New("Timed out waiting for caches to sync") | ||
| logrus.Error("Timed out waiting for caches to sync") |
There was a problem hiding this comment.
This would slip the error down.
If we can't handle the error here, just panic initially.
| } | ||
| resource := &metav1.APIResource{ | ||
| Name: mapping.Resource, | ||
| Namespaced: mapping.Scope == meta.RESTScopeNamespace, |
There was a problem hiding this comment.
I just curious when will an APIResource not be in a Namespace?
There was a problem hiding this comment.
So it's not common but if you watch cluster-wide resources like StorageClass or ClusterRole then those are non-namespaced.
There was a problem hiding this comment.
I see. Thanks for the clarification!
|
Fixed. PTAL and double check the Watch() API comment. |
| // resourceClient is the rest client for the resource, e.g. `kubeclient.CoreV1().RESTClient()`. | ||
| // apiVersion for a resource is of the format "Group/Version" except for the "Core" group whose APIVersion is just "v1". For e.g: | ||
| // - Deployments have Group "apps" and Version "v1beta2" giving the APIVersion "apps/v1beta2" | ||
| // - Pods have Group "Core" and Version "v1" giving the APIVersion "v1" |
There was a problem hiding this comment.
Maybe add an example on what's the APIVersion for Custom Resource? I am guessing that is what most user will be using SDK.Watch for.
| // kind is the Kind of the resource, e.g "Pod" for pods | ||
| // Consult the API reference for the Group, Version and Kind of a resource: https://kubernetes.io/docs/reference/ | ||
| // namespace is the Namespace to watch for the resource | ||
| // opts provide more options for doing the watch. |
There was a problem hiding this comment.
I don't think we have opts yet right? maybe remove it and add later?
|
some suggestions around |
|
Added the APIVersion example comment for Memcached CR. |
|
LGTM |
|
lgtm |
Currently, scorecard's XUnit output does not conform to any XUnit
schema. This makes the scorecard XUnit output conform with the XUnit
schema defined by the Jenkins XUnit plugin.
This is sample output from running scorecard against the sample Go v3
Memcached operator:
```xml
<testsuites name="scorecard">
<testsuite name="olm-bundle-validation-test" tests="1" skipped="0" failures="0" errors="0">
<properties>
<property name="spec.image" value="quay.io/operator-framework/scorecard-test:v1.19.0"></property>
<property name="spec.entrypoint" value="scorecard-test olm-bundle-validation"></property>
<property name="labels.test" value="olm-bundle-validation-test"></property>
</properties>
<testcase name="olm-bundle-validation" time="0001-01-01T00:00:00Z">
<system-out>time=&operator-framework#34;2022-04-12T19:21:52Z&operator-framework#34; level=debug msg=&operator-framework#34;Found manifests directory&operator-framework#34; name=bundle-test
time=&operator-framework#34;2022-04-12T19:21:52Z&operator-framework#34; level=debug msg=&operator-framework#34;Found metadata directory&operator-framework#34; name=bundle-test
time=&operator-framework#34;2022-04-12T19:21:52Z&operator-framework#34; level=debug msg=&operator-framework#34;Getting mediaType info from manifests directory&operator-framework#34; name=bundle-test
time=&operator-framework#34;2022-04-12T19:21:52Z&operator-framework#34; level=debug msg=&operator-framework#34;Found annotations file&operator-framework#34; name=bundle-test
time=&operator-framework#34;2022-04-12T19:21:52Z&operator-framework#34; level=debug msg=&operator-framework#34;Could not find optional dependencies file&operator-framework#34; name=bundle-test
</system-out>
</testcase>
</testsuite>
<!-- Some suites omitted for readability -->
</testsuites>
```
The full output can be recreated by installing the `operator-sdk` tool
built from this commit and running scorecard against the sample Go v3
memcached operator:
```
cd /path/to/operator-sdk
make install
operator-sdk scorecard testdata/go/v3/memcached-operator
```
The XUnit XSD schema can be found here:
https://github.com/jenkinsci/xunit-plugin/blob/master/src/main/resources/org/jenkinsci/plugins/xunit/types/model/xsd/junit-10.xsd
The tool used to validate the output against the schema can be found
here: https://www.freeformatter.com/xml-validator-xsd.html
Signed-off-by: Ryan King <ryking@redhat.com>
Currently, scorecard's XUnit output does not conform to any XUnit
schema. This makes the scorecard XUnit output conform with the XUnit
schema defined by the Jenkins XUnit plugin.
This is sample output from running scorecard against the sample Go v3
Memcached operator:
```xml
<testsuites name="scorecard">
<testsuite name="olm-bundle-validation-test" tests="1" skipped="0" failures="0" errors="0">
<properties>
<property name="spec.image" value="quay.io/operator-framework/scorecard-test:v1.19.0"></property>
<property name="spec.entrypoint" value="scorecard-test olm-bundle-validation"></property>
<property name="labels.test" value="olm-bundle-validation-test"></property>
</properties>
<testcase name="olm-bundle-validation" time="0001-01-01T00:00:00Z">
<system-out>time=&operator-framework#34;2022-04-12T19:21:52Z&operator-framework#34; level=debug msg=&operator-framework#34;Found manifests directory&operator-framework#34; name=bundle-test
time=&operator-framework#34;2022-04-12T19:21:52Z&operator-framework#34; level=debug msg=&operator-framework#34;Found metadata directory&operator-framework#34; name=bundle-test
time=&operator-framework#34;2022-04-12T19:21:52Z&operator-framework#34; level=debug msg=&operator-framework#34;Getting mediaType info from manifests directory&operator-framework#34; name=bundle-test
time=&operator-framework#34;2022-04-12T19:21:52Z&operator-framework#34; level=debug msg=&operator-framework#34;Found annotations file&operator-framework#34; name=bundle-test
time=&operator-framework#34;2022-04-12T19:21:52Z&operator-framework#34; level=debug msg=&operator-framework#34;Could not find optional dependencies file&operator-framework#34; name=bundle-test
</system-out>
</testcase>
</testsuite>
<!-- Some suites omitted for readability -->
</testsuites>
```
The full output can be recreated by installing the `operator-sdk` tool
built from this commit and running scorecard against the sample Go v3
memcached operator:
```
cd /path/to/operator-sdk
make install
operator-sdk scorecard testdata/go/v3/memcached-operator
```
The XUnit XSD schema can be found here:
https://github.com/jenkinsci/xunit-plugin/blob/master/src/main/resources/org/jenkinsci/plugins/xunit/types/model/xsd/junit-10.xsd
The tool used to validate the output against the schema can be found
here: https://www.freeformatter.com/xml-validator-xsd.html
Signed-off-by: Ryan King <ryking@redhat.com>
Currently, scorecard's XUnit output does not conform to any XUnit
schema. This makes the scorecard XUnit output conform with the XUnit
schema defined by the Jenkins XUnit plugin.
This is sample output from running scorecard against the sample Go v3
Memcached operator:
```xml
<testsuites name="scorecard">
<testsuite name="olm-bundle-validation-test" tests="1" skipped="0" failures="0" errors="0">
<properties>
<property name="spec.image" value="quay.io/operator-framework/scorecard-test:v1.19.0"></property>
<property name="spec.entrypoint" value="scorecard-test olm-bundle-validation"></property>
<property name="labels.test" value="olm-bundle-validation-test"></property>
</properties>
<testcase name="olm-bundle-validation" time="0001-01-01T00:00:00Z">
<system-out>time="2022-04-12T19:21:52Z" level=debug msg="Found manifests directory" name=bundle-test
time="2022-04-12T19:21:52Z" level=debug msg="Found metadata directory" name=bundle-test
time="2022-04-12T19:21:52Z" level=debug msg="Getting mediaType info from manifests directory" name=bundle-test
time="2022-04-12T19:21:52Z" level=debug msg="Found annotations file" name=bundle-test
time="2022-04-12T19:21:52Z" level=debug msg="Could not find optional dependencies file" name=bundle-test
</system-out>
</testcase>
</testsuite>
<!-- Some suites omitted for readability -->
</testsuites>
```
The full output can be recreated by installing the `operator-sdk` tool
built from this commit and running scorecard against the sample Go v3
memcached operator:
```
cd /path/to/operator-sdk
make install
operator-sdk scorecard testdata/go/v3/memcached-operator
```
The XUnit XSD schema can be found here:
https://github.com/jenkinsci/xunit-plugin/blob/master/src/main/resources/org/jenkinsci/plugins/xunit/types/model/xsd/junit-10.xsd
The tool used to validate the output against the schema can be found
here: https://www.freeformatter.com/xml-validator-xsd.html
Signed-off-by: Ryan King <ryking@redhat.com>
This PR simplifies the Watch() API by removing the rest client argument. Instead the user now passes in the apiVersion and Kind which can be used to create a dynamic resource client which is used to setup the informer for that resource.