Skip to content

Commit 0b7a1f5

Browse files
committed
Implement sideload
This also removes the last references to the old env package!
1 parent 36c1749 commit 0b7a1f5

File tree

6 files changed

+140
-76
lines changed

6 files changed

+140
-76
lines changed

pkg/envtest/setup/list/config.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ type config struct {
1616
// Option is a functional option for configuring the list workflow
1717
type Option func(*config)
1818

19-
// WithEnvOption provides an option for the env.Env used by the workflow
20-
func WithEnvOption(o env.Option) Option {
21-
return func(c *config) { c.envOpts = append(c.envOpts, o) }
19+
// WithEnvOptions provides options for the env.Env used by the workflow
20+
func WithEnvOptions(opts ...env.Option) Option {
21+
return func(c *config) { c.envOpts = append(c.envOpts, opts...) }
2222
}
2323

2424
// WithPlatform sets the target OS and architecture for the download.

pkg/envtest/setup/setup-envtest.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"sigs.k8s.io/controller-runtime/pkg/envtest/setup/cleanup"
88
"sigs.k8s.io/controller-runtime/pkg/envtest/setup/list"
9+
"sigs.k8s.io/controller-runtime/pkg/envtest/setup/sideload"
910
"sigs.k8s.io/controller-runtime/pkg/envtest/setup/use"
1011
"sigs.k8s.io/controller-runtime/pkg/envtest/setup/versions"
1112
)
@@ -41,6 +42,16 @@ func Cleanup(ctx context.Context, version string, options ...cleanup.Option) (cl
4142
return cleanup.Cleanup(ctx, spec, options...)
4243
}
4344

45+
// Sideload reads a binary package from an input stream, and stores it where Use can find it
46+
func Sideload(ctx context.Context, version string, options ...sideload.Option) error {
47+
spec, err := readSpec(version)
48+
if err != nil {
49+
return err
50+
}
51+
52+
return sideload.Sideload(ctx, spec, options...)
53+
}
54+
4455
func readSpec(version string) (versions.Spec, error) {
4556
switch version {
4657
case "", "latest":

pkg/envtest/setup/sideload/config.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package sideload
2+
3+
import (
4+
"io"
5+
"os"
6+
"runtime"
7+
8+
"sigs.k8s.io/controller-runtime/pkg/envtest/setup/env"
9+
"sigs.k8s.io/controller-runtime/pkg/envtest/setup/versions"
10+
)
11+
12+
type config struct {
13+
envOpts []env.Option
14+
input io.Reader
15+
platform versions.Platform
16+
}
17+
18+
// Option is a functional option for configuring the sideload process.
19+
type Option func(*config)
20+
21+
// WithEnvOptions configures the environment options for sideloading.
22+
func WithEnvOptions(options ...env.Option) Option {
23+
return func(cfg *config) {
24+
cfg.envOpts = append(cfg.envOpts, options...)
25+
}
26+
}
27+
28+
// WithInput configures the source to read the binary package from
29+
func WithInput(input io.Reader) Option {
30+
return func(cfg *config) {
31+
cfg.input = input
32+
}
33+
}
34+
35+
// WithPlatform sets the target OS and architecture for the sideload.
36+
func WithPlatform(os string, arch string) Option {
37+
return func(cfg *config) {
38+
cfg.platform = versions.Platform{
39+
OS: os,
40+
Arch: arch,
41+
}
42+
}
43+
}
44+
45+
func configure(options ...Option) config {
46+
cfg := config{
47+
input: os.Stdin,
48+
platform: versions.Platform{
49+
OS: runtime.GOOS,
50+
Arch: runtime.GOARCH,
51+
},
52+
}
53+
54+
for _, option := range options {
55+
option(&cfg)
56+
}
57+
58+
return cfg
59+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package sideload
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/go-logr/logr"
8+
"sigs.k8s.io/controller-runtime/pkg/envtest/setup/env"
9+
"sigs.k8s.io/controller-runtime/pkg/envtest/setup/store"
10+
"sigs.k8s.io/controller-runtime/pkg/envtest/setup/versions"
11+
)
12+
13+
// Sideload reads a binary package from an input stream and stores it in the environment store.
14+
func Sideload(ctx context.Context, version versions.Spec, options ...Option) error {
15+
cfg := configure(options...)
16+
17+
if !version.IsConcrete() || cfg.platform.IsWildcard() {
18+
return fmt.Errorf("must specify a concrete version and platform to sideload; got version %s, platform %s", version, cfg.platform)
19+
}
20+
21+
env, err := env.New(cfg.envOpts...)
22+
if err != nil {
23+
return err
24+
}
25+
26+
if err := env.Store.Initialize(ctx); err != nil {
27+
return err
28+
}
29+
30+
log, err := logr.FromContext(ctx)
31+
if err != nil {
32+
return err
33+
}
34+
log.Info("sideloading from input stream", "version", version, "platform", cfg.platform)
35+
if err := env.Store.Add(ctx, store.Item{Version: *version.AsConcrete(), Platform: cfg.platform}, cfg.input); err != nil {
36+
return fmt.Errorf("sideload item to disk: %w", err)
37+
}
38+
39+
return nil
40+
}

pkg/envtest/setup/use/config.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ func WithPlatform(os string, arch string) Option {
4747
return func(c *config) { c.platform = versions.Platform{OS: os, Arch: arch} }
4848
}
4949

50-
// WithEnvOption provides an option for the env.Env used by the workflow
51-
func WithEnvOption(o env.Option) Option {
52-
return func(c *config) { c.envOpts = append(c.envOpts, o) }
50+
// WithEnvOptions provides options for the env.Env used by the workflow
51+
func WithEnvOptions(opts ...env.Option) Option {
52+
return func(c *config) { c.envOpts = append(c.envOpts, opts...) }
5353
}
5454

5555
// VerifySum turns on md5 verification of the downloaded package

tools/setup-envtest/main.go

Lines changed: 24 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,18 @@ import (
1313

1414
"github.com/go-logr/logr"
1515
"github.com/go-logr/zapr"
16-
"github.com/spf13/afero"
1716
flag "github.com/spf13/pflag"
1817
"go.uber.org/zap"
1918

2019
"sigs.k8s.io/controller-runtime/pkg/envtest/setup"
2120
"sigs.k8s.io/controller-runtime/pkg/envtest/setup/cleanup"
22-
senv "sigs.k8s.io/controller-runtime/pkg/envtest/setup/env"
21+
"sigs.k8s.io/controller-runtime/pkg/envtest/setup/env"
2322
"sigs.k8s.io/controller-runtime/pkg/envtest/setup/list"
24-
sremote "sigs.k8s.io/controller-runtime/pkg/envtest/setup/remote"
23+
"sigs.k8s.io/controller-runtime/pkg/envtest/setup/remote"
24+
"sigs.k8s.io/controller-runtime/pkg/envtest/setup/sideload"
2525
"sigs.k8s.io/controller-runtime/pkg/envtest/setup/use"
2626
envp "sigs.k8s.io/controller-runtime/tools/setup-envtest/env"
27-
"sigs.k8s.io/controller-runtime/tools/setup-envtest/remote"
28-
"sigs.k8s.io/controller-runtime/tools/setup-envtest/store"
2927
"sigs.k8s.io/controller-runtime/tools/setup-envtest/versions"
30-
"sigs.k8s.io/controller-runtime/tools/setup-envtest/workflows"
3128
)
3229

3330
const (
@@ -76,61 +73,6 @@ func setupLogging() logr.Logger {
7673
return zapr.NewLogger(zapLog)
7774
}
7875

79-
// setupEnv initializes the environment from flags.
80-
func setupEnv(globalLog logr.Logger, version string) *envp.Env {
81-
log := globalLog.WithName("setup")
82-
if *binDir == "" {
83-
dataDir, err := store.DefaultStoreDir()
84-
if err != nil {
85-
envp.ExitCause(1, err, "unable to deterimine default binaries directory (use --bin-dir to manually override)")
86-
}
87-
88-
*binDir = dataDir
89-
}
90-
log.V(1).Info("using binaries directory", "dir", *binDir)
91-
92-
env := &envp.Env{
93-
Log: globalLog,
94-
Client: &remote.Client{
95-
Log: globalLog.WithName("storage-client"),
96-
Bucket: *remoteBucket,
97-
Server: *remoteServer,
98-
},
99-
VerifySum: *verify,
100-
ForceDownload: *force,
101-
NoDownload: *installedOnly,
102-
Platform: versions.PlatformItem{
103-
Platform: versions.Platform{
104-
OS: *targetOS,
105-
Arch: *targetArch,
106-
},
107-
},
108-
FS: afero.Afero{Fs: afero.NewOsFs()},
109-
Store: store.NewAt(*binDir),
110-
Out: os.Stdout,
111-
}
112-
113-
switch version {
114-
case "", "latest":
115-
env.Version = versions.LatestVersion
116-
case "latest-on-disk":
117-
// we sort by version, latest first, so this'll give us the latest on
118-
// disk (as per the contract from env.List & store.List)
119-
env.Version = versions.AnyVersion
120-
env.NoDownload = true
121-
default:
122-
var err error
123-
env.Version, err = versions.FromExpr(version)
124-
if err != nil {
125-
envp.ExitCause(1, err, "version be a valid version, or simply 'latest' or 'latest-on-disk'")
126-
}
127-
}
128-
129-
env.CheckCoherence()
130-
131-
return env
132-
}
133-
13476
func main() {
13577
// exit with appropriate error codes -- this should be the first defer so
13678
// that it's the last one executed.
@@ -263,7 +205,6 @@ Environment Variables:
263205
if flag.NArg() > 1 {
264206
version = flag.Arg(1)
265207
}
266-
env := setupEnv(globalLog, version)
267208

268209
// perform our main set of actions
269210
switch action := flag.Arg(0); action {
@@ -275,7 +216,7 @@ Environment Variables:
275216
use.ForceDownload(*force),
276217
use.NoDownload(*installedOnly),
277218
use.VerifySum(*verify),
278-
use.WithEnvOption(senv.WithClient(&sremote.Client{
219+
use.WithEnvOption(env.WithClient(&sremote.Client{
279220
Bucket: *remoteBucket,
280221
Server: *remoteServer,
281222
Log: globalLog.WithName("remote-client"),
@@ -296,7 +237,7 @@ Environment Variables:
296237
logr.NewContext(context.Background(), globalLog.WithName("list")),
297238
version,
298239
list.NoDownload(*installedOnly),
299-
list.WithEnvOption(senv.WithClient(&sremote.Client{
240+
list.WithEnvOption(env.WithClient(&sremote.Client{
300241
Bucket: *remoteBucket,
301242
Server: *remoteServer,
302243
Log: globalLog.WithName("remote-client"),
@@ -317,11 +258,12 @@ Environment Variables:
317258
logr.NewContext(context.Background(), globalLog.WithName("cleanup")),
318259
version,
319260
cleanup.WithEnvOptions(
320-
senv.WithClient(&sremote.Client{
261+
env.WithClient(&remote.Client{
321262
Bucket: *remoteBucket,
322263
Server: *remoteServer,
323264
Log: globalLog.WithName("remote-client"),
324-
})),
265+
}),
266+
),
325267
cleanup.WithPlatform(*targetOS, *targetArch),
326268
)
327269

@@ -335,10 +277,22 @@ Environment Variables:
335277
}
336278

337279
case "sideload":
338-
workflows.Sideload{
339-
Input: os.Stdin,
340-
PrintFormat: printFormat,
341-
}.Do(env)
280+
if err := setup.Sideload(
281+
logr.NewContext(context.Background(), globalLog.WithName("sideload")),
282+
version,
283+
sideload.WithInput(os.Stdin),
284+
sideload.WithPlatform(*targetOS, *targetArch),
285+
sideload.WithEnvOptions(
286+
env.WithClient(&remote.Client{
287+
Bucket: *remoteBucket,
288+
Server: *remoteServer,
289+
Log: globalLog.WithName("remote-client"),
290+
}),
291+
env.WithStoreAt(*binDir),
292+
),
293+
); err != nil {
294+
envp.Exit(2, err.Error())
295+
}
342296
default:
343297
flag.Usage()
344298
envp.Exit(2, "unknown action %q", action)

0 commit comments

Comments
 (0)