mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-20 01:01:22 +00:00
Add "Sandboxes" to the kubecontainer.Pod
This field will only be used by the kuberuntime package and should be ignored by other type of runtimes.
This commit is contained in:
parent
f488d118ad
commit
0fd2385e0b
@ -153,6 +153,11 @@ type Pod struct {
|
|||||||
// List of containers that belongs to this pod. It may contain only
|
// List of containers that belongs to this pod. It may contain only
|
||||||
// running containers, or mixed with dead ones (when GetPods(true)).
|
// running containers, or mixed with dead ones (when GetPods(true)).
|
||||||
Containers []*Container
|
Containers []*Container
|
||||||
|
// List of sandboxes associated with this pod. The sandboxes are converted
|
||||||
|
// to Container temporariliy to avoid substantial changes to other
|
||||||
|
// components. This is only populated by kuberuntime.
|
||||||
|
// TODO: use the runtimeApi.PodSandbox type directly.
|
||||||
|
Sandboxes []*Container
|
||||||
}
|
}
|
||||||
|
|
||||||
// PodPair contains both runtime#Pod and api#Pod
|
// PodPair contains both runtime#Pod and api#Pod
|
||||||
|
@ -64,6 +64,21 @@ func toKubeContainerState(state runtimeApi.ContainerState) kubecontainer.Contain
|
|||||||
return kubecontainer.ContainerStateUnknown
|
return kubecontainer.ContainerStateUnknown
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// sandboxToKubeContainerState converts runtimeApi.PodSandboxState to
|
||||||
|
// kubecontainer.ContainerState.
|
||||||
|
// This is only needed because we need to return sandboxes as if they were
|
||||||
|
// kubecontainer.Containers to avoid substantial changes to PLEG.
|
||||||
|
// TODO: Remove this once it becomes obsolete.
|
||||||
|
func sandboxToKubeContainerState(state runtimeApi.PodSandBoxState) kubecontainer.ContainerState {
|
||||||
|
switch state {
|
||||||
|
case runtimeApi.PodSandBoxState_READY:
|
||||||
|
return kubecontainer.ContainerStateRunning
|
||||||
|
case runtimeApi.PodSandBoxState_NOTREADY:
|
||||||
|
return kubecontainer.ContainerStateExited
|
||||||
|
}
|
||||||
|
return kubecontainer.ContainerStateUnknown
|
||||||
|
}
|
||||||
|
|
||||||
// toRuntimeProtocol converts api.Protocol to runtimeApi.Protocol.
|
// toRuntimeProtocol converts api.Protocol to runtimeApi.Protocol.
|
||||||
func toRuntimeProtocol(protocol api.Protocol) runtimeApi.Protocol {
|
func toRuntimeProtocol(protocol api.Protocol) runtimeApi.Protocol {
|
||||||
switch protocol {
|
switch protocol {
|
||||||
@ -94,6 +109,21 @@ func (m *kubeGenericRuntimeManager) toKubeContainer(c *runtimeApi.Container) (*k
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// sandboxToKubeContainer converts runtimeApi.PodSandbox to kubecontainer.Container.
|
||||||
|
// This is only needed because we need to return sandboxes as if they were
|
||||||
|
// kubecontainer.Containers to avoid substantial changes to PLEG.
|
||||||
|
// TODO: Remove this once it becomes obsolete.
|
||||||
|
func (m *kubeGenericRuntimeManager) sandboxToKubeContainer(s *runtimeApi.PodSandbox) (*kubecontainer.Container, error) {
|
||||||
|
if s == nil || s.Id == nil || s.State == nil {
|
||||||
|
return nil, fmt.Errorf("unable to convert a nil pointer to a runtime container")
|
||||||
|
}
|
||||||
|
|
||||||
|
return &kubecontainer.Container{
|
||||||
|
ID: kubecontainer.ContainerID{Type: m.runtimeName, ID: s.GetId()},
|
||||||
|
State: sandboxToKubeContainerState(s.GetState()),
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
// milliCPUToShares converts milliCPU to CPU shares
|
// milliCPUToShares converts milliCPU to CPU shares
|
||||||
func milliCPUToShares(milliCPU int64) int64 {
|
func milliCPUToShares(milliCPU int64) int64 {
|
||||||
if milliCPU == 0 {
|
if milliCPU == 0 {
|
||||||
|
@ -231,11 +231,20 @@ func (m *kubeGenericRuntimeManager) GetPods(all bool) ([]*kubecontainer.Pod, err
|
|||||||
}
|
}
|
||||||
for _, s := range sandboxes {
|
for _, s := range sandboxes {
|
||||||
podUID := kubetypes.UID(s.Metadata.GetUid())
|
podUID := kubetypes.UID(s.Metadata.GetUid())
|
||||||
pods[podUID] = &kubecontainer.Pod{
|
if _, ok := pods[podUID]; !ok {
|
||||||
ID: podUID,
|
pods[podUID] = &kubecontainer.Pod{
|
||||||
Name: s.Metadata.GetName(),
|
ID: podUID,
|
||||||
Namespace: s.Metadata.GetNamespace(),
|
Name: s.Metadata.GetName(),
|
||||||
|
Namespace: s.Metadata.GetNamespace(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
p := pods[podUID]
|
||||||
|
converted, err := m.sandboxToKubeContainer(s)
|
||||||
|
if err != nil {
|
||||||
|
glog.Warningf("Convert %q sandbox %v of pod %q failed: %v", m.runtimeName, s, podUID, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
p.Sandboxes = append(p.Sandboxes, converted)
|
||||||
}
|
}
|
||||||
|
|
||||||
containers, err := m.getKubeletContainers(all)
|
containers, err := m.getKubeletContainers(all)
|
||||||
|
@ -200,7 +200,7 @@ func TestGetPods(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Set fake sandbox and fake containers to fakeRuntime.
|
// Set fake sandbox and fake containers to fakeRuntime.
|
||||||
_, fakeContainers, err := makeAndSetFakePod(m, fakeRuntime, pod)
|
fakeSandbox, fakeContainers, err := makeAndSetFakePod(m, fakeRuntime, pod)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
// Convert the fakeContainers to kubecontainer.Container
|
// Convert the fakeContainers to kubecontainer.Container
|
||||||
@ -220,12 +220,25 @@ func TestGetPods(t *testing.T) {
|
|||||||
}
|
}
|
||||||
containers[i] = c
|
containers[i] = c
|
||||||
}
|
}
|
||||||
|
// Convert fakeSandbox to kubecontainer.Container
|
||||||
|
sandbox, err := m.sandboxToKubeContainer(&runtimeApi.PodSandbox{
|
||||||
|
Id: fakeSandbox.Id,
|
||||||
|
Metadata: fakeSandbox.Metadata,
|
||||||
|
State: fakeSandbox.State,
|
||||||
|
CreatedAt: fakeSandbox.CreatedAt,
|
||||||
|
Labels: fakeSandbox.Labels,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected error %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
expected := []*kubecontainer.Pod{
|
expected := []*kubecontainer.Pod{
|
||||||
{
|
{
|
||||||
ID: kubetypes.UID("12345678"),
|
ID: kubetypes.UID("12345678"),
|
||||||
Name: "foo",
|
Name: "foo",
|
||||||
Namespace: "new",
|
Namespace: "new",
|
||||||
Containers: []*kubecontainer.Container{containers[0], containers[1]},
|
Containers: []*kubecontainer.Container{containers[0], containers[1]},
|
||||||
|
Sandboxes: []*kubecontainer.Container{sandbox},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user