Skip to content

Commit 9ae2ed9

Browse files
author
Eric Stroczynski
authored
internal/util/operator-registry: fix default channel check (#2116)
* internal/util/operator-registry: error if package manifest has no default channel and more than one channel * CHANGELOG.md: add default channel bug fix * internal/util/operator-registry: add test cases for default channels
1 parent 947126b commit 9ae2ed9

File tree

3 files changed

+65
-36
lines changed

3 files changed

+65
-36
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@
2222
### Bug Fixes
2323

2424
- OLM internal manager is not returning errors in the initialization. ([#1976](https://github.com/operator-framework/operator-sdk/pull/1976))
25-
- Added missing default role permission for `deployments`, which is required to create the metrics service for the operator. ([#2090](https://github.com/operator-framework/operator-sdk/pull/2090))
25+
- Added missing default role permission for `deployments`, which is required to create the metrics service for the operator. ([#2090](https://github.com/operator-framework/operator-sdk/pull/2090))
2626
- Handle invalid maxArtifacts annotation on CRs for Ansible based operators. ([2093](https://github.com/operator-framework/operator-sdk/pull/2093))
27+
- When validating package manifests, only return an error if default channel is not set and more than one channel is available. ([#2116](https://github.com/operator-framework/operator-sdk/pull/2116))
2728

2829
## v0.11.0
2930

@@ -78,7 +79,7 @@
7879
```
7980
- [`pkg/test.FrameworkClient`](https://github.com/operator-framework/operator-sdk/blob/master/pkg/test/client.go#L33) methods `List()` and `Delete()` have new signatures corresponding to the homonymous methods of `sigs.k8s.io/controller-runtime/pkg/client.Client`. ([#1876](https://github.com/operator-framework/operator-sdk/pull/1876))
8081
- CRD file names were previously of the form `<group>_<version>_<kind>_crd.yaml`. Now that CRD manifest `spec.version` is deprecated in favor of `spec.versions`, i.e. multiple versions can be specified in one CRD, CRD file names have the form `<full group>_<resource>_crd.yaml`. `<full group>` is the full group name of your CRD while `<group>` is the last subdomain of `<full group>`, ex. `foo.bar.com` vs `foo`. `<resource>` is the plural lower-case CRD Kind found at `spec.names.plural`. ([#1876](https://github.com/operator-framework/operator-sdk/pull/1876))
81-
- Upgrade Python version from `2.7` to `3.6`, Ansible version from `2.8.0` to `~=2.8` and ansible-runner from `1.2` to `1.3.4` in the Ansible based images. ([#1947](https://github.com/operator-framework/operator-sdk/pull/1947))
82+
- Upgrade Python version from `2.7` to `3.6`, Ansible version from `2.8.0` to `~=2.8` and ansible-runner from `1.2` to `1.3.4` in the Ansible based images. ([#1947](https://github.com/operator-framework/operator-sdk/pull/1947))
8283
- Made the default scorecard version `v1alpha2` which is new for this release and could break users that were parsing the older scorecard output (`v1alpha1`). Users can still specify version `v1alpha1` on the scorecard configuration to use the older style for some period of time until `v1alpha1` is removed.
8384
- Replaced `pkg/kube-metrics.NewCollectors()` with `pkg/kube-metrics.NewMetricsStores()` and changed exported function signature for `pkg/kube-metrics.ServeMetrics()` due to a [breaking change in kube-state-metrics](https://github.com/kubernetes/kube-state-metrics/pull/786). ([#1943](https://github.com/operator-framework/operator-sdk/pull/1943))
8485

internal/util/operator-registry/package_manifest.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,20 @@ import (
2121
"github.com/pkg/errors"
2222
)
2323

24-
func ValidatePackageManifest(pm *registry.PackageManifest) error {
25-
if pm.PackageName == "" {
24+
func ValidatePackageManifest(pkg *registry.PackageManifest) error {
25+
if pkg.PackageName == "" {
2626
return errors.New("package name cannot be empty")
2727
}
28-
if len(pm.Channels) == 0 {
28+
numChannels := len(pkg.Channels)
29+
if numChannels == 0 {
2930
return errors.New("channels cannot be empty")
3031
}
31-
if pm.DefaultChannelName == "" {
32+
if pkg.DefaultChannelName == "" && numChannels > 1 {
3233
return errors.New("default channel cannot be empty")
3334
}
3435

3536
seen := map[string]struct{}{}
36-
for i, c := range pm.Channels {
37+
for i, c := range pkg.Channels {
3738
if c.Name == "" {
3839
return fmt.Errorf("channel %d name cannot be empty", i)
3940
}
@@ -45,8 +46,8 @@ func ValidatePackageManifest(pm *registry.PackageManifest) error {
4546
}
4647
seen[c.Name] = struct{}{}
4748
}
48-
if _, ok := seen[pm.DefaultChannelName]; !ok {
49-
return fmt.Errorf("default channel %s does not exist in channels", pm.DefaultChannelName)
49+
if _, found := seen[pkg.DefaultChannelName]; pkg.DefaultChannelName != "" && !found {
50+
return fmt.Errorf("default channel %s does not exist in channels", pkg.DefaultChannelName)
5051
}
5152

5253
return nil

internal/util/operator-registry/package_manifest_test.go

Lines changed: 54 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,62 +21,89 @@ import (
2121
)
2222

2323
func TestValidatePackageManifest(t *testing.T) {
24-
channels := []registry.PackageChannel{
25-
{Name: "foo", CurrentCSVName: "bar"},
26-
}
27-
pm := &registry.PackageManifest{
28-
Channels: channels,
29-
DefaultChannelName: "baz",
30-
PackageName: "test-package",
31-
}
32-
3324
cases := []struct {
3425
description string
3526
wantErr bool
3627
errMsg string
37-
operation func(*registry.PackageManifest)
28+
pkg *registry.PackageManifest
3829
}{
3930
{
40-
"default channel does not exist",
41-
true, "default channel baz does not exist in channels", nil,
31+
"successful validation",
32+
false, "",
33+
&registry.PackageManifest{
34+
Channels: []registry.PackageChannel{
35+
{Name: "foo", CurrentCSVName: "bar"},
36+
},
37+
DefaultChannelName: "foo",
38+
PackageName: "test-package",
39+
},
4240
},
4341
{
44-
"successful validation",
42+
"successful validation no default channel with only one channel",
4543
false, "",
46-
func(pm *registry.PackageManifest) {
47-
pm.DefaultChannelName = pm.Channels[0].Name
44+
&registry.PackageManifest{
45+
Channels: []registry.PackageChannel{
46+
{Name: "foo", CurrentCSVName: "bar"},
47+
},
48+
PackageName: "test-package",
49+
},
50+
},
51+
{
52+
"no default channel and more than one channel",
53+
true, "default channel cannot be empty",
54+
&registry.PackageManifest{
55+
Channels: []registry.PackageChannel{
56+
{Name: "foo", CurrentCSVName: "bar"},
57+
{Name: "foo2", CurrentCSVName: "baz"},
58+
},
59+
PackageName: "test-package",
60+
},
61+
},
62+
{
63+
"default channel does not exist",
64+
true, "default channel baz does not exist in channels",
65+
&registry.PackageManifest{
66+
Channels: []registry.PackageChannel{
67+
{Name: "foo", CurrentCSVName: "bar"},
68+
},
69+
DefaultChannelName: "baz",
70+
PackageName: "test-package",
4871
},
4972
},
5073
{
5174
"channels are empty",
5275
true, "channels cannot be empty",
53-
func(pm *registry.PackageManifest) {
54-
pm.Channels = nil
76+
&registry.PackageManifest{
77+
Channels: nil,
78+
DefaultChannelName: "baz",
79+
PackageName: "test-package",
5580
},
5681
},
5782
{
5883
"one channel's CSVName is empty",
5984
true, "channel foo currentCSV cannot be empty",
60-
func(pm *registry.PackageManifest) {
61-
pm.Channels = make([]registry.PackageChannel, 1)
62-
copy(pm.Channels, channels)
63-
pm.Channels[0].CurrentCSVName = ""
85+
&registry.PackageManifest{
86+
Channels: []registry.PackageChannel{{Name: "foo"}},
87+
DefaultChannelName: "baz",
88+
PackageName: "test-package",
6489
},
6590
},
6691
{
6792
"duplicate channel name",
6893
true, "duplicate package manifest channel name foo; channel names must be unique",
69-
func(pm *registry.PackageManifest) {
70-
pm.Channels = append(channels, channels...)
94+
&registry.PackageManifest{
95+
Channels: []registry.PackageChannel{
96+
{Name: "foo", CurrentCSVName: "bar"},
97+
{Name: "foo", CurrentCSVName: "baz"},
98+
},
99+
DefaultChannelName: "baz",
100+
PackageName: "test-package",
71101
},
72102
},
73103
}
74104

75105
for _, c := range cases {
76-
if c.operation != nil {
77-
c.operation(pm)
78-
}
79-
err := ValidatePackageManifest(pm)
106+
err := ValidatePackageManifest(c.pkg)
80107
if c.wantErr {
81108
if err == nil {
82109
t.Errorf(`%s: expected error "%s", got none`, c.description, c.errMsg)

0 commit comments

Comments
 (0)