Move more things into docke label, and add label test

This commit is contained in:
Lantao Liu 2015-11-13 11:37:33 -08:00
parent abbed4f7e8
commit f08097515f
4 changed files with 129 additions and 36 deletions

View File

@ -21,6 +21,8 @@ import (
"github.com/golang/glog" "github.com/golang/glog"
"k8s.io/kubernetes/pkg/api" "k8s.io/kubernetes/pkg/api"
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
"k8s.io/kubernetes/pkg/types"
) )
// This file contains all docker label related constants and functions, including: // This file contains all docker label related constants and functions, including:
@ -30,52 +32,79 @@ import (
const ( const (
kubernetesPodNameLabel = "io.kubernetes.pod.name" kubernetesPodNameLabel = "io.kubernetes.pod.name"
kubernetesPodNamespaceLabel = "io.kubernetes.pod.namespace" kubernetesPodNamespaceLabel = "io.kubernetes.pod.namespace"
kubernetesPodUID = "io.kubernetes.pod.uid" kubernetesPodUIDLabel = "io.kubernetes.pod.uid"
kubernetesContainerNameLabel = "io.kubernetes.container.name"
kubernetesContainerHashLabel = "io.kubernetes.container.hash"
kubernetesContainerRestartCountLabel = "io.kubernetes.container.restartCount" kubernetesContainerRestartCountLabel = "io.kubernetes.container.restartCount"
kubernetesContainerTerminationMessagePath = "io.kubernetes.container.terminationMessagePath" kubernetesContainerTerminationMessagePathLabel = "io.kubernetes.container.terminationMessagePath"
kubernetesPodLabel = "io.kubernetes.pod.data" kubernetesPodLabel = "io.kubernetes.pod.data"
kubernetesTerminationGracePeriodLabel = "io.kubernetes.pod.terminationGracePeriod" kubernetesTerminationGracePeriodLabel = "io.kubernetes.pod.terminationGracePeriod"
kubernetesContainerLabel = "io.kubernetes.container.name" kubernetesContainerLabel = "io.kubernetes.container.name"
) )
// Container information which has been labelled on each docker container
type labelledContainerInfo struct {
PodName string
PodNamespace string
PodUID types.UID
Name string
Hash string
RestartCount int
TerminationMessagePath string
}
func newLabels(container *api.Container, pod *api.Pod, restartCount int) map[string]string { func newLabels(container *api.Container, pod *api.Pod, restartCount int) map[string]string {
// TODO (random-liu) Move more label initialization here // TODO (random-liu) Move more label initialization here
labels := map[string]string{} labels := map[string]string{}
labels[kubernetesPodNameLabel] = pod.Name labels[kubernetesPodNameLabel] = pod.Name
labels[kubernetesPodNamespaceLabel] = pod.Namespace labels[kubernetesPodNamespaceLabel] = pod.Namespace
labels[kubernetesPodUID] = string(pod.UID) labels[kubernetesPodUIDLabel] = string(pod.UID)
labels[kubernetesContainerNameLabel] = container.Name
labels[kubernetesContainerHashLabel] = strconv.FormatUint(kubecontainer.HashContainer(container), 16)
labels[kubernetesContainerRestartCountLabel] = strconv.Itoa(restartCount) labels[kubernetesContainerRestartCountLabel] = strconv.Itoa(restartCount)
labels[kubernetesContainerTerminationMessagePath] = container.TerminationMessagePath labels[kubernetesContainerTerminationMessagePathLabel] = container.TerminationMessagePath
return labels return labels
} }
func getRestartCountFromLabel(labels map[string]string) (restartCount int, err error) { func getContainerInfoFromLabel(labels map[string]string) (*labelledContainerInfo, error) {
if restartCountString, found := labels[kubernetesContainerRestartCountLabel]; found { var err error
restartCount, err = strconv.Atoi(restartCountString) containerInfo := labelledContainerInfo{
if err != nil { PodName: getStringValueFromLabel(labels, kubernetesPodNameLabel),
// This really should not happen. Just set restartCount to 0 to handle this abnormal case PodNamespace: getStringValueFromLabel(labels, kubernetesPodNamespaceLabel),
restartCount = 0 PodUID: types.UID(getStringValueFromLabel(labels, kubernetesPodUIDLabel)),
Name: getStringValueFromLabel(labels, kubernetesContainerNameLabel),
Hash: getStringValueFromLabel(labels, kubernetesContainerHashLabel),
TerminationMessagePath: getStringValueFromLabel(labels, kubernetesContainerTerminationMessagePathLabel),
} }
} else { containerInfo.RestartCount, err = getIntValueFromLabel(labels, kubernetesContainerRestartCountLabel)
// Get restartCount from docker label. If there is no restart count label in a container, return &containerInfo, err
// it should be an old container or an invalid container, we just set restart count to 0.
// Do not report error, because there should be many old containers without this label now
glog.V(3).Infof("Container doesn't have label %s, it may be an old or invalid container", kubernetesContainerRestartCountLabel)
}
return restartCount, err
} }
func getTerminationMessagePathFromLabel(labels map[string]string) string { func getStringValueFromLabel(labels map[string]string, label string) string {
if terminationMessagePath, found := labels[kubernetesContainerTerminationMessagePath]; found { if value, found := labels[label]; found {
return terminationMessagePath return value
} else {
// Do not report error, because there should be many old containers without this label now.
// Return empty string "" for these containers, the caller will get terminationMessagePath by other ways.
glog.V(3).Infof("Container doesn't have label %s, it may be an old or invalid container", kubernetesContainerTerminationMessagePath)
return ""
} }
// Do not report error, because there should be many old containers without label now.
glog.V(3).Infof("Container doesn't have label %s, it may be an old or invalid container", label)
// Return empty string "" for these containers, the caller will get value by other ways.
return ""
}
func getIntValueFromLabel(labels map[string]string, label string) (int, error) {
if strValue, found := labels[label]; found {
intValue, err := strconv.Atoi(strValue)
if err != nil {
// This really should not happen. Just set value to 0 to handle this abnormal case
return 0, err
}
return intValue, nil
}
// Do not report error, because there should be many old containers without label now.
glog.V(3).Infof("Container doesn't have label %s, it may be an old or invalid container", label)
// Just set the value to 0
return 0, nil
} }

View File

@ -0,0 +1,59 @@
/*
Copyright 2015 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package dockertools
import (
"reflect"
"strconv"
"testing"
"k8s.io/kubernetes/pkg/api"
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
)
func TestLabels(t *testing.T) {
restartCount := 5
container := &api.Container{
Name: "test_container",
TerminationMessagePath: "/tmp",
}
pod := &api.Pod{
ObjectMeta: api.ObjectMeta{
Name: "test_pod",
Namespace: "test_pod_namespace",
UID: "test_pod_uid",
},
}
expected := &labelledContainerInfo{
PodName: pod.Name,
PodNamespace: pod.Namespace,
PodUID: pod.UID,
Name: container.Name,
Hash: strconv.FormatUint(kubecontainer.HashContainer(container), 16),
RestartCount: restartCount,
TerminationMessagePath: container.TerminationMessagePath,
}
labels := newLabels(container, pod, restartCount)
containerInfo, err := getContainerInfoFromLabel(labels)
if err != nil {
t.Errorf("Unexpected error when getContainerInfoFromLabel: %v", err)
}
if !reflect.DeepEqual(containerInfo, expected) {
t.Errorf("expected %v, got %v", expected, containerInfo)
}
}

View File

@ -357,14 +357,14 @@ func (dm *DockerManager) inspectContainer(dockerID, containerName string, pod *a
glog.V(4).Infof("Container inspect result: %+v", *inspectResult) glog.V(4).Infof("Container inspect result: %+v", *inspectResult)
var restartCount int var containerInfo *labelledContainerInfo
if restartCount, err = getRestartCountFromLabel(inspectResult.Config.Labels); err != nil { if containerInfo, err = getContainerInfoFromLabel(inspectResult.Config.Labels); err != nil {
glog.Errorf("Get restart count error for container %v: %v", dockerID, err) glog.Errorf("Get labelled container info error for container %v: %v", dockerID, err)
} }
result.status = api.ContainerStatus{ result.status = api.ContainerStatus{
Name: containerName, Name: containerName,
RestartCount: restartCount, RestartCount: containerInfo.RestartCount,
Image: inspectResult.Config.Image, Image: inspectResult.Config.Image,
ImageID: DockerPrefix + inspectResult.Image, ImageID: DockerPrefix + inspectResult.Image,
ContainerID: DockerPrefix + dockerID, ContainerID: DockerPrefix + dockerID,
@ -408,7 +408,7 @@ func (dm *DockerManager) inspectContainer(dockerID, containerName string, pod *a
FinishedAt: finishedAt, FinishedAt: finishedAt,
ContainerID: DockerPrefix + dockerID, ContainerID: DockerPrefix + dockerID,
} }
terminationMessagePath := getTerminationMessagePathFromLabel(inspectResult.Config.Labels) terminationMessagePath := containerInfo.TerminationMessagePath
if terminationMessagePath != "" { if terminationMessagePath != "" {
path, found := inspectResult.Volumes[terminationMessagePath] path, found := inspectResult.Volumes[terminationMessagePath]
if found { if found {

15
pkg/kubelet/dockertools/manager_test.go Executable file → Normal file
View File

@ -1360,7 +1360,7 @@ func TestGetRestartCount(t *testing.T) {
runSyncPod(t, dm, fakeDocker, pod, nil, false) runSyncPod(t, dm, fakeDocker, pod, nil, false)
status, err := dm.GetPodStatus(pod) status, err := dm.GetPodStatus(pod)
if err != nil { if err != nil {
t.Fatalf("unexpected error %v", err) t.Fatalf("Unexpected error %v", err)
} }
restartCount := status.ContainerStatuses[0].RestartCount restartCount := status.ContainerStatuses[0].RestartCount
if restartCount != expectedCount { if restartCount != expectedCount {
@ -1372,7 +1372,7 @@ func TestGetRestartCount(t *testing.T) {
killOneContainer := func(pod *api.Pod) { killOneContainer := func(pod *api.Pod) {
status, err := dm.GetPodStatus(pod) status, err := dm.GetPodStatus(pod)
if err != nil { if err != nil {
t.Fatalf("unexpected error %v", err) t.Fatalf("Unexpected error %v", err)
} }
containerID := kubecontainer.ParseContainerID(status.ContainerStatuses[0].ContainerID) containerID := kubecontainer.ParseContainerID(status.ContainerStatuses[0].ContainerID)
dm.KillContainerInPod(containerID, &pod.Spec.Containers[0], pod, "test container restart count.") dm.KillContainerInPod(containerID, &pod.Spec.Containers[0], pod, "test container restart count.")
@ -1438,13 +1438,18 @@ func TestGetTerminationMessagePath(t *testing.T) {
containerList := fakeDocker.ContainerList containerList := fakeDocker.ContainerList
if len(containerList) != 2 { if len(containerList) != 2 {
// One for infra container, one for container "bar" // One for infra container, one for container "bar"
t.Fatalf("unexpected container list length %d", len(containerList)) t.Fatalf("Unexpected container list length %d", len(containerList))
} }
inspectResult, err := dm.client.InspectContainer(containerList[0].ID) inspectResult, err := dm.client.InspectContainer(containerList[0].ID)
if err != nil { if err != nil {
t.Fatalf("unexpected inspect error %v", err) t.Fatalf("Unexpected inspect error: %v", err)
} }
terminationMessagePath := getTerminationMessagePathFromLabel(inspectResult.Config.Labels) var containerInfo *labelledContainerInfo
containerInfo, err = getContainerInfoFromLabel(inspectResult.Config.Labels)
if err != nil {
t.Fatalf("Unexpected error when getContainerInfoFromLabel: %v", err)
}
terminationMessagePath := containerInfo.TerminationMessagePath
if terminationMessagePath != containers[0].TerminationMessagePath { if terminationMessagePath != containers[0].TerminationMessagePath {
t.Errorf("expected termination message path %s, got %s", containers[0].TerminationMessagePath, terminationMessagePath) t.Errorf("expected termination message path %s, got %s", containers[0].TerminationMessagePath, terminationMessagePath)
} }