Skip to content

Commit

Permalink
olm/manager: don't use a global label selector
Browse files Browse the repository at this point in the history
Signed-off-by: Steve Kuznetsov <skuznets@redhat.com>
  • Loading branch information
stevekuznetsov committed Oct 3, 2023
1 parent c5e6b63 commit 2f02237
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 26 deletions.
36 changes: 35 additions & 1 deletion cmd/olm/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@ package main
import (
"context"

appsv1 "k8s.io/api/apps/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/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/selection"
apiregistrationv1 "k8s.io/kube-aggregator/pkg/apis/apiregistration/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -51,8 +56,37 @@ func Manager(ctx context.Context, debug bool) (ctrl.Manager, error) {
Scheme: scheme,
MetricsBindAddress: "0", // TODO(njhale): Enable metrics on non-conflicting port (not 8080)
Cache: cache.Options{
DefaultLabelSelector: labels.SelectorFromValidatedSet(map[string]string{install.OLMManagedLabelKey: install.OLMManagedLabelValue}),
ByObject: map[client.Object]cache.ByObject{
&appsv1.Deployment{}: {
Label: labels.SelectorFromValidatedSet(map[string]string{install.OLMManagedLabelKey: install.OLMManagedLabelValue}),
},
&corev1.Service{}: {
Label: labels.SelectorFromValidatedSet(map[string]string{install.OLMManagedLabelKey: install.OLMManagedLabelValue}),
},
&apiextensionsv1.CustomResourceDefinition{}: {
Label: labels.SelectorFromValidatedSet(map[string]string{install.OLMManagedLabelKey: install.OLMManagedLabelValue}),
},
&apiregistrationv1.APIService{}: {
Label: labels.SelectorFromValidatedSet(map[string]string{install.OLMManagedLabelKey: install.OLMManagedLabelValue}),
},
&corev1.ConfigMap{}: {
Label: labels.SelectorFromValidatedSet(map[string]string{install.OLMManagedLabelKey: install.OLMManagedLabelValue}),
},
&corev1.ServiceAccount{}: {
Label: labels.SelectorFromValidatedSet(map[string]string{install.OLMManagedLabelKey: install.OLMManagedLabelValue}),
},
&rbacv1.Role{}: {
Label: labels.SelectorFromValidatedSet(map[string]string{install.OLMManagedLabelKey: install.OLMManagedLabelValue}),
},
&rbacv1.RoleBinding{}: {
Label: labels.SelectorFromValidatedSet(map[string]string{install.OLMManagedLabelKey: install.OLMManagedLabelValue}),
},
&rbacv1.ClusterRole{}: {
Label: labels.SelectorFromValidatedSet(map[string]string{install.OLMManagedLabelKey: install.OLMManagedLabelValue}),
},
&rbacv1.ClusterRoleBinding{}: {
Label: labels.SelectorFromValidatedSet(map[string]string{install.OLMManagedLabelKey: install.OLMManagedLabelValue}),
},
&operatorsv1alpha1.ClusterServiceVersion{}: {
Label: copiedLabelDoesNotExist,
},
Expand Down
47 changes: 22 additions & 25 deletions test/e2e/operator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/onsi/gomega/format"
gomegatypes "github.com/onsi/gomega/types"
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/install"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -266,20 +267,22 @@ var _ = Describe("Operator API", func() {
)

BeforeEach(func() {
// Subscribe to a package and await a successful install
By("Subscribe to a package and await a successful install")
ns = &corev1.Namespace{}
ns.SetName(genName("ns-"))
Eventually(func() error {
return client.Create(clientCtx, ns)
}).Should(Succeed())
By(fmt.Sprintf("created namespace %s", ns.Name))

// Default to AllNamespaces
By("Default to AllNamespaces")
og := &operatorsv1.OperatorGroup{}
og.SetNamespace(ns.GetName())
og.SetName(genName("og-"))
Eventually(func() error {
return client.Create(clientCtx, og)
}).Should(Succeed())
By(fmt.Sprintf("created operator group %s/%s", og.Namespace, og.Name))

cs := &operatorsv1alpha1.CatalogSource{
Spec: operatorsv1alpha1.CatalogSourceSpec{
Expand All @@ -295,8 +298,9 @@ var _ = Describe("Operator API", func() {
Eventually(func() error {
return client.Create(clientCtx, cs)
}).Should(Succeed())
By(fmt.Sprintf("created catalog source %s/%s", cs.Namespace, cs.Name))

// Wait for the CatalogSource to be ready
By("Wait for the CatalogSource to be ready")
_, err := fetchCatalogSourceOnStatus(newCRClient(), cs.GetName(), cs.GetNamespace(), catalogSourceRegistryPodSynced())
Expect(err).ToNot(HaveOccurred())

Expand All @@ -314,38 +318,31 @@ var _ = Describe("Operator API", func() {
Eventually(func() error {
return client.Create(clientCtx, sub)
}).Should(Succeed())
By(fmt.Sprintf("created subscription %s/%s", sub.Namespace, sub.Name))

Eventually(func() (operatorsv1alpha1.SubscriptionState, error) {
s := sub.DeepCopy()
if err := client.Get(clientCtx, testobj.NamespacedName(s), s); err != nil {
return operatorsv1alpha1.SubscriptionStateNone, err
}

return s.Status.State, nil
}).Should(BeEquivalentTo(operatorsv1alpha1.SubscriptionStateAtLatest))
_, err = fetchSubscription(newCRClient(), sub.Namespace, sub.Name, subscriptionStateAtLatestChecker())
require.NoError(GinkgoT(), err)

var ipRef *corev1.ObjectReference
Eventually(func() (*corev1.ObjectReference, error) {
if err := client.Get(clientCtx, testobj.NamespacedName(sub), sub); err != nil {
return nil, err
}
ipRef = sub.Status.InstallPlanRef
subscriptionWithInstallPLan, err := fetchSubscription(newCRClient(), sub.Namespace, sub.Name, subscriptionHasInstallPlanChecker())
require.NoError(GinkgoT(), err)
require.NotNil(GinkgoT(), subscriptionWithInstallPLan)
ipRef := subscriptionWithInstallPLan.Status.InstallPlanRef

return ipRef, nil
}).ShouldNot(BeNil())

ip = &operatorsv1alpha1.InstallPlan{}
Eventually(func() error {
return client.Get(clientCtx, types.NamespacedName{Namespace: ipRef.Namespace, Name: ipRef.Name}, ip)
}).Should(Succeed())
_, err = fetchInstallPlan(GinkgoT(), newCRClient(), ipRef.Name, ipRef.Namespace, buildInstallPlanPhaseCheckFunc(operatorsv1alpha1.InstallPlanPhaseComplete))
Expect(err).To(BeNil())

operator, err := operatorFactory.NewPackageOperator(sub.Spec.Package, sub.GetNamespace())
Expect(err).ToNot(HaveOccurred())
operatorName = testobj.NamespacedName(operator)
By(fmt.Sprintf("waiting for operator %s/%s to exist", operator.Namespace, operator.Name))
})

AfterEach(func() {
Eventually(func() error {
if env := os.Getenv("SKIP_CLEANUP"); env != "" {
fmt.Printf("Skipping cleanup of namespace %s...\n", ns.Name)
return nil
}
err := client.Delete(clientCtx, ns)
if apierrors.IsNotFound(err) {
return nil
Expand Down Expand Up @@ -386,7 +383,7 @@ var _ = Describe("Operator API", func() {
var newNs *corev1.Namespace

BeforeEach(func() {
// Subscribe to a package and await a successful install
By("Subscribe to a package and await a successful install")
newNs = &corev1.Namespace{}
newNs.SetName(genName("ns-"))
Eventually(func() error {
Expand Down

0 comments on commit 2f02237

Please sign in to comment.