-
Notifications
You must be signed in to change notification settings - Fork 562
resolver: Add support for excluding global catalogs from resolution #2788
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
Merged
openshift-ci
merged 1 commit into
operator-framework:master
from
exdx:exclude-global-ns
Jun 16, 2022
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,82 @@ | ||
package catalog | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
v1listers "github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/listers/operators/v1" | ||
|
||
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/registry/resolver/cache" | ||
"github.com/sirupsen/logrus" | ||
"k8s.io/apimachinery/pkg/labels" | ||
) | ||
|
||
type OperatorGroupToggleSourceProvider struct { | ||
sp cache.SourceProvider | ||
logger *logrus.Logger | ||
ogLister v1listers.OperatorGroupLister | ||
} | ||
|
||
func NewOperatorGroupToggleSourceProvider(sp cache.SourceProvider, logger *logrus.Logger, | ||
ogLister v1listers.OperatorGroupLister) *OperatorGroupToggleSourceProvider { | ||
return &OperatorGroupToggleSourceProvider{ | ||
sp: sp, | ||
logger: logger, | ||
ogLister: ogLister, | ||
} | ||
} | ||
|
||
const exclusionAnnotation string = "olm.operatorframework.io/exclude-global-namespace-resolution" | ||
|
||
func (e *OperatorGroupToggleSourceProvider) Sources(namespaces ...string) map[cache.SourceKey]cache.Source { | ||
// Check if annotation is set first | ||
resolutionNamespaces, err := e.CheckForExclusion(namespaces...) | ||
if err != nil { | ||
e.logger.Errorf("error checking namespaces %#v for global resolution exlusion: %s", namespaces, err) | ||
// Fail early with a dummy Source that returns an error | ||
// TODO: Update the Sources interface to return an error | ||
m := make(map[cache.SourceKey]cache.Source) | ||
key := cache.SourceKey{Name: "operatorgroup-unavailable", Namespace: namespaces[0]} | ||
source := errorSource{err} | ||
m[key] = source | ||
return m | ||
} | ||
return e.sp.Sources(resolutionNamespaces...) | ||
} | ||
|
||
type errorSource struct { | ||
error | ||
} | ||
|
||
func (e errorSource) Snapshot(ctx context.Context) (*cache.Snapshot, error) { | ||
return nil, e.error | ||
} | ||
|
||
func (e *OperatorGroupToggleSourceProvider) CheckForExclusion(namespaces ...string) ([]string, error) { | ||
var defaultResult = namespaces | ||
// The first namespace provided is always the current namespace being synced | ||
var ownNamespace = namespaces[0] | ||
var toggledResult = []string{ownNamespace} | ||
|
||
// Check the OG on the NS provided for the exclusion annotation | ||
ogs, err := e.ogLister.OperatorGroups(ownNamespace).List(labels.Everything()) | ||
if err != nil { | ||
return nil, fmt.Errorf("listing operatorgroups in namespace %s: %s", ownNamespace, err) | ||
} | ||
|
||
if len(ogs) != 1 { | ||
// Problem with the operatorgroup configuration in the namespace, or the operatorgroup has not yet been persisted | ||
// Note: a resync will be triggered if/when the operatorgroup becomes available | ||
return nil, fmt.Errorf("found %d operatorgroups in namespace %s: expected 1", len(ogs), ownNamespace) | ||
} | ||
|
||
var og = ogs[0] | ||
if val, ok := og.Annotations[exclusionAnnotation]; ok && val == "true" { | ||
// Exclusion specified | ||
// Ignore the globalNamespace for the purposes of resolution in this namespace | ||
e.logger.Printf("excluding global catalogs from resolution in namespace %s", ownNamespace) | ||
return toggledResult, nil | ||
} | ||
|
||
return defaultResult, nil | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,133 @@ | ||
package e2e | ||
|
||
import ( | ||
"context" | ||
"path/filepath" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
operatorsv1 "github.com/operator-framework/api/pkg/operators/v1" | ||
"github.com/operator-framework/api/pkg/operators/v1alpha1" | ||
"github.com/operator-framework/operator-lifecycle-manager/test/e2e/ctx" | ||
"github.com/operator-framework/operator-lifecycle-manager/test/e2e/util" | ||
. "github.com/operator-framework/operator-lifecycle-manager/test/e2e/util/gomega" | ||
"google.golang.org/grpc/connectivity" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
k8scontrollerclient "sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
const magicCatalogDir = "magiccatalog" | ||
|
||
var _ = Describe("Global Catalog Exclusion", func() { | ||
var ( | ||
testNamespace corev1.Namespace | ||
determinedE2eClient *util.DeterminedE2EClient | ||
operatorGroup operatorsv1.OperatorGroup | ||
localCatalog *MagicCatalog | ||
) | ||
|
||
BeforeEach(func() { | ||
determinedE2eClient = util.NewDeterminedClient(ctx.Ctx().E2EClient()) | ||
|
||
By("creating a namespace with an own namespace operator group without annotations") | ||
e2eTestNamespace := genName("global-catalog-exclusion-e2e-") | ||
operatorGroup = operatorsv1.OperatorGroup{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: e2eTestNamespace, | ||
Name: genName("og-"), | ||
Annotations: nil, | ||
}, | ||
Spec: operatorsv1.OperatorGroupSpec{ | ||
TargetNamespaces: []string{e2eTestNamespace}, | ||
}, | ||
} | ||
testNamespace = SetupGeneratedTestNamespaceWithOperatorGroup(e2eTestNamespace, operatorGroup) | ||
|
||
By("creating a broken catalog in the global namespace") | ||
globalCatalog := &v1alpha1.CatalogSource{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: genName("bad-global-catalog-"), | ||
Namespace: operatorNamespace, | ||
}, | ||
Spec: v1alpha1.CatalogSourceSpec{ | ||
DisplayName: "Broken Global Catalog Source", | ||
SourceType: v1alpha1.SourceTypeGrpc, | ||
Address: "1.1.1.1:1337", // points to non-existing service | ||
}, | ||
} | ||
_ = determinedE2eClient.Create(context.Background(), globalCatalog) | ||
|
||
By("creating a healthy catalog in the test namespace") | ||
localCatalogName := genName("good-catsrc-") | ||
var err error = nil | ||
|
||
fbcPath := filepath.Join(testdataDir, magicCatalogDir, "fbc_initial.yaml") | ||
localCatalog, err = NewMagicCatalogFromFile(determinedE2eClient, testNamespace.GetName(), localCatalogName, fbcPath) | ||
Expect(err).To(Succeed()) | ||
|
||
// deploy catalog blocks until the catalog has reached a ready state or fails | ||
Expect(localCatalog.DeployCatalog(context.Background())).To(Succeed()) | ||
|
||
By("checking that the global catalog is broken") | ||
// Adding this check here to speed up the test | ||
// the global catalog can fail while we wait for the local catalog to get to a ready state | ||
EventuallyResource(globalCatalog).Should(HaveGrpcConnectionWithLastConnectionState(connectivity.TransientFailure)) | ||
}) | ||
|
||
AfterEach(func() { | ||
TeardownNamespace(testNamespace.GetName()) | ||
}) | ||
|
||
When("a subscription referring to the local catalog is created", func() { | ||
var subscription *v1alpha1.Subscription | ||
|
||
BeforeEach(func() { | ||
subscription = &v1alpha1.Subscription{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: testNamespace.GetName(), | ||
Name: genName("local-subscription-"), | ||
}, | ||
Spec: &v1alpha1.SubscriptionSpec{ | ||
CatalogSource: localCatalog.GetName(), | ||
CatalogSourceNamespace: localCatalog.GetNamespace(), | ||
Package: "packageA", | ||
Channel: "stable", | ||
InstallPlanApproval: v1alpha1.ApprovalAutomatic, | ||
}, | ||
} | ||
|
||
By("creating a subscription") | ||
_ = determinedE2eClient.Create(context.Background(), subscription) | ||
}) | ||
|
||
When("the operator group is annotated with olm.operatorframework.io/exclude-global-namespace-resolution=true", func() { | ||
|
||
It("the broken subscription should resolve and have state AtLatest", func() { | ||
By("checking that the subscription is not resolving and has a condition with type ResolutionFailed") | ||
EventuallyResource(subscription).Should(ContainSubscriptionConditionOfType(v1alpha1.SubscriptionResolutionFailed)) | ||
|
||
By("annotating the operator group with olm.operatorframework.io/exclude-global-namespace-resolution=true") | ||
Eventually(func() error { | ||
annotatedOperatorGroup := operatorGroup.DeepCopy() | ||
if err := determinedE2eClient.Get(context.Background(), k8scontrollerclient.ObjectKeyFromObject(annotatedOperatorGroup), annotatedOperatorGroup); err != nil { | ||
return err | ||
} | ||
|
||
if annotatedOperatorGroup.Annotations == nil { | ||
annotatedOperatorGroup.Annotations = map[string]string{} | ||
} | ||
|
||
annotatedOperatorGroup.Annotations["olm.operatorframework.io/exclude-global-namespace-resolution"] = "true" | ||
if err := determinedE2eClient.Update(context.Background(), annotatedOperatorGroup); err != nil { | ||
return err | ||
} | ||
return nil | ||
}).Should(Succeed()) | ||
|
||
By("checking that the subscription resolves and has state AtLatest") | ||
EventuallyResource(subscription).Should(HaveSubscriptionState(v1alpha1.SubscriptionStateAtLatest)) | ||
}) | ||
}) | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.