Skip to content
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

olm,catalog: only validate the resources we label #3165

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion pkg/controller/operators/catalog/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func NewOperator(ctx context.Context, kubeconfigPath string, clock utilclock.Clo
return nil, err
}

canFilter, err := labeller.Validate(ctx, logger, metadataClient, crClient)
canFilter, err := labeller.Validate(ctx, logger, metadataClient, crClient, labeller.IdentityCatalogOperator)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -541,6 +541,49 @@ func NewOperator(ctx context.Context, kubeconfigPath string, clock utilclock.Clo
return nil, err
}

{
gvr := servicesgvk
informer := serviceInformer.Informer()

logger := op.logger.WithFields(logrus.Fields{"gvr": gvr.String()})
logger.Info("registering owner reference fixer")

queue := workqueue.NewRateLimitingQueueWithConfig(workqueue.DefaultControllerRateLimiter(), workqueue.RateLimitingQueueConfig{
Name: gvr.String(),
})
queueInformer, err := queueinformer.NewQueueInformer(
ctx,
queueinformer.WithQueue(queue),
queueinformer.WithLogger(op.logger),
queueinformer.WithInformer(informer),
queueinformer.WithSyncer(queueinformer.LegacySyncHandler(func(obj interface{}) error {
service, ok := obj.(*corev1.Service)
if !ok {
err := fmt.Errorf("wrong type %T, expected %T: %#v", obj, new(*corev1.Service), obj)
logger.WithError(err).Error("casting failed")
return fmt.Errorf("casting failed: %w", err)
}

deduped := deduplicateOwnerReferences(service.OwnerReferences)
if len(deduped) != len(service.OwnerReferences) {
localCopy := service.DeepCopy()
localCopy.OwnerReferences = deduped
if _, err := op.opClient.KubernetesInterface().CoreV1().Services(service.Namespace).Update(ctx, localCopy, metav1.UpdateOptions{}); err != nil {
return err
}
}
return nil
}).ToSyncer()),
)
if err != nil {
return nil, err
}

if err := op.RegisterQueueInformer(queueInformer); err != nil {
return nil, err
}
}

// Wire Pods for CatalogSource
catsrcReq, err := labels.NewRequirement(reconciler.CatalogSourceLabelKey, selection.Exists, nil)
if err != nil {
Expand Down Expand Up @@ -2860,3 +2903,7 @@ func (o *Operator) apiresourceFromGVK(gvk schema.GroupVersionKind) (metav1.APIRe
logger.Info("couldn't find GVK in api discovery")
return metav1.APIResource{}, olmerrors.GroupVersionKindNotFoundError{Group: gvk.Group, Version: gvk.Version, Kind: gvk.Kind}
}

func deduplicateOwnerReferences(refs []metav1.OwnerReference) []metav1.OwnerReference {
return sets.New(refs...).UnsortedList()
}
42 changes: 42 additions & 0 deletions pkg/controller/operators/catalog/ownerrefs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package catalog

import (
"testing"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestDedupe(t *testing.T) {
yes := true
refs := []metav1.OwnerReference{
{
APIVersion: "apiversion",
Kind: "kind",
Name: "name",
UID: "uid",
Controller: &yes,
BlockOwnerDeletion: &yes,
},
{
APIVersion: "apiversion",
Kind: "kind",
Name: "name",
UID: "uid",
Controller: &yes,
BlockOwnerDeletion: &yes,
},
{
APIVersion: "apiversion",
Kind: "kind",
Name: "name",
UID: "uid",
Controller: &yes,
BlockOwnerDeletion: &yes,
},
}
deduped := deduplicateOwnerReferences(refs)
t.Logf("got %d deduped from %d", len(deduped), len(refs))
if len(deduped) == len(refs) {
t.Errorf("didn't dedupe: %#v", deduped)
}
}
38 changes: 37 additions & 1 deletion pkg/controller/operators/labeller/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,39 @@ var filters = map[schema.GroupVersionResource]func(metav1.Object) bool{
},
}

func Validate(ctx context.Context, logger *logrus.Logger, metadataClient metadata.Interface, operatorClient operators.Interface) (bool, error) {
type Identity string

const (
IdentityCatalogOperator = "catalog-operator"
IdentityOLMOperator = "olm-operator"
)

func gvrRelevant(identity Identity) func(schema.GroupVersionResource) bool {
switch identity {
case IdentityCatalogOperator:
return sets.New(
rbacv1.SchemeGroupVersion.WithResource("roles"),
rbacv1.SchemeGroupVersion.WithResource("rolebindings"),
corev1.SchemeGroupVersion.WithResource("serviceaccounts"),
corev1.SchemeGroupVersion.WithResource("services"),
corev1.SchemeGroupVersion.WithResource("pods"),
corev1.SchemeGroupVersion.WithResource("configmaps"),
batchv1.SchemeGroupVersion.WithResource("jobs"),
apiextensionsv1.SchemeGroupVersion.WithResource("customresourcedefinitions"),
).Has
case IdentityOLMOperator:
return sets.New(
appsv1.SchemeGroupVersion.WithResource("deployments"),
rbacv1.SchemeGroupVersion.WithResource("clusterroles"),
rbacv1.SchemeGroupVersion.WithResource("clusterrolebindings"),
apiextensionsv1.SchemeGroupVersion.WithResource("customresourcedefinitions"),
).Has
default:
panic("programmer error: invalid identity")
}
}

func Validate(ctx context.Context, logger *logrus.Logger, metadataClient metadata.Interface, operatorClient operators.Interface, identity Identity) (bool, error) {
okLock := sync.Mutex{}
ok := true
g, ctx := errgroup.WithContext(ctx)
Expand Down Expand Up @@ -125,6 +157,10 @@ func Validate(ctx context.Context, logger *logrus.Logger, metadataClient metadat
})

for gvr, filter := range allFilters {
if !gvrRelevant(identity)(gvr) {
logger.WithField("gvr", gvr.String()).Info("skipping irrelevant gvr")
continue
}
gvr, filter := gvr, filter
g.Go(func() error {
list, err := metadataClient.Resource(gvr).List(ctx, metav1.ListOptions{})
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/operators/olm/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func newOperatorWithConfig(ctx context.Context, config *operatorConfig) (*Operat
return nil, err
}

canFilter, err := labeller.Validate(ctx, config.logger, config.metadataClient, config.externalClient)
canFilter, err := labeller.Validate(ctx, config.logger, config.metadataClient, config.externalClient, labeller.IdentityOLMOperator)
if err != nil {
return nil, err
}
Expand Down
25 changes: 12 additions & 13 deletions pkg/lib/ownerutil/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,20 +167,13 @@ func AddNonBlockingOwner(object metav1.Object, owner Owner) {
ownerRefs = []metav1.OwnerReference{}
}

// Infer TypeMeta for the target
if err := InferGroupVersionKind(owner); err != nil {
log.Warn(err.Error())
}
gvk := owner.GetObjectKind().GroupVersionKind()

nonBlockingOwner := NonBlockingOwner(owner)
for _, item := range ownerRefs {
if item.Kind == gvk.Kind {
if item.Name == owner.GetName() && item.UID == owner.GetUID() {
return
}
if item.Kind == nonBlockingOwner.Kind && item.Name == nonBlockingOwner.Name && item.UID == nonBlockingOwner.UID {
return
}
}
ownerRefs = append(ownerRefs, NonBlockingOwner(owner))
ownerRefs = append(ownerRefs, nonBlockingOwner)
object.SetOwnerReferences(ownerRefs)
}

Expand Down Expand Up @@ -284,14 +277,20 @@ func AddOwner(object metav1.Object, owner Owner, blockOwnerDeletion, isControlle
}
gvk := owner.GetObjectKind().GroupVersionKind()
apiVersion, kind := gvk.ToAPIVersionAndKind()
ownerRefs = append(ownerRefs, metav1.OwnerReference{
ownerRef := metav1.OwnerReference{
APIVersion: apiVersion,
Kind: kind,
Name: owner.GetName(),
UID: owner.GetUID(),
BlockOwnerDeletion: &blockOwnerDeletion,
Controller: &isController,
})
}
for _, ref := range ownerRefs {
if ref.Kind == ownerRef.Kind && ref.Name == ownerRef.Name && ref.UID == ownerRef.UID {
return
}
Comment on lines +289 to +291
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any chance that we call this function with the same object, but with different blockOwnerDeletion or isController values?

Curious if we find the owner ref, rather than leaving what's there untouched, we should update what's there?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe the codebase does any of that today. Generally speaking I would not expect isController to be something that logically could change.

}
ownerRefs = append(ownerRefs, ownerRef)
object.SetOwnerReferences(ownerRefs)
}

Expand Down
11 changes: 9 additions & 2 deletions pkg/lib/testobj/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,21 @@ func WithOwner(owner, obj RuntimeMetaObject) RuntimeMetaObject {
out := obj.DeepCopyObject().(RuntimeMetaObject)
gvk := owner.GetObjectKind().GroupVersionKind()
apiVersion, kind := gvk.ToAPIVersionAndKind()
refs := append(out.GetOwnerReferences(), metav1.OwnerReference{

nonBlockingOwner := metav1.OwnerReference{
APIVersion: apiVersion,
Kind: kind,
Name: owner.GetName(),
UID: owner.GetUID(),
BlockOwnerDeletion: &dontBlockOwnerDeletion,
Controller: &notController,
})
}
for _, item := range out.GetOwnerReferences() {
if item.Kind == nonBlockingOwner.Kind && item.Name == nonBlockingOwner.Name && item.UID == nonBlockingOwner.UID {
return out
}
}
refs := append(out.GetOwnerReferences(), nonBlockingOwner)
out.SetOwnerReferences(refs)

return out
Expand Down
Loading