-
Notifications
You must be signed in to change notification settings - Fork 185
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
add generic 2-way merge handler. #26
Merged
openshift-merge-robot
merged 3 commits into
openshift:master
from
abhinavdahiya:generix_handlers
Sep 28, 2018
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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 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 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 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 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,90 @@ | ||
package dynamicclient | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
"time" | ||
|
||
"k8s.io/apimachinery/pkg/api/meta" | ||
|
||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"k8s.io/client-go/discovery/cached" | ||
"k8s.io/client-go/dynamic" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/rest" | ||
"k8s.io/client-go/restmapper" | ||
) | ||
|
||
type resourceClientFactory struct { | ||
dynamicClient dynamic.Interface | ||
restMapper *restmapper.DeferredDiscoveryRESTMapper | ||
} | ||
|
||
var ( | ||
// this stores the singleton in a package local | ||
singletonFactory *resourceClientFactory | ||
once sync.Once | ||
) | ||
|
||
// Private constructor for once.Do | ||
func newSingletonFactory(config *rest.Config) func() { | ||
return func() { | ||
cachedDiscoveryClient := cached.NewMemCacheClient(kubernetes.NewForConfigOrDie(config).Discovery()) | ||
restMapper := restmapper.NewDeferredDiscoveryRESTMapper(cachedDiscoveryClient) | ||
restMapper.Reset() | ||
|
||
dynamicClient, err := dynamic.NewForConfig(config) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
singletonFactory = &resourceClientFactory{ | ||
dynamicClient: dynamicClient, | ||
restMapper: restMapper, | ||
} | ||
singletonFactory.runBackgroundCacheReset(1 * time.Minute) | ||
} | ||
} | ||
|
||
// New returns the resource client using a singleton factory | ||
func New(config *rest.Config, gvk schema.GroupVersionKind, namespace string) (dynamic.ResourceInterface, error) { | ||
once.Do(newSingletonFactory(config)) | ||
return singletonFactory.getResourceClient(gvk, namespace) | ||
} | ||
|
||
// getResourceClient returns the dynamic client for the resource specified by the gvk. | ||
func (c *resourceClientFactory) getResourceClient(gvk schema.GroupVersionKind, namespace string) (dynamic.ResourceInterface, error) { | ||
gvr, namespaced, err := gvkToGVR(gvk, c.restMapper) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get resource type: %v", err) | ||
} | ||
|
||
// sometimes manifests of non-namespaced resources | ||
// might have namespace set. | ||
// preventing such cases. | ||
ns := namespace | ||
if !namespaced { | ||
ns = "" | ||
} | ||
return c.dynamicClient.Resource(*gvr).Namespace(ns), nil | ||
} | ||
|
||
func gvkToGVR(gvk schema.GroupVersionKind, restMapper *restmapper.DeferredDiscoveryRESTMapper) (*schema.GroupVersionResource, bool, error) { | ||
mapping, err := restMapper.RESTMapping(gvk.GroupKind(), gvk.Version) | ||
if err != nil { | ||
return nil, false, fmt.Errorf("failed to get the resource REST mapping for GroupVersionKind(%s): %v", gvk.String(), err) | ||
} | ||
|
||
return &mapping.Resource, mapping.Scope.Name() == meta.RESTScopeNameNamespace, nil | ||
} | ||
|
||
// runBackgroundCacheReset - Starts the rest mapper cache reseting | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: resetting? |
||
// at a duration given. | ||
func (c *resourceClientFactory) runBackgroundCacheReset(duration time.Duration) { | ||
ticker := time.NewTicker(duration) | ||
go func() { | ||
for range ticker.C { | ||
c.restMapper.Reset() | ||
} | ||
}() | ||
} |
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.
This is formatted strangely.