-
Notifications
You must be signed in to change notification settings - Fork 1.8k
generate: add bundle subcommand for current project layouts
#3088
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
estroz
merged 1 commit into
operator-framework:master
from
estroz:feature/generate-bundle-legacy
May 23, 2020
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| entries: | ||
| - description: > | ||
| 'bundle generate' generates bundles for current project layouts; this | ||
| has the same behavior as 'generate csv --make-manifests=true' | ||
|
|
||
| kind: addition |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| // Copyright 2020 The Operator-SDK Authors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package bundle | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "path/filepath" | ||
|
|
||
| "github.com/operator-framework/operator-registry/pkg/lib/bundle" | ||
| log "github.com/sirupsen/logrus" | ||
|
|
||
| genutil "github.com/operator-framework/operator-sdk/cmd/operator-sdk/generate/internal" | ||
| gencsv "github.com/operator-framework/operator-sdk/internal/generate/clusterserviceversion" | ||
| "github.com/operator-framework/operator-sdk/internal/generate/collector" | ||
| "github.com/operator-framework/operator-sdk/internal/util/projutil" | ||
| ) | ||
|
|
||
| // setCommonDefaultsLegacy sets defaults useful to all modes of this subcommand. | ||
| func (c *bundleCmd) setCommonDefaultsLegacy() { | ||
|
estroz marked this conversation as resolved.
|
||
| if c.operatorName == "" { | ||
| c.operatorName = filepath.Base(projutil.MustGetwd()) | ||
| } | ||
| } | ||
|
|
||
| // validateManifestsLegacy validates c for bundle manifests generation for | ||
| // legacy project layouts. | ||
| func (c bundleCmd) validateManifestsLegacy() error { | ||
| if c.version != "" { | ||
| if err := genutil.ValidateVersion(c.version); err != nil { | ||
| return err | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // runManifestsLegacy generates bundle manifests for legacy project layouts. | ||
| func (c bundleCmd) runManifestsLegacy() (err error) { | ||
|
|
||
| if !c.quiet { | ||
| if c.version == "" { | ||
| log.Info("Generating bundle manifests") | ||
| } else { | ||
| log.Info("Generating bundle manifests version", c.version) | ||
| } | ||
| } | ||
|
|
||
| if c.apisDir == "" { | ||
| c.apisDir = filepath.Join("pkg", "apis") | ||
| } | ||
| if c.manifestRoot == "" { | ||
| c.manifestRoot = "deploy" | ||
| } | ||
| if c.crdsDir == "" { | ||
| c.crdsDir = filepath.Join(c.manifestRoot, "crds") | ||
| } | ||
| defaultBundleDir := filepath.Join(c.manifestRoot, "olm-catalog", c.operatorName) | ||
| if c.inputDir == "" { | ||
| c.inputDir = defaultBundleDir | ||
| } | ||
| if c.outputDir == "" { | ||
| c.outputDir = defaultBundleDir | ||
| } | ||
|
|
||
| col := &collector.Manifests{} | ||
| if err := col.UpdateFromDirs(c.manifestRoot, c.crdsDir); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| csvGen := gencsv.Generator{ | ||
| OperatorName: c.operatorName, | ||
| OperatorType: projutil.GetOperatorType(), | ||
| Version: c.version, | ||
| Collector: col, | ||
| } | ||
|
|
||
| opts := []gencsv.LegacyOption{ | ||
| gencsv.WithBundleBase(c.inputDir, c.apisDir, c.interactiveLevel), | ||
| gencsv.LegacyOption(gencsv.WithBundleWriter(c.outputDir)), | ||
| } | ||
| if err := csvGen.GenerateLegacy(opts...); err != nil { | ||
| return fmt.Errorf("error generating ClusterServiceVersion: %v", err) | ||
| } | ||
|
|
||
| dir := filepath.Join(c.outputDir, bundle.ManifestsDir) | ||
| if err := genutil.WriteCRDFilesLegacy(dir, col.CustomResourceDefinitions...); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if !c.quiet { | ||
| log.Infoln("Bundle manifests generated successfully in", c.outputDir) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // runMetadataLegacy generates a bundle.Dockerfile and bundle metadata for | ||
| // legacy project layouts. | ||
| func (c bundleCmd) runMetadataLegacy() error { | ||
|
|
||
| directory := c.inputDir | ||
| if directory == "" { | ||
| // There may be no existing bundle at the default path, so assume manifests | ||
| // were generated in the output directs. | ||
| defaultDirectory := filepath.Join("deploy", "olm-catalog", c.operatorName, bundle.ManifestsDir) | ||
| if c.outputDir != "" && genutil.IsNotExist(defaultDirectory) { | ||
| directory = filepath.Join(c.outputDir, bundle.ManifestsDir) | ||
| } else { | ||
| directory = defaultDirectory | ||
| } | ||
| } else { | ||
| directory = filepath.Join(directory, bundle.ManifestsDir) | ||
| } | ||
| outputDir := c.outputDir | ||
| if filepath.Clean(outputDir) == filepath.Clean(directory) { | ||
| outputDir = "" | ||
| } | ||
|
|
||
| return c.generateMetadata(directory, outputDir) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need to create the legacy one? Is not it == the new one and just change the paths?
Is not harder to have the same implementation duplicated instead of having the code centralized to keep it maintained? WDYT? See: #2948 (comment)
PS.; My first approach was to do it as you did here, however, after some comments from @joelanford I think it makes more sense just change the path instead of it as he suggested.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm of the opinion that some code duplication is OK, especially with complex command setup like this. It's easier to maintain, then remove when we remove current project layout support.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm okay with duplication in this case as well. From my experience with trying to reuse the code for
generate csvto be used by both the old and new CLI commands it made the underlying command a lot more convoluted.@camilamacedo86
operator-sdk run localwas a much simpler command where we just had to change 1 path.