-
Notifications
You must be signed in to change notification settings - Fork 552
Bug 2076187: Identify fail forward in csvSources #2743
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-merge-robot
merged 1 commit into
operator-framework:master
from
awgreene:update-csvSource
Apr 18, 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 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
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,103 @@ | ||
package resolver | ||
|
||
import ( | ||
"fmt" | ||
|
||
operatorsv1 "github.com/operator-framework/api/pkg/operators/v1" | ||
operatorsv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1" | ||
v1listers "github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/listers/operators/v1" | ||
"k8s.io/apimachinery/pkg/labels" | ||
) | ||
|
||
// IsFailForwardEnabled takes a namespaced operatorGroup lister and returns | ||
// True if an operatorGroup exists in the namespace and its upgradeStrategy | ||
// is set to UnsafeFailForward and false otherwise. An error is returned if | ||
// an more than one operatorGroup exists in the namespace. | ||
// No error is returned if no OperatorGroups are found to keep the resolver | ||
// backwards compatible. | ||
func IsFailForwardEnabled(ogLister v1listers.OperatorGroupNamespaceLister) (bool, error) { | ||
ogs, err := ogLister.List(labels.Everything()) | ||
if err != nil || len(ogs) == 0 { | ||
return false, nil | ||
} | ||
if len(ogs) != 1 { | ||
return false, fmt.Errorf("found %d operatorGroups, expected 1", len(ogs)) | ||
} | ||
return ogs[0].UpgradeStrategy() == operatorsv1.UpgradeStrategyUnsafeFailForward, nil | ||
} | ||
|
||
type walkOption func(csv *operatorsv1alpha1.ClusterServiceVersion) error | ||
|
||
// WithCSVPhase returns an error if the CSV is not in the given phase. | ||
func WithCSVPhase(phase operatorsv1alpha1.ClusterServiceVersionPhase) walkOption { | ||
return func(csv *operatorsv1alpha1.ClusterServiceVersion) error { | ||
if csv == nil || csv.Status.Phase != phase { | ||
return fmt.Errorf("csv %s/%s in phase %s instead of %s", csv.GetNamespace(), csv.GetName(), csv.Status.Phase, phase) | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
// WithUniqueCSVs returns an error if the CSV has been seen before. | ||
func WithUniqueCSVs() walkOption { | ||
visited := map[string]struct{}{} | ||
return func(csv *operatorsv1alpha1.ClusterServiceVersion) error { | ||
// Check if we have visited the CSV before | ||
if _, ok := visited[csv.GetName()]; ok { | ||
return fmt.Errorf("csv %s/%s has already been seen", csv.GetNamespace(), csv.GetName()) | ||
} | ||
|
||
visited[csv.GetName()] = struct{}{} | ||
return nil | ||
} | ||
} | ||
|
||
// WalkReplacementChain walks along the chain of clusterServiceVersions being replaced and returns | ||
// the last clusterServiceVersions in the replacement chain. An error is returned if any of the | ||
// clusterServiceVersions before the last is not in the replaces phase or if an infinite replacement | ||
// chain is detected. | ||
func WalkReplacementChain(csv *operatorsv1alpha1.ClusterServiceVersion, csvToReplacement map[string]*operatorsv1alpha1.ClusterServiceVersion, options ...walkOption) (*operatorsv1alpha1.ClusterServiceVersion, error) { | ||
if csv == nil { | ||
return nil, fmt.Errorf("csv cannot be nil") | ||
} | ||
|
||
for { | ||
// Check if there is a CSV that replaces this CSVs | ||
next, ok := csvToReplacement[csv.GetName()] | ||
if !ok { | ||
break | ||
} | ||
|
||
// Check walk options | ||
for _, o := range options { | ||
if err := o(csv); err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
// Move along replacement chain. | ||
csv = next | ||
} | ||
return csv, nil | ||
} | ||
|
||
// isReplacementChainThatEndsInFailure returns true if the last CSV in the chain is in the failed phase and all other | ||
// CSVs are in the replacing phase. | ||
func isReplacementChainThatEndsInFailure(csv *operatorsv1alpha1.ClusterServiceVersion, csvToReplacement map[string]*operatorsv1alpha1.ClusterServiceVersion) (bool, error) { | ||
lastCSV, err := WalkReplacementChain(csv, csvToReplacement, WithCSVPhase(operatorsv1alpha1.CSVPhaseReplacing), WithUniqueCSVs()) | ||
if err != nil { | ||
return false, err | ||
} | ||
return (lastCSV != nil && lastCSV.Status.Phase == operatorsv1alpha1.CSVPhaseFailed), nil | ||
} | ||
|
||
// ReplacementMapping takes a list of CSVs and returns a map that maps a CSV's name to the CSV that replaces it. | ||
func ReplacementMapping(csvs []*operatorsv1alpha1.ClusterServiceVersion) map[string]*operatorsv1alpha1.ClusterServiceVersion { | ||
replacementMapping := map[string]*operatorsv1alpha1.ClusterServiceVersion{} | ||
for _, csv := range csvs { | ||
if csv.Spec.Replaces != "" { | ||
replacementMapping[csv.Spec.Replaces] = csv | ||
} | ||
} | ||
return replacementMapping | ||
} |
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
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 |
---|---|---|
|
@@ -191,85 +191,6 @@ func TestSolveOperators_WithSystemConstraints(t *testing.T) { | |
} | ||
} | ||
|
||
func WithInstalledCSV(sub *v1alpha1.Subscription, csvName string) *v1alpha1.Subscription { | ||
sub.Status.InstalledCSV = csvName | ||
return sub | ||
} | ||
|
||
func TestSolveOperators_WithFailForward(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed since the changes are not done on the resolver level anymore. |
||
const namespace = "test-namespace" | ||
catalog := cache.SourceKey{Name: "test-catalog", Namespace: namespace} | ||
|
||
packageASubV2 := newSub(namespace, "packageA", "alpha", catalog) | ||
APISet := cache.APISet{opregistry.APIKey{Group: "g", Version: "v", Kind: "k", Plural: "ks"}: struct{}{}} | ||
|
||
// packageA provides an API | ||
packageAV1 := genEntry("packageA.v1", "0.0.1", "", "packageA", "alpha", catalog.Name, catalog.Namespace, nil, APISet, nil, "", false) | ||
packageAV2 := genEntry("packageA.v2", "0.0.2", "packageA.v1", "packageA", "alpha", catalog.Name, catalog.Namespace, nil, APISet, nil, "", false) | ||
packageAV3 := genEntry("packageA.v3", "0.0.3", "packageA.v2", "packageA", "alpha", catalog.Name, catalog.Namespace, nil, APISet, nil, "", false) | ||
|
||
existingPackageAV1 := existingOperator(namespace, "packageA.v1", "packageA", "alpha", "", APISet, nil, nil, nil) | ||
existingPackageAV2 := existingOperator(namespace, "packageA.v2", "packageA", "alpha", "packageA.v1", APISet, nil, nil, nil) | ||
|
||
testCases := []struct { | ||
title string | ||
expectedOperators []*cache.Entry | ||
csvs []*v1alpha1.ClusterServiceVersion | ||
subs []*v1alpha1.Subscription | ||
snapshotEntries []*cache.Entry | ||
failForwardPredicates []cache.Predicate | ||
err string | ||
}{ | ||
{ | ||
title: "Resolver fails if v1 and v2 provide the same APIs and v1 is not omitted from the resolver", | ||
snapshotEntries: []*cache.Entry{packageAV1, packageAV2}, | ||
expectedOperators: nil, | ||
csvs: []*v1alpha1.ClusterServiceVersion{existingPackageAV1, existingPackageAV2}, | ||
subs: []*v1alpha1.Subscription{WithInstalledCSV(packageASubV2, existingPackageAV2.Name)}, | ||
err: "provide k (g/v)", | ||
}, | ||
{ | ||
title: "Resolver succeeds if v1 and v2 provide the same APIs and v1 is omitted from the resolver", | ||
snapshotEntries: []*cache.Entry{packageAV1, packageAV2}, | ||
expectedOperators: nil, | ||
csvs: []*v1alpha1.ClusterServiceVersion{existingPackageAV1, existingPackageAV2}, | ||
subs: []*v1alpha1.Subscription{WithInstalledCSV(packageASubV2, existingPackageAV2.Name)}, | ||
failForwardPredicates: []cache.Predicate{cache.Not(cache.CSVNamePredicate("packageA.v1"))}, | ||
err: "", | ||
}, | ||
{ | ||
title: "Resolver succeeds if v1 and v2 provide the same APIs, v1 is omitted from the resolver, and an upgrade for v2 exists", | ||
snapshotEntries: []*cache.Entry{packageAV1, packageAV2, packageAV3}, | ||
expectedOperators: []*cache.Entry{packageAV3}, | ||
csvs: []*v1alpha1.ClusterServiceVersion{existingPackageAV1, existingPackageAV2}, | ||
subs: []*v1alpha1.Subscription{WithInstalledCSV(packageASubV2, existingPackageAV2.Name)}, | ||
failForwardPredicates: []cache.Predicate{cache.Not(cache.CSVNamePredicate("packageA.v1"))}, | ||
err: "", | ||
}, | ||
} | ||
|
||
for _, testCase := range testCases { | ||
resolver := Resolver{ | ||
cache: cache.New(cache.StaticSourceProvider{ | ||
catalog: &cache.Snapshot{ | ||
Entries: testCase.snapshotEntries, | ||
}, | ||
cache.NewVirtualSourceKey(namespace): csvSnapshotOrPanic(namespace, testCase.subs, testCase.csvs...), | ||
}), | ||
log: logrus.New(), | ||
} | ||
operators, err := resolver.Resolve([]string{namespace}, testCase.subs, testCase.failForwardPredicates...) | ||
|
||
if testCase.err != "" { | ||
require.Error(t, err) | ||
require.Containsf(t, err.Error(), testCase.err, "Test %s failed", testCase.title) | ||
} else { | ||
require.NoErrorf(t, err, "Test %s failed", testCase.title) | ||
} | ||
require.ElementsMatch(t, testCase.expectedOperators, operators, "Test %s failed", testCase.title) | ||
} | ||
} | ||
|
||
func TestDisjointChannelGraph(t *testing.T) { | ||
const namespace = "test-namespace" | ||
catalog := cache.SourceKey{Name: "test-catalog", Namespace: namespace} | ||
|
@@ -1521,6 +1442,7 @@ func TestSolveOperators_TransferApiOwnership(t *testing.T) { | |
key: cache.NewVirtualSourceKey(namespace), | ||
csvLister: &csvs, | ||
subLister: fakeSubscriptionLister(p.subs), | ||
ogLister: fakeOperatorGroupLister{}, | ||
logger: logger, | ||
}, | ||
}), | ||
|
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: