From 4499fe761026306544c3c4f6253f04ee2785b5c7 Mon Sep 17 00:00:00 2001 From: Abiola Ibrahim Date: Sun, 15 May 2022 08:15:35 +0100 Subject: [PATCH] chore: fix container runtime list (#283) --- environment/container.go | 20 +++++++++++-------- .../container/containerd/containerd.go | 2 +- environment/container/docker/docker.go | 2 +- .../container/kubernetes/kubernetes.go | 2 +- environment/container/ubuntu/ubuntu.go | 2 +- 5 files changed, 16 insertions(+), 12 deletions(-) diff --git a/environment/container.go b/environment/container.go index b8f44f337..0ed92ad18 100644 --- a/environment/container.go +++ b/environment/container.go @@ -33,28 +33,32 @@ func NewContainer(runtime string, host HostActions, guest GuestActions) (Contain return nil, fmt.Errorf("unsupported container runtime '%s'", runtime) } - return containerRuntimes[runtime](host, guest), nil + return containerRuntimes[runtime].Func(host, guest), nil } // NewContainerFunc is implemented by container runtime implementations to create a new instance. type NewContainerFunc func(host HostActions, guest GuestActions) Container -var containerRuntimes = map[string]NewContainerFunc{} +var containerRuntimes = map[string]containerRuntimeFunc{} + +type containerRuntimeFunc struct { + Func NewContainerFunc + Hidden bool +} // RegisterContainer registers a new container runtime. -func RegisterContainer(name string, f NewContainerFunc) { +// If hidden is true, the container is not displayed as an available runtime. +func RegisterContainer(name string, f NewContainerFunc, hidden bool) { if _, ok := containerRuntimes[name]; ok { log.Fatalf("container runtime '%s' already registered", name) } - containerRuntimes[name] = f + containerRuntimes[name] = containerRuntimeFunc{Func: f, Hidden: hidden} } // ContainerRuntimes return the names of available container runtimes. func ContainerRuntimes() (names []string) { - for name := range containerRuntimes { - // exclude kubernetes from the runtime list - // TODO find a cleaner way to not hardcode kubernetes - if name == "kubernetes" { + for name, cont := range containerRuntimes { + if cont.Hidden { continue } names = append(names, name) diff --git a/environment/container/containerd/containerd.go b/environment/container/containerd/containerd.go index 5737e8a2d..b077c69cd 100644 --- a/environment/container/containerd/containerd.go +++ b/environment/container/containerd/containerd.go @@ -30,7 +30,7 @@ func newRuntime(host environment.HostActions, guest environment.GuestActions) en } func init() { - environment.RegisterContainer(Name, newRuntime) + environment.RegisterContainer(Name, newRuntime, false) } var _ environment.Container = (*containerdRuntime)(nil) diff --git a/environment/container/docker/docker.go b/environment/container/docker/docker.go index af3c3bd56..2d432cd98 100644 --- a/environment/container/docker/docker.go +++ b/environment/container/docker/docker.go @@ -15,7 +15,7 @@ const Name = "docker" var _ environment.Container = (*dockerRuntime)(nil) func init() { - environment.RegisterContainer(Name, newRuntime) + environment.RegisterContainer(Name, newRuntime, false) } type dockerRuntime struct { diff --git a/environment/container/kubernetes/kubernetes.go b/environment/container/kubernetes/kubernetes.go index 1f3a34616..67242e043 100644 --- a/environment/container/kubernetes/kubernetes.go +++ b/environment/container/kubernetes/kubernetes.go @@ -32,7 +32,7 @@ func newRuntime(host environment.HostActions, guest environment.GuestActions) en } func init() { - environment.RegisterContainer(Name, newRuntime) + environment.RegisterContainer(Name, newRuntime, true) } var _ environment.Container = (*kubernetesRuntime)(nil) diff --git a/environment/container/ubuntu/ubuntu.go b/environment/container/ubuntu/ubuntu.go index 34f72879e..b4d5bc051 100644 --- a/environment/container/ubuntu/ubuntu.go +++ b/environment/container/ubuntu/ubuntu.go @@ -30,7 +30,7 @@ func nerdctl(args ...string) []string { } func init() { - environment.RegisterContainer(Name, newRuntime) + environment.RegisterContainer(Name, newRuntime, true) } type ubuntuRuntime struct {