-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathpatch_csv.go
More file actions
142 lines (120 loc) · 3.96 KB
/
patch_csv.go
File metadata and controls
142 lines (120 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package cmd
import (
"errors"
"flag"
"fmt"
"io"
"os"
"slices"
"strings"
"github.com/stackrox/rox/operator/bundle_helpers/pkg/csv"
"helm.sh/helm/v3/pkg/chartutil"
)
// PatchCSV patches a ClusterServiceVersion YAML file with version and image information.
func PatchCSV(args []string) error {
flags := flag.NewFlagSet("patch-csv", flag.ExitOnError)
version := flags.String("use-version", "", "SemVer version of the operator (required)")
firstVersion := flags.String("first-version", "", "First version of operator ever published (required)")
operatorImage := flags.String("operator-image", "", "Operator image reference (required)")
relatedImagesMode := flags.String("related-images-mode", "downstream", "Mode for related images: downstream, omit, konflux")
addSupportedArch := flags.String("add-supported-arch", "amd64,arm64,ppc64le,s390x", "Comma-separated list of supported architectures")
echoReplacedVersionOnly := flags.Bool("echo-replaced-version-only", false, "Only compute and print replaced version")
unreleased := flags.String("unreleased", "", "Not yet released version, if any")
flags.Usage = func() {
fmt.Fprintln(os.Stderr, "Usage: bundle-helper patch-csv [options] < input.yaml > output.yaml")
fmt.Fprintln(os.Stderr, "")
fmt.Fprintln(os.Stderr, "Patches ClusterServiceVersion files with version updates, image replacements,")
fmt.Fprintln(os.Stderr, "and related images configuration.")
fmt.Fprintln(os.Stderr, "")
fmt.Fprintln(os.Stderr, "Options:")
flags.PrintDefaults()
}
if err := flags.Parse(args); err != nil {
return err
}
// Handle help
if len(args) > 0 && (args[0] == "-h" || args[0] == "--help") {
flags.Usage()
return nil
}
// Validate required flags
if *version == "" || *firstVersion == "" || *operatorImage == "" {
fmt.Fprintln(os.Stderr, "Error: --use-version, --first-version, and --operator-image are required")
flags.Usage()
return errors.New("missing required flags")
}
// Validate related-images-mode
validModes := []string{"downstream", "omit", "konflux"}
if !slices.Contains(validModes, *relatedImagesMode) {
return fmt.Errorf("--related-images-mode must be one of: downstream, omit, konflux (got: %s)", *relatedImagesMode)
}
// Read CSV from stdin
input, err := io.ReadAll(os.Stdin)
if err != nil {
return fmt.Errorf("failed to read stdin: %w", err)
}
// Parse YAML
doc, err := chartutil.ReadValues(input)
if err != nil {
return fmt.Errorf("failed to parse YAML: %w", err)
}
// Handle --echo-replaced-version-only mode
if *echoReplacedVersionOnly {
return echoReplacedVersion(doc, *version, *firstVersion, *unreleased)
}
// Parse supported architectures
var arches []string
if *addSupportedArch != "" {
arches = extractArches(*addSupportedArch)
}
// Patch the CSV
opts := csv.PatchOptions{
Version: *version,
OperatorImage: *operatorImage,
FirstVersion: *firstVersion,
RelatedImagesMode: *relatedImagesMode,
ExtraSupportedArchs: arches,
Unreleased: *unreleased,
}
if err := csv.PatchCSV(doc, opts); err != nil {
return fmt.Errorf("failed to patch CSV: %w", err)
}
// Encode to YAML and normalize through Python to match PyYAML's exact formatting
return encodeAndNormalizeYAML(doc, os.Stdout)
}
func echoReplacedVersion(doc chartutil.Values, version, firstVersion, unreleased string) error {
rawName, err := csv.GetRawName(doc)
if err != nil {
return err
}
spec, err := doc.Table("spec")
if err != nil {
return fmt.Errorf("failed to get spec: %w", err)
}
_, replacedVersion, err := csv.CalculateReplacedVersionForCSV(
version,
firstVersion,
unreleased,
rawName,
spec,
)
if err != nil {
return err
}
if replacedVersion != nil {
fmt.Println(replacedVersion.String())
}
return nil
}
func extractArches(s string) []string {
if s == "" {
return nil
}
parts := []string{}
for p := range strings.SplitSeq(s, ",") {
if trimmed := strings.TrimSpace(p); trimmed != "" {
parts = append(parts, trimmed)
}
}
return parts
}