-
Notifications
You must be signed in to change notification settings - Fork 505
/
announce.go
198 lines (169 loc) · 5.54 KB
/
announce.go
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package announce
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/sirupsen/logrus"
"sigs.k8s.io/release-utils/command"
"sigs.k8s.io/release-utils/util"
"k8s.io/release/pkg/kubecross"
)
const (
announcementFile = "announcement.html"
subjectFile = "announcement-subject.txt"
)
const branchAnnouncement = `Kubernetes Community,
<p>
Kubernetes' %s branch has been created.
<p>
The release owner will be sending updates on how to interact with this branch
shortly. The <a href=https://git.k8s.io/community/contributors/devel/sig-release/cherry-picks.md>Cherrypick
Guide</A> has some general guidance on how things will proceed.
<p>
Announced by your
<a href=https://git.k8s.io/website/content/en/releases/release-managers.md>Kubernetes Release
Managers</A>.
`
const releaseAnnouncement = `Kubernetes Community,
<p>
Kubernetes <b>%s</b> has been built and pushed using Golang version <b>%s</b>.
<p>
The release notes have been updated in
<a href=https://git.k8s.io/kubernetes/%s>%s</a>, with a pointer to them on
<a href=https://github.com/kubernetes/kubernetes/releases/tag/%s>GitHub</a>:
<p>
<hr>
%s
<hr>
<p><br>
Contributors, the
<a href=https://git.k8s.io/kubernetes/%s>%s</a> has been bootstrapped with
%s release notes and you may edit now as needed.
<p><br><br>
Published by your
<a href=https://git.k8s.io/website/content/en/releases/release-managers.md>Kubernetes Release
Managers</a>.
`
func CreateForBranch(opts *Options) error {
logrus.Infof(
"Creating %s branch announcement in %s",
opts.branch, opts.workDir,
)
if err := create(
opts.workDir,
fmt.Sprintf("Kubernetes %s branch has been created", opts.branch),
fmt.Sprintf(branchAnnouncement, opts.branch),
); err != nil {
return fmt.Errorf("creating branch announcement: %w", err)
}
// TODO: When we create a new branch, we notify the publishing-bot folks by
// creating an issue for them (see anago)
logrus.Infof("Branch announcement created")
return nil
}
func CreateForRelease(opts *Options) error {
logrus.Infof("Creating %s announcement in %s", opts.tag, opts.workDir)
changelog := ""
// Read the changelog from the specified file if we got one
if opts.changelogFile != "" {
changelogData, err := os.ReadFile(opts.changelogFile)
if err != nil {
return fmt.Errorf("reading changelog html file: %w", err)
}
changelog = string(changelogData)
}
// ... unless it is overridden by passing the HTML directly
if opts.changelogHTML != "" {
changelog = opts.changelogHTML
}
logrus.Infof("Trying to get the Go version used to build %s...", opts.tag)
goVersion, err := getGoVersion(opts.tag)
if err != nil {
return err
}
logrus.Infof("Found the following Go version: %s", goVersion)
if err := create(
opts.workDir,
fmt.Sprintf("Kubernetes %s is live!", opts.tag),
fmt.Sprintf(releaseAnnouncement,
opts.tag, goVersion, opts.changelogPath,
filepath.Base(opts.changelogPath), opts.tag, changelog,
opts.changelogPath, filepath.Base(opts.changelogPath), opts.tag,
),
); err != nil {
return fmt.Errorf("creating release announcement: %w", err)
}
logrus.Infof("Release announcement created")
return nil
}
func create(workDir, subject, message string) error {
subjectFile := filepath.Join(workDir, subjectFile)
//nolint:gosec // TODO(gosec): G306: Expect WriteFile permissions to be
// 0600 or less
if err := os.WriteFile(
subjectFile, []byte(subject), 0o755,
); err != nil {
return fmt.Errorf(
"writing subject to file %s: %w",
subjectFile,
err,
)
}
logrus.Debugf("Wrote file %s", subjectFile)
announcementFile := filepath.Join(workDir, announcementFile)
//nolint:gosec // TODO(gosec): G306: Expect WriteFile permissions to be
// 0600 or less
if err := os.WriteFile(
announcementFile, []byte(message), 0o755,
); err != nil {
return fmt.Errorf(
"writing announcement to file %s: %w",
announcementFile,
err,
)
}
logrus.Debugf("Wrote file %s", announcementFile)
return nil
}
// getGoVersion runs kube-cross container and go version inside it.
// We're running kube-cross container because it's not guaranteed that
// k8s-cloud-builder container will be running the same Go version as
// the kube-cross container used to build the release.
func getGoVersion(tag string) (string, error) {
semver, err := util.TagStringToSemver(tag)
if err != nil {
return "", fmt.Errorf("parse version tag: %w", err)
}
branch := fmt.Sprintf("release-%d.%d", semver.Major, semver.Minor)
kc := kubecross.New()
kubecrossVer, err := kc.ForBranch(branch)
if err != nil {
kubecrossVer, err = kc.Latest()
if err != nil {
return "", fmt.Errorf("get kubecross version: %w", err)
}
}
kubecrossImg := fmt.Sprintf("k8s.gcr.io/build-image/kube-cross:%s", kubecrossVer)
res, err := command.New(
"docker", "run", "--rm", kubecrossImg, "go", "version",
).RunSilentSuccessOutput()
if err != nil {
return "", fmt.Errorf("get go version: %w", err)
}
versionRegex := regexp.MustCompile(`^?(\d+)(\.\d+)?(\.\d+)`)
return versionRegex.FindString(strings.TrimSpace(res.OutputTrimNL())), nil
}