Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to remember to upgrade the Getting Started as well.

### 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
Expand Down
45 changes: 32 additions & 13 deletions internal/generate/olm-catalog/package_manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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.
Expand Down
117 changes: 19 additions & 98 deletions internal/generate/olm-catalog/package_manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@
package olmcatalog

import (
"io/ioutil"
"log"
"os"
"path/filepath"
"reflect"
"testing"

"github.com/operator-framework/operator-registry/pkg/registry"
Expand All @@ -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()
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions internal/util/projutil/project_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
}