Skip to content

Commit

Permalink
chore: fix container runtime list (#283)
Browse files Browse the repository at this point in the history
  • Loading branch information
abiosoft authored May 15, 2022
1 parent e514fdb commit 4499fe7
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 12 deletions.
20 changes: 12 additions & 8 deletions environment/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion environment/container/containerd/containerd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion environment/container/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion environment/container/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion environment/container/ubuntu/ubuntu.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func nerdctl(args ...string) []string {
}

func init() {
environment.RegisterContainer(Name, newRuntime)
environment.RegisterContainer(Name, newRuntime, true)
}

type ubuntuRuntime struct {
Expand Down

0 comments on commit 4499fe7

Please sign in to comment.