Skip to content

Commit a5fa4a4

Browse files
committed
feat(packagemanifest): add default os/arch labels if not present
1 parent 869cc2f commit a5fa4a4

File tree

7 files changed

+214
-130
lines changed

7 files changed

+214
-130
lines changed

pkg/package-server/provider/labels.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package provider
2+
3+
import "strings"
4+
5+
const (
6+
OsLabelPrefix = "operatorframework.io/os"
7+
ArchLabelPrefix = "operatorframework.io/arch"
8+
DefaultOsLabel = "operatorframework.io/os.linux"
9+
DefaultArchLabel = "operatorframework.io/arch.amd64"
10+
Supported = "supported"
11+
)
12+
13+
func setDefaultOsArchLabels(labels map[string]string) {
14+
needsOsLabel := true
15+
needsArchLabel := true
16+
for k := range labels {
17+
if strings.HasPrefix(k, OsLabelPrefix) {
18+
needsOsLabel = false
19+
}
20+
if strings.HasPrefix(k, ArchLabelPrefix) {
21+
needsArchLabel = false
22+
}
23+
}
24+
if needsOsLabel {
25+
labels[DefaultOsLabel] = Supported
26+
}
27+
if needsArchLabel {
28+
labels[DefaultArchLabel] = Supported
29+
}
30+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package provider
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
)
8+
9+
func TestSetDefaultOsArchLabels(t *testing.T) {
10+
type args struct {
11+
labels map[string]string
12+
}
13+
tests := []struct {
14+
name string
15+
args args
16+
expected map[string]string
17+
}{
18+
{
19+
name: "NoneSet",
20+
args: args{labels: map[string]string{}},
21+
expected: map[string]string{
22+
DefaultArchLabel: Supported,
23+
DefaultOsLabel: Supported,
24+
},
25+
},
26+
{
27+
name: "OthersPreserved",
28+
args: args{labels: map[string]string{"other": "label"}},
29+
expected: map[string]string{
30+
"other": "label",
31+
DefaultArchLabel: Supported,
32+
DefaultOsLabel: Supported,
33+
},
34+
},
35+
{
36+
name: "OsSet",
37+
args: args{labels: map[string]string{OsLabelPrefix + ".windows": Supported}},
38+
expected: map[string]string{
39+
DefaultArchLabel: Supported,
40+
OsLabelPrefix + ".windows": Supported,
41+
},
42+
},
43+
{
44+
name: "ArchSet",
45+
args: args{labels: map[string]string{ArchLabelPrefix + ".arm": Supported}},
46+
expected: map[string]string{
47+
ArchLabelPrefix + ".arm": Supported,
48+
DefaultOsLabel: Supported,
49+
},
50+
},
51+
}
52+
for _, tt := range tests {
53+
t.Run(tt.name, func(t *testing.T) {
54+
labels := tt.args.labels
55+
setDefaultOsArchLabels(labels)
56+
require.Equal(t, tt.expected, labels)
57+
})
58+
}
59+
}

pkg/package-server/provider/registry.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,7 @@ func newPackageManifest(ctx context.Context, logger *logrus.Entry, pkg *api.Pack
448448
var (
449449
providerSet bool
450450
defaultElided bool
451+
defaultCsv *operatorsv1alpha1.ClusterServiceVersion
451452
)
452453
for _, pkgChannel := range pkgChannels {
453454
bundle, err := client.GetBundleForChannel(ctx, &api.GetBundleInChannelRequest{PkgName: pkg.GetName(), ChannelName: pkgChannel.GetName()})
@@ -464,11 +465,9 @@ func newPackageManifest(ctx context.Context, logger *logrus.Entry, pkg *api.Pack
464465
defaultElided = defaultElided || pkgChannel.Name == manifest.Status.DefaultChannel
465466
continue
466467
}
467-
manifestLabels := manifest.GetLabels()
468-
for k, v := range csv.GetLabels() {
469-
manifestLabels[k] = v
468+
if defaultCsv == nil || pkgChannel.GetName() == manifest.Status.DefaultChannel {
469+
defaultCsv = &csv
470470
}
471-
manifest.SetLabels(manifest.GetLabels())
472471
manifest.Status.Channels = append(manifest.Status.Channels, operators.PackageChannel{
473472
Name: pkgChannel.GetName(),
474473
CurrentCSV: csv.GetName(),
@@ -494,6 +493,11 @@ func newPackageManifest(ctx context.Context, logger *logrus.Entry, pkg *api.Pack
494493
logger.Warn("default channel elided, setting as first in packagemanifest")
495494
manifest.Status.DefaultChannel = manifest.Status.Channels[0].Name
496495
}
497-
496+
manifestLabels := manifest.GetLabels()
497+
for k, v := range defaultCsv.GetLabels() {
498+
manifestLabels[k] = v
499+
}
500+
setDefaultOsArchLabels(manifestLabels)
501+
manifest.SetLabels(manifestLabels)
498502
return manifest, nil
499503
}

pkg/package-server/provider/registry_test.go

Lines changed: 74 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import (
3434
)
3535

3636
const (
37-
port = "50051"
37+
port = "50054"
3838
address = "localhost:"
3939
dbName = "test.db"
4040
)
@@ -171,10 +171,12 @@ func TestToPackageManifest(t *testing.T) {
171171
Name: "etcd",
172172
Namespace: "ns",
173173
Labels: labels.Set{
174-
"catalog": "cool-operators",
175-
"catalog-namespace": "ns",
176-
"provider": "CoreOS, Inc",
177-
"provider-url": "",
174+
"catalog": "cool-operators",
175+
"catalog-namespace": "ns",
176+
"provider": "CoreOS, Inc",
177+
"provider-url": "",
178+
"operatorframework.io/arch.amd64": "supported",
179+
"operatorframework.io/os.linux": "supported",
178180
},
179181
},
180182
Status: operators.PackageManifestStatus{
@@ -230,11 +232,13 @@ func TestToPackageManifest(t *testing.T) {
230232
Name: "etcd",
231233
Namespace: "ns",
232234
Labels: labels.Set{
233-
"test": "label",
234-
"catalog": "cool-operators",
235-
"catalog-namespace": "ns",
236-
"provider": "CoreOS, Inc",
237-
"provider-url": "",
235+
"test": "label",
236+
"catalog": "cool-operators",
237+
"catalog-namespace": "ns",
238+
"provider": "CoreOS, Inc",
239+
"provider-url": "",
240+
"operatorframework.io/arch.amd64": "supported",
241+
"operatorframework.io/os.linux": "supported",
238242
},
239243
},
240244
Status: operators.PackageManifestStatus{
@@ -294,10 +298,12 @@ func TestToPackageManifest(t *testing.T) {
294298
Name: "etcd",
295299
Namespace: "ns",
296300
Labels: labels.Set{
297-
"catalog": "cool-operators",
298-
"catalog-namespace": "ns",
299-
"provider": "CoreOS, Inc",
300-
"provider-url": "",
301+
"catalog": "cool-operators",
302+
"catalog-namespace": "ns",
303+
"provider": "CoreOS, Inc",
304+
"provider-url": "",
305+
"operatorframework.io/arch.amd64": "supported",
306+
"operatorframework.io/os.linux": "supported",
301307
},
302308
},
303309
Status: operators.PackageManifestStatus{
@@ -357,10 +363,12 @@ func TestToPackageManifest(t *testing.T) {
357363
Name: "etcd",
358364
Namespace: "ns",
359365
Labels: labels.Set{
360-
"catalog": "cool-operators",
361-
"catalog-namespace": "ns",
362-
"provider": "CoreOS, Inc",
363-
"provider-url": "",
366+
"catalog": "cool-operators",
367+
"catalog-namespace": "ns",
368+
"provider": "CoreOS, Inc",
369+
"provider-url": "",
370+
"operatorframework.io/arch.amd64": "supported",
371+
"operatorframework.io/os.linux": "supported",
364372
},
365373
},
366374
Status: operators.PackageManifestStatus{
@@ -475,10 +483,12 @@ func TestRegistryProviderGet(t *testing.T) {
475483
Name: "etcd",
476484
Namespace: "ns",
477485
Labels: labels.Set{
478-
"catalog": "cool-operators",
479-
"catalog-namespace": "ns",
480-
"provider": "CoreOS, Inc",
481-
"provider-url": "",
486+
"catalog": "cool-operators",
487+
"catalog-namespace": "ns",
488+
"provider": "CoreOS, Inc",
489+
"provider-url": "",
490+
"operatorframework.io/arch.amd64": "supported",
491+
"operatorframework.io/os.linux": "supported",
482492
},
483493
},
484494
Status: operators.PackageManifestStatus{
@@ -522,10 +532,12 @@ func TestRegistryProviderGet(t *testing.T) {
522532
Name: "etcd",
523533
Namespace: "ns",
524534
Labels: labels.Set{
525-
"catalog": "cool-operators",
526-
"catalog-namespace": "ns",
527-
"provider": "CoreOS, Inc",
528-
"provider-url": "",
535+
"catalog": "cool-operators",
536+
"catalog-namespace": "ns",
537+
"provider": "CoreOS, Inc",
538+
"provider-url": "",
539+
"operatorframework.io/arch.amd64": "supported",
540+
"operatorframework.io/os.linux": "supported",
529541
},
530542
},
531543
Status: operators.PackageManifestStatus{
@@ -568,10 +580,12 @@ func TestRegistryProviderGet(t *testing.T) {
568580
Name: "etcd",
569581
Namespace: "ns",
570582
Labels: labels.Set{
571-
"catalog": "cool-operators",
572-
"catalog-namespace": "global",
573-
"provider": "CoreOS, Inc",
574-
"provider-url": "",
583+
"catalog": "cool-operators",
584+
"catalog-namespace": "global",
585+
"provider": "CoreOS, Inc",
586+
"provider-url": "",
587+
"operatorframework.io/arch.amd64": "supported",
588+
"operatorframework.io/os.linux": "supported",
575589
},
576590
},
577591
Status: operators.PackageManifestStatus{
@@ -653,10 +667,12 @@ func TestRegistryProviderList(t *testing.T) {
653667
Name: "prometheus",
654668
Namespace: "ns",
655669
Labels: labels.Set{
656-
"catalog": "cool-operators",
657-
"catalog-namespace": "ns",
658-
"provider": "Red Hat",
659-
"provider-url": "",
670+
"catalog": "cool-operators",
671+
"catalog-namespace": "ns",
672+
"provider": "Red Hat",
673+
"provider-url": "",
674+
"operatorframework.io/arch.amd64": "supported",
675+
"operatorframework.io/os.linux": "supported",
660676
},
661677
},
662678
Status: operators.PackageManifestStatus{
@@ -685,10 +701,12 @@ func TestRegistryProviderList(t *testing.T) {
685701
Name: "etcd",
686702
Namespace: "ns",
687703
Labels: labels.Set{
688-
"catalog": "cool-operators",
689-
"catalog-namespace": "ns",
690-
"provider": "CoreOS, Inc",
691-
"provider-url": "",
704+
"catalog": "cool-operators",
705+
"catalog-namespace": "ns",
706+
"provider": "CoreOS, Inc",
707+
"provider-url": "",
708+
"operatorframework.io/arch.amd64": "supported",
709+
"operatorframework.io/os.linux": "supported",
692710
},
693711
},
694712
Status: operators.PackageManifestStatus{
@@ -729,10 +747,12 @@ func TestRegistryProviderList(t *testing.T) {
729747
Name: "prometheus",
730748
Namespace: "ns",
731749
Labels: labels.Set{
732-
"catalog": "cool-operators",
733-
"catalog-namespace": "ns",
734-
"provider": "Red Hat",
735-
"provider-url": "",
750+
"catalog": "cool-operators",
751+
"catalog-namespace": "ns",
752+
"provider": "Red Hat",
753+
"provider-url": "",
754+
"operatorframework.io/arch.amd64": "supported",
755+
"operatorframework.io/os.linux": "supported",
736756
},
737757
},
738758
Status: operators.PackageManifestStatus{
@@ -761,10 +781,12 @@ func TestRegistryProviderList(t *testing.T) {
761781
Name: "etcd",
762782
Namespace: "ns",
763783
Labels: labels.Set{
764-
"catalog": "cool-operators",
765-
"catalog-namespace": "ns",
766-
"provider": "CoreOS, Inc",
767-
"provider-url": "",
784+
"catalog": "cool-operators",
785+
"catalog-namespace": "ns",
786+
"provider": "CoreOS, Inc",
787+
"provider-url": "",
788+
"operatorframework.io/arch.amd64": "supported",
789+
"operatorframework.io/os.linux": "supported",
768790
},
769791
},
770792
Status: operators.PackageManifestStatus{
@@ -848,10 +870,12 @@ func TestRegistryProviderList(t *testing.T) {
848870
Name: "has-bundle",
849871
Namespace: "ns",
850872
Labels: labels.Set{
851-
"catalog": "cool-operators",
852-
"catalog-namespace": "ns",
853-
"provider": "CoreOS, Inc",
854-
"provider-url": "",
873+
"catalog": "cool-operators",
874+
"catalog-namespace": "ns",
875+
"provider": "CoreOS, Inc",
876+
"provider-url": "",
877+
"operatorframework.io/arch.amd64": "supported",
878+
"operatorframework.io/os.linux": "supported",
855879
},
856880
},
857881
Status: operators.PackageManifestStatus{

pkg/package-server/storage/reststorage.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ package storage
22

33
import (
44
"context"
5+
"fmt"
6+
7+
"k8s.io/apimachinery/pkg/fields"
58

69
k8serrors "k8s.io/apimachinery/pkg/api/errors"
710
metainternalversion "k8s.io/apimachinery/pkg/apis/meta/internalversion"
@@ -66,12 +69,23 @@ func (m *PackageManifestStorage) List(ctx context.Context, options *metainternal
6669
labelSelector = options.LabelSelector
6770
}
6871

72+
name, err := nameFor(options.FieldSelector)
73+
if err != nil {
74+
return nil, err
75+
}
76+
6977
res, err := m.prov.List(namespace, labelSelector)
7078
if err != nil {
7179
return nil, k8serrors.NewInternalError(err)
7280
}
7381

74-
filtered := res.Items
82+
filtered := []operators.PackageManifest{}
83+
for _, manifest := range res.Items {
84+
if matches(manifest, name) {
85+
filtered = append(filtered, manifest)
86+
}
87+
}
88+
7589
for i := range filtered {
7690
for j := range filtered[i].Status.Channels {
7791
filtered[i].Status.Channels[j].CurrentCSVDesc.Icon = []operators.Icon{}
@@ -101,3 +115,23 @@ func (m *PackageManifestStorage) Get(ctx context.Context, name string, opts *met
101115
func (m *PackageManifestStorage) NamespaceScoped() bool {
102116
return true
103117
}
118+
119+
func nameFor(fs fields.Selector) (string, error) {
120+
if fs == nil {
121+
fs = fields.Everything()
122+
}
123+
name := ""
124+
if value, found := fs.RequiresExactMatch("metadata.name"); found {
125+
name = value
126+
} else if !fs.Empty() {
127+
return "", fmt.Errorf("field label not supported: %s", fs.Requirements()[0].Field)
128+
}
129+
return name, nil
130+
}
131+
132+
func matches(pm operators.PackageManifest, name string) bool {
133+
if name == "" {
134+
name = pm.GetName()
135+
}
136+
return pm.GetName() == name
137+
}

0 commit comments

Comments
 (0)