Skip to content

Commit

Permalink
generate bundle: Support overwriting of annotations.yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
KeenonLee committed Nov 13, 2024
1 parent 95b3f89 commit ace4a20
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 4 deletions.
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
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 ace4a20

Please sign in to comment.