2015-10-30 05:42:25 +00:00
|
|
|
/*
|
|
|
|
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 (
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/golang/glog"
|
|
|
|
"k8s.io/kubernetes/pkg/api"
|
2015-11-13 19:37:33 +00:00
|
|
|
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
|
|
|
|
"k8s.io/kubernetes/pkg/types"
|
2015-10-30 05:42:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// This file contains all docker label related constants and functions, including:
|
|
|
|
// * label setters and getters
|
|
|
|
// * label filters (maybe in the future)
|
|
|
|
|
|
|
|
const (
|
|
|
|
kubernetesPodNameLabel = "io.kubernetes.pod.name"
|
|
|
|
kubernetesPodNamespaceLabel = "io.kubernetes.pod.namespace"
|
2015-11-13 19:37:33 +00:00
|
|
|
kubernetesPodUIDLabel = "io.kubernetes.pod.uid"
|
2015-10-30 05:42:25 +00:00
|
|
|
|
2015-11-13 19:37:33 +00:00
|
|
|
kubernetesContainerNameLabel = "io.kubernetes.container.name"
|
|
|
|
kubernetesContainerHashLabel = "io.kubernetes.container.hash"
|
|
|
|
kubernetesContainerRestartCountLabel = "io.kubernetes.container.restartCount"
|
|
|
|
kubernetesContainerTerminationMessagePathLabel = "io.kubernetes.container.terminationMessagePath"
|
2015-11-02 22:32:58 +00:00
|
|
|
|
2015-10-30 05:42:25 +00:00
|
|
|
kubernetesPodLabel = "io.kubernetes.pod.data"
|
|
|
|
kubernetesTerminationGracePeriodLabel = "io.kubernetes.pod.terminationGracePeriod"
|
|
|
|
kubernetesContainerLabel = "io.kubernetes.container.name"
|
|
|
|
)
|
|
|
|
|
2015-11-13 19:37:33 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2015-10-30 05:42:25 +00:00
|
|
|
func newLabels(container *api.Container, pod *api.Pod, restartCount int) map[string]string {
|
2015-12-31 08:41:05 +00:00
|
|
|
// TODO(random-liu): Move more label initialization here
|
2015-10-30 05:42:25 +00:00
|
|
|
labels := map[string]string{}
|
|
|
|
labels[kubernetesPodNameLabel] = pod.Name
|
|
|
|
labels[kubernetesPodNamespaceLabel] = pod.Namespace
|
2015-11-13 19:37:33 +00:00
|
|
|
labels[kubernetesPodUIDLabel] = string(pod.UID)
|
2015-10-30 05:42:25 +00:00
|
|
|
|
2015-11-13 19:37:33 +00:00
|
|
|
labels[kubernetesContainerNameLabel] = container.Name
|
|
|
|
labels[kubernetesContainerHashLabel] = strconv.FormatUint(kubecontainer.HashContainer(container), 16)
|
2015-10-30 05:42:25 +00:00
|
|
|
labels[kubernetesContainerRestartCountLabel] = strconv.Itoa(restartCount)
|
2015-11-13 19:37:33 +00:00
|
|
|
labels[kubernetesContainerTerminationMessagePathLabel] = container.TerminationMessagePath
|
2015-10-30 05:42:25 +00:00
|
|
|
|
|
|
|
return labels
|
|
|
|
}
|
|
|
|
|
2015-11-13 19:37:33 +00:00
|
|
|
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),
|
2015-10-30 05:42:25 +00:00
|
|
|
}
|
2015-11-13 19:37:33 +00:00
|
|
|
containerInfo.RestartCount, err = getIntValueFromLabel(labels, kubernetesContainerRestartCountLabel)
|
|
|
|
return &containerInfo, err
|
2015-10-30 05:42:25 +00:00
|
|
|
}
|
2015-11-02 22:32:58 +00:00
|
|
|
|
2015-11-13 19:37:33 +00:00
|
|
|
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
|
2015-11-02 22:32:58 +00:00
|
|
|
}
|
2015-11-13 19:37:33 +00:00
|
|
|
// 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
|
2015-11-02 22:32:58 +00:00
|
|
|
}
|