-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
version_checker.go
201 lines (173 loc) · 5.65 KB
/
version_checker.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
199
200
201
/*
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 cmd
import (
"context"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"time"
"github.com/blang/semver"
"github.com/google/go-github/v45/github"
"github.com/pkg/errors"
"golang.org/x/oauth2"
"k8s.io/client-go/util/homedir"
"sigs.k8s.io/yaml"
"sigs.k8s.io/cluster-api/cmd/clusterctl/client/config"
logf "sigs.k8s.io/cluster-api/cmd/clusterctl/log"
"sigs.k8s.io/cluster-api/version"
)
var (
// gitVersionRegEx matches git versions of style 0.3.7-45-c1aeccb679cd56
// see ./hack/version.sh for more info.
gitVersionRegEx = regexp.MustCompile(`(.*)-(\d+)-([0-9,a-f]{14})`)
)
type versionChecker struct {
versionFilePath string
cliVersion func() version.Info
githubClient *github.Client
}
// newVersionChecker returns a versionChecker. Its behavior has been inspired
// by https://github.com/cli/cli.
func newVersionChecker(vc config.VariablesClient) *versionChecker {
var client *github.Client
token, err := vc.Get("GITHUB_TOKEN")
if err == nil {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(context.TODO(), ts)
client = github.NewClient(tc)
} else {
client = github.NewClient(nil)
}
return &versionChecker{
versionFilePath: filepath.Join(homedir.HomeDir(), config.ConfigFolder, "version.yaml"),
cliVersion: version.Get,
githubClient: client,
}
}
// ReleaseInfo stores information about the release.
type ReleaseInfo struct {
Version string
URL string
}
// VersionState stores the release info and the last time it was updated.
type VersionState struct {
LastCheck time.Time
LatestRelease ReleaseInfo
}
// Check returns a message if the current clusterctl version is less than the
// latest available release for CAPI
// (https://github.com/kubernetes-sigs/cluster-api). It gets the latest
// release from github at most once during a 24 hour period and caches the
// state by default in $HOME/.cluster-api/state.yaml. If the clusterctl
// version is the same or greater it returns nothing.
func (v *versionChecker) Check() (string, error) {
log := logf.Log
cliVer, err := semver.ParseTolerant(v.cliVersion().GitVersion)
if err != nil {
return "", errors.Wrap(err, "unable to semver parse clusterctl GitVersion")
}
release, err := v.getLatestRelease()
if err != nil {
return "", err
}
if release == nil {
return "", nil
}
latestVersion, err := semver.ParseTolerant(release.Version)
if err != nil {
return "", errors.Wrap(err, "unable to semver parse latest release version")
}
// if we are using a dirty dev build, just log it out
if strings.HasSuffix(cliVer.String(), "-dirty") {
log.V(1).Info("⚠️ Using a development build of clusterctl.", "CLIVersion", cliVer.String(), "LatestGithubRelease", release.Version)
return "", nil
}
// if the cli version is a dev build off of the latest available release,
// the just log it out as informational.
if strings.HasPrefix(cliVer.String(), latestVersion.String()) && gitVersionRegEx.MatchString(cliVer.String()) {
log.V(1).Info("⚠️ Using a development build of clusterctl.", "CLIVersion", cliVer.String(), "LatestGithubRelease", release.Version)
return "", nil
}
if cliVer.Major == latestVersion.Major &&
cliVer.Minor == latestVersion.Minor &&
latestVersion.GT(cliVer) {
return fmt.Sprintf(`
New clusterctl version available: v%s -> v%s
%s
`, cliVer, latestVersion.String(), release.URL), nil
}
return "", nil
}
func (v *versionChecker) getLatestRelease() (*ReleaseInfo, error) {
log := logf.Log
vs, err := readStateFile(v.versionFilePath)
if err != nil {
return nil, errors.Wrap(err, "unable to read version state file")
}
// if there is no release info in the state file, pull latest release from github
if vs == nil {
release, _, err := v.githubClient.Repositories.GetLatestRelease(context.TODO(), "kubernetes-sigs", "cluster-api")
if err != nil {
log.V(1).Info("⚠️ Unable to get latest github release for clusterctl")
// failing silently here so we don't error out in air-gapped
// environments.
return nil, nil //nolint:nilerr
}
vs = &VersionState{
LastCheck: time.Now(),
LatestRelease: ReleaseInfo{
Version: release.GetTagName(),
URL: release.GetHTMLURL(),
},
}
}
if err := writeStateFile(v.versionFilePath, vs); err != nil {
return nil, errors.Wrap(err, "unable to write version state file")
}
return &vs.LatestRelease, nil
}
func writeStateFile(path string, vs *VersionState) error {
vsb, err := yaml.Marshal(vs)
if err != nil {
return err
}
if err := os.MkdirAll(filepath.Dir(path), os.ModePerm); err != nil {
return err
}
return os.WriteFile(path, vsb, 0600)
}
func readStateFile(filepath string) (*VersionState, error) {
b, err := os.ReadFile(filepath) //nolint:gosec
if err != nil {
if os.IsNotExist(err) {
// if the file doesn't exist yet, don't error
return nil, nil
}
return nil, err
}
vs := new(VersionState)
if err := yaml.Unmarshal(b, vs); err != nil {
return nil, err
}
// If the last check is more than 24 hours ago, then don't return the
// version state of the file.
if time.Since(vs.LastCheck).Hours() > 24 {
return nil, nil
}
return vs, nil
}