Skip to content

Commit

Permalink
Never use docker inspect, always provide the type
Browse files Browse the repository at this point in the history
Should avoid errors like: map has no entry for key "State"

When inspecting a volume, rather than a container or image
  • Loading branch information
afbjorklund committed May 21, 2020
1 parent 25045cf commit 946c9d0
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 27 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ out/docker-machine-driver-kvm2-aarch64: out/docker-machine-driver-kvm2-arm64

out/docker-machine-driver-kvm2-%:
ifeq ($(MINIKUBE_BUILD_IN_DOCKER),y)
docker inspect -f '{{.Id}} {{.RepoTags}}' $(KVM_BUILD_IMAGE) || $(MAKE) kvm-image
docker image inspect -f '{{.Id}} {{.RepoTags}}' $(KVM_BUILD_IMAGE) || $(MAKE) kvm-image
$(call DOCKER,$(KVM_BUILD_IMAGE),/usr/bin/make $@ COMMIT=$(COMMIT))
# make extra sure that we are linking with the older version of libvirt (1.3.1)
test "`strings $@ | grep '^LIBVIRT_[0-9]' | sort | tail -n 1`" = "LIBVIRT_1.2.9"
Expand Down Expand Up @@ -619,7 +619,7 @@ kvm-image: installers/linux/kvm/Dockerfile ## Convenient alias to build the doc
@echo "$(@) successfully built"

kvm_in_docker:
docker inspect -f '{{.Id}} {{.RepoTags}}' $(KVM_BUILD_IMAGE) || $(MAKE) kvm-image
docker image inspect -f '{{.Id}} {{.RepoTags}}' $(KVM_BUILD_IMAGE) || $(MAKE) kvm-image
rm -f out/docker-machine-driver-kvm2
$(call DOCKER,$(KVM_BUILD_IMAGE),/usr/bin/make out/docker-machine-driver-kvm2 COMMIT=$(COMMIT))

Expand Down
17 changes: 8 additions & 9 deletions pkg/drivers/kic/oci/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func dockerGatewayIP() (net.IP, error) {
}

bridgeID := strings.TrimSpace(rr.Stdout.String())
rr, err = runCmd(exec.Command(Docker, "inspect",
rr, err = runCmd(exec.Command(Docker, "network", "inspect",
"--format", "{{(index .IPAM.Config 0).Gateway}}", bridgeID))
if err != nil {
return nil, errors.Wrapf(err, "inspect IP bridge network %q.", bridgeID)
Expand All @@ -80,7 +80,7 @@ func dockerGatewayIP() (net.IP, error) {

// containerGatewayIP gets the default gateway ip for the container
func containerGatewayIP(ociBin, containerName string) (net.IP, error) {
rr, err := runCmd(exec.Command(ociBin, "inspect", "--format", "{{.NetworkSettings.Gateway}}", containerName))
rr, err := runCmd(exec.Command(ociBin, "container", "inspect", "--format", "{{.NetworkSettings.Gateway}}", containerName))
if err != nil {
return nil, errors.Wrapf(err, "inspect gateway")
}
Expand All @@ -98,13 +98,12 @@ func ForwardedPort(ociBin string, ociID string, contPort int) (int, error) {
var err error

if ociBin == Podman {
//podman inspect -f "{{range .NetworkSettings.Ports}}{{if eq .ContainerPort "80"}}{{.HostPort}}{{end}}{{end}}"
rr, err = runCmd(exec.Command(ociBin, "inspect", "-f", fmt.Sprintf("{{range .NetworkSettings.Ports}}{{if eq .ContainerPort %s}}{{.HostPort}}{{end}}{{end}}", fmt.Sprint(contPort)), ociID))
rr, err = runCmd(exec.Command(ociBin, "container", "inspect", "-f", fmt.Sprintf("{{range .NetworkSettings.Ports}}{{if eq .ContainerPort %s}}{{.HostPort}}{{end}}{{end}}", fmt.Sprint(contPort)), ociID))
if err != nil {
return 0, errors.Wrapf(err, "get port %d for %q", contPort, ociID)
}
} else {
rr, err = runCmd(exec.Command(ociBin, "inspect", "-f", fmt.Sprintf("'{{(index (index .NetworkSettings.Ports \"%d/tcp\") 0).HostPort}}'", contPort), ociID))
rr, err = runCmd(exec.Command(ociBin, "container", "inspect", "-f", fmt.Sprintf("'{{(index (index .NetworkSettings.Ports \"%d/tcp\") 0).HostPort}}'", contPort), ociID))
if err != nil {
return 0, errors.Wrapf(err, "get port %d for %q", contPort, ociID)
}
Expand All @@ -124,14 +123,14 @@ func ForwardedPort(ociBin string, ociID string, contPort int) (int, error) {
// ContainerIPs returns ipv4,ipv6, error of a container by their name
func ContainerIPs(ociBin string, name string) (string, string, error) {
if ociBin == Podman {
return podmanConttainerIP(name)
return podmanContainerIP(name)
}
return dockerContainerIP(name)
}

// podmanConttainerIP returns ipv4, ipv6 of container or error
func podmanConttainerIP(name string) (string, string, error) {
rr, err := runCmd(exec.Command(Podman, "inspect",
// podmanContainerIP returns ipv4, ipv6 of container or error
func podmanContainerIP(name string) (string, string, error) {
rr, err := runCmd(exec.Command(Podman, "container", "inspect",
"-f", "{{.NetworkSettings.IPAddress}}",
name))
if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions pkg/drivers/kic/oci/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ func StartContainer(ociBin string, container string) error {

// ContainerID returns id of a container name
func ContainerID(ociBin string, nameOrID string) (string, error) {
rr, err := runCmd(exec.Command(ociBin, "inspect", "-f", "{{.Id}}", nameOrID))
rr, err := runCmd(exec.Command(ociBin, "container", "inspect", "-f", "{{.Id}}", nameOrID))
if err != nil { // don't return error if not found, only return empty string
if strings.Contains(rr.Stdout.String(), "Error: No such object:") || strings.Contains(rr.Stdout.String(), "unable to find") {
err = nil
Expand Down Expand Up @@ -307,7 +307,7 @@ func ContainerExists(ociBin string, name string, warnSlow ...bool) (bool, error)
// IsCreatedByMinikube returns true if the container was created by minikube
// with default assumption that it is not created by minikube when we don't know for sure
func IsCreatedByMinikube(ociBin string, nameOrID string) bool {
rr, err := runCmd(exec.Command(ociBin, "inspect", nameOrID, "--format", "{{.Config.Labels}}"))
rr, err := runCmd(exec.Command(ociBin, "container", "inspect", nameOrID, "--format", "{{.Config.Labels}}"))
if err != nil {
return false
}
Expand All @@ -326,7 +326,7 @@ func ListOwnedContainers(ociBin string) ([]string, error) {

// inspect return low-level information on containers
func inspect(ociBin string, containerNameOrID, format string) ([]string, error) {
cmd := exec.Command(ociBin, "inspect",
cmd := exec.Command(ociBin, "container", "inspect",
"-f", format,
containerNameOrID) // ... against the "node" container
var buff bytes.Buffer
Expand Down Expand Up @@ -506,7 +506,7 @@ func PointToHostPodman() error {

// ContainerRunning returns running state of a container
func ContainerRunning(ociBin string, name string, warnSlow ...bool) (bool, error) {
rr, err := runCmd(exec.Command(ociBin, "inspect", name, "--format={{.State.Running}}"), warnSlow...)
rr, err := runCmd(exec.Command(ociBin, "container", "inspect", name, "--format={{.State.Running}}"), warnSlow...)
if err != nil {
return false, err
}
Expand All @@ -515,7 +515,7 @@ func ContainerRunning(ociBin string, name string, warnSlow ...bool) (bool, error

// ContainerStatus returns status of a container running,exited,...
func ContainerStatus(ociBin string, name string, warnSlow ...bool) (state.State, error) {
cmd := exec.Command(ociBin, "inspect", name, "--format={{.State.Status}}")
cmd := exec.Command(ociBin, "container", "inspect", name, "--format={{.State.Status}}")
rr, err := runCmd(cmd, warnSlow...)
o := strings.TrimSpace(rr.Stdout.String())
switch o {
Expand Down
2 changes: 1 addition & 1 deletion pkg/minikube/cruntime/crio.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (r *CRIO) Disable() error {
// ImageExists checks if an image exists
func (r *CRIO) ImageExists(name string, sha string) bool {
// expected output looks like [NAME@sha256:SHA]
c := exec.Command("sudo", "podman", "inspect", "--format", "{{.Id}}", name)
c := exec.Command("sudo", "podman", "image", "inspect", "--format", "{{.Id}}", name)
rr, err := r.Runner.RunCmd(c)
if err != nil {
return false
Expand Down
27 changes: 18 additions & 9 deletions pkg/minikube/cruntime/cruntime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,16 @@ func (f *FakeRunner) dockerRm(args []string) (string, error) {
return "", nil
}

func (f *FakeRunner) dockerInspect(args []string) (string, error) {
if args[1] == "--format" && args[2] == "{{.Id}}" {
if args[3] == "missing" {
return "", &exec.ExitError{Stderr: []byte("Error: No such object: missing")}
}
return "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", nil
}
return "", nil
}

// docker is a fake implementation of docker
func (f *FakeRunner) docker(args []string, _ bool) (string, error) {
switch cmd := args[0]; cmd {
Expand All @@ -285,15 +295,14 @@ func (f *FakeRunner) docker(args []string, _ bool) (string, error) {
return "18.06.2-ce", nil
}

case "inspect":

if args[1] == "--format" && args[2] == "{{.Id}}" {
if args[3] == "missing" {
return "", &exec.ExitError{Stderr: []byte("Error: No such object: missing")}
}
return "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", nil
case "image":
if args[1] == "inspect" {
return f.dockerInspect(args[1:])
}

case "inspect":
return f.dockerInspect(args)

case "info":

if args[1] == "--format" && args[2] == "{{.CgroupDriver}}" {
Expand All @@ -309,9 +318,9 @@ func (f *FakeRunner) podman(args []string, _ bool) (string, error) {
case "--version":
return "podman version 1.6.4", nil

case "inspect":
case "image":

if args[1] == "--format" && args[2] == "{{.Id}}" {
if args[1] == "inspect" && args[2] == "--format" && args[3] == "{{.Id}}" {
if args[3] == "missing" {
return "", &exec.ExitError{Stderr: []byte("Error: error getting image \"missing\": unable to find a name and tag match for missing in repotags: no such image")}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/minikube/cruntime/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func (r *Docker) Disable() error {
// ImageExists checks if an image exists
func (r *Docker) ImageExists(name string, sha string) bool {
// expected output looks like [SHA_ALGO:SHA]
c := exec.Command("docker", "inspect", "--format", "{{.Id}}", name)
c := exec.Command("docker", "image", "inspect", "--format", "{{.Id}}", name)
rr, err := r.Runner.RunCmd(c)
if err != nil {
return false
Expand Down

0 comments on commit 946c9d0

Please sign in to comment.