-
Notifications
You must be signed in to change notification settings - Fork 265
configobserver: add helper function for observing the Proxy object #485
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
openshift-merge-robot
merged 2 commits into
openshift:master
from
stlaz:proxy_observing
Jul 29, 2019
Merged
Changes from all commits
Commits
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
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,88 @@ | ||
| package proxy | ||
|
|
||
| import ( | ||
| "reflect" | ||
|
|
||
| "k8s.io/apimachinery/pkg/api/errors" | ||
| "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
|
|
||
| configv1 "github.com/openshift/api/config/v1" | ||
| configlistersv1 "github.com/openshift/client-go/config/listers/config/v1" | ||
| "github.com/openshift/library-go/pkg/operator/configobserver" | ||
| "github.com/openshift/library-go/pkg/operator/events" | ||
| ) | ||
|
|
||
| type ProxyLister interface { | ||
| ProxyLister() configlistersv1.ProxyLister | ||
| } | ||
|
|
||
| func NewProxyObserveFunc(configPath []string) configobserver.ObserveConfigFunc { | ||
| return (&observeProxyFlags{ | ||
| configPath: configPath, | ||
| }).ObserveProxyConfig | ||
| } | ||
|
|
||
| type observeProxyFlags struct { | ||
| configPath []string | ||
| } | ||
|
|
||
| // ObserveProxyConfig observes the proxy.config.openshift.io/cluster object and writes | ||
| // its content to an unstructured object in a string map at the path from the constructor | ||
| func (f *observeProxyFlags) ObserveProxyConfig(genericListers configobserver.Listers, recorder events.Recorder, existingConfig map[string]interface{}) (map[string]interface{}, []error) { | ||
| proxyLister := genericListers.(ProxyLister) | ||
|
|
||
| errs := []error{} | ||
| prevObservedProxyConfig := map[string]interface{}{} | ||
|
|
||
| // grab the current Proxy config to later check whether it was updated | ||
| currentProxyMap, _, err := unstructured.NestedStringMap(existingConfig, f.configPath...) | ||
| if err != nil { | ||
| return prevObservedProxyConfig, append(errs, err) | ||
| } | ||
|
|
||
| if len(currentProxyMap) > 0 { | ||
| unstructured.SetNestedStringMap(prevObservedProxyConfig, currentProxyMap, f.configPath...) | ||
| } | ||
|
|
||
| observedConfig := map[string]interface{}{} | ||
| proxyConfig, err := proxyLister.ProxyLister().Get("cluster") | ||
| if errors.IsNotFound(err) { | ||
| recorder.Warningf("ObserveProxyConfig", "proxy.%s/cluster not found", configv1.GroupName) | ||
| return observedConfig, errs | ||
| } | ||
| if err != nil { | ||
| errs = append(errs, err) | ||
|
Contributor
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. 👍 |
||
| return existingConfig, errs | ||
| } | ||
|
|
||
| newProxyMap := proxyToMap(proxyConfig) | ||
| if len(newProxyMap) > 0 { | ||
| if err := unstructured.SetNestedStringMap(observedConfig, newProxyMap, f.configPath...); err != nil { | ||
| errs = append(errs, err) | ||
| } | ||
| } | ||
|
|
||
| if !reflect.DeepEqual(currentProxyMap, newProxyMap) { | ||
| recorder.Eventf("ObserveProxyConfig", "proxy changed to %q", newProxyMap) | ||
| } | ||
|
|
||
| return observedConfig, errs | ||
| } | ||
|
|
||
| func proxyToMap(proxy *configv1.Proxy) map[string]string { | ||
| proxyMap := map[string]string{} | ||
|
|
||
| if noProxy := proxy.Spec.NoProxy; len(noProxy) > 0 { | ||
| proxyMap["NO_PROXY"] = noProxy | ||
| } | ||
|
|
||
| if httpProxy := proxy.Spec.HTTPProxy; len(httpProxy) > 0 { | ||
| proxyMap["HTTP_PROXY"] = httpProxy | ||
| } | ||
|
|
||
| if httpsProxy := proxy.Spec.HTTPSProxy; len(httpsProxy) > 0 { | ||
| proxyMap["HTTPS_PROXY"] = httpsProxy | ||
| } | ||
|
|
||
| return proxyMap | ||
| } | ||
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,91 @@ | ||
| package proxy | ||
|
|
||
| import ( | ||
| "reflect" | ||
| "testing" | ||
|
|
||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/client-go/tools/cache" | ||
|
|
||
| configv1 "github.com/openshift/api/config/v1" | ||
| configlistersv1 "github.com/openshift/client-go/config/listers/config/v1" | ||
| "github.com/openshift/library-go/pkg/operator/events" | ||
| "github.com/openshift/library-go/pkg/operator/resourcesynccontroller" | ||
| ) | ||
|
|
||
| type testLister struct { | ||
| lister configlistersv1.ProxyLister | ||
| } | ||
|
|
||
| func (l testLister) ProxyLister() configlistersv1.ProxyLister { | ||
| return l.lister | ||
| } | ||
|
|
||
| func (l testLister) ResourceSyncer() resourcesynccontroller.ResourceSyncer { | ||
| return nil | ||
| } | ||
|
|
||
| func (l testLister) PreRunHasSynced() []cache.InformerSynced { | ||
| return nil | ||
| } | ||
|
|
||
| func TestObserveProxyConfig(t *testing.T) { | ||
| configPath := []string{"openshift", "proxy"} | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| proxySpec configv1.ProxySpec | ||
| expected map[string]interface{} | ||
| expectedError []error | ||
| }{ | ||
| { | ||
| name: "all unset", | ||
| proxySpec: configv1.ProxySpec{}, | ||
| expected: map[string]interface{}{}, | ||
| expectedError: []error{}, | ||
| }, | ||
| { | ||
| name: "all set", | ||
| proxySpec: configv1.ProxySpec{ | ||
| HTTPProxy: "http://someplace.it", | ||
| HTTPSProxy: "https://someplace.it", | ||
| NoProxy: "127.0.0.1", | ||
| }, | ||
| expected: map[string]interface{}{ | ||
| "openshift": map[string]interface{}{ | ||
| "proxy": map[string]interface{}{ | ||
| "HTTP_PROXY": "http://someplace.it", | ||
| "HTTPS_PROXY": "https://someplace.it", | ||
| "NO_PROXY": "127.0.0.1", | ||
| }, | ||
| }, | ||
| }, | ||
| expectedError: []error{}, | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| indexer := cache.NewIndexer(cache.MetaNamespaceKeyFunc, cache.Indexers{}) | ||
| indexer.Add(&configv1.Proxy{ | ||
| ObjectMeta: metav1.ObjectMeta{Name: "cluster"}, | ||
| Spec: tt.proxySpec, | ||
| }) | ||
| listers := testLister{ | ||
| lister: configlistersv1.NewProxyLister(indexer), | ||
| } | ||
| eventRecorder := events.NewInMemoryRecorder("") | ||
|
|
||
| initialExistingConfig := map[string]interface{}{} | ||
|
|
||
| observeFn := NewProxyObserveFunc(configPath) | ||
|
|
||
| got, errorsGot := observeFn(listers, eventRecorder, initialExistingConfig) | ||
| if !reflect.DeepEqual(got, tt.expected) { | ||
| t.Errorf("observeProxyFlags.ObserveProxyConfig() got = %v, want %v", got, tt.expected) | ||
| } | ||
| if !reflect.DeepEqual(errorsGot, tt.expectedError) { | ||
| t.Errorf("observeProxyFlags.ObserveProxyConfig() errorsGot = %v, want %v", errorsGot, tt.expectedError) | ||
| } | ||
| }) | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.