mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-06 02:34:03 +00:00
Remove more docker references in kubelet
This change also renames TrimRuntimePrefixFromImage to TrimRuntimePrefix to better reflect that the usage is not limited to images (e.g. ID).
This commit is contained in:
parent
11be6811b3
commit
2f7b951140
@ -34,13 +34,13 @@ type RunContainerOptionsGenerator interface {
|
|||||||
GenerateRunContainerOptions(pod *api.Pod, container *api.Container, netMode, ipcMode string) (*RunContainerOptions, error)
|
GenerateRunContainerOptions(pod *api.Pod, container *api.Container, netMode, ipcMode string) (*RunContainerOptions, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Trims runtime prefix from image name (e.g.: docker://busybox -> busybox).
|
// Trims runtime prefix from ID or image name (e.g.: docker://busybox -> busybox).
|
||||||
func TrimRuntimePrefixFromImage(img string) string {
|
func TrimRuntimePrefix(fullString string) string {
|
||||||
const prefixSeparator = "://"
|
const prefixSeparator = "://"
|
||||||
|
|
||||||
idx := strings.Index(img, prefixSeparator)
|
idx := strings.Index(fullString, prefixSeparator)
|
||||||
if idx < 0 {
|
if idx < 0 {
|
||||||
return img
|
return fullString
|
||||||
}
|
}
|
||||||
return img[idx+len(prefixSeparator):]
|
return fullString[idx+len(prefixSeparator):]
|
||||||
}
|
}
|
||||||
|
@ -1299,7 +1299,7 @@ func shouldContainerBeRestarted(container *api.Container, pod *api.Pod, podStatu
|
|||||||
|
|
||||||
// Set dead containers to unready state.
|
// Set dead containers to unready state.
|
||||||
for _, c := range resultStatus {
|
for _, c := range resultStatus {
|
||||||
readinessManager.RemoveReadiness(kubecontainer.TrimRuntimePrefixFromImage(c.ContainerID))
|
readinessManager.RemoveReadiness(kubecontainer.TrimRuntimePrefix(c.ContainerID))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check RestartPolicy for dead container.
|
// Check RestartPolicy for dead container.
|
||||||
|
@ -1435,7 +1435,7 @@ func (kl *Kubelet) validatePodPhase(podStatus *api.PodStatus) error {
|
|||||||
return fmt.Errorf("pod is not in 'Running', 'Succeeded' or 'Failed' state - State: %q", podStatus.Phase)
|
return fmt.Errorf("pod is not in 'Running', 'Succeeded' or 'Failed' state - State: %q", podStatus.Phase)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (kl *Kubelet) validateContainerStatus(podStatus *api.PodStatus, containerName string) (dockerID string, err error) {
|
func (kl *Kubelet) validateContainerStatus(podStatus *api.PodStatus, containerName string) (containerID string, err error) {
|
||||||
cStatus, found := api.GetContainerStatus(podStatus.ContainerStatuses, containerName)
|
cStatus, found := api.GetContainerStatus(podStatus.ContainerStatuses, containerName)
|
||||||
if !found {
|
if !found {
|
||||||
return "", fmt.Errorf("container %q not found in pod", containerName)
|
return "", fmt.Errorf("container %q not found in pod", containerName)
|
||||||
@ -1443,7 +1443,7 @@ func (kl *Kubelet) validateContainerStatus(podStatus *api.PodStatus, containerNa
|
|||||||
if cStatus.State.Waiting != nil {
|
if cStatus.State.Waiting != nil {
|
||||||
return "", fmt.Errorf("container %q is in waiting state.", containerName)
|
return "", fmt.Errorf("container %q is in waiting state.", containerName)
|
||||||
}
|
}
|
||||||
return kubecontainer.TrimRuntimePrefixFromImage(cStatus.ContainerID), nil
|
return kubecontainer.TrimRuntimePrefix(cStatus.ContainerID), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetKubeletContainerLogs returns logs from the container
|
// GetKubeletContainerLogs returns logs from the container
|
||||||
@ -1458,13 +1458,13 @@ func (kl *Kubelet) GetKubeletContainerLogs(podFullName, containerName, tail stri
|
|||||||
// No log is available if pod is not in a "known" phase (e.g. Unknown).
|
// No log is available if pod is not in a "known" phase (e.g. Unknown).
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
dockerContainerID, err := kl.validateContainerStatus(&podStatus, containerName)
|
containerID, err := kl.validateContainerStatus(&podStatus, containerName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// No log is available if the container status is missing or is in the
|
// No log is available if the container status is missing or is in the
|
||||||
// waiting state.
|
// waiting state.
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return kl.containerManager.GetContainerLogs(dockerContainerID, tail, follow, stdout, stderr)
|
return kl.containerManager.GetContainerLogs(containerID, tail, follow, stdout, stderr)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetHostname Returns the hostname as the kubelet sees it.
|
// GetHostname Returns the hostname as the kubelet sees it.
|
||||||
@ -1756,7 +1756,7 @@ func (kl *Kubelet) generatePodStatus(pod *api.Pod) (api.PodStatus, error) {
|
|||||||
for _, c := range spec.Containers {
|
for _, c := range spec.Containers {
|
||||||
for i, st := range podStatus.ContainerStatuses {
|
for i, st := range podStatus.ContainerStatuses {
|
||||||
if st.Name == c.Name {
|
if st.Name == c.Name {
|
||||||
ready := st.State.Running != nil && kl.readinessManager.GetReadiness(strings.TrimPrefix(st.ContainerID, "docker://"))
|
ready := st.State.Running != nil && kl.readinessManager.GetReadiness(kubecontainer.TrimRuntimePrefix(st.ContainerID))
|
||||||
podStatus.ContainerStatuses[i].Ready = ready
|
podStatus.ContainerStatuses[i].Ready = ready
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@ -1855,23 +1855,19 @@ func (kl *Kubelet) StreamingConnectionIdleTimeout() time.Duration {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetContainerInfo returns stats (from Cadvisor) for a container.
|
// GetContainerInfo returns stats (from Cadvisor) for a container.
|
||||||
func (kl *Kubelet) GetContainerInfo(podFullName string, uid types.UID, containerName string, req *cadvisorApi.ContainerInfoRequest) (*cadvisorApi.ContainerInfo, error) {
|
func (kl *Kubelet) GetContainerInfo(podFullName string, podUID types.UID, containerName string, req *cadvisorApi.ContainerInfoRequest) (*cadvisorApi.ContainerInfo, error) {
|
||||||
|
|
||||||
uid = kl.podManager.TranslatePodUID(uid)
|
podUID = kl.podManager.TranslatePodUID(podUID)
|
||||||
|
|
||||||
dockerContainers, err := dockertools.GetKubeletDockerContainers(kl.dockerClient, false)
|
container, err := kl.findContainer(podFullName, podUID, containerName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if len(dockerContainers) == 0 {
|
if container == nil {
|
||||||
return nil, ErrNoKubeletContainers
|
|
||||||
}
|
|
||||||
dockerContainer, found, _ := dockerContainers.FindPodContainer(podFullName, uid, containerName)
|
|
||||||
if !found {
|
|
||||||
return nil, ErrContainerNotFound
|
return nil, ErrContainerNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
ci, err := kl.cadvisor.DockerContainer(dockerContainer.ID, req)
|
ci, err := kl.cadvisor.DockerContainer(string(container.ID), req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -1416,16 +1416,6 @@ func TestMakeVolumesAndBinds(t *testing.T) {
|
|||||||
verifyStringArrayEquals(t, binds, expectedBinds)
|
verifyStringArrayEquals(t, binds, expectedBinds)
|
||||||
}
|
}
|
||||||
|
|
||||||
type errorTestingDockerClient struct {
|
|
||||||
dockertools.FakeDockerClient
|
|
||||||
listContainersError error
|
|
||||||
containerList []docker.APIContainers
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *errorTestingDockerClient) ListContainers(options docker.ListContainersOptions) ([]docker.APIContainers, error) {
|
|
||||||
return f.containerList, f.listContainersError
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGetContainerInfo(t *testing.T) {
|
func TestGetContainerInfo(t *testing.T) {
|
||||||
containerID := "ab2cdf"
|
containerID := "ab2cdf"
|
||||||
containerPath := fmt.Sprintf("/docker/%v", containerID)
|
containerPath := fmt.Sprintf("/docker/%v", containerID)
|
||||||
@ -1573,15 +1563,16 @@ func TestGetContainerInfoWhenDockerToolsFailed(t *testing.T) {
|
|||||||
testKubelet := newTestKubelet(t)
|
testKubelet := newTestKubelet(t)
|
||||||
kubelet := testKubelet.kubelet
|
kubelet := testKubelet.kubelet
|
||||||
mockCadvisor := testKubelet.fakeCadvisor
|
mockCadvisor := testKubelet.fakeCadvisor
|
||||||
|
fakeDocker := testKubelet.fakeDocker
|
||||||
expectedErr := fmt.Errorf("List containers error")
|
expectedErr := fmt.Errorf("List containers error")
|
||||||
kubelet.dockerClient = &errorTestingDockerClient{listContainersError: expectedErr}
|
fakeDocker.Errors["list"] = expectedErr
|
||||||
|
|
||||||
stats, err := kubelet.GetContainerInfo("qux", "", "foo", nil)
|
stats, err := kubelet.GetContainerInfo("qux", "", "foo", nil)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("Expected error from dockertools, got none")
|
t.Errorf("expected error from dockertools, got none")
|
||||||
}
|
}
|
||||||
if err.Error() != expectedErr.Error() {
|
if err.Error() != expectedErr.Error() {
|
||||||
t.Errorf("Expected error %v got %v", expectedErr.Error(), err.Error())
|
t.Errorf("expected error %v got %v", expectedErr.Error(), err.Error())
|
||||||
}
|
}
|
||||||
if stats != nil {
|
if stats != nil {
|
||||||
t.Errorf("non-nil stats when dockertools failed")
|
t.Errorf("non-nil stats when dockertools failed")
|
||||||
@ -1594,13 +1585,12 @@ func TestGetContainerInfoWithNoContainers(t *testing.T) {
|
|||||||
kubelet := testKubelet.kubelet
|
kubelet := testKubelet.kubelet
|
||||||
mockCadvisor := testKubelet.fakeCadvisor
|
mockCadvisor := testKubelet.fakeCadvisor
|
||||||
|
|
||||||
kubelet.dockerClient = &errorTestingDockerClient{listContainersError: nil}
|
|
||||||
stats, err := kubelet.GetContainerInfo("qux_ns", "", "foo", nil)
|
stats, err := kubelet.GetContainerInfo("qux_ns", "", "foo", nil)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("Expected error from cadvisor client, got none")
|
t.Errorf("expected error from cadvisor client, got none")
|
||||||
}
|
}
|
||||||
if err != ErrNoKubeletContainers {
|
if err != ErrContainerNotFound {
|
||||||
t.Errorf("Expected error %v, got %v", ErrNoKubeletContainers.Error(), err.Error())
|
t.Errorf("expected error %v, got %v", ErrContainerNotFound.Error(), err.Error())
|
||||||
}
|
}
|
||||||
if stats != nil {
|
if stats != nil {
|
||||||
t.Errorf("non-nil stats when dockertools returned no containers")
|
t.Errorf("non-nil stats when dockertools returned no containers")
|
||||||
@ -1612,15 +1602,14 @@ func TestGetContainerInfoWithNoMatchingContainers(t *testing.T) {
|
|||||||
testKubelet := newTestKubelet(t)
|
testKubelet := newTestKubelet(t)
|
||||||
kubelet := testKubelet.kubelet
|
kubelet := testKubelet.kubelet
|
||||||
mockCadvisor := testKubelet.fakeCadvisor
|
mockCadvisor := testKubelet.fakeCadvisor
|
||||||
|
fakeDocker := testKubelet.fakeDocker
|
||||||
containerList := []docker.APIContainers{
|
fakeDocker.ContainerList = []docker.APIContainers{
|
||||||
{
|
{
|
||||||
ID: "fakeId",
|
ID: "fakeId",
|
||||||
Names: []string{"/k8s_bar_qux_ns_1234_42"},
|
Names: []string{"/k8s_bar_qux_ns_1234_42"},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
kubelet.dockerClient = &errorTestingDockerClient{listContainersError: nil, containerList: containerList}
|
|
||||||
stats, err := kubelet.GetContainerInfo("qux_ns", "", "foo", nil)
|
stats, err := kubelet.GetContainerInfo("qux_ns", "", "foo", nil)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("Expected error from cadvisor client, got none")
|
t.Errorf("Expected error from cadvisor client, got none")
|
||||||
|
Loading…
Reference in New Issue
Block a user