Skip to content

Commit 754017f

Browse files
*: track and label user-provided service-accounts
Signed-off-by: Steve Kuznetsov <[email protected]>
1 parent d2c8252 commit 754017f

File tree

5 files changed

+55
-15
lines changed

5 files changed

+55
-15
lines changed

pkg/controller/operators/catalog/operator.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ func NewOperator(ctx context.Context, kubeconfigPath string, clock utilclock.Clo
185185
return nil, err
186186
}
187187

188-
canFilter, err := labeller.Validate(ctx, logger, metadataClient)
188+
canFilter, err := labeller.Validate(ctx, logger, metadataClient, crClient)
189189
if err != nil {
190190
return nil, err
191191
}
@@ -490,7 +490,18 @@ func NewOperator(ctx context.Context, kubeconfigPath string, clock utilclock.Clo
490490

491491
serviceaccountsgvk := corev1.SchemeGroupVersion.WithResource("serviceaccounts")
492492
if err := labelObjects(serviceaccountsgvk, serviceAccountInformer.Informer(), labeller.ObjectLabeler[*corev1.ServiceAccount, *corev1applyconfigurations.ServiceAccountApplyConfiguration](
493-
ctx, op.logger, labeller.Filter(serviceaccountsgvk),
493+
ctx, op.logger, labeller.ServiceAccountFilter(func(namespace, name string) bool {
494+
operatorGroups, err := operatorGroupInformer.Lister().OperatorGroups(namespace).List(labels.Everything())
495+
if err != nil {
496+
return false
497+
}
498+
for _, operatorGroup := range operatorGroups {
499+
if operatorGroup.Spec.ServiceAccountName == name {
500+
return true
501+
}
502+
}
503+
return false
504+
}),
494505
serviceAccountInformer.Lister().List,
495506
corev1applyconfigurations.ServiceAccount,
496507
func(namespace string, ctx context.Context, cfg *corev1applyconfigurations.ServiceAccountApplyConfiguration, opts metav1.ApplyOptions) (*corev1.ServiceAccount, error) {

pkg/controller/operators/labeller/filters.go

+31-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"strings"
77
"sync"
88

9+
operators "github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/clientset/versioned"
910
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/registry/reconciler"
1011
"github.com/sirupsen/logrus"
1112
"golang.org/x/sync/errgroup"
@@ -16,6 +17,8 @@ import (
1617
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
1718
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1819
"k8s.io/apimachinery/pkg/runtime/schema"
20+
"k8s.io/apimachinery/pkg/types"
21+
"k8s.io/apimachinery/pkg/util/sets"
1922
"k8s.io/client-go/metadata"
2023

2124
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/operators/internal/alongside"
@@ -49,15 +52,18 @@ func JobFilter(getConfigMap func(namespace, name string) (metav1.Object, error))
4952
}
5053
}
5154

55+
func ServiceAccountFilter(isServiceAccountReferenced func(namespace, name string) bool) func(object metav1.Object) bool {
56+
return func(object metav1.Object) bool {
57+
return HasOLMOwnerRef(object) || HasOLMLabel(object) || isServiceAccountReferenced(object.GetNamespace(), object.GetName())
58+
}
59+
}
60+
5261
var filters = map[schema.GroupVersionResource]func(metav1.Object) bool{
5362
corev1.SchemeGroupVersion.WithResource("services"): HasOLMOwnerRef,
5463
corev1.SchemeGroupVersion.WithResource("pods"): func(object metav1.Object) bool {
5564
_, ok := object.GetLabels()[reconciler.CatalogSourceLabelKey]
5665
return ok
5766
},
58-
corev1.SchemeGroupVersion.WithResource("serviceaccounts"): func(object metav1.Object) bool {
59-
return HasOLMOwnerRef(object) || HasOLMLabel(object)
60-
},
6167
appsv1.SchemeGroupVersion.WithResource("deployments"): HasOLMOwnerRef,
6268
rbacv1.SchemeGroupVersion.WithResource("roles"): HasOLMOwnerRef,
6369
rbacv1.SchemeGroupVersion.WithResource("rolebindings"): HasOLMOwnerRef,
@@ -73,7 +79,7 @@ var filters = map[schema.GroupVersionResource]func(metav1.Object) bool{
7379
},
7480
}
7581

76-
func Validate(ctx context.Context, logger *logrus.Logger, metadataClient metadata.Interface) (bool, error) {
82+
func Validate(ctx context.Context, logger *logrus.Logger, metadataClient metadata.Interface, operatorClient operators.Interface) (bool, error) {
7783
okLock := sync.Mutex{}
7884
ok := true
7985
g, ctx := errgroup.WithContext(ctx)
@@ -96,6 +102,27 @@ func Validate(ctx context.Context, logger *logrus.Logger, metadataClient metadat
96102
return previous != nil && previous(object) && ContentHashFilter(object)
97103
}
98104
}
105+
106+
operatorGroups, err := operatorClient.OperatorsV1().OperatorGroups(metav1.NamespaceAll).List(ctx, metav1.ListOptions{})
107+
if err != nil {
108+
return false, err
109+
}
110+
userProvidedServiceAccounts := sets.New[types.NamespacedName]()
111+
for _, operatorGroup := range operatorGroups.Items {
112+
if operatorGroup.Spec.ServiceAccountName != "" {
113+
userProvidedServiceAccounts.Insert(types.NamespacedName{
114+
Namespace: operatorGroup.Namespace,
115+
Name: operatorGroup.Spec.ServiceAccountName,
116+
})
117+
}
118+
}
119+
allFilters[corev1.SchemeGroupVersion.WithResource("serviceaccounts")] = ServiceAccountFilter(func(namespace, name string) bool {
120+
return userProvidedServiceAccounts.Has(types.NamespacedName{
121+
Namespace: namespace,
122+
Name: name,
123+
})
124+
})
125+
99126
for gvr, filter := range allFilters {
100127
gvr, filter := gvr, filter
101128
g.Go(func() error {

pkg/controller/operators/olm/operator.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ func newOperatorWithConfig(ctx context.Context, config *operatorConfig) (*Operat
186186
return nil, err
187187
}
188188

189-
canFilter, err := labeller.Validate(ctx, config.logger, config.metadataClient)
189+
canFilter, err := labeller.Validate(ctx, config.logger, config.metadataClient, config.externalClient)
190190
if err != nil {
191191
return nil, err
192192
}

pkg/controller/operators/olm/operator_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -4364,6 +4364,7 @@ func TestSyncOperatorGroups(t *testing.T) {
43644364
annotatedDeployment := ownedDeployment.DeepCopy()
43654365
annotatedDeployment.Spec.Template.SetAnnotations(map[string]string{operatorsv1.OperatorGroupTargetsAnnotationKey: operatorNamespace + "," + targetNamespace, operatorsv1.OperatorGroupAnnotationKey: "operator-group-1", operatorsv1.OperatorGroupNamespaceAnnotationKey: operatorNamespace})
43664366
annotatedDeployment.SetLabels(map[string]string{
4367+
"olm.managed": "true",
43674368
"olm.owner": "csv1",
43684369
"olm.owner.namespace": "operator-ns",
43694370
"olm.owner.kind": "ClusterServiceVersion",
@@ -4373,6 +4374,7 @@ func TestSyncOperatorGroups(t *testing.T) {
43734374
annotatedGlobalDeployment := ownedDeployment.DeepCopy()
43744375
annotatedGlobalDeployment.Spec.Template.SetAnnotations(map[string]string{operatorsv1.OperatorGroupTargetsAnnotationKey: "", operatorsv1.OperatorGroupAnnotationKey: "operator-group-1", operatorsv1.OperatorGroupNamespaceAnnotationKey: operatorNamespace})
43754376
annotatedGlobalDeployment.SetLabels(map[string]string{
4377+
"olm.managed": "true",
43764378
"olm.owner": "csv1",
43774379
"olm.owner.namespace": "operator-ns",
43784380
"olm.owner.kind": "ClusterServiceVersion",

pkg/lib/scoped/syncer.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,6 @@ func (s *UserDefinedServiceAccountSyncer) SyncOperatorGroup(in *v1.OperatorGroup
7878
return
7979
}
8080

81-
// A service account has been specified, but likely does not have the labels we expect it to have so it will
82-
// show up in our listers, so let's add that and queue again later
83-
config := corev1applyconfigurations.ServiceAccount(serviceAccountName, namespace)
84-
config.Labels = map[string]string{install.OLMManagedLabelKey: install.OLMManagedLabelValue}
85-
if _, err := s.client.KubernetesInterface().CoreV1().ServiceAccounts(namespace).Apply(context.TODO(), config, metav1.ApplyOptions{FieldManager: "operator-lifecycle-manager"}); err != nil {
86-
return out, fmt.Errorf("failed to apply labels[%s]=%s to serviceaccount %s/%s: %w", install.OLMManagedLabelKey, install.OLMManagedLabelValue, namespace, serviceAccountName, err)
87-
}
88-
8981
// A service account has been specified, we need to update the status.
9082
sa, err := s.client.KubernetesInterface().CoreV1().ServiceAccounts(namespace).Get(context.TODO(), serviceAccountName, metav1.GetOptions{})
9183
if err != nil {
@@ -108,6 +100,14 @@ func (s *UserDefinedServiceAccountSyncer) SyncOperatorGroup(in *v1.OperatorGroup
108100
return
109101
}
110102

103+
// A service account has been specified, but likely does not have the labels we expect it to have so it will
104+
// show up in our listers, so let's add that and queue again later
105+
config := corev1applyconfigurations.ServiceAccount(serviceAccountName, namespace)
106+
config.Labels = map[string]string{install.OLMManagedLabelKey: install.OLMManagedLabelValue}
107+
if _, err := s.client.KubernetesInterface().CoreV1().ServiceAccounts(namespace).Apply(context.TODO(), config, metav1.ApplyOptions{FieldManager: "operator-lifecycle-manager"}); err != nil {
108+
return out, fmt.Errorf("failed to apply labels[%s]=%s to serviceaccount %s/%s: %w", install.OLMManagedLabelKey, install.OLMManagedLabelValue, namespace, serviceAccountName, err)
109+
}
110+
111111
ref, err := reference.GetReference(s.scheme, sa)
112112
if err != nil {
113113
return

0 commit comments

Comments
 (0)