Skip to content

Commit

Permalink
generate bundle: Support overwriting of annotations.yaml
Browse files Browse the repository at this point in the history
Signed-off-by: Keenon Lee <[email protected]>
  • Loading branch information
KeenonLee committed Nov 20, 2024
1 parent 95b3f89 commit 7dda757
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 5 deletions.
6 changes: 6 additions & 0 deletions changelog/fragments/feat-overwrite-annotations.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
entries:
- description: >
Add `--overwrite-annotations` option to the `operator-sdk generate bundle` command.
This allows developers to overwrite `annotations.yaml` without modifying `bundle.Dockerfile`.
kind: "addition"
breaking: false
7 changes: 6 additions & 1 deletion internal/cmd/operator-sdk/generate/bundle/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ func (c bundleCmd) runMetadata() error {
if !errors.As(err, &merr) {
return err
}
} else if !c.overwrite {
} else if !c.overwrite && !c.overwriteAnnotations {
return nil
}
}
Expand All @@ -302,6 +302,11 @@ func (c bundleCmd) runMetadata() error {
IsScoreConfigPresent: genutil.IsExist(scorecardConfigPath),
}

if c.overwriteAnnotations {
return bundleMetadata.GenerateAnnotations() // Overwrite only annotations.yaml
}

// If overwrite or metadata is generated for the first time, all files will be overwritten.
return bundleMetadata.GenerateMetadata()
}

Expand Down
13 changes: 10 additions & 3 deletions internal/cmd/operator-sdk/generate/bundle/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ type bundleCmd struct {
extraServiceAccounts []string

// Metadata options.
channels string
defaultChannel string
overwrite bool
channels string
defaultChannel string
overwrite bool
overwriteAnnotations bool

// These are set if a PROJECT config is not present.
layout string
Expand All @@ -63,6 +64,11 @@ func NewCmd() *cobra.Command {
Long: longHelp,
Example: examples,
RunE: func(cmd *cobra.Command, args []string) error {
if c.overwriteAnnotations {
// Priority control, when checking --overwrite-annotations, set --overwrite to false
c.overwrite = false
}

if len(args) != 0 {
return fmt.Errorf("command %s doesn't accept any arguments", cmd.CommandPath())
}
Expand Down Expand Up @@ -138,6 +144,7 @@ func (c *bundleCmd) addFlagsTo(fs *pflag.FlagSet) {
"Names of service accounts, outside of the operator's Deployment account, "+
"that have bindings to {Cluster}Roles that should be added to the CSV")
fs.BoolVar(&c.overwrite, "overwrite", true, "Overwrite the bundle's metadata and Dockerfile if they exist")
fs.BoolVar(&c.overwriteAnnotations, "overwrite-annotations", false, "Only overwrite annotations.yaml without modifying bundle.Dockerfile")
fs.BoolVarP(&c.quiet, "quiet", "q", false, "Run in quiet mode")
fs.BoolVar(&c.stdout, "stdout", false, "Write bundle manifest to stdout")

Expand Down
2 changes: 1 addition & 1 deletion internal/generate/internal/genutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func WriteObject(w io.Writer, obj interface{}) error {
return write(w, b)
}

// WriteObject writes any object to w.
// WriteYAML writes any object to w.
func WriteYAML(w io.Writer, obj interface{}) error {
b, err := yaml.Marshal(obj)
if err != nil {
Expand Down
48 changes: 48 additions & 0 deletions internal/util/bundleutil/bundleutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,54 @@ func (meta *BundleMetaData) GenerateMetadata() error {
return nil
}

// GenerateAnnotations generates the annotations.yaml file using the provided
// annotation values for the Operator Bundle.
func (meta *BundleMetaData) GenerateAnnotations() error {
// Ensure the output directory exists
metadataDir := filepath.Join(meta.BundleDir, defaultMetadataDir)
if err := os.MkdirAll(metadataDir, projutil.DirMode); err != nil {
return err
}

// Prepare annotation values
values := annotationsValues{
BundleDir: meta.BundleDir,
PackageName: meta.PackageName,
Channels: meta.Channels,
DefaultChannel: meta.DefaultChannel,
IsScorecardConfigPresent: meta.IsScoreConfigPresent,
}

// Add any other labels to the values
for k, v := range meta.OtherLabels {
values.OtherLabels = append(values.OtherLabels, fmt.Sprintf("%s=%s", k, v))
}
sort.Strings(values.OtherLabels)

// Define the path to annotations.yaml
annotationsPath := filepath.Join(metadataDir, "annotations.yaml")

// Open (or create) the annotations.yaml file
f, err := os.OpenFile(annotationsPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
if err != nil {
return err
}
defer func() {
if err := f.Close(); err != nil {
log.Error(err)
}
}()

// Create and execute the annotations template
err = annotationsTemplate.Execute(f, values)
if err != nil {
return err
}

log.Infof("Annotations generated successfully at %s", annotationsPath)
return nil
}

// CopyOperatorManifests copies packagemanifestsDir/manifests to bundleDir/manifests.
func (meta *BundleMetaData) CopyOperatorManifests() error {
return copyOperatorManifests(meta.PkgmanifestPath, filepath.Join(meta.BundleDir, defaultManifestDir))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ operator-sdk generate bundle [flags]
--metadata Generate bundle metadata and Dockerfile
--output-dir string Directory to write the bundle to
--overwrite Overwrite the bundle's metadata and Dockerfile if they exist (default true)
--overwrite-annotations Only overwrite annotations.yaml without modifying bundle.Dockerfile
--package string Bundle's package name
-q, --quiet Run in quiet mode
--stdout Write bundle manifest to stdout
Expand Down

0 comments on commit 7dda757

Please sign in to comment.