Skip to content

Commit 66159c4

Browse files
[v0.18.x] bundle generate: ensure default channel exists (#3165)
Co-authored-by: Eric Stroczynski <[email protected]>
1 parent 7cdd394 commit 66159c4

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed

cmd/operator-sdk/bundle/create.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ bundle.Dockerfile for your latest operator version without building the image:
108108
}
109109

110110
if err = c.validate(args); err != nil {
111-
return fmt.Errorf("error validating args: %v", err)
111+
return fmt.Errorf("invalid command args: %v", err)
112112
}
113113

114114
if c.generateOnly {
@@ -172,7 +172,7 @@ func (c *bundleCreateCmd) setDefaults() (err error) {
172172
c.directory = dir
173173
}
174174

175-
// Ensure a default channel is present if there only one channel. Don't infer
175+
// A default channel can be inferred if there is only one channel. Don't infer
176176
// default otherwise; the user must set this value.
177177
if c.defaultChannel == "" && strings.Count(c.channels, ",") == 0 {
178178
c.defaultChannel = c.channels
@@ -199,11 +199,18 @@ func (c bundleCreateCmd) validate(args []string) error {
199199
if c.packageName == "" {
200200
return fmt.Errorf("--package must be set")
201201
}
202+
203+
// Ensure a default channel is present.
204+
if c.defaultChannel == "" {
205+
return fmt.Errorf("--default-channel must be set")
206+
}
207+
202208
// Bundle commands only work with bundle directory formats, not package
203209
// manifests formats.
204210
if isPackageManifestsDir(c.directory, c.packageName) {
205211
return fmt.Errorf("bundle commands can only be used on bundle directory formats")
206212
}
213+
207214
if c.generateOnly {
208215
if len(args) != 0 {
209216
return errors.New("the command does not accept any arguments if --generate-only=true")

cmd/operator-sdk/generate/bundle/bundle.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"fmt"
2020
"os"
2121
"path/filepath"
22+
"strings"
2223

2324
"github.com/operator-framework/operator-registry/pkg/lib/bundle"
2425
"sigs.k8s.io/kubebuilder/pkg/model/config"
@@ -33,6 +34,11 @@ func (c *bundleCmd) setCommonDefaults(cfg *config.Config) {
3334
if c.operatorName == "" {
3435
c.operatorName = filepath.Base(cfg.Repo)
3536
}
37+
// A default channel can be inferred if there is only one channel. Don't infer
38+
// default otherwise; the user must set this value.
39+
if c.defaultChannel == "" && strings.Count(c.channels, ",") == 0 {
40+
c.defaultChannel = c.channels
41+
}
3642
}
3743

3844
// runKustomize generates kustomize bundle bases.
@@ -189,6 +195,16 @@ func (c bundleCmd) runManifests(cfg *config.Config) (err error) {
189195
return nil
190196
}
191197

198+
// validateMetadata validates c for bundle metadata generation.
199+
func (c bundleCmd) validateMetadata(*config.Config) (err error) {
200+
// Ensure a default channel is present.
201+
if c.defaultChannel == "" {
202+
return fmt.Errorf("--default-channel must be set if setting multiple channels")
203+
}
204+
205+
return nil
206+
}
207+
192208
// runMetadata generates a bundle.Dockerfile and bundle metadata.
193209
func (c bundleCmd) runMetadata() error {
194210

cmd/operator-sdk/generate/bundle/bundle_legacy.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package bundle
1717
import (
1818
"fmt"
1919
"path/filepath"
20+
"strings"
2021

2122
"github.com/operator-framework/operator-registry/pkg/lib/bundle"
2223
log "github.com/sirupsen/logrus"
@@ -32,6 +33,11 @@ func (c *bundleCmd) setCommonDefaultsLegacy() {
3233
if c.operatorName == "" {
3334
c.operatorName = filepath.Base(projutil.MustGetwd())
3435
}
36+
// A default channel can be inferred if there is only one channel. Don't infer
37+
// default otherwise; the user must set this value.
38+
if c.defaultChannel == "" && strings.Count(c.channels, ",") == 0 {
39+
c.defaultChannel = c.channels
40+
}
3541
}
3642

3743
// validateManifestsLegacy validates c for bundle manifests generation for
@@ -112,6 +118,17 @@ func (c bundleCmd) runManifestsLegacy() (err error) {
112118
return nil
113119
}
114120

121+
// validateMetadataLegacy validates c for bundle metadata generation for
122+
// legacy project layouts.
123+
func (c bundleCmd) validateMetadataLegacy() (err error) {
124+
// Ensure a default channel is present.
125+
if c.defaultChannel == "" {
126+
return fmt.Errorf("--default-channel must be set if setting multiple channels")
127+
}
128+
129+
return nil
130+
}
131+
115132
// runMetadataLegacy generates a bundle.Dockerfile and bundle metadata for
116133
// legacy project layouts.
117134
func (c bundleCmd) runMetadataLegacy() error {

cmd/operator-sdk/generate/bundle/cmd.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ func NewCmd() *cobra.Command {
104104
}
105105
}
106106
if c.metadata {
107+
if err = c.validateMetadata(cfg); err != nil {
108+
return fmt.Errorf("invalid command options: %v", err)
109+
}
107110
if err = c.runMetadata(); err != nil {
108111
log.Fatalf("Error generating bundle metadata: %v", err)
109112
}
@@ -164,6 +167,9 @@ func NewCmdLegacy() *cobra.Command {
164167
}
165168
}
166169
if c.metadata {
170+
if err = c.validateMetadataLegacy(); err != nil {
171+
return fmt.Errorf("invalid command options: %v", err)
172+
}
167173
if err = c.runMetadataLegacy(); err != nil {
168174
log.Fatalf("Error generating bundle metadata: %v", err)
169175
}

0 commit comments

Comments
 (0)