From 8c0bafbe2a91bf2c833dc868f9db671b9f97826e Mon Sep 17 00:00:00 2001 From: Eric Stroczynski Date: Thu, 15 Aug 2019 15:19:24 -0700 Subject: [PATCH] internal/pkg/scaffold/olm-catalog: do not add duplicate package manifest channels (#1693) * internal/pkg/scaffold/olm-catalog: do not add duplicate channels, use PackageManifest.OperatorName instead of ProjectName --- CHANGELOG.md | 2 + cmd/operator-sdk/olmcatalog/gen-csv.go | 1 + .../scaffold/olm-catalog/package_manifest.go | 43 +++++++++++++------ .../olm-catalog/package_manifest_test.go | 1 + 4 files changed, 33 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 25bab61559..cdca0fe442 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - Added flag `--dep-manager` to command [`operator-sdk print-deps`](https://github.com/operator-framework/operator-sdk/blob/master/doc/sdk-cli-reference.md#print-deps) to specify the type of dependency manager file to print. The choice of dependency manager is inferred from top-level dependency manager files present if `--dep-manager` is not set. ([#1819](https://github.com/operator-framework/operator-sdk/pull/1819)) ### Changed + - The Helm operator now uses the CR name for the release name for newly created CRs. Existing CRs will continue to use their existing UID-based release name. When a release name collision occurs (when CRs of different types share the same name), the second CR will fail to install with an error about a duplicate name. ([#1818](https://github.com/operator-framework/operator-sdk/pull/1818)) - Commands [`olm uninstall`](https://github.com/operator-framework/operator-sdk/blob/master/doc/sdk-cli-reference.md#uninstall) and [`olm status`](https://github.com/operator-framework/operator-sdk/blob/master/doc/sdk-cli-reference.md#status) no longer use a `--version` flag to specify OLM version. This information is now retrieved from the running cluster. ([#1634](https://github.com/operator-framework/operator-sdk/pull/1634)) @@ -19,6 +20,7 @@ - Configure the repo path correctly in `operator-sdk add crd` and prevent the command from running outside of an operator project. ([#1660](https://github.com/operator-framework/operator-sdk/pull/1660)) - In the Helm operator, skip owner reference injection for cluster-scoped resources in release manifests. The Helm operator only supports namespace-scoped CRs, and namespaced resources cannot own cluster-scoped resources. ([#1817](https://github.com/operator-framework/operator-sdk/pull/1817)) +- Package manifests generated with [`gen-csv`](https://github.com/operator-framework/operator-sdk/blob/master/doc/sdk-cli-reference.md#gen-csv) respect the `--operator-name` flag, channel names are checked for duplicates before (re-)generation. ([#1693](https://github.com/operator-framework/operator-sdk/pull/1693)) ## v0.10.0 diff --git a/cmd/operator-sdk/olmcatalog/gen-csv.go b/cmd/operator-sdk/olmcatalog/gen-csv.go index 997f07df0c..3d6bbc6769 100644 --- a/cmd/operator-sdk/olmcatalog/gen-csv.go +++ b/cmd/operator-sdk/olmcatalog/gen-csv.go @@ -107,6 +107,7 @@ func genCSVFunc(cmd *cobra.Command, args []string) error { CSVVersion: csvVersion, Channel: csvChannel, ChannelIsDefault: defaultChannel, + OperatorName: operatorName, }, ) if err != nil { diff --git a/internal/pkg/scaffold/olm-catalog/package_manifest.go b/internal/pkg/scaffold/olm-catalog/package_manifest.go index 3fd72f3c54..5a6aa4499d 100644 --- a/internal/pkg/scaffold/olm-catalog/package_manifest.go +++ b/internal/pkg/scaffold/olm-catalog/package_manifest.go @@ -45,6 +45,8 @@ type PackageManifest struct { // If ChannelIsDefault is true, Channel will be the package manifests' // default channel. ChannelIsDefault bool + // OperatorName is the operator's name, ex. app-operator + OperatorName string } var _ input.File = &PackageManifest{} @@ -52,11 +54,11 @@ var _ input.File = &PackageManifest{} // GetInput gets s' Input. func (s *PackageManifest) GetInput() (input.Input, error) { if s.Path == "" { - lowerProjName := strings.ToLower(s.ProjectName) + lowerOperatorName := strings.ToLower(s.OperatorName) // Path is what the operator-registry expects: // {manifests -> olm-catalog}/{operator_name}/{operator_name}.package.yaml - s.Path = filepath.Join(OLMCatalogDir, lowerProjName, - lowerProjName+PackageManifestFileExt) + s.Path = filepath.Join(OLMCatalogDir, lowerOperatorName, + lowerOperatorName+PackageManifestFileExt) } return s.Input, nil } @@ -117,10 +119,11 @@ func (s *PackageManifest) newPackageManifest() *olmregistry.PackageManifest { if s.Channel != "" { channel = s.Channel } + lowerOperatorName := strings.ToLower(s.OperatorName) pm := &olmregistry.PackageManifest{ - PackageName: s.ProjectName, + PackageName: lowerOperatorName, Channels: []olmregistry.PackageChannel{ - {Name: channel, CurrentCSVName: getCSVName(s.ProjectName, s.CSVVersion)}, + {Name: channel, CurrentCSVName: getCSVName(lowerOperatorName, s.CSVVersion)}, }, DefaultChannelName: channel, } @@ -131,17 +134,29 @@ func (s *PackageManifest) newPackageManifest() *olmregistry.PackageManifest { // channel if possible. func (s *PackageManifest) setChannels(pm *olmregistry.PackageManifest) error { if s.Channel != "" { - pm.Channels = append(pm.Channels, olmregistry.PackageChannel{ - Name: s.Channel, - CurrentCSVName: getCSVName(s.ProjectName, s.CSVVersion), - }) + channelIdx := -1 + for i, channel := range pm.Channels { + if channel.Name == s.Channel { + channelIdx = i + break + } + } + lowerOperatorName := strings.ToLower(s.OperatorName) + if channelIdx == -1 { + pm.Channels = append(pm.Channels, olmregistry.PackageChannel{ + Name: s.Channel, + CurrentCSVName: getCSVName(lowerOperatorName, s.CSVVersion), + }) + } else { + pm.Channels[channelIdx].CurrentCSVName = getCSVName(lowerOperatorName, s.CSVVersion) + } + // Use s.Channel as the default channel if caller has specified it as the + // default. + if s.ChannelIsDefault { + pm.DefaultChannelName = s.Channel + } } - // Use s.Channel as the default channel if caller has specified it as the - // default. - if s.ChannelIsDefault && s.Channel != "" { - pm.DefaultChannelName = s.Channel - } if pm.DefaultChannelName == "" { log.Warn("Package manifest default channel is empty and should be set to an existing channel.") } diff --git a/internal/pkg/scaffold/olm-catalog/package_manifest_test.go b/internal/pkg/scaffold/olm-catalog/package_manifest_test.go index 33e7e0e837..f8ac006ca1 100644 --- a/internal/pkg/scaffold/olm-catalog/package_manifest_test.go +++ b/internal/pkg/scaffold/olm-catalog/package_manifest_test.go @@ -48,6 +48,7 @@ func TestPackageManifest(t *testing.T) { CSVVersion: csvVer, Channel: "stable", ChannelIsDefault: true, + OperatorName: projectName, } err = s.Execute(cfg, pm) if err != nil {