-
Notifications
You must be signed in to change notification settings - Fork 252
csi: add configobserver controller and hook helpers #976
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| package csiconfigobservercontroller | ||
|
|
||
| import ( | ||
| "strings" | ||
|
|
||
| "k8s.io/client-go/tools/cache" | ||
|
|
||
| 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/configobserver" | ||
| "github.com/openshift/library-go/pkg/operator/configobserver/proxy" | ||
| "github.com/openshift/library-go/pkg/operator/events" | ||
| "github.com/openshift/library-go/pkg/operator/resourcesynccontroller" | ||
| "github.com/openshift/library-go/pkg/operator/v1helpers" | ||
| ) | ||
|
|
||
| // ProxyConfigPath returns the path for the observed proxy config. This is a | ||
| // function to avoid exposing a slice that could potentially be appended. | ||
| func ProxyConfigPath() []string { | ||
| return []string{"targetcsiconfig", "proxy"} | ||
| } | ||
|
|
||
| // Listers implement the configobserver.Listers interface. | ||
| type Listers struct { | ||
| ProxyLister_ configlistersv1.ProxyLister | ||
|
|
||
| ResourceSync resourcesynccontroller.ResourceSyncer | ||
| PreRunCachesSynced []cache.InformerSynced | ||
| } | ||
|
|
||
| func (l Listers) ProxyLister() configlistersv1.ProxyLister { | ||
| return l.ProxyLister_ | ||
| } | ||
|
|
||
| func (l Listers) ResourceSyncer() resourcesynccontroller.ResourceSyncer { | ||
| return l.ResourceSync | ||
| } | ||
|
|
||
| func (l Listers) PreRunHasSynced() []cache.InformerSynced { | ||
| return l.PreRunCachesSynced | ||
| } | ||
|
|
||
| // CISConfigObserverController watches information that's relevant to CSI driver operators. | ||
| // For now it only observes proxy information, (through the proxy.config.openshift.io/cluster | ||
| // object), but more will be added. | ||
| type CSIConfigObserverController struct { | ||
| factory.Controller | ||
| } | ||
|
|
||
| // NewCSIConfigObserverController returns a new CSIConfigObserverController. | ||
| func NewCSIConfigObserverController( | ||
| name string, | ||
| operatorClient v1helpers.OperatorClient, | ||
| configinformers configinformers.SharedInformerFactory, | ||
| eventRecorder events.Recorder, | ||
| ) *CSIConfigObserverController { | ||
| informers := []factory.Informer{ | ||
| operatorClient.Informer(), | ||
| configinformers.Config().V1().Proxies().Informer(), | ||
| } | ||
|
|
||
| c := &CSIConfigObserverController{ | ||
| Controller: configobserver.NewConfigObserver( | ||
| operatorClient, | ||
| eventRecorder.WithComponentSuffix("csi-config-observer-controller-"+strings.ToLower(name)), | ||
| Listers{ | ||
| ProxyLister_: configinformers.Config().V1().Proxies().Lister(), | ||
| PreRunCachesSynced: append([]cache.InformerSynced{}, | ||
| operatorClient.Informer().HasSynced, | ||
| configinformers.Config().V1().Proxies().Informer().HasSynced, | ||
| ), | ||
| }, | ||
| informers, | ||
| proxy.NewProxyObserveFunc(ProxyConfigPath()), | ||
| ), | ||
| } | ||
|
|
||
| return c | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| package csidrivercontrollerservicecontroller | ||
|
|
||
| import ( | ||
| "crypto/sha256" | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| appsv1 "k8s.io/api/apps/v1" | ||
| corev1 "k8s.io/client-go/informers/core/v1" | ||
|
|
||
| opv1 "github.com/openshift/api/operator/v1" | ||
|
|
||
| "github.com/openshift/library-go/pkg/operator/csi/csiconfigobservercontroller" | ||
| "github.com/openshift/library-go/pkg/operator/resource/resourcehash" | ||
| "github.com/openshift/library-go/pkg/operator/v1helpers" | ||
| ) | ||
|
|
||
| // WithObservedProxyDeploymentHook creates a deployment hook that injects into the deployment's containers the observed proxy config. | ||
| func WithObservedProxyDeploymentHook() DeploymentHookFunc { | ||
| return func(opSpec *opv1.OperatorSpec, deployment *appsv1.Deployment) error { | ||
| containerNamesString := deployment.Annotations["config.openshift.io/inject-proxy"] | ||
| err := v1helpers.InjectObservedProxyIntoContainers( | ||
| &deployment.Spec.Template.Spec, | ||
| strings.Split(containerNamesString, ","), | ||
| opSpec.ObservedConfig.Raw, | ||
| csiconfigobservercontroller.ProxyConfigPath()..., | ||
| ) | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| // With SecretHashAnnotationHook creates a deployment hook that annotates a Deployment with a secret's hash. | ||
| func WithSecretHashAnnotationHook( | ||
| namespace string, | ||
| secretName string, | ||
| secretInformer corev1.SecretInformer, | ||
| ) DeploymentHookFunc { | ||
| return func(opSpec *opv1.OperatorSpec, deployment *appsv1.Deployment) error { | ||
| inputHashes, err := resourcehash.MultipleObjectHashStringMapForObjectReferenceFromLister( | ||
| nil, | ||
| secretInformer.Lister(), | ||
| resourcehash.NewObjectRef().ForSecret().InNamespace(namespace).Named(secretName), | ||
| ) | ||
| if err != nil { | ||
| return fmt.Errorf("invalid dependency reference: %w", err) | ||
| } | ||
| if deployment.Annotations == nil { | ||
| deployment.Annotations = map[string]string{} | ||
| } | ||
| if deployment.Spec.Template.Annotations == nil { | ||
| deployment.Spec.Template.Annotations = map[string]string{} | ||
| } | ||
| for k, v := range inputHashes { | ||
| annotationKey := fmt.Sprintf("operator.openshift.io/dep-%s", k) | ||
|
Member
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. lets move this into a var/constant
Member
Author
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. I'd like to leave this on as is. I prefer to leave format strings inline, plus I couldn't re-use the const for the annotation below ( |
||
| if len(annotationKey) > 63 { | ||
| hash := sha256.Sum256([]byte(k)) | ||
| annotationKey = fmt.Sprintf("operator.openshift.io/dep-%x", hash) | ||
| annotationKey = annotationKey[:63] | ||
| } | ||
| deployment.Annotations[annotationKey] = v | ||
| deployment.Spec.Template.Annotations[annotationKey] = v | ||
| } | ||
| return nil | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package csidrivernodeservicecontroller | ||
|
|
||
| import ( | ||
| "strings" | ||
|
|
||
| opv1 "github.com/openshift/api/operator/v1" | ||
| appsv1 "k8s.io/api/apps/v1" | ||
|
|
||
| "github.com/openshift/library-go/pkg/operator/csi/csiconfigobservercontroller" | ||
| "github.com/openshift/library-go/pkg/operator/v1helpers" | ||
| ) | ||
|
|
||
| // WithObservedProxyDaemonSetHook creates a hook that injects into the daemonSet's containers the observed proxy config. | ||
| func WithObservedProxyDaemonSetHook() DaemonSetHookFunc { | ||
| return func(opSpec *opv1.OperatorSpec, daemonSet *appsv1.DaemonSet) error { | ||
| containerNamesString := daemonSet.Annotations["config.openshift.io/inject-proxy"] | ||
| err := v1helpers.InjectObservedProxyIntoContainers( | ||
| &daemonSet.Spec.Template.Spec, | ||
| strings.Split(containerNamesString, ","), | ||
| opSpec.ObservedConfig.Raw, | ||
| csiconfigobservercontroller.ProxyConfigPath()..., | ||
| ) | ||
| return err | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.