-
Notifications
You must be signed in to change notification settings - Fork 96
Bug 1826183: Sync proxy CA to openshift-controller-manager #162
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
adambkaplan:sync-user-ca
Jul 22, 2020
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
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
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
108 changes: 108 additions & 0 deletions
108
pkg/operator/usercaobservation/user_ca_observation_controller.go
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,108 @@ | ||
| package usercaobservation | ||
|
|
||
| import ( | ||
| "context" | ||
| "time" | ||
|
|
||
| configinformers "github.com/openshift/client-go/config/informers/externalversions" | ||
| configlistersv1 "github.com/openshift/client-go/config/listers/config/v1" | ||
| "github.com/openshift/library-go/pkg/controller/factory" | ||
| "github.com/openshift/library-go/pkg/operator/events" | ||
| "github.com/openshift/library-go/pkg/operator/management" | ||
| "github.com/openshift/library-go/pkg/operator/resourcesynccontroller" | ||
| "github.com/openshift/library-go/pkg/operator/v1helpers" | ||
| "k8s.io/apimachinery/pkg/api/errors" | ||
|
|
||
| "github.com/openshift/cluster-openshift-controller-manager-operator/pkg/util" | ||
| ) | ||
|
|
||
| // Controller watches the cluster proxy config resource to see if a custom trusted CA has been | ||
| // added or removed. In the event a change is detected, this Controller makes appropriate calls to | ||
| // the provided ResourceSyncer instance. | ||
| type Controller struct { | ||
| name string | ||
| operatorConfigClient v1helpers.OperatorClient | ||
| proxyLister configlistersv1.ProxyLister | ||
| resourceSyncer resourcesynccontroller.ResourceSyncer | ||
| runFn func(ctx context.Context, workers int) | ||
| syncCtxt factory.SyncContext | ||
| } | ||
|
|
||
| // NewController creates a new usercaobservation.Controller instance. | ||
| func NewController(operatorConfigClient v1helpers.OperatorClient, | ||
| configInformers configinformers.SharedInformerFactory, | ||
| resourceSyncer resourcesynccontroller.ResourceSyncer, | ||
| eventRecorder events.Recorder) *Controller { | ||
| c := &Controller{ | ||
| name: "UserCAObservationController", | ||
| operatorConfigClient: operatorConfigClient, | ||
| proxyLister: configInformers.Config().V1().Proxies().Lister(), | ||
| resourceSyncer: resourceSyncer, | ||
| } | ||
| informers := []factory.Informer{ | ||
| operatorConfigClient.Informer(), | ||
| configInformers.Config().V1().Proxies().Informer(), | ||
| } | ||
| f := factory.New(). | ||
| WithSync(c.Sync). | ||
| WithSyncContext(c.syncCtxt). | ||
| WithInformers(informers...). | ||
| ResyncEvery(10*time.Minute). | ||
| ToController(c.name, eventRecorder.WithComponentSuffix("user-ca-observation-controller")) | ||
| c.runFn = f.Run | ||
| return c | ||
| } | ||
|
|
||
| // Run starts the controller with the provided context and number of workers. | ||
| func (c *Controller) Run(ctx context.Context, workers int) { | ||
| c.runFn(ctx, workers) | ||
| } | ||
|
|
||
| // Sync runs the main synchronization logic for the controller. | ||
| func (c *Controller) Sync(ctx context.Context, syncCtx factory.SyncContext) error { | ||
| operatorSpec, _, _, err := c.operatorConfigClient.GetOperatorState() | ||
| if err != nil { | ||
| return nil | ||
| } | ||
|
|
||
| if !management.IsOperatorManaged(operatorSpec.ManagementState) { | ||
| return nil | ||
| } | ||
|
|
||
| // Bug 1826183: copy the proxy CA trust bundle to the openshift-controller-manager namespace. | ||
| // If this ConfigMap exists, the build controller will copy the contents into a ConfigMap for | ||
| // the build pod and mount it into each build pod container. The build pod containers will then | ||
| // run `update-ca-trust extract` on startup, merging the proxy CA with the default trust bundle | ||
| // provided by the openshift/builder image. | ||
| // | ||
| // If `source` returns an empty instance, the resourceSyncer will delete the destination | ||
| // ConfigMap. The build controller will expect this and mount an empty file in this situation. | ||
| source, err := c.findProxyCASource() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| destination := resourcesynccontroller.ResourceLocation{ | ||
| Namespace: util.TargetNamespace, | ||
| Name: "openshift-user-ca", | ||
| } | ||
| err = c.resourceSyncer.SyncConfigMap(destination, source) | ||
| return err | ||
| } | ||
|
|
||
| func (c *Controller) findProxyCASource() (resourcesynccontroller.ResourceLocation, error) { | ||
| source := resourcesynccontroller.ResourceLocation{} | ||
| proxy, err := c.proxyLister.Get("cluster") | ||
| if errors.IsNotFound(err) { | ||
| return source, nil | ||
| } | ||
| if err != nil { | ||
| return source, err | ||
| } | ||
| if len(proxy.Spec.TrustedCA.Name) > 0 { | ||
| source = resourcesynccontroller.ResourceLocation{ | ||
| Namespace: util.UserSpecifiedGlobalConfigNamespace, | ||
| Name: proxy.Spec.TrustedCA.Name, | ||
| } | ||
| } | ||
| return source, nil | ||
| } | ||
138 changes: 138 additions & 0 deletions
138
pkg/operator/usercaobservation/user_ca_observation_controller_test.go
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,138 @@ | ||
| package usercaobservation | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "sync" | ||
| "testing" | ||
| "time" | ||
|
|
||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/runtime" | ||
|
|
||
| configv1 "github.com/openshift/api/config/v1" | ||
| operatorv1 "github.com/openshift/api/operator/v1" | ||
| fakeconfig "github.com/openshift/client-go/config/clientset/versioned/fake" | ||
| configinformers "github.com/openshift/client-go/config/informers/externalversions" | ||
| "github.com/openshift/library-go/pkg/operator/events" | ||
| "github.com/openshift/library-go/pkg/operator/resourcesynccontroller" | ||
|
|
||
| "github.com/openshift/library-go/pkg/operator/v1helpers" | ||
|
|
||
| "github.com/openshift/cluster-openshift-controller-manager-operator/pkg/util" | ||
| ) | ||
|
|
||
| type fakeSyncer struct { | ||
| configMaps map[string]string | ||
| configMapMux sync.Mutex | ||
| configMapSynced chan struct{} | ||
| configMapSyncClosed bool | ||
| } | ||
|
|
||
| func newFakeSyncer() *fakeSyncer { | ||
| return &fakeSyncer{ | ||
| configMaps: make(map[string]string), | ||
| configMapSynced: make(chan struct{}), | ||
| } | ||
| } | ||
|
|
||
| func (f *fakeSyncer) SyncConfigMap(destination, source resourcesynccontroller.ResourceLocation) error { | ||
| f.configMapMux.Lock() | ||
| defer f.configMapMux.Unlock() | ||
| if f.configMapSyncClosed { | ||
| return nil | ||
| } | ||
| f.configMaps[canonicalLocation(destination)] = canonicalLocation(source) | ||
| close(f.configMapSynced) | ||
| f.configMapSyncClosed = true | ||
| return nil | ||
| } | ||
|
|
||
| func (f *fakeSyncer) SyncSecret(destination, source resourcesynccontroller.ResourceLocation) error { | ||
| return nil | ||
| } | ||
|
|
||
| func canonicalLocation(r resourcesynccontroller.ResourceLocation) string { | ||
| return fmt.Sprintf("%s/%s", r.Namespace, r.Name) | ||
| } | ||
|
|
||
| func TestFindProxyCASource(t *testing.T) { | ||
| destination := canonicalLocation(resourcesynccontroller.ResourceLocation{ | ||
| Namespace: util.TargetNamespace, | ||
| Name: "openshift-user-ca", | ||
| }) | ||
| cases := []struct { | ||
| name string | ||
| proxy *configv1.Proxy | ||
| expectedSource string | ||
| }{ | ||
| { | ||
| name: "default", | ||
| proxy: &configv1.Proxy{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "cluster", | ||
| }, | ||
| }, | ||
| expectedSource: "/", | ||
| }, | ||
| { | ||
| name: "no found", | ||
| expectedSource: "/", | ||
| }, | ||
| { | ||
| name: "has user CA", | ||
| proxy: &configv1.Proxy{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "cluster", | ||
| }, | ||
| Spec: configv1.ProxySpec{ | ||
| TrustedCA: configv1.ConfigMapNameReference{ | ||
| Name: "user-ca", | ||
| }, | ||
| }, | ||
| }, | ||
| expectedSource: "openshift-config/user-ca", | ||
| }, | ||
| } | ||
| for _, tc := range cases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| fakeOperatorClient := v1helpers.NewFakeOperatorClient( | ||
| &operatorv1.OperatorSpec{ | ||
| ManagementState: operatorv1.Managed, | ||
| }, | ||
| &operatorv1.OperatorStatus{}, | ||
| nil, | ||
| ) | ||
|
|
||
| configObjects := []runtime.Object{} | ||
| if tc.proxy != nil { | ||
| configObjects = append(configObjects, tc.proxy) | ||
| } | ||
| fakeConfigClient := fakeconfig.NewSimpleClientset(configObjects...) | ||
| configInformer := configinformers.NewSharedInformerFactory(fakeConfigClient, 1*time.Minute) | ||
| syncer := newFakeSyncer() | ||
| controller := NewController(fakeOperatorClient, | ||
| configInformer, | ||
| syncer, | ||
| events.NewInMemoryRecorder("test")) | ||
|
|
||
| ctx, ctxCancel := context.WithCancel(context.TODO()) | ||
| defer ctxCancel() | ||
| go configInformer.Start(ctx.Done()) | ||
| go controller.Run(ctx, 1) | ||
|
|
||
| select { | ||
| case <-syncer.configMapSynced: | ||
| case <-time.After(20 * time.Second): | ||
| t.Fatal("timeout waiting for configMap to by synced") | ||
| } | ||
| actualSource, ok := syncer.configMaps[destination] | ||
| if !ok { | ||
| t.Errorf("Expected source for %s, found none", destination) | ||
| } | ||
| if tc.expectedSource != actualSource { | ||
| t.Errorf("Expected source for %s to be %s, got %s", destination, tc.expectedSource, actualSource) | ||
| } | ||
| }) | ||
| } | ||
| } |
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.
so either user ca is a singleton like proxy ca, or SyncConfigMap is doing a merge of some sort
come clarification either way would be good
also, some details on why we need to copy ... i.e. the specfics around what breaks down with update-ca-trust if we read the "original" global CA vs. the user CA we've copied to would be good
How does the fact that we copy the proxy's trusted CA reconcile, get us out of jail, play nice, etc. with what I read in https://github.com/openshift/api/blob/master/config/v1/types_proxy.go#L44-L69
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 updated the comment above to clarify what is going on overall, and why we need to copy the user CA to the openshift-controller-manager namespace. We get out of jail because build pods run
update-ca-trust extractthemselves.My updated commit message indicates why we need to do this (namely, the issue with
keytoolformatted trust bundles).