|
| 1 | +package plugins |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/openshift/cluster-policy-controller/pkg/psalabelsyncer/nsexemptions" |
| 9 | + "github.com/operator-framework/api/pkg/operators/v1alpha1" |
| 10 | + "github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/clientset/versioned" |
| 11 | + "github.com/operator-framework/operator-lifecycle-manager/pkg/controller/operators/internal/pruning" |
| 12 | + "github.com/operator-framework/operator-lifecycle-manager/pkg/lib/kubestate" |
| 13 | + "github.com/operator-framework/operator-lifecycle-manager/pkg/lib/operatorclient" |
| 14 | + "github.com/operator-framework/operator-lifecycle-manager/pkg/lib/queueinformer" |
| 15 | + "github.com/sirupsen/logrus" |
| 16 | + v1 "k8s.io/api/core/v1" |
| 17 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 18 | + "k8s.io/client-go/informers" |
| 19 | + listerv1 "k8s.io/client-go/listers/core/v1" |
| 20 | + "k8s.io/client-go/tools/cache" |
| 21 | + "k8s.io/client-go/util/workqueue" |
| 22 | +) |
| 23 | + |
| 24 | +const NamespaceLabelSyncerLabelKey = "security.openshift.io/scc.podSecurityLabelSync" |
| 25 | + |
| 26 | +// csvNamespaceLabelerPlugin is responsible for labeling non-payload openshift-* namespaces |
| 27 | +// with the label "security.openshift.io/scc.podSecurityLabelSync=true" so that the PSA Label Syncer |
| 28 | +// see https://github.com/openshift/cluster-policy-controller/blob/master/pkg/psalabelsyncer/podsecurity_label_sync_controller.go |
| 29 | +// can help ensure that the operator payloads in the namespace continue to work even if they don't yet respect the |
| 30 | +// upstream Pod Security Admission controller, which will become active in k8s 1.15. |
| 31 | +// see https://kubernetes.io/docs/concepts/security/pod-security-admission/ |
| 32 | +// If a CSV is created or modified, this controller will look at the csv's namespace. If it is a non-payload namespace, |
| 33 | +// if the namespace name is prefixed with 'openshift-', and if the namespace does not contain the label (whatever |
| 34 | +// value it may be set to), it will add the "security.openshift.io/scc.podSecurityLabelSync=true" to the namespace. |
| 35 | +type csvNamespaceLabelerPlugin struct { |
| 36 | + namespaceListerMap map[string]listerv1.NamespaceLister |
| 37 | + kubeClient operatorclient.ClientInterface |
| 38 | + externalClient versioned.Interface |
| 39 | + logger *logrus.Logger |
| 40 | +} |
| 41 | + |
| 42 | +func NewCsvNamespaceLabelerPlugInfunc(ctx context.Context, config OperatorConfig, hostOperator HostOperator) (OperatorPlugin, error) { |
| 43 | + |
| 44 | + if hostOperator == nil { |
| 45 | + return nil, fmt.Errorf("cannot initialize plugin: operator undefined") |
| 46 | + } |
| 47 | + |
| 48 | + plugin := &csvNamespaceLabelerPlugin{ |
| 49 | + kubeClient: config.OperatorClient(), |
| 50 | + externalClient: config.ExternalClient(), |
| 51 | + logger: config.Logger(), |
| 52 | + namespaceListerMap: map[string]listerv1.NamespaceLister{}, |
| 53 | + } |
| 54 | + |
| 55 | + for _, namespace := range config.WatchedNamespaces() { |
| 56 | + |
| 57 | + // create a namespace informer for namespaces that do not include |
| 58 | + // the label syncer label |
| 59 | + namespaceInformer := informers.NewSharedInformerFactoryWithOptions( |
| 60 | + plugin.kubeClient.KubernetesInterface(), |
| 61 | + config.ResyncPeriod()(), |
| 62 | + informers.WithNamespace(namespace), |
| 63 | + ).Core().V1().Namespaces() |
| 64 | + |
| 65 | + if err := hostOperator.RegisterInformer(namespaceInformer.Informer()); err != nil { |
| 66 | + return nil, err |
| 67 | + } |
| 68 | + plugin.namespaceListerMap[namespace] = namespaceInformer.Lister() |
| 69 | + |
| 70 | + // create a new csv informer and prune status to reduce memory footprint |
| 71 | + csvNamespaceLabelerInformer := cache.NewSharedIndexInformer( |
| 72 | + pruning.NewListerWatcher( |
| 73 | + plugin.externalClient, |
| 74 | + namespace, |
| 75 | + func(opts *metav1.ListOptions) { |
| 76 | + opts.LabelSelector = fmt.Sprintf("!%s", v1alpha1.CopiedLabelKey) |
| 77 | + }, |
| 78 | + pruning.PrunerFunc(func(csv *v1alpha1.ClusterServiceVersion) { |
| 79 | + *csv = v1alpha1.ClusterServiceVersion{ |
| 80 | + TypeMeta: csv.TypeMeta, |
| 81 | + ObjectMeta: csv.ObjectMeta, |
| 82 | + } |
| 83 | + }), |
| 84 | + ), |
| 85 | + &v1alpha1.ClusterServiceVersion{}, |
| 86 | + config.ResyncPeriod()(), |
| 87 | + cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc}, |
| 88 | + ) |
| 89 | + |
| 90 | + csvNamespaceLabelerPluginQueue := workqueue.NewNamedRateLimitingQueue( |
| 91 | + workqueue.DefaultControllerRateLimiter(), |
| 92 | + fmt.Sprintf("%s/csv-ns-labeler-plugin", namespace), |
| 93 | + ) |
| 94 | + csvNamespaceLabelerPluginQueueInformer, err := queueinformer.NewQueueInformer( |
| 95 | + ctx, |
| 96 | + queueinformer.WithInformer(csvNamespaceLabelerInformer), |
| 97 | + queueinformer.WithLogger(config.Logger()), |
| 98 | + queueinformer.WithQueue(csvNamespaceLabelerPluginQueue), |
| 99 | + queueinformer.WithIndexer(csvNamespaceLabelerInformer.GetIndexer()), |
| 100 | + queueinformer.WithSyncer(plugin), |
| 101 | + ) |
| 102 | + if err != nil { |
| 103 | + return nil, err |
| 104 | + } |
| 105 | + if err := hostOperator.RegisterQueueInformer(csvNamespaceLabelerPluginQueueInformer); err != nil { |
| 106 | + return nil, err |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + return plugin, nil |
| 111 | +} |
| 112 | + |
| 113 | +func (p *csvNamespaceLabelerPlugin) Shutdown() error { |
| 114 | + return nil |
| 115 | +} |
| 116 | + |
| 117 | +func (p *csvNamespaceLabelerPlugin) Sync(ctx context.Context, event kubestate.ResourceEvent) error { |
| 118 | + // only act on csv added and updated events |
| 119 | + if event.Type() != kubestate.ResourceAdded && event.Type() != kubestate.ResourceUpdated { |
| 120 | + return nil |
| 121 | + } |
| 122 | + |
| 123 | + csv, ok := event.Resource().(*v1alpha1.ClusterServiceVersion) |
| 124 | + if !ok { |
| 125 | + return fmt.Errorf("event resource is not a ClusterServiceVersion") |
| 126 | + } |
| 127 | + |
| 128 | + // ignore copied csvs |
| 129 | + // informer should already be filtering these out - but just in case |
| 130 | + if csv.IsCopied() { |
| 131 | + return nil |
| 132 | + } |
| 133 | + |
| 134 | + // ignore non-openshift-* and payload openshift-* namespaces |
| 135 | + if !strings.HasPrefix(csv.GetNamespace(), "openshift-") || nsexemptions.IsNamespacePSALabelSyncExemptedInVendoredOCPVersion(csv.GetNamespace()) { |
| 136 | + return nil |
| 137 | + } |
| 138 | + |
| 139 | + namespace, err := p.getNamespace(csv.GetNamespace()) |
| 140 | + if err != nil { |
| 141 | + return fmt.Errorf("error getting csv namespace (%s) for label sync'er labeling", csv.GetNamespace()) |
| 142 | + } |
| 143 | + |
| 144 | + // add label sync'er label if it does not exist |
| 145 | + if _, ok := namespace.GetLabels()[NamespaceLabelSyncerLabelKey]; !ok { |
| 146 | + nsCopy := namespace.DeepCopy() |
| 147 | + if nsCopy.GetLabels() == nil { |
| 148 | + nsCopy.SetLabels(map[string]string{}) |
| 149 | + } |
| 150 | + nsCopy.GetLabels()[NamespaceLabelSyncerLabelKey] = "true" |
| 151 | + if _, err := p.kubeClient.KubernetesInterface().CoreV1().Namespaces().Update(ctx, nsCopy, metav1.UpdateOptions{}); err != nil { |
| 152 | + return fmt.Errorf("error updating csv namespace (%s) with label sync'er label", nsCopy.GetNamespace()) |
| 153 | + } |
| 154 | + |
| 155 | + if p.logger != nil { |
| 156 | + p.logger.Infof("applied %s=true label to namespace %s", NamespaceLabelSyncerLabelKey, nsCopy.GetNamespace()) |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + return nil |
| 161 | +} |
| 162 | + |
| 163 | +func (p *csvNamespaceLabelerPlugin) getNamespace(namespace string) (*v1.Namespace, error) { |
| 164 | + lister, ok := p.namespaceListerMap[namespace] |
| 165 | + if !ok { |
| 166 | + lister, ok = p.namespaceListerMap[metav1.NamespaceAll] |
| 167 | + if !ok { |
| 168 | + return nil, fmt.Errorf("no namespace lister found for namespace: %s", namespace) |
| 169 | + } |
| 170 | + } |
| 171 | + return lister.Get(namespace) |
| 172 | +} |
0 commit comments