Skip to content

Commit

Permalink
Add a preflight check that asserts docker connectivity
Browse files Browse the repository at this point in the history
Assert that the docker daemon is running, and that we're able to connect to it.
This is done by executing 'docker ps', and checking the exit code.

This updates kubernetes-sigs#39
This fixes kubernetes-sigs#554
  • Loading branch information
zegl committed Jun 2, 2019
1 parent ab59991 commit 35b2c26
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 0 deletions.
6 changes: 6 additions & 0 deletions cmd/kind/create/cluster/createcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"sigs.k8s.io/kind/pkg/cluster"
"sigs.k8s.io/kind/pkg/cluster/config/encoding"
"sigs.k8s.io/kind/pkg/cluster/create"
"sigs.k8s.io/kind/pkg/preflight/runtime"
"sigs.k8s.io/kind/pkg/util"
)

Expand Down Expand Up @@ -77,6 +78,11 @@ func runE(flags *flagpole, cmd *cobra.Command, args []string) error {
return errors.New("aborting due to invalid configuration")
}

// Run container runtime preflight checks, connectivity etc.
if err := runtime.Preflight(); err != nil {
return err
}

// Check if the cluster name already exists
known, err := cluster.IsKnown(flags.Name)
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions cmd/kind/delete/cluster/deletecluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/spf13/cobra"

"sigs.k8s.io/kind/pkg/cluster"
"sigs.k8s.io/kind/pkg/preflight/runtime"
)

type flagpole struct {
Expand All @@ -49,6 +50,11 @@ func NewCommand() *cobra.Command {
}

func runE(flags *flagpole, cmd *cobra.Command, args []string) error {
// Run container runtime preflight checks, connectivity etc.
if err := runtime.Preflight(); err != nil {
return err
}

// Check if the cluster name exists
known, err := cluster.IsKnown(flags.Name)
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions cmd/kind/export/logs/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"sigs.k8s.io/kind/pkg/cluster"
"sigs.k8s.io/kind/pkg/fs"
"sigs.k8s.io/kind/pkg/preflight/runtime"
)

type flagpole struct {
Expand All @@ -49,6 +50,11 @@ func NewCommand() *cobra.Command {
}

func runE(flags *flagpole, cmd *cobra.Command, args []string) error {
// Run container runtime preflight checks, connectivity etc.
if err := runtime.Preflight(); err != nil {
return err
}

// Check if the cluster name exists
known, err := cluster.IsKnown(flags.Name)
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions cmd/kind/get/clusters/clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/spf13/cobra"

"sigs.k8s.io/kind/pkg/cluster"
"sigs.k8s.io/kind/pkg/preflight/runtime"
)

// NewCommand returns a new cobra.Command for getting the list of clusters
Expand All @@ -41,6 +42,11 @@ func NewCommand() *cobra.Command {
}

func runE(cmd *cobra.Command, args []string) error {
// Run container runtime preflight checks, connectivity etc.
if err := runtime.Preflight(); err != nil {
return err
}

clusters, err := cluster.List()
if err != nil {
return err
Expand Down
6 changes: 6 additions & 0 deletions cmd/kind/get/nodes/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"sigs.k8s.io/kind/pkg/cluster"
clusternodes "sigs.k8s.io/kind/pkg/cluster/nodes"
"sigs.k8s.io/kind/pkg/preflight/runtime"
)

type flagpole struct {
Expand Down Expand Up @@ -53,6 +54,11 @@ func NewCommand() *cobra.Command {
}

func runE(flags *flagpole, cmd *cobra.Command, args []string) error {
// Run container runtime preflight checks, connectivity etc.
if err := runtime.Preflight(); err != nil {
return err
}

// List nodes by cluster context name
n, err := clusternodes.ListByCluster()
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions cmd/kind/load/docker-image/docker-image.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
clusternodes "sigs.k8s.io/kind/pkg/cluster/nodes"
"sigs.k8s.io/kind/pkg/container/docker"
"sigs.k8s.io/kind/pkg/fs"
"sigs.k8s.io/kind/pkg/preflight/runtime"
)

type flagpole struct {
Expand Down Expand Up @@ -68,6 +69,11 @@ func NewCommand() *cobra.Command {
}

func runE(flags *flagpole, cmd *cobra.Command, args []string) error {
// Run container runtime preflight checks, connectivity etc.
if err := runtime.Preflight(); err != nil {
return err
}

// List nodes by cluster context name
n, err := clusternodes.ListByCluster()
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions cmd/kind/load/image-archive/image-archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"sigs.k8s.io/kind/pkg/cluster"
clusternodes "sigs.k8s.io/kind/pkg/cluster/nodes"
"sigs.k8s.io/kind/pkg/preflight/runtime"
)

type flagpole struct {
Expand Down Expand Up @@ -65,6 +66,11 @@ func NewCommand() *cobra.Command {
}

func runE(flags *flagpole, cmd *cobra.Command, args []string) error {
// Run container runtime preflight checks, connectivity etc.
if err := runtime.Preflight(); err != nil {
return err
}

// List nodes by cluster context name
n, err := clusternodes.ListByCluster()
if err != nil {
Expand Down
31 changes: 31 additions & 0 deletions pkg/preflight/runtime/docker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package runtime

import (
"github.com/pkg/errors"
"os/exec"
)

// Preflight runs checks to make sure that the container runtime
// is working as expected
func Preflight() error {
checks := []func() error{
dockerIsRunning,
}

for _, check := range checks {
if err := check(); err != nil {
return errors.Wrap(err, "Preflight check failed")
}
}

return nil
}

// dockerIsRunning asserts that the docker daemon is running and is responsive
func dockerIsRunning() error {
err := exec.Command("docker", "ps").Run()
if err != nil {
return errors.Wrap(err, "Could not connect to a docker daemon")
}
return nil
}

0 comments on commit 35b2c26

Please sign in to comment.