Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: fix container runtime list #283

Merged
merged 1 commit into from
May 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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