-
Notifications
You must be signed in to change notification settings - Fork 545
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
*: filter informers when preconditions are met
When we can detect at startup time that all of the objects we're about to look at have the labels we're expecting, we can filter our informer factories upfront. Signed-off-by: Steve Kuznetsov <skuznets@redhat.com>
- Loading branch information
1 parent
8ccd442
commit 96cca36
Showing
3 changed files
with
173 additions
and
51 deletions.
There are no files selected for viewing
This file contains 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 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,106 @@ | ||
package labeller | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
"sync" | ||
|
||
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/registry/reconciler" | ||
"github.com/sirupsen/logrus" | ||
"golang.org/x/sync/errgroup" | ||
appsv1 "k8s.io/api/apps/v1" | ||
batchv1 "k8s.io/api/batch/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
rbacv1 "k8s.io/api/rbac/v1" | ||
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"k8s.io/client-go/metadata" | ||
|
||
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/operators/internal/alongside" | ||
) | ||
|
||
func Filter(gvr schema.GroupVersionResource) func(metav1.Object) bool { | ||
if f, ok := filters[gvr]; ok { | ||
return f | ||
} | ||
return func(object metav1.Object) bool { | ||
return false | ||
} | ||
} | ||
|
||
var filters = map[schema.GroupVersionResource]func(metav1.Object) bool{ | ||
corev1.SchemeGroupVersion.WithResource("services"): HasOLMOwnerRef, | ||
corev1.SchemeGroupVersion.WithResource("pods"): func(object metav1.Object) bool { | ||
if labels := object.GetLabels(); labels != nil { | ||
if _, ok := labels[reconciler.CatalogSourceLabelKey]; ok { | ||
return true | ||
} | ||
} | ||
return false | ||
}, | ||
corev1.SchemeGroupVersion.WithResource("serviceaccounts"): func(object metav1.Object) bool { | ||
return HasOLMOwnerRef(object) || HasOLMLabel(object) | ||
}, | ||
batchv1.SchemeGroupVersion.WithResource("jobs"): func(object metav1.Object) bool { | ||
job, ok := object.(*batchv1.Job) | ||
if !ok { | ||
return false | ||
} | ||
for _, container := range job.Spec.Template.Spec.Containers { | ||
if strings.Join(container.Command[0:3], " ") == "opm alpha bundle extract" { | ||
return true | ||
} | ||
} | ||
return false | ||
}, | ||
appsv1.SchemeGroupVersion.WithResource("deployments"): HasOLMOwnerRef, | ||
rbacv1.SchemeGroupVersion.WithResource("roles"): HasOLMOwnerRef, | ||
rbacv1.SchemeGroupVersion.WithResource("rolebindings"): HasOLMOwnerRef, | ||
rbacv1.SchemeGroupVersion.WithResource("clusterroles"): HasOLMOwnerRef, | ||
rbacv1.SchemeGroupVersion.WithResource("clusterrolebindingss"): HasOLMOwnerRef, | ||
apiextensionsv1.SchemeGroupVersion.WithResource("customresourcedefinitions"): func(object metav1.Object) bool { | ||
for key := range object.GetAnnotations() { | ||
if strings.HasPrefix(key, alongside.AnnotationPrefix) { | ||
return true | ||
} | ||
} | ||
return false | ||
}, | ||
} | ||
|
||
func Validate(ctx context.Context, logger *logrus.Logger, metadataClient metadata.Interface) (bool, error) { | ||
okLock := sync.Mutex{} | ||
var ok bool | ||
g, ctx := errgroup.WithContext(ctx) | ||
for gvr, filter := range filters { | ||
gvr, filter := gvr, filter | ||
g.Go(func() error { | ||
list, err := metadataClient.Resource(gvr).List(ctx, metav1.ListOptions{}) | ||
if err != nil { | ||
return fmt.Errorf("failed to list %s: %w", gvr.String(), err) | ||
} | ||
var count int | ||
for _, item := range list.Items { | ||
if filter(&item) && !hasLabel(&item) { | ||
count++ | ||
} | ||
} | ||
if count > 0 { | ||
logger.WithFields(logrus.Fields{ | ||
"gvr": gvr.String(), | ||
"nonconforming": count, | ||
}).Info("found nonconforming items") | ||
} | ||
okLock.Lock() | ||
ok = ok && count == 0 | ||
okLock.Unlock() | ||
return nil | ||
}) | ||
} | ||
if err := g.Wait(); err != nil { | ||
return false, err | ||
} | ||
return ok, nil | ||
} |
This file contains 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