diff --git a/CHANGELOG.md b/CHANGELOG.md index 86fea0a1fe2..181457a3fec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,12 +25,13 @@ ### Deprecated - **Breaking Change:** The `--namespace` flag from `operator-sdk run --local`, `operator-sdk test --local` and `operator-sdk cleanup` command was deprecated and will be removed in the future versions. Use `--watch-namespace` and `--operator-namespace` instead of. ([#2617](https://github.com/operator-framework/operator-sdk/pull/2617)) -- **Breaking Change:** The method `ctx.GetNamespace()` from the `pkg/test` is deprecated and will be removed in the future versions. Use `ctx.GetOperatorNamespace()` and `ctx.GetWatchNamespace()` instead of. ([#2617](https://github.com/operator-framework/operator-sdk/pull/2617)) +- **Breaking Change:** The method `ctx.GetNamespace()` from the `pkg/test` is deprecated and will be removed in future versions. Use `ctx.GetOperatorNamespace()` and `ctx.GetWatchNamespace()` instead of. ([#2617](https://github.com/operator-framework/operator-sdk/pull/2617)) +- **Breaking Change:** package manifests are deprecated and new manifests are no longer generated; existing manifests are still updated by `operator-sdk generate csv`, but updates will not occur in future versions. Use [`operator-sdk bundle create`](./doc/cli/operator-sdk_bundle_create.md) to manage operator bundle metadata. ([#2755](https://github.com/operator-framework/operator-sdk/pull/2755)) ### Removed - **Breaking Change:** remove `pkg/restmapper` which was deprecated in `v0.14.0`. Projects that use this package must switch to the `DynamicRESTMapper` implementation in [controller-runtime](https://godoc.org/github.com/kubernetes-sigs/controller-runtime/pkg/client/apiutil#NewDynamicRESTMapper). ([#2544](https://github.com/operator-framework/operator-sdk/pull/2544)) -- **Breaking Change:** remove deprecated `operator-sdk generate openapi` subcommand. ([#2740](https://github.com/operator-framework/operator-sdk/pull/2740)) +- **Breaking Change:** remove deprecated `operator-sdk generate openapi` subcommand. ([#2740](https://github.com/operator-framework/operator-sdk/pull/2740)) - **Breaking Change:** Removed CSV configuration file support (defaulting to deploy/olm-catalog/csv-config.yaml) in favor of specifying inputs to the generator via [`generate csv --deploy-dir --apis-dir --crd-dir`](doc/cli/operator-sdk_generate_csv.md#options), and configuring output locations via [`generate csv --output-dir`](doc/cli/operator-sdk_generate_csv.md#options). ([#2511](https://github.com/operator-framework/operator-sdk/pull/2511)) ### Bug Fixes diff --git a/internal/generate/olm-catalog/package_manifest.go b/internal/generate/olm-catalog/package_manifest.go index bb65626954d..305ef3f60f3 100644 --- a/internal/generate/olm-catalog/package_manifest.go +++ b/internal/generate/olm-catalog/package_manifest.go @@ -28,11 +28,15 @@ import ( "github.com/operator-framework/operator-sdk/internal/generate/gen" "github.com/operator-framework/operator-sdk/internal/scaffold" "github.com/operator-framework/operator-sdk/internal/util/fileutil" + "github.com/operator-framework/operator-sdk/internal/util/projutil" "github.com/ghodss/yaml" log "github.com/sirupsen/logrus" ) +// Deprecated: The package manifest generator will no longer create new package +// manifests, only update existing ones. This generator will be removed in v0.19.0. + const ( packageManifestFileExt = ".package.yaml" ) @@ -94,7 +98,7 @@ func (g pkgGenerator) Generate() error { return err } if len(fileMap) == 0 { - return errors.New("error generating package manifest: no generated file found") + return nil } pkgManifestOutputDir := filepath.Join(g.OutputDir, OLMCatalogChildDir, g.OperatorName) if err = os.MkdirAll(pkgManifestOutputDir, fileutil.DefaultDirFileMode); err != nil { @@ -113,17 +117,30 @@ func (g pkgGenerator) Generate() error { // generate either reads an existing package manifest or creates a new // manifest and modifies it based on values set in s. func (g pkgGenerator) generate() (map[string][]byte, error) { - pkg, err := g.buildPackageManifest() - if err != nil { - return nil, err + pkgManifestOutputDir := filepath.Join(g.OutputDir, OLMCatalogChildDir, g.OperatorName) + path := filepath.Join(pkgManifestOutputDir, g.fileName) + pkg := registry.PackageManifest{} + if _, err := os.Stat(path); os.IsNotExist(err) { + return nil, nil + } else if err == nil || os.IsExist(err) { + projutil.PrintDeprecationWarning("Package manifests are deprecated. " + + "Run `operator-sdk bundle create --generate-only` to create operator metadata") + b, err := ioutil.ReadFile(path) + if err != nil { + return nil, fmt.Errorf("failed to read package manifest %s: %v", path, err) + } + if err = yaml.Unmarshal(b, &pkg); err != nil { + return nil, fmt.Errorf("failed to unmarshal package manifest %s: %v", path, err) + } + } else { + return nil, fmt.Errorf("error reading package manifest %s: %v", path, err) } g.setChannels(&pkg) sortChannelsByName(&pkg) if err := validatePackageManifest(&pkg); err != nil { - log.Error(err) - os.Exit(1) + return nil, err } b, err := yaml.Marshal(pkg) @@ -139,17 +156,18 @@ func (g pkgGenerator) generate() (map[string][]byte, error) { // buildPackageManifest will create a registry.PackageManifest from scratch, or reads // an existing one if found at the expected path. +// Deprecated: only used for testing other methods on g. func (g pkgGenerator) buildPackageManifest() (registry.PackageManifest, error) { + pkgManifestOutputDir := filepath.Join(g.OutputDir, OLMCatalogChildDir, g.OperatorName) + path := filepath.Join(pkgManifestOutputDir, g.fileName) pkg := registry.PackageManifest{} - olmCatalogDir := filepath.Join(g.OutputDir, OLMCatalogChildDir) - existingPkgManifest := filepath.Join(olmCatalogDir, g.OperatorName, g.fileName) - if isFileExist(existingPkgManifest) { - b, err := ioutil.ReadFile(existingPkgManifest) + if isFileExist(path) { + b, err := ioutil.ReadFile(path) if err != nil { - return pkg, fmt.Errorf("failed to read package manifest %s: %v", existingPkgManifest, err) + return pkg, fmt.Errorf("failed to read package manifest %s: %v", path, err) } if err = yaml.Unmarshal(b, &pkg); err != nil { - return pkg, fmt.Errorf("failed to unmarshal package manifest %s: %v", existingPkgManifest, err) + return pkg, fmt.Errorf("failed to unmarshal package manifest %s: %v", path, err) } } else { pkg = newPackageManifest(g.OperatorName, g.channel, g.csvVersion) @@ -187,7 +205,8 @@ func validatePackageManifest(pkg *registry.PackageManifest) error { return nil } -// newPackageManifest will return the registry.PackageManifest populated +// newPackageManifest will return the registry.PackageManifest populated. +// Deprecated: only used for testing other methods on g. func newPackageManifest(operatorName, channelName, version string) registry.PackageManifest { // Take the current CSV version to be the "alpha" channel, as an operator // should only be designated anything more stable than "alpha" by a human. diff --git a/internal/generate/olm-catalog/package_manifest_test.go b/internal/generate/olm-catalog/package_manifest_test.go index 466b2df724e..cf450f96ac5 100644 --- a/internal/generate/olm-catalog/package_manifest_test.go +++ b/internal/generate/olm-catalog/package_manifest_test.go @@ -15,11 +15,6 @@ package olmcatalog import ( - "io/ioutil" - "log" - "os" - "path/filepath" - "reflect" "testing" "github.com/operator-framework/operator-registry/pkg/registry" @@ -32,51 +27,34 @@ func TestGeneratePkgManifestToOutput(t *testing.T) { cleanupFunc := chDirWithCleanup(t, testNonStandardLayoutDataDir) defer cleanupFunc() - // Temporary output dir for generating catalog bundle - outputDir, err := ioutil.TempDir("", t.Name()+"-output-catalog") - if err != nil { - log.Fatal(err) - } - // Clean up output catalog dir - defer func() { - if err := os.RemoveAll(outputDir); err != nil && !os.IsNotExist(err) { - // Not a test failure since files in /tmp will eventually get deleted - t.Logf("Failed to remove tmp generated catalog directory (%s): %v", outputDir, err) - } - }() - cfg := gen.Config{ OperatorName: testProjectName, - OutputDir: outputDir, - } - - g := NewPackageManifest(cfg, csvVersion, "stable", true) - if err := g.Generate(); err != nil { - t.Fatalf("Failed to execute package manifest generator: %v", err) + OutputDir: "expected-catalog", } - - pkgManFileName := getPkgFileName(testProjectName) - - // Read expected Package Manifest - expCatalogDir := filepath.Join("expected-catalog", OLMCatalogChildDir) - pkgManExpBytes, err := ioutil.ReadFile(filepath.Join(expCatalogDir, testProjectName, pkgManFileName)) + g := NewPackageManifest(cfg, csvVersion, "beta", false) + fileMap, err := g.(pkgGenerator).generate() if err != nil { - t.Fatalf("Failed to read expected package manifest file: %v", err) + t.Fatalf("Failed to execute package manifest generator: %v", err) } - pkgManExp := string(pkgManExpBytes) - // Read generated Package Manifest from OutputDir/olm-catalog - outputCatalogDir := filepath.Join(cfg.OutputDir, OLMCatalogChildDir) - pkgManOutputBytes, err := ioutil.ReadFile(filepath.Join(outputCatalogDir, testProjectName, pkgManFileName)) - if err != nil { - t.Fatalf("Failed to read output package manifest file: %v", err) + if b, ok := fileMap[g.(pkgGenerator).fileName]; !ok { + t.Error("Failed to generate package manifest") + } else { + assert.Equal(t, packageManifestNonStandardExp, string(b)) } - pkgManOutput := string(pkgManOutputBytes) - - assert.Equal(t, pkgManExp, pkgManOutput) - } +const packageManifestNonStandardExp = `channels: +- currentCSV: memcached-operator.v0.0.1 + name: alpha +- currentCSV: memcached-operator.v0.0.3 + name: beta +- currentCSV: memcached-operator.v0.0.4 + name: stable +defaultChannel: stable +packageName: memcached-operator +` + func TestGeneratePackageManifest(t *testing.T) { cleanupFunc := chDirWithCleanup(t, testGoDataDir) defer cleanupFunc() @@ -157,63 +135,6 @@ func TestValidatePackageManifest(t *testing.T) { } } -func TestNewPackageManifest(t *testing.T) { - type args struct { - operatorName string - channelName string - version string - } - tests := []struct { - name string - args args - want registry.PackageManifest - }{ - { - name: "Should return a valid registry.PackageManifest", - want: registry.PackageManifest{ - PackageName: "memcached-operator", - Channels: []registry.PackageChannel{ - registry.PackageChannel{ - Name: "stable", - CurrentCSVName: "memcached-operator.v0.0.3", - }, - }, - DefaultChannelName: "stable", - }, - args: args{ - operatorName: testProjectName, - channelName: "stable", - version: csvVersion, - }, - }, - { - name: "Should return a valid registry.PackageManifest with channel == alpha when it is not informed", - want: registry.PackageManifest{ - PackageName: "memcached-operator", - Channels: []registry.PackageChannel{ - registry.PackageChannel{ - Name: "alpha", - CurrentCSVName: "memcached-operator.v0.0.3", - }, - }, - DefaultChannelName: "alpha", - }, - args: args{ - operatorName: testProjectName, - version: csvVersion, - }, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got := newPackageManifest(tt.args.operatorName, tt.args.channelName, tt.args.version) - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("NewPackageManifest() = %v, want %v", got, tt.want) - } - }) - } -} - const packageManifestExp = `channels: - currentCSV: memcached-operator.v0.0.2 name: alpha diff --git a/internal/generate/testdata/non-standard-layout/expected-catalog/olm-catalog/memcached-operator/memcached-operator.package.yaml b/internal/generate/testdata/non-standard-layout/expected-catalog/olm-catalog/memcached-operator/memcached-operator.package.yaml index 64d34201345..d861520b58f 100644 --- a/internal/generate/testdata/non-standard-layout/expected-catalog/olm-catalog/memcached-operator/memcached-operator.package.yaml +++ b/internal/generate/testdata/non-standard-layout/expected-catalog/olm-catalog/memcached-operator/memcached-operator.package.yaml @@ -1,5 +1,7 @@ channels: -- currentCSV: memcached-operator.v0.0.3 +- currentCSV: memcached-operator.v0.0.1 + name: alpha +- currentCSV: memcached-operator.v0.0.4 name: stable defaultChannel: stable packageName: memcached-operator diff --git a/internal/util/projutil/project_util.go b/internal/util/projutil/project_util.go index 4ab7c609a12..f1e0112deee 100644 --- a/internal/util/projutil/project_util.go +++ b/internal/util/projutil/project_util.go @@ -40,6 +40,8 @@ const ( rolesDir = "roles" helmChartsDir = "helm-charts" goModFile = "go.mod" + + noticeColor = "\033[1;36m%s\033[0m" ) // OperatorType - the type of operator @@ -283,3 +285,9 @@ func CheckGoModules() error { } return nil } + +// PrintDeprecationWarning prints a colored warning wrapping msg to the terminal. +func PrintDeprecationWarning(msg string) { + fmt.Printf(noticeColor, "[Deprecation Notice] "+msg+". Refer to the version upgrade guide "+ + "for more information: https://operator-sdk.netlify.com/docs/migration/version-upgrade-guide\n\n") +}