Skip to content

Commit a5c0daf

Browse files
committed
generate bundle: Support overwriting of annotations.yaml
Signed-off-by: Keenon Lee <[email protected]>
1 parent 95b3f89 commit a5c0daf

File tree

4 files changed

+65
-4
lines changed

4 files changed

+65
-4
lines changed

internal/cmd/operator-sdk/generate/bundle/bundle.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ func (c bundleCmd) runMetadata() error {
286286
if !errors.As(err, &merr) {
287287
return err
288288
}
289-
} else if !c.overwrite {
289+
} else if !c.overwrite && !c.overwriteAnnotations {
290290
return nil
291291
}
292292
}
@@ -302,6 +302,11 @@ func (c bundleCmd) runMetadata() error {
302302
IsScoreConfigPresent: genutil.IsExist(scorecardConfigPath),
303303
}
304304

305+
if c.overwriteAnnotations {
306+
return bundleMetadata.GenerateAnnotations() // Overwrite only annotations.yaml
307+
}
308+
309+
// If overwrite or metadata is generated for the first time, all files will be overwritten.
305310
return bundleMetadata.GenerateMetadata()
306311
}
307312

internal/cmd/operator-sdk/generate/bundle/cmd.go

+10-3
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ type bundleCmd struct {
4242
extraServiceAccounts []string
4343

4444
// Metadata options.
45-
channels string
46-
defaultChannel string
47-
overwrite bool
45+
channels string
46+
defaultChannel string
47+
overwrite bool
48+
overwriteAnnotations bool
4849

4950
// These are set if a PROJECT config is not present.
5051
layout string
@@ -63,6 +64,11 @@ func NewCmd() *cobra.Command {
6364
Long: longHelp,
6465
Example: examples,
6566
RunE: func(cmd *cobra.Command, args []string) error {
67+
if c.overwriteAnnotations {
68+
// Priority control, when checking --overwrite-annotations, set --overwrite to false
69+
c.overwrite = false
70+
}
71+
6672
if len(args) != 0 {
6773
return fmt.Errorf("command %s doesn't accept any arguments", cmd.CommandPath())
6874
}
@@ -138,6 +144,7 @@ func (c *bundleCmd) addFlagsTo(fs *pflag.FlagSet) {
138144
"Names of service accounts, outside of the operator's Deployment account, "+
139145
"that have bindings to {Cluster}Roles that should be added to the CSV")
140146
fs.BoolVar(&c.overwrite, "overwrite", true, "Overwrite the bundle's metadata and Dockerfile if they exist")
147+
fs.BoolVar(&c.overwriteAnnotations, "overwrite-annotations", false, "Only overwrite annotations.yaml without modifying bundle.Dockerfile")
141148
fs.BoolVarP(&c.quiet, "quiet", "q", false, "Run in quiet mode")
142149
fs.BoolVar(&c.stdout, "stdout", false, "Write bundle manifest to stdout")
143150

internal/util/bundleutil/bundleutil.go

+48
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,54 @@ func (meta *BundleMetaData) GenerateMetadata() error {
140140
return nil
141141
}
142142

143+
// GenerateAnnotations generates the annotations.yaml file using the provided
144+
// annotation values for the Operator Bundle.
145+
func (meta *BundleMetaData) GenerateAnnotations() error {
146+
// Ensure the output directory exists
147+
metadataDir := filepath.Join(meta.BundleDir, defaultMetadataDir)
148+
if err := os.MkdirAll(metadataDir, projutil.DirMode); err != nil {
149+
return err
150+
}
151+
152+
// Prepare annotation values
153+
values := annotationsValues{
154+
BundleDir: meta.BundleDir,
155+
PackageName: meta.PackageName,
156+
Channels: meta.Channels,
157+
DefaultChannel: meta.DefaultChannel,
158+
IsScorecardConfigPresent: meta.IsScoreConfigPresent,
159+
}
160+
161+
// Add any other labels to the values
162+
for k, v := range meta.OtherLabels {
163+
values.OtherLabels = append(values.OtherLabels, fmt.Sprintf("%s=%s", k, v))
164+
}
165+
sort.Strings(values.OtherLabels)
166+
167+
// Define the path to annotations.yaml
168+
annotationsPath := filepath.Join(metadataDir, "annotations.yaml")
169+
170+
// Open (or create) the annotations.yaml file
171+
f, err := os.OpenFile(annotationsPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
172+
if err != nil {
173+
return err
174+
}
175+
defer func() {
176+
if err := f.Close(); err != nil {
177+
log.Error(err)
178+
}
179+
}()
180+
181+
// Create and execute the annotations template
182+
err = annotationsTemplate.Execute(f, values)
183+
if err != nil {
184+
return err
185+
}
186+
187+
log.Infof("Annotations generated successfully at %s", annotationsPath)
188+
return nil
189+
}
190+
143191
// CopyOperatorManifests copies packagemanifestsDir/manifests to bundleDir/manifests.
144192
func (meta *BundleMetaData) CopyOperatorManifests() error {
145193
return copyOperatorManifests(meta.PkgmanifestPath, filepath.Join(meta.BundleDir, defaultManifestDir))

website/content/en/docs/cli/operator-sdk_generate_bundle.md

+1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ operator-sdk generate bundle [flags]
100100
--metadata Generate bundle metadata and Dockerfile
101101
--output-dir string Directory to write the bundle to
102102
--overwrite Overwrite the bundle's metadata and Dockerfile if they exist (default true)
103+
--overwrite-annotations Only overwrite annotations.yaml without modifying bundle.Dockerfile
103104
--package string Bundle's package name
104105
-q, --quiet Run in quiet mode
105106
--stdout Write bundle manifest to stdout

0 commit comments

Comments
 (0)