Skip to content

Commit

Permalink
modify default channels for previous versions of manifests (#5067)
Browse files Browse the repository at this point in the history
Previously, the versions of manifests other than the latest version,
used to have the channel as `candidate`. This caused errors
while running the bundle. With this commit, the channels for those
manifests would be the defaultchannel specified in package.yaml.

Signed-off-by: varshaprasad96 <varshaprasad96@gmail.com>

Co-authored-by: varshaprasad96 <varshaprasad96@gmail.com>
  • Loading branch information
1 parent 5714a46 commit cfbb076
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 22 deletions.
17 changes: 17 additions & 0 deletions changelog/fragments/modify-default-channel.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# entries is a list of entries to include in
# release notes and/or the migration guide
entries:
- description: >
In the `pkgman-to-bundle` command, changed the default channel name used for CSV's
not specified in `package.yaml` to `defaultChannel` instead of "candidate".
# kind is one of:
# - addition
# - change
# - deprecation
# - removal
# - bugfix
kind: "bugfix"
# Is this a breaking change?
breaking: false
26 changes: 11 additions & 15 deletions internal/cmd/operator-sdk/pkgmantobundle/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func (p *pkgManToBundleCmd) run() (err error) {
for _, dir := range directories {
if dir.IsDir() {
// this is required to extract project layout and SDK version information.
otherLabels, channels, err := getSDKStampsAndChannels(filepath.Join(p.pkgmanifestDir, dir.Name()), channelsByCSV)
otherLabels, channels, err := getSDKStampsAndChannels(filepath.Join(p.pkgmanifestDir, dir.Name()), defaultChannel, channelsByCSV)
if err != nil {
return fmt.Errorf("error getting CSV from provided packagemanifest %v", err)
}
Expand Down Expand Up @@ -268,7 +268,7 @@ func getScorecardConfigPath(inputDir string) (string, error) {
return scorecardConfigPath, nil
}

func getSDKStampsAndChannels(path string, channelsByCSV map[string][]string) (map[string]string, string, error) {
func getSDKStampsAndChannels(path, defaultChannel string, channelsByCSV map[string][]string) (map[string]string, string, error) {
bundle, err := apimanifests.GetBundleFromDir(path)
if err != nil {
return nil, "", err
Expand All @@ -280,7 +280,7 @@ func getSDKStampsAndChannels(path string, channelsByCSV map[string][]string) (ma
}

// Find channels matching the CSV names
channels := getChannelsByCSV(bundle, channelsByCSV)
channels := getChannelsByCSV(bundle, channelsByCSV, defaultChannel)

return sdkLabels, channels, nil
}
Expand Down Expand Up @@ -308,23 +308,18 @@ func getSDKStamps(bundle *apimanifests.Bundle) (map[string]string, error) {
return sdkLabels, nil
}

// getChannelsByCSV creates a list for channels for the currentCSV,
func getChannelsByCSV(bundle *apimanifests.Bundle, channelsByCSV map[string][]string) (channels string) {
// getChannelsByCSV creates a list for channels for the currentCSV. For other versions of manifests which
// are not present in the manifest, the defaultChannel is added.
func getChannelsByCSV(bundle *apimanifests.Bundle, channelsByCSV map[string][]string, defaultChannel string) (channels string) {
// Find channels matching the CSV names
var channelNames []string
for csv, ch := range channelsByCSV {
if csv == bundle.CSV.GetName() {
channelNames = ch
break
}
}
channelNames := channelsByCSV[bundle.CSV.GetName()]
channels = strings.Join(channelNames, ",")

// TODO: verify if we have to add this validation since while building bundles if channel is not specified
// we add the default channel.
if channels == "" {
channels = "candidate"
log.Infof("Supported channels cannot be identified from CSV %s, using default channel 'preview'", bundle.CSV.GetName())
channels = defaultChannel
log.Infof("Supported channels cannot be identified from CSV %s, using default channel %s", bundle.CSV.GetName(), defaultChannel)
}

return channels
Expand All @@ -339,7 +334,8 @@ func getPackageMetadata(pkg *apimanifests.PackageManifest) (packagename, default

defaultChannel = pkg.DefaultChannelName
if defaultChannel == "" {
defaultChannel = "candidate"
err = fmt.Errorf("cannot find the default channel for package %q", packagename)
return
}

channelsByCSV = make(map[string][]string)
Expand Down
16 changes: 9 additions & 7 deletions internal/cmd/operator-sdk/pkgmantobundle/pkgmantobundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,19 +164,21 @@ var _ = Describe("Running pkgmanToBundle command", func() {
},
}

defaultChannel := "gamma"

It("should get the list of channels for corresponding CSV", func() {
channels := map[string][]string{
"memcached-operator:0.0.1": {"alpha", "beta"},
}

ch := getChannelsByCSV(&bundle, channels)
ch := getChannelsByCSV(&bundle, channels, defaultChannel)
Expect(ch).To(BeEquivalentTo("alpha,beta"))
})

It("if no channel is provided, default to candidate", func() {
channels := map[string][]string{}
ch := getChannelsByCSV(&bundle, channels)
Expect(ch).To(BeEquivalentTo("candidate"))
ch := getChannelsByCSV(&bundle, channels, defaultChannel)
Expect(ch).To(BeEquivalentTo(defaultChannel))
})
})
})
Expand Down Expand Up @@ -224,11 +226,11 @@ var _ = Describe("Running pkgmanToBundle command", func() {
Expect(err.Error()).To(ContainSubstring("cannot find packagename from the manifest directory"))
})

It("should assign default channel name of not provided", func() {
It("should error when defaultChannel name is empty", func() {
pkg.DefaultChannelName = ""
_, defaultChannel, _, err := getPackageMetadata(&pkg)
Expect(err).NotTo(HaveOccurred())
Expect(defaultChannel).To(BeEquivalentTo("candidate"))
_, _, _, err := getPackageMetadata(&pkg)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("cannot find the default channel for package"))
})
})
})
Expand Down

0 comments on commit cfbb076

Please sign in to comment.