From 729699dc2f8c2ada60c98ef72d0326a71da507d1 Mon Sep 17 00:00:00 2001 From: Eric Stroczynski Date: Wed, 17 Jul 2019 14:28:53 -0700 Subject: [PATCH 1/4] internal/pkg/scaffold/olm-catalog: do not add duplicate channels --- .../scaffold/olm-catalog/package_manifest.go | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/internal/pkg/scaffold/olm-catalog/package_manifest.go b/internal/pkg/scaffold/olm-catalog/package_manifest.go index 3fd72f3c54f..39c863140b5 100644 --- a/internal/pkg/scaffold/olm-catalog/package_manifest.go +++ b/internal/pkg/scaffold/olm-catalog/package_manifest.go @@ -131,17 +131,24 @@ 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 := sort.Search(len(pm.Channels), func(i int) bool { + return pm.Channels[i].Name == s.Channel }) + if channelIdx == len(pm.Channels) { + pm.Channels = append(pm.Channels, olmregistry.PackageChannel{ + Name: s.Channel, + CurrentCSVName: getCSVName(s.ProjectName, s.CSVVersion), + }) + } else { + pm.Channels[channelIdx].CurrentCSVName = getCSVName(s.ProjectName, 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.") } From 0ee5998f52cdd983c4192fbe43f681643d265c45 Mon Sep 17 00:00:00 2001 From: Eric Stroczynski Date: Fri, 19 Jul 2019 09:54:24 -0700 Subject: [PATCH 2/4] use PackageManifest.OperatorName instead of ProjectName --- cmd/operator-sdk/olmcatalog/gen-csv.go | 1 + .../pkg/scaffold/olm-catalog/package_manifest.go | 14 +++++++++----- .../scaffold/olm-catalog/package_manifest_test.go | 1 + 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/cmd/operator-sdk/olmcatalog/gen-csv.go b/cmd/operator-sdk/olmcatalog/gen-csv.go index 997f07df0c7..3d6bbc67691 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 39c863140b5..20e26f957aa 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,7 +54,7 @@ var _ input.File = &PackageManifest{} // GetInput gets s' Input. func (s *PackageManifest) GetInput() (input.Input, error) { if s.Path == "" { - lowerProjName := strings.ToLower(s.ProjectName) + lowerProjName := 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, @@ -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, } @@ -134,13 +137,14 @@ func (s *PackageManifest) setChannels(pm *olmregistry.PackageManifest) error { channelIdx := sort.Search(len(pm.Channels), func(i int) bool { return pm.Channels[i].Name == s.Channel }) + lowerOperatorName := strings.ToLower(s.OperatorName) if channelIdx == len(pm.Channels) { pm.Channels = append(pm.Channels, olmregistry.PackageChannel{ Name: s.Channel, - CurrentCSVName: getCSVName(s.ProjectName, s.CSVVersion), + CurrentCSVName: getCSVName(lowerOperatorName, s.CSVVersion), }) } else { - pm.Channels[channelIdx].CurrentCSVName = getCSVName(s.ProjectName, s.CSVVersion) + pm.Channels[channelIdx].CurrentCSVName = getCSVName(lowerOperatorName, s.CSVVersion) } // Use s.Channel as the default channel if caller has specified it as the // default. diff --git a/internal/pkg/scaffold/olm-catalog/package_manifest_test.go b/internal/pkg/scaffold/olm-catalog/package_manifest_test.go index 33e7e0e837f..f8ac006ca17 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 { From 6a76eb0ea36ab703db8c6c5f534312ed88d03eaf Mon Sep 17 00:00:00 2001 From: Eric Stroczynski Date: Thu, 15 Aug 2019 09:30:51 -0700 Subject: [PATCH 3/4] PR changes --- CHANGELOG.md | 4 ++++ internal/pkg/scaffold/olm-catalog/package_manifest.go | 6 +++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7789fb31434..7d0946144fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,11 @@ ## Unreleased ### Added + - The `operator-sdk olm-catalog gen-csv` command now produces indented JSON for the `alm-examples` annotation. ([#1793](https://github.com/operator-framework/operator-sdk/pull/1793)) ### 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)) ### Deprecated @@ -11,8 +13,10 @@ ### Removed ### Bug Fixes + - 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/internal/pkg/scaffold/olm-catalog/package_manifest.go b/internal/pkg/scaffold/olm-catalog/package_manifest.go index 20e26f957aa..3ec9557094e 100644 --- a/internal/pkg/scaffold/olm-catalog/package_manifest.go +++ b/internal/pkg/scaffold/olm-catalog/package_manifest.go @@ -54,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.OperatorName) + 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 } From f937bd3dc4761c03ae6129fb3670a2b2465abc05 Mon Sep 17 00:00:00 2001 From: Eric Stroczynski Date: Thu, 15 Aug 2019 10:18:56 -0700 Subject: [PATCH 4/4] linear search --- .../pkg/scaffold/olm-catalog/package_manifest.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/internal/pkg/scaffold/olm-catalog/package_manifest.go b/internal/pkg/scaffold/olm-catalog/package_manifest.go index 3ec9557094e..5a6aa4499d4 100644 --- a/internal/pkg/scaffold/olm-catalog/package_manifest.go +++ b/internal/pkg/scaffold/olm-catalog/package_manifest.go @@ -134,11 +134,15 @@ func (s *PackageManifest) newPackageManifest() *olmregistry.PackageManifest { // channel if possible. func (s *PackageManifest) setChannels(pm *olmregistry.PackageManifest) error { if s.Channel != "" { - channelIdx := sort.Search(len(pm.Channels), func(i int) bool { - return pm.Channels[i].Name == s.Channel - }) + channelIdx := -1 + for i, channel := range pm.Channels { + if channel.Name == s.Channel { + channelIdx = i + break + } + } lowerOperatorName := strings.ToLower(s.OperatorName) - if channelIdx == len(pm.Channels) { + if channelIdx == -1 { pm.Channels = append(pm.Channels, olmregistry.PackageChannel{ Name: s.Channel, CurrentCSVName: getCSVName(lowerOperatorName, s.CSVVersion),