-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
7 changed files
with
193 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package list | ||
|
||
import ( | ||
"cmp" | ||
"os" | ||
"runtime" | ||
|
||
"sigs.k8s.io/controller-runtime/pkg/envtest/setup/env" | ||
"sigs.k8s.io/controller-runtime/pkg/envtest/setup/versions" | ||
) | ||
|
||
type config struct { | ||
platform versions.Platform | ||
assetPath string | ||
noDownload bool | ||
envOpts []env.Option | ||
} | ||
|
||
// Option is a functional option for configuring the list workflow | ||
type Option func(*config) | ||
|
||
// WithEnvOption provides an option for the env.Env used by the workflow | ||
func WithEnvOption(o env.Option) Option { | ||
return func(c *config) { c.envOpts = append(c.envOpts, o) } | ||
} | ||
|
||
// WithAssetsAt sets the path to the assets directory. | ||
func WithAssetsAt(dir string) Option { | ||
return func(c *config) { c.assetPath = dir } | ||
} | ||
|
||
// WithAssetsFromEnv sets the path to the assets directory from the environment. | ||
func WithAssetsFromEnv(useEnv bool) Option { | ||
return func(c *config) { | ||
if useEnv { | ||
c.assetPath = cmp.Or(os.Getenv(env.KubebuilderAssetsEnvVar), c.assetPath) | ||
} | ||
} | ||
} | ||
|
||
// WithPlatform sets the target OS and architecture for the download. | ||
func WithPlatform(os string, arch string) Option { | ||
return func(c *config) { c.platform = versions.Platform{OS: os, Arch: arch} } | ||
} | ||
|
||
// NoDownload ensures only local versions are considered | ||
func NoDownload(noDownload bool) Option { return func(c *config) { c.noDownload = noDownload } } | ||
|
||
func configure(options ...Option) *config { | ||
cfg := &config{} | ||
|
||
for _, opt := range options { | ||
opt(cfg) | ||
} | ||
|
||
if cfg.platform == (versions.Platform{}) { | ||
cfg.platform = versions.Platform{ | ||
OS: runtime.GOOS, | ||
Arch: runtime.GOARCH, | ||
} | ||
} | ||
return cfg | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package list | ||
|
||
import ( | ||
"cmp" | ||
"context" | ||
"fmt" | ||
"slices" | ||
|
||
"sigs.k8s.io/controller-runtime/pkg/envtest/setup/env" | ||
"sigs.k8s.io/controller-runtime/pkg/envtest/setup/store" | ||
"sigs.k8s.io/controller-runtime/pkg/envtest/setup/versions" | ||
) | ||
|
||
// Status indicates whether a version is available locally or remotely | ||
type Status string | ||
|
||
const ( | ||
// Installed indicates that this version is installed on the local system | ||
Installed Status = "installed" | ||
// Available indicates that this version is available to download | ||
Available Status = "available" | ||
) | ||
|
||
// Result encapsulates a single item in the list of versions | ||
type Result struct { | ||
Version versions.Concrete | ||
Platform versions.Platform | ||
Status Status | ||
} | ||
|
||
// List lists available versions, local and remote | ||
func List(ctx context.Context, version versions.Spec, options ...Option) ([]Result, error) { | ||
cfg := configure(options...) | ||
env, err := env.New(cfg.envOpts...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := env.Store.Initialize(ctx); err != nil { | ||
return nil, err | ||
} | ||
|
||
vs, err := env.Store.List(ctx, store.Filter{Version: version, Platform: cfg.platform}) | ||
if err != nil { | ||
return nil, fmt.Errorf("list installed versions: %w", err) | ||
} | ||
|
||
results := make([]Result, 0, len(vs)) | ||
for _, v := range vs { | ||
results = append(results, Result{Version: v.Version, Platform: v.Platform, Status: Installed}) | ||
} | ||
|
||
if cfg.noDownload { | ||
return results, nil | ||
} | ||
|
||
remoteVersions, err := env.Client.ListVersions(ctx) | ||
if err != nil { | ||
return nil, fmt.Errorf("list available versions: %w", err) | ||
} | ||
|
||
for _, set := range remoteVersions { | ||
if !version.Matches(set.Version) { | ||
continue | ||
} | ||
slices.SortFunc(set.Platforms, func(a, b versions.PlatformItem) int { | ||
return cmp.Or(cmp.Compare(a.OS, b.OS), cmp.Compare(a.Arch, b.Arch)) | ||
}) | ||
for _, plat := range set.Platforms { | ||
if cfg.platform.Matches(plat.Platform) { | ||
results = append(results, Result{ | ||
Version: set.Version, | ||
Platform: plat.Platform, | ||
Status: Available, | ||
}) | ||
} | ||
} | ||
} | ||
|
||
return results, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters