mirror of https://github.com/k3s-io/k3s
Add determinePodIPBySandboxID.
parent
6d07fc2f44
commit
5936e81b2e
|
@ -317,9 +317,15 @@ func (ds *dockerService) PodSandboxStatus(podSandboxID string) (*runtimeapi.PodS
|
||||||
if r.State.Running {
|
if r.State.Running {
|
||||||
state = runtimeapi.PodSandboxState_SANDBOX_READY
|
state = runtimeapi.PodSandboxState_SANDBOX_READY
|
||||||
}
|
}
|
||||||
IP, err := ds.getIP(r)
|
|
||||||
if err != nil {
|
var IP string
|
||||||
return nil, err
|
// 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}
|
network := &runtimeapi.PodSandboxNetworkStatus{Ip: IP}
|
||||||
hostNetwork := sharesHostNetwork(r)
|
hostNetwork := sharesHostNetwork(r)
|
||||||
|
|
|
@ -82,3 +82,7 @@ func (ds *dockerService) updateCreateConfig(
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ds *dockerService) determinePodIPBySandboxID(uid string) string {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
|
@ -42,3 +42,8 @@ func (ds *dockerService) updateCreateConfig(
|
||||||
glog.Warningf("updateCreateConfig is unsupported in this build")
|
glog.Warningf("updateCreateConfig is unsupported in this build")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ds *dockerService) determinePodIPBySandboxID(uid string) string {
|
||||||
|
glog.Warningf("determinePodIPBySandboxID is unsupported in this build")
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
|
@ -24,6 +24,7 @@ import (
|
||||||
"github.com/blang/semver"
|
"github.com/blang/semver"
|
||||||
dockertypes "github.com/docker/engine-api/types"
|
dockertypes "github.com/docker/engine-api/types"
|
||||||
dockercontainer "github.com/docker/engine-api/types/container"
|
dockercontainer "github.com/docker/engine-api/types/container"
|
||||||
|
dockerfilters "github.com/docker/engine-api/types/filters"
|
||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
"k8s.io/kubernetes/pkg/api/v1"
|
"k8s.io/kubernetes/pkg/api/v1"
|
||||||
runtimeapi "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1"
|
runtimeapi "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1"
|
||||||
|
@ -60,3 +61,41 @@ func (ds *dockerService) updateCreateConfig(
|
||||||
|
|
||||||
return nil
|
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 ""
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue