-
Notifications
You must be signed in to change notification settings - Fork 1.8k
*: simplify Watch API by using dynamic resource client #34
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
Merged
hongchaodeng
merged 2 commits into
operator-framework:master
from
hasbro17:haseeb/use-dynamic-client-for-watch
Feb 22, 2018
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| package k8sclient | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "net" | ||
| "os" | ||
|
|
||
| "k8s.io/apimachinery/pkg/api/meta" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/runtime/schema" | ||
| "k8s.io/client-go/discovery" | ||
| "k8s.io/client-go/discovery/cached" | ||
| "k8s.io/client-go/dynamic" | ||
| "k8s.io/client-go/kubernetes" | ||
| "k8s.io/client-go/rest" | ||
| ) | ||
|
|
||
| var ( | ||
| restMapper *discovery.DeferredDiscoveryRESTMapper | ||
| clientPool dynamic.ClientPool | ||
| ) | ||
|
|
||
| // init initializes the restMapper and clientPool needed to create a resource client dynamically | ||
| func init() { | ||
| kubeClient, kubeConfig := mustNewKubeClientAndConfig() | ||
| cachedDiscoveryClient := cached.NewMemCacheClient(kubeClient.Discovery()) | ||
| restMapper = discovery.NewDeferredDiscoveryRESTMapper(cachedDiscoveryClient, meta.InterfacesForUnstructured) | ||
| restMapper.Reset() | ||
| kubeConfig.ContentConfig = dynamic.ContentConfig() | ||
| clientPool = dynamic.NewClientPool(kubeConfig, restMapper, dynamic.LegacyAPIPathResolverFunc) | ||
| } | ||
|
|
||
| // GetResourceClient returns the dynamic client and pluralName for the resource specified by the apiVersion and kind | ||
| func GetResourceClient(apiVersion, kind, namespace string) (dynamic.ResourceInterface, string, error) { | ||
| gv, err := schema.ParseGroupVersion(apiVersion) | ||
| if err != nil { | ||
| return nil, "", fmt.Errorf("failed to parse apiVersion: %v", err) | ||
| } | ||
| gvk := schema.GroupVersionKind{ | ||
| Group: gv.Group, | ||
| Version: gv.Version, | ||
| Kind: kind, | ||
| } | ||
|
|
||
| client, err := clientPool.ClientForGroupVersionKind(gvk) | ||
| if err != nil { | ||
| return nil, "", fmt.Errorf("failed to get client for GroupVersionKind(%s): %v", gvk.String(), err) | ||
| } | ||
| resource, err := apiResource(gvk, restMapper) | ||
| if err != nil { | ||
| return nil, "", fmt.Errorf("failed to get resource type: %v", err) | ||
| } | ||
| pluralName := resource.Name | ||
| resourceClient := client.Resource(resource, namespace) | ||
| return resourceClient, pluralName, nil | ||
| } | ||
|
|
||
| // apiResource consults the REST mapper to translate an <apiVersion, kind, namespace> tuple to a metav1.APIResource struct. | ||
| func apiResource(gvk schema.GroupVersionKind, restMapper *discovery.DeferredDiscoveryRESTMapper) (*metav1.APIResource, error) { | ||
| mapping, err := restMapper.RESTMapping(gvk.GroupKind(), gvk.Version) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to get the resource REST mapping for GroupVersionKind(%s): %v", gvk.String(), err) | ||
| } | ||
| resource := &metav1.APIResource{ | ||
| Name: mapping.Resource, | ||
| Namespaced: mapping.Scope == meta.RESTScopeNamespace, | ||
| Kind: gvk.Kind, | ||
| } | ||
| return resource, nil | ||
| } | ||
|
|
||
| // mustNewKubeClientAndConfig returns the in-cluster config and kubernetes client | ||
| func mustNewKubeClientAndConfig() (kubernetes.Interface, *rest.Config) { | ||
| cfg, err := inClusterConfig() | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| return kubernetes.NewForConfigOrDie(cfg), cfg | ||
| } | ||
|
|
||
| // inClusterConfig returns the in-cluster config accessible inside a pod | ||
| func inClusterConfig() (*rest.Config, error) { | ||
| // Work around https://github.com/kubernetes/kubernetes/issues/40973 | ||
| // See https://github.com/coreos/etcd-operator/issues/731#issuecomment-283804819 | ||
| if len(os.Getenv("KUBERNETES_SERVICE_HOST")) == 0 { | ||
| addrs, err := net.LookupHost("kubernetes.default.svc") | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| os.Setenv("KUBERNETES_SERVICE_HOST", addrs[0]) | ||
| } | ||
| if len(os.Getenv("KUBERNETES_SERVICE_PORT")) == 0 { | ||
| os.Setenv("KUBERNETES_SERVICE_PORT", "443") | ||
| } | ||
| return rest.InClusterConfig() | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I just curious when will an
APIResourcenot be in a Namespace?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.
So it's not common but if you watch cluster-wide resources like
StorageClassorClusterRolethen those are non-namespaced.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.
I see. Thanks for the clarification!