@@ -20,14 +20,8 @@ import (
2020 "fmt"
2121 "os"
2222 "path/filepath"
23- "regexp"
24- "strings"
2523
2624 "github.com/sirupsen/logrus"
27- "sigs.k8s.io/release-utils/command"
28- "sigs.k8s.io/release-utils/util"
29-
30- "k8s.io/release/pkg/kubecross"
3125)
3226
3327const (
@@ -69,16 +63,29 @@ Published by your
6963Managers</a>.
7064`
7165
72- func CreateForBranch (opts * Options ) error {
66+ type Announce struct {
67+ options * Options
68+ impl
69+ }
70+
71+ // NewAnnounce returns a new Announce instance.
72+ func NewAnnounce (opts * Options ) * Announce {
73+ return & Announce {
74+ impl : & defaultImpl {},
75+ options : opts ,
76+ }
77+ }
78+
79+ func (a * Announce ) CreateForBranch () error {
7380 logrus .Infof (
7481 "Creating %s branch announcement in %s" ,
75- opts . branch , opts .workDir ,
82+ a . options . branch , a . options .workDir ,
7683 )
7784
78- if err := create (
79- opts .workDir ,
80- fmt .Sprintf ("Kubernetes %s branch has been created" , opts .branch ),
81- fmt .Sprintf (branchAnnouncement , opts .branch ),
85+ if err := a . impl . create (
86+ a . options .workDir ,
87+ fmt .Sprintf ("Kubernetes %s branch has been created" , a . options .branch ),
88+ fmt .Sprintf (branchAnnouncement , a . options .branch ),
8289 ); err != nil {
8390 return fmt .Errorf ("creating branch announcement: %w" , err )
8491 }
@@ -90,39 +97,39 @@ func CreateForBranch(opts *Options) error {
9097 return nil
9198}
9299
93- func CreateForRelease ( opts * Options ) error {
94- logrus .Infof ("Creating %s announcement in %s" , opts . tag , opts .workDir )
100+ func ( a * Announce ) CreateForRelease ( ) error {
101+ logrus .Infof ("Creating %s announcement in %s" , a . options . tag , a . options .workDir )
95102
96103 changelog := ""
97104
98105 // Read the changelog from the specified file if we got one
99- if opts .changelogFile != "" {
100- changelogData , err := os .ReadFile (opts .changelogFile )
106+ if a . options .changelogFile != "" {
107+ changelogData , err := os .ReadFile (a . options .changelogFile )
101108 if err != nil {
102109 return fmt .Errorf ("reading changelog html file: %w" , err )
103110 }
104111 changelog = string (changelogData )
105112 }
106113
107114 // ... unless it is overridden by passing the HTML directly
108- if opts .changelogHTML != "" {
109- changelog = opts .changelogHTML
115+ if a . options .changelogHTML != "" {
116+ changelog = a . options .changelogHTML
110117 }
111118
112- logrus .Infof ("Trying to get the Go version used to build %s..." , opts .tag )
113- goVersion , err := getGoVersion (opts .tag )
119+ logrus .Infof ("Trying to get the Go version used to build %s..." , a . options .tag )
120+ goVersion , err := a . impl . getGoVersion (a . options .tag )
114121 if err != nil {
115122 return err
116123 }
117124 logrus .Infof ("Found the following Go version: %s" , goVersion )
118125
119- if err := create (
120- opts .workDir ,
121- fmt .Sprintf ("Kubernetes %s is live!" , opts .tag ),
126+ if err := a . impl . create (
127+ a . options .workDir ,
128+ fmt .Sprintf ("Kubernetes %s is live!" , a . options .tag ),
122129 fmt .Sprintf (releaseAnnouncement ,
123- opts . tag , goVersion , opts .changelogPath ,
124- filepath .Base (opts . changelogPath ), opts .tag , changelog ,
125- opts . changelogPath , filepath .Base (opts . changelogPath ), opts .tag ,
130+ a . options . tag , goVersion , a . options .changelogPath ,
131+ filepath .Base (a . options . changelogPath ), a . options .tag , changelog ,
132+ a . options . changelogPath , filepath .Base (a . options . changelogPath ), a . options .tag ,
126133 ),
127134 ); err != nil {
128135 return fmt .Errorf ("creating release announcement: %w" , err )
@@ -131,68 +138,3 @@ func CreateForRelease(opts *Options) error {
131138 logrus .Infof ("Release announcement created" )
132139 return nil
133140}
134-
135- func create (workDir , subject , message string ) error {
136- subjectFile := filepath .Join (workDir , subjectFile )
137- //nolint:gosec // TODO(gosec): G306: Expect WriteFile permissions to be
138- // 0600 or less
139- if err := os .WriteFile (
140- subjectFile , []byte (subject ), 0o755 ,
141- ); err != nil {
142- return fmt .Errorf (
143- "writing subject to file %s: %w" ,
144- subjectFile ,
145- err ,
146- )
147- }
148- logrus .Debugf ("Wrote file %s" , subjectFile )
149-
150- announcementFile := filepath .Join (workDir , announcementFile )
151- //nolint:gosec // TODO(gosec): G306: Expect WriteFile permissions to be
152- // 0600 or less
153- if err := os .WriteFile (
154- announcementFile , []byte (message ), 0o755 ,
155- ); err != nil {
156- return fmt .Errorf (
157- "writing announcement to file %s: %w" ,
158- announcementFile ,
159- err ,
160- )
161- }
162- logrus .Debugf ("Wrote file %s" , announcementFile )
163-
164- return nil
165- }
166-
167- // getGoVersion runs kube-cross container and go version inside it.
168- // We're running kube-cross container because it's not guaranteed that
169- // k8s-cloud-builder container will be running the same Go version as
170- // the kube-cross container used to build the release.
171- func getGoVersion (tag string ) (string , error ) {
172- semver , err := util .TagStringToSemver (tag )
173- if err != nil {
174- return "" , fmt .Errorf ("parse version tag: %w" , err )
175- }
176-
177- branch := fmt .Sprintf ("release-%d.%d" , semver .Major , semver .Minor )
178- kc := kubecross .New ()
179- kubecrossVer , err := kc .ForBranch (branch )
180- if err != nil {
181- kubecrossVer , err = kc .Latest ()
182- if err != nil {
183- return "" , fmt .Errorf ("get kubecross version: %w" , err )
184- }
185- }
186-
187- kubecrossImg := fmt .Sprintf ("registry.k8s.io/build-image/kube-cross:%s" , kubecrossVer )
188-
189- res , err := command .New (
190- "docker" , "run" , "--rm" , kubecrossImg , "go" , "version" ,
191- ).RunSilentSuccessOutput ()
192- if err != nil {
193- return "" , fmt .Errorf ("get go version: %w" , err )
194- }
195-
196- versionRegex := regexp .MustCompile (`^?(\d+)(\.\d+)?(\.\d+)` )
197- return versionRegex .FindString (strings .TrimSpace (res .OutputTrimNL ())), nil
198- }
0 commit comments