Add determinePodIPBySandboxID.

pull/6/head
Dong Liu 2017-06-02 07:31:44 -05:00
parent 6d07fc2f44
commit 5936e81b2e
4 changed files with 57 additions and 3 deletions

View File

@ -317,9 +317,15 @@ func (ds *dockerService) PodSandboxStatus(podSandboxID string) (*runtimeapi.PodS
if r.State.Running {
state = runtimeapi.PodSandboxState_SANDBOX_READY
}
IP, err := ds.getIP(r)
if err != nil {
return nil, err
var IP string
// TODO: Remove this when sandbox is available on windows
// This is a workaround for windows, where sandbox is not in use, and pod IP is determined through containers belonging to the Pod.
if IP = ds.determinePodIPBySandboxID(podSandboxID); IP == "" {
IP, err = ds.getIP(r)
if err != nil {
return nil, err
}
}
network := &runtimeapi.PodSandboxNetworkStatus{Ip: IP}
hostNetwork := sharesHostNetwork(r)

View File

@ -82,3 +82,7 @@ func (ds *dockerService) updateCreateConfig(
return nil
}
func (ds *dockerService) determinePodIPBySandboxID(uid string) string {
return ""
}

View File

@ -42,3 +42,8 @@ func (ds *dockerService) updateCreateConfig(
glog.Warningf("updateCreateConfig is unsupported in this build")
return nil
}
func (ds *dockerService) determinePodIPBySandboxID(uid string) string {
glog.Warningf("determinePodIPBySandboxID is unsupported in this build")
return ""
}

View File

@ -24,6 +24,7 @@ import (
"github.com/blang/semver"
dockertypes "github.com/docker/engine-api/types"
dockercontainer "github.com/docker/engine-api/types/container"
dockerfilters "github.com/docker/engine-api/types/filters"
"github.com/golang/glog"
"k8s.io/kubernetes/pkg/api/v1"
runtimeapi "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1"
@ -60,3 +61,41 @@ func (ds *dockerService) updateCreateConfig(
return nil
}
func (ds *dockerService) determinePodIPBySandboxID(sandboxID string) string {
opts := dockertypes.ContainerListOptions{
All: true,
Filter: dockerfilters.NewArgs(),
}
f := newDockerFilter(&opts.Filter)
f.AddLabel(containerTypeLabelKey, containerTypeLabelContainer)
f.AddLabel(sandboxIDLabelKey, sandboxID)
containers, err := ds.client.ListContainers(opts)
if err != nil {
return ""
}
for _, c := range containers {
r, err := ds.client.InspectContainer(c.ID)
if err != nil {
continue
}
if containerIP := getContainerIP(r); containerIP != "" {
return containerIP
}
}
return ""
}
func getContainerIP(container *dockertypes.ContainerJSON) string {
if container.NetworkSettings != nil {
for _, network := range container.NetworkSettings.Networks {
if network.IPAddress != "" {
return network.IPAddress
}
}
}
return ""
}