Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move more things into docker label, and add label test #17234

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
81 changes: 55 additions & 26 deletions pkg/kubelet/dockertools/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (

"github.com/golang/glog"
"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:
Expand All @@ -30,52 +32,79 @@ import (
const (
kubernetesPodNameLabel = "io.kubernetes.pod.name"
kubernetesPodNamespaceLabel = "io.kubernetes.pod.namespace"
kubernetesPodUID = "io.kubernetes.pod.uid"
kubernetesPodUIDLabel = "io.kubernetes.pod.uid"

kubernetesContainerRestartCountLabel = "io.kubernetes.container.restartCount"
kubernetesContainerTerminationMessagePath = "io.kubernetes.container.terminationMessagePath"
kubernetesContainerNameLabel = "io.kubernetes.container.name"
kubernetesContainerHashLabel = "io.kubernetes.container.hash"
kubernetesContainerRestartCountLabel = "io.kubernetes.container.restartCount"
kubernetesContainerTerminationMessagePathLabel = "io.kubernetes.container.terminationMessagePath"

kubernetesPodLabel = "io.kubernetes.pod.data"
kubernetesTerminationGracePeriodLabel = "io.kubernetes.pod.terminationGracePeriod"
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 {
// TODO (random-liu) Move more label initialization here
labels := map[string]string{}
labels[kubernetesPodNameLabel] = pod.Name
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[kubernetesContainerTerminationMessagePath] = container.TerminationMessagePath
labels[kubernetesContainerTerminationMessagePathLabel] = container.TerminationMessagePath

return labels
}

func getRestartCountFromLabel(labels map[string]string) (restartCount int, err error) {
if restartCountString, found := labels[kubernetesContainerRestartCountLabel]; found {
restartCount, err = strconv.Atoi(restartCountString)
if err != nil {
// This really should not happen. Just set restartCount to 0 to handle this abnormal case
restartCount = 0
}
} else {
// Get restartCount from docker label. If there is no restart count label in a container,
// 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)
func getContainerInfoFromLabel(labels map[string]string) (*labelledContainerInfo, error) {
var err error
containerInfo := labelledContainerInfo{
PodName: getStringValueFromLabel(labels, kubernetesPodNameLabel),
PodNamespace: getStringValueFromLabel(labels, kubernetesPodNamespaceLabel),
PodUID: types.UID(getStringValueFromLabel(labels, kubernetesPodUIDLabel)),
Name: getStringValueFromLabel(labels, kubernetesContainerNameLabel),
Hash: getStringValueFromLabel(labels, kubernetesContainerHashLabel),
TerminationMessagePath: getStringValueFromLabel(labels, kubernetesContainerTerminationMessagePathLabel),
}
return restartCount, err
containerInfo.RestartCount, err = getIntValueFromLabel(labels, kubernetesContainerRestartCountLabel)
return &containerInfo, err
}

func getTerminationMessagePathFromLabel(labels map[string]string) string {
if terminationMessagePath, found := labels[kubernetesContainerTerminationMessagePath]; found {
return terminationMessagePath
} 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 ""
func getStringValueFromLabel(labels map[string]string, label string) string {
if value, found := labels[label]; found {
return value
}
// 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
}
59 changes: 59 additions & 0 deletions pkg/kubelet/dockertools/labels_test.go
Original file line number Diff line number Diff line change
@@ -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",
},
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: for readability, perhaps you can just create an expected result and compare with it.
E.g.,

expected := &labelledContainerInfo{
   PodName: pod.Name,
   ...
}

Then you can use reflect.DeepEqual(containerInfo, expected) to compare the two. You can even print the diff using util.ObjectDiff(expected, actual)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, learned that! Thanks a lot! It makes the code much cleaner.

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)
}
}
10 changes: 5 additions & 5 deletions pkg/kubelet/dockertools/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,14 +357,14 @@ func (dm *DockerManager) inspectContainer(dockerID, containerName string, pod *a

glog.V(4).Infof("Container inspect result: %+v", *inspectResult)

var restartCount int
if restartCount, err = getRestartCountFromLabel(inspectResult.Config.Labels); err != nil {
glog.Errorf("Get restart count error for container %v: %v", dockerID, err)
var containerInfo *labelledContainerInfo
if containerInfo, err = getContainerInfoFromLabel(inspectResult.Config.Labels); err != nil {
glog.Errorf("Get labelled container info error for container %v: %v", dockerID, err)
}

result.status = api.ContainerStatus{
Name: containerName,
RestartCount: restartCount,
RestartCount: containerInfo.RestartCount,
Image: inspectResult.Config.Image,
ImageID: DockerPrefix + inspectResult.Image,
ContainerID: DockerPrefix + dockerID,
Expand Down Expand Up @@ -408,7 +408,7 @@ func (dm *DockerManager) inspectContainer(dockerID, containerName string, pod *a
FinishedAt: finishedAt,
ContainerID: DockerPrefix + dockerID,
}
terminationMessagePath := getTerminationMessagePathFromLabel(inspectResult.Config.Labels)
terminationMessagePath := containerInfo.TerminationMessagePath
if terminationMessagePath != "" {
path, found := inspectResult.Volumes[terminationMessagePath]
if found {
Expand Down
15 changes: 10 additions & 5 deletions pkg/kubelet/dockertools/manager_test.go
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -1360,7 +1360,7 @@ func TestGetRestartCount(t *testing.T) {
runSyncPod(t, dm, fakeDocker, pod, nil, false)
status, err := dm.GetPodStatus(pod)
if err != nil {
t.Fatalf("unexpected error %v", err)
t.Fatalf("Unexpected error %v", err)
}
restartCount := status.ContainerStatuses[0].RestartCount
if restartCount != expectedCount {
Expand All @@ -1372,7 +1372,7 @@ func TestGetRestartCount(t *testing.T) {
killOneContainer := func(pod *api.Pod) {
status, err := dm.GetPodStatus(pod)
if err != nil {
t.Fatalf("unexpected error %v", err)
t.Fatalf("Unexpected error %v", err)
}
containerID := kubecontainer.ParseContainerID(status.ContainerStatuses[0].ContainerID)
dm.KillContainerInPod(containerID, &pod.Spec.Containers[0], pod, "test container restart count.")
Expand Down Expand Up @@ -1438,13 +1438,18 @@ func TestGetTerminationMessagePath(t *testing.T) {
containerList := fakeDocker.ContainerList
if len(containerList) != 2 {
// 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)
if err != nil {
t.Fatalf("unexpected inspect error %v", err)
t.Fatalf("Unexpected inspect error: %v", err)
}
var containerInfo *labelledContainerInfo
containerInfo, err = getContainerInfoFromLabel(inspectResult.Config.Labels)
if err != nil {
t.Fatalf("Unexpected error when getContainerInfoFromLabel: %v", err)
}
terminationMessagePath := getTerminationMessagePathFromLabel(inspectResult.Config.Labels)
terminationMessagePath := containerInfo.TerminationMessagePath
if terminationMessagePath != containers[0].TerminationMessagePath {
t.Errorf("expected termination message path %s, got %s", containers[0].TerminationMessagePath, terminationMessagePath)
}
Expand Down