Skip to content

Commit 3d63ab6

Browse files
committed
fixup! Add in catalog label selection support
Signed-off-by: Todd Short <[email protected]>
1 parent 7b9fa47 commit 3d63ab6

File tree

2 files changed

+50
-5
lines changed

2 files changed

+50
-5
lines changed

internal/resolve/catalog_test.go

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/stretchr/testify/assert"
1111
"github.com/stretchr/testify/require"
1212
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
13+
"k8s.io/apimachinery/pkg/labels"
1314
"k8s.io/apimachinery/pkg/util/rand"
1415
"k8s.io/apimachinery/pkg/util/sets"
1516
featuregatetesting "k8s.io/component-base/featuregate/testing"
@@ -516,10 +517,22 @@ type getPackageFunc func() (*declcfg.DeclarativeConfig, error)
516517

517518
type staticCatalogWalker map[string]getPackageFunc
518519

519-
func (w staticCatalogWalker) WalkCatalogs(ctx context.Context, _ string, f CatalogWalkFunc, _ ...client.ListOption) error {
520+
func (w staticCatalogWalker) WalkCatalogs(ctx context.Context, _ string, f CatalogWalkFunc, opts ...client.ListOption) error {
520521
for k, v := range w {
521522
cat := &catalogd.ClusterCatalog{
522-
ObjectMeta: metav1.ObjectMeta{Name: k},
523+
ObjectMeta: metav1.ObjectMeta{
524+
Name: k,
525+
Labels: map[string]string{
526+
"olm.operatorframework.io/name": k,
527+
},
528+
},
529+
}
530+
options := client.ListOptions{}
531+
for _, opt := range opts {
532+
opt.ApplyToList(&options)
533+
}
534+
if !options.LabelSelector.Matches(labels.Set(cat.ObjectMeta.Labels)) {
535+
continue
523536
}
524537
fbc, fbcErr := v()
525538
if err := f(ctx, cat, fbc, fbcErr); err != nil {
@@ -630,8 +643,9 @@ func TestInvalidClusterExtensionCatalogMatchLabelsName(t *testing.T) {
630643
},
631644
}
632645
_, _, _, err := r.Resolve(context.Background(), ce, nil)
633-
assert.EqualError(t, err, "desired catalog selector is invalid: key: Invalid value: \"\": name part must be non-empty; name part must consist of alphanumeric characters, '-', '_' or '.', and must start and end with an alphanumeric character (e.g. 'MyName', or 'my.name', or '123-abc', regex used for validation is '([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9]')")
646+
assert.ErrorContains(t, err, "desired catalog selector is invalid: key: Invalid value:")
634647
}
648+
635649
func TestInvalidClusterExtensionCatalogMatchLabelsValue(t *testing.T) {
636650
w := staticCatalogWalker{
637651
"a": func() (*declcfg.DeclarativeConfig, error) { return genPackage("foo"), nil },
@@ -649,5 +663,36 @@ func TestInvalidClusterExtensionCatalogMatchLabelsValue(t *testing.T) {
649663
},
650664
}
651665
_, _, _, err := r.Resolve(context.Background(), ce, nil)
652-
assert.EqualError(t, err, "desired catalog selector is invalid: values[0][name]: Invalid value: \"&value\": a valid label must be an empty string or consist of alphanumeric characters, '-', '_' or '.', and must start and end with an alphanumeric character (e.g. 'MyValue', or 'my_value', or '12345', regex used for validation is '(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?')")
666+
assert.ErrorContains(t, err, "desired catalog selector is invalid: values[0][name]: Invalid value:")
667+
}
668+
669+
func TestClusterExtensionMatchLabel(t *testing.T) {
670+
defer featuregatetesting.SetFeatureGateDuringTest(t, features.OperatorControllerFeatureGate, features.ForceSemverUpgradeConstraints, false)()
671+
pkgName := randPkg()
672+
w := staticCatalogWalker{
673+
"a": func() (*declcfg.DeclarativeConfig, error) { return &declcfg.DeclarativeConfig{}, nil },
674+
"b": func() (*declcfg.DeclarativeConfig, error) { return genPackage(pkgName), nil },
675+
}
676+
r := CatalogResolver{WalkCatalogsFunc: w.WalkCatalogs}
677+
ce := buildFooClusterExtension(pkgName, "", "", ocv1alpha1.UpgradeConstraintPolicyEnforce)
678+
ce.Spec.CatalogSelector.MatchLabels = map[string]string{"olm.operatorframework.io/name": "b"}
679+
680+
_, _, _, err := r.Resolve(context.Background(), ce, nil)
681+
require.NoError(t, err)
682+
}
683+
684+
func TestClusterExtensionNoMatchLabel(t *testing.T) {
685+
defer featuregatetesting.SetFeatureGateDuringTest(t, features.OperatorControllerFeatureGate, features.ForceSemverUpgradeConstraints, false)()
686+
pkgName := randPkg()
687+
w := staticCatalogWalker{
688+
"a": func() (*declcfg.DeclarativeConfig, error) { return &declcfg.DeclarativeConfig{}, nil },
689+
"b": func() (*declcfg.DeclarativeConfig, error) { return genPackage(pkgName), nil },
690+
}
691+
r := CatalogResolver{WalkCatalogsFunc: w.WalkCatalogs}
692+
ce := buildFooClusterExtension(pkgName, "", "", ocv1alpha1.UpgradeConstraintPolicyEnforce)
693+
ce.Spec.CatalogSelector.MatchLabels = map[string]string{"olm.operatorframework.io/name": "a"}
694+
695+
_, _, _, err := r.Resolve(context.Background(), ce, nil)
696+
require.Error(t, err)
697+
require.ErrorContains(t, err, fmt.Sprintf("no package %q found", pkgName))
653698
}

test/e2e/cluster_extension_install_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ func TestClusterExtensionInstallRegistryMultipleBundles(t *testing.T) {
306306
if !assert.NotNil(ct, cond) {
307307
return
308308
}
309-
// TODO(tmshort/dfranz): This should fail due to multiple bundles
309+
// TODO(tmshort/dtfranz): This should fail due to multiple bundles
310310
assert.Equal(ct, metav1.ConditionTrue, cond.Status)
311311
//assert.Equal(ct, metav1.ConditionFalse, cond.Status)
312312
//assert.Equal(ct, ocv1alpha1.ReasonResolutionFailed, cond.Reason)

0 commit comments

Comments
 (0)