-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathbinaries.go
344 lines (294 loc) · 10.7 KB
/
binaries.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
Copyright 2025 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 envtest
import (
"archive/tar"
"bytes"
"compress/gzip"
"context"
"crypto/sha512"
"encoding/hex"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"runtime"
"sort"
"strings"
"github.com/blang/semver/v4"
"sigs.k8s.io/yaml"
)
// DefaultBinaryAssetsIndexURL is the default index used in HTTPClient.
var DefaultBinaryAssetsIndexURL = "https://raw.githubusercontent.com/kubernetes-sigs/controller-tools/HEAD/envtest-releases.yaml"
// SetupEnvtestDefaultBinaryAssetsDirectory returns the default location that setup-envtest uses to store envtest binaries.
// Setting BinaryAssetsDirectory to this directory allows sharing envtest binaries with setup-envtest.
//
// The directory is dependent on operating system:
//
// - Windows: %LocalAppData%\kubebuilder-envtest
// - OSX: ~/Library/Application Support/io.kubebuilder.envtest
// - Others: ${XDG_DATA_HOME:-~/.local/share}/kubebuilder-envtest
//
// Otherwise, it errors out. Note that these paths must not be relied upon
// manually.
func SetupEnvtestDefaultBinaryAssetsDirectory() (string, error) {
var baseDir string
// find the base data directory
switch runtime.GOOS {
case "windows":
baseDir = os.Getenv("LocalAppData")
if baseDir == "" {
return "", errors.New("%LocalAppData% is not defined")
}
case "darwin":
homeDir := os.Getenv("HOME")
if homeDir == "" {
return "", errors.New("$HOME is not defined")
}
baseDir = filepath.Join(homeDir, "Library/Application Support")
default:
baseDir = os.Getenv("XDG_DATA_HOME")
if baseDir == "" {
homeDir := os.Getenv("HOME")
if homeDir == "" {
return "", errors.New("neither $XDG_DATA_HOME nor $HOME are defined")
}
baseDir = filepath.Join(homeDir, ".local/share")
}
}
// append our program-specific dir to it (OSX has a slightly different
// convention so try to follow that).
switch runtime.GOOS {
case "darwin", "ios":
return filepath.Join(baseDir, "io.kubebuilder.envtest", "k8s"), nil
default:
return filepath.Join(baseDir, "kubebuilder-envtest", "k8s"), nil
}
}
// index represents an index of envtest binary archives. Example:
//
// releases:
// v1.28.0:
// envtest-v1.28.0-darwin-amd64.tar.gz:
// hash: <sha512-hash>
// selfLink: <url-to-archive-with-envtest-binaries>
type index struct {
// Releases maps Kubernetes versions to Releases (envtest archives).
Releases map[string]release `json:"releases"`
}
// release maps an archive name to an archive.
type release map[string]archive
// archive contains the self link to an archive and its hash.
type archive struct {
Hash string `json:"hash"`
SelfLink string `json:"selfLink"`
}
func downloadBinaryAssets(ctx context.Context, binaryAssetsDirectory, binaryAssetsVersion, binaryAssetsIndexURL string) (string, string, string, error) {
if binaryAssetsIndexURL == "" {
binaryAssetsIndexURL = DefaultBinaryAssetsIndexURL
}
downloadRootDir := binaryAssetsDirectory
if downloadRootDir == "" {
var err error
if downloadRootDir, err = os.MkdirTemp("", "envtest-binaries-"); err != nil {
return "", "", "", fmt.Errorf("failed to create tmp directory for envtest binaries: %w", err)
}
}
var binaryAssetsIndex *index
if binaryAssetsVersion == "" {
var err error
binaryAssetsIndex, err = getIndex(ctx, binaryAssetsIndexURL)
if err != nil {
return "", "", "", err
}
binaryAssetsVersion, err = latestStableVersionFromIndex(binaryAssetsIndex)
if err != nil {
return "", "", "", err
}
}
// Storing the envtest binaries in a directory structure that is compatible with setup-envtest.
// This makes it possible to share the envtest binaries with setup-envtest if the BinaryAssetsDirectory is set to SetupEnvtestDefaultBinaryAssetsDirectory().
downloadDir := path.Join(downloadRootDir, fmt.Sprintf("%s-%s-%s", strings.TrimPrefix(binaryAssetsVersion, "v"), runtime.GOOS, runtime.GOARCH))
if !fileExists(downloadDir) {
if err := os.MkdirAll(downloadDir, 0700); err != nil {
return "", "", "", fmt.Errorf("failed to create directory %q for envtest binaries: %w", downloadDir, err)
}
}
apiServerPath := path.Join(downloadDir, "kube-apiserver")
etcdPath := path.Join(downloadDir, "etcd")
kubectlPath := path.Join(downloadDir, "kubectl")
if fileExists(apiServerPath) && fileExists(etcdPath) && fileExists(kubectlPath) {
// Nothing to do if the binaries already exist.
return apiServerPath, etcdPath, kubectlPath, nil
}
// Get Index if we didn't have to get it above to get the latest stable version.
if binaryAssetsIndex == nil {
var err error
binaryAssetsIndex, err = getIndex(ctx, binaryAssetsIndexURL)
if err != nil {
return "", "", "", err
}
}
buf := &bytes.Buffer{}
if err := downloadBinaryAssetsArchive(ctx, binaryAssetsIndex, binaryAssetsVersion, buf); err != nil {
return "", "", "", err
}
gzStream, err := gzip.NewReader(buf)
if err != nil {
return "", "", "", fmt.Errorf("failed to create gzip reader to extract envtest binaries: %w", err)
}
tarReader := tar.NewReader(gzStream)
var header *tar.Header
for header, err = tarReader.Next(); err == nil; header, err = tarReader.Next() {
if header.Typeflag != tar.TypeReg {
// Skip non-regular file entry in archive.
continue
}
// Just dump all files directly into the download directory, ignoring the prefixed directory paths.
// We also ignore bits for the most part (except for X).
fileName := filepath.Base(header.Name)
perms := 0555 & header.Mode // make sure we're at most r+x
// Setting O_EXCL to get an error if the file already exists.
f, err := os.OpenFile(path.Join(downloadDir, fileName), os.O_RDWR|os.O_CREATE|os.O_EXCL|os.O_TRUNC, os.FileMode(perms))
if err != nil {
if os.IsExist(err) {
// Nothing to do if the file already exists. We assume another process created the file concurrently.
continue
}
return "", "", "", fmt.Errorf("failed to create file %s in directory %s: %w", fileName, downloadDir, err)
}
if err := func() error {
defer f.Close()
if _, err := io.Copy(f, tarReader); err != nil {
return fmt.Errorf("failed to write file %s in directory %s: %w", fileName, downloadDir, err)
}
return nil
}(); err != nil {
return "", "", "", fmt.Errorf("failed to close file %s in directory %s: %w", fileName, downloadDir, err)
}
}
return apiServerPath, etcdPath, kubectlPath, nil
}
func fileExists(path string) bool {
if _, err := os.Stat(path); err == nil {
return true
}
return false
}
func downloadBinaryAssetsArchive(ctx context.Context, index *index, version string, out io.Writer) error {
archives, ok := index.Releases[version]
if !ok {
return fmt.Errorf("failed to find envtest binaries for version %s", version)
}
archiveName := fmt.Sprintf("envtest-%s-%s-%s.tar.gz", version, runtime.GOOS, runtime.GOARCH)
archive, ok := archives[archiveName]
if !ok {
return fmt.Errorf("failed to find envtest binaries for version %s with archiveName %s", version, archiveName)
}
archiveURL, err := url.Parse(archive.SelfLink)
if err != nil {
return fmt.Errorf("failed to parse envtest binaries archive URL %q: %w", archiveURL, err)
}
req, err := http.NewRequestWithContext(ctx, "GET", archiveURL.String(), nil)
if err != nil {
return fmt.Errorf("failed to create request to download %s: %w", archiveURL.String(), err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return fmt.Errorf("failed to download %s: %w", archiveURL.String(), err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return fmt.Errorf("failed to download %s, got status %q", archiveURL.String(), resp.Status)
}
return readBody(resp, out, archiveName, archive.Hash)
}
func latestStableVersionFromIndex(index *index) (string, error) {
if len(index.Releases) == 0 {
return "", fmt.Errorf("failed to find latest stable version from index: index is empty")
}
parsedVersions := []semver.Version{}
for releaseVersion := range index.Releases {
v, err := semver.ParseTolerant(releaseVersion)
if err != nil {
return "", fmt.Errorf("failed to parse version %q: %w", releaseVersion, err)
}
// Filter out pre-releases.
if len(v.Pre) > 0 {
continue
}
parsedVersions = append(parsedVersions, v)
}
if len(parsedVersions) == 0 {
return "", fmt.Errorf("failed to find latest stable version from index: index does not have stable versions")
}
sort.Slice(parsedVersions, func(i, j int) bool {
return parsedVersions[i].GT(parsedVersions[j])
})
return "v" + parsedVersions[0].String(), nil
}
func getIndex(ctx context.Context, indexURL string) (*index, error) {
loc, err := url.Parse(indexURL)
if err != nil {
return nil, fmt.Errorf("unable to parse index URL: %w", err)
}
req, err := http.NewRequestWithContext(ctx, "GET", loc.String(), nil)
if err != nil {
return nil, fmt.Errorf("unable to construct request to get index: %w", err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, fmt.Errorf("unable to perform request to get index: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("unable to get index -- got status %q", resp.Status)
}
responseBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("unable to get index -- unable to read body %w", err)
}
var index index
if err := yaml.Unmarshal(responseBody, &index); err != nil {
return nil, fmt.Errorf("unable to unmarshal index: %w", err)
}
return &index, nil
}
func readBody(resp *http.Response, out io.Writer, archiveName string, expectedHash string) error {
// Stream in chunks to do the checksum
buf := make([]byte, 32*1024) // 32KiB, same as io.Copy
hasher := sha512.New()
for cont := true; cont; {
amt, err := resp.Body.Read(buf)
if err != nil && !errors.Is(err, io.EOF) {
return fmt.Errorf("unable read next chunk of %s: %w", archiveName, err)
}
if amt > 0 {
// checksum never returns errors according to docs
hasher.Write(buf[:amt])
if _, err := out.Write(buf[:amt]); err != nil {
return fmt.Errorf("unable write next chunk of %s: %w", archiveName, err)
}
}
cont = amt > 0 && !errors.Is(err, io.EOF)
}
actualHash := hex.EncodeToString(hasher.Sum(nil))
if actualHash != expectedHash {
return fmt.Errorf("checksum mismatch for %s: %s (computed) != %s (expected)", archiveName, actualHash, expectedHash)
}
return nil
}