mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 11:50:44 +00:00
Merge pull request #45442 from verb/pod-tshoot-1
Automatic merge from submit-queue. If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>. Add a container type to the runtime manager's container status **What this PR does / why we need it**: This is Step 1 of the "Debug Containers" feature proposed in #35584 and is hidden behind a feature gate. Debug containers exist as container status with no associated spec, so this new runtime label allows the kubelet to treat containers differently without relying on spec. **Which issue this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close that issue when PR gets merged)*: cc #27140 **Special notes for your reviewer**: **Release note**: ```release-note NONE ``` **Integrating feedback**: - [x] Remove Type field in favor of a help method **Dependencies:** - [x] #46261 Feature gate for Debug Containers
This commit is contained in:
commit
4024b59844
@ -265,6 +265,13 @@ const (
|
|||||||
ContainerStateUnknown ContainerState = "unknown"
|
ContainerStateUnknown ContainerState = "unknown"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type ContainerType string
|
||||||
|
|
||||||
|
const (
|
||||||
|
ContainerTypeInit ContainerType = "INIT"
|
||||||
|
ContainerTypeRegular ContainerType = "REGULAR"
|
||||||
|
)
|
||||||
|
|
||||||
// Container provides the runtime information for a container, such as ID, hash,
|
// Container provides the runtime information for a container, such as ID, hash,
|
||||||
// state of the container.
|
// state of the container.
|
||||||
type Container struct {
|
type Container struct {
|
||||||
|
@ -29,6 +29,7 @@ go_library(
|
|||||||
"//pkg/apis/core/v1/helper:go_default_library",
|
"//pkg/apis/core/v1/helper:go_default_library",
|
||||||
"//pkg/credentialprovider:go_default_library",
|
"//pkg/credentialprovider:go_default_library",
|
||||||
"//pkg/credentialprovider/secrets:go_default_library",
|
"//pkg/credentialprovider/secrets:go_default_library",
|
||||||
|
"//pkg/features:go_default_library",
|
||||||
"//pkg/kubelet/apis/cri:go_default_library",
|
"//pkg/kubelet/apis/cri:go_default_library",
|
||||||
"//pkg/kubelet/apis/cri/v1alpha1/runtime:go_default_library",
|
"//pkg/kubelet/apis/cri/v1alpha1/runtime:go_default_library",
|
||||||
"//pkg/kubelet/cm:go_default_library",
|
"//pkg/kubelet/cm:go_default_library",
|
||||||
@ -59,6 +60,7 @@ go_library(
|
|||||||
"//vendor/k8s.io/apimachinery/pkg/util/errors:go_default_library",
|
"//vendor/k8s.io/apimachinery/pkg/util/errors:go_default_library",
|
||||||
"//vendor/k8s.io/apimachinery/pkg/util/runtime:go_default_library",
|
"//vendor/k8s.io/apimachinery/pkg/util/runtime:go_default_library",
|
||||||
"//vendor/k8s.io/apimachinery/pkg/util/sets:go_default_library",
|
"//vendor/k8s.io/apimachinery/pkg/util/sets:go_default_library",
|
||||||
|
"//vendor/k8s.io/apiserver/pkg/util/feature:go_default_library",
|
||||||
"//vendor/k8s.io/client-go/tools/record:go_default_library",
|
"//vendor/k8s.io/client-go/tools/record:go_default_library",
|
||||||
"//vendor/k8s.io/client-go/tools/reference:go_default_library",
|
"//vendor/k8s.io/client-go/tools/reference:go_default_library",
|
||||||
"//vendor/k8s.io/client-go/util/flowcontrol:go_default_library",
|
"//vendor/k8s.io/client-go/util/flowcontrol:go_default_library",
|
||||||
@ -99,6 +101,7 @@ go_test(
|
|||||||
"//vendor/k8s.io/apimachinery/pkg/types:go_default_library",
|
"//vendor/k8s.io/apimachinery/pkg/types:go_default_library",
|
||||||
"//vendor/k8s.io/apimachinery/pkg/util/intstr:go_default_library",
|
"//vendor/k8s.io/apimachinery/pkg/util/intstr:go_default_library",
|
||||||
"//vendor/k8s.io/apimachinery/pkg/util/sets:go_default_library",
|
"//vendor/k8s.io/apimachinery/pkg/util/sets:go_default_library",
|
||||||
|
"//vendor/k8s.io/apiserver/pkg/util/feature:go_default_library",
|
||||||
"//vendor/k8s.io/client-go/util/flowcontrol:go_default_library",
|
"//vendor/k8s.io/client-go/util/flowcontrol:go_default_library",
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
@ -85,7 +85,7 @@ func (m *kubeGenericRuntimeManager) recordContainerEvent(pod *v1.Pod, container
|
|||||||
// * create the container
|
// * create the container
|
||||||
// * start the container
|
// * start the container
|
||||||
// * run the post start lifecycle hooks (if applicable)
|
// * run the post start lifecycle hooks (if applicable)
|
||||||
func (m *kubeGenericRuntimeManager) startContainer(podSandboxID string, podSandboxConfig *runtimeapi.PodSandboxConfig, container *v1.Container, pod *v1.Pod, podStatus *kubecontainer.PodStatus, pullSecrets []v1.Secret, podIP string) (string, error) {
|
func (m *kubeGenericRuntimeManager) startContainer(podSandboxID string, podSandboxConfig *runtimeapi.PodSandboxConfig, container *v1.Container, pod *v1.Pod, podStatus *kubecontainer.PodStatus, pullSecrets []v1.Secret, podIP string, containerType kubecontainer.ContainerType) (string, error) {
|
||||||
// Step 1: pull the image.
|
// Step 1: pull the image.
|
||||||
imageRef, msg, err := m.imagePuller.EnsureImageExists(pod, container, pullSecrets)
|
imageRef, msg, err := m.imagePuller.EnsureImageExists(pod, container, pullSecrets)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -107,7 +107,7 @@ func (m *kubeGenericRuntimeManager) startContainer(podSandboxID string, podSandb
|
|||||||
restartCount = containerStatus.RestartCount + 1
|
restartCount = containerStatus.RestartCount + 1
|
||||||
}
|
}
|
||||||
|
|
||||||
containerConfig, err := m.generateContainerConfig(container, pod, restartCount, podIP, imageRef)
|
containerConfig, err := m.generateContainerConfig(container, pod, restartCount, podIP, imageRef, containerType)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
m.recordContainerEvent(pod, container, "", v1.EventTypeWarning, events.FailedToCreateContainer, "Error: %v", grpc.ErrorDesc(err))
|
m.recordContainerEvent(pod, container, "", v1.EventTypeWarning, events.FailedToCreateContainer, "Error: %v", grpc.ErrorDesc(err))
|
||||||
return grpc.ErrorDesc(err), ErrCreateContainerConfig
|
return grpc.ErrorDesc(err), ErrCreateContainerConfig
|
||||||
@ -173,7 +173,7 @@ func (m *kubeGenericRuntimeManager) startContainer(podSandboxID string, podSandb
|
|||||||
}
|
}
|
||||||
|
|
||||||
// generateContainerConfig generates container config for kubelet runtime v1.
|
// generateContainerConfig generates container config for kubelet runtime v1.
|
||||||
func (m *kubeGenericRuntimeManager) generateContainerConfig(container *v1.Container, pod *v1.Pod, restartCount int, podIP, imageRef string) (*runtimeapi.ContainerConfig, error) {
|
func (m *kubeGenericRuntimeManager) generateContainerConfig(container *v1.Container, pod *v1.Pod, restartCount int, podIP, imageRef string, containerType kubecontainer.ContainerType) (*runtimeapi.ContainerConfig, error) {
|
||||||
opts, err := m.runtimeHelper.GenerateRunContainerOptions(pod, container, podIP)
|
opts, err := m.runtimeHelper.GenerateRunContainerOptions(pod, container, podIP)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -201,7 +201,7 @@ func (m *kubeGenericRuntimeManager) generateContainerConfig(container *v1.Contai
|
|||||||
Command: command,
|
Command: command,
|
||||||
Args: args,
|
Args: args,
|
||||||
WorkingDir: container.WorkingDir,
|
WorkingDir: container.WorkingDir,
|
||||||
Labels: newContainerLabels(container, pod),
|
Labels: newContainerLabels(container, pod, containerType),
|
||||||
Annotations: newContainerAnnotations(container, pod, restartCount),
|
Annotations: newContainerAnnotations(container, pod, restartCount),
|
||||||
Devices: makeDevices(opts),
|
Devices: makeDevices(opts),
|
||||||
Mounts: m.makeMounts(opts, container),
|
Mounts: m.makeMounts(opts, container),
|
||||||
|
@ -221,7 +221,7 @@ func makeExpectedConfig(m *kubeGenericRuntimeManager, pod *v1.Pod, containerInde
|
|||||||
Command: container.Command,
|
Command: container.Command,
|
||||||
Args: []string(nil),
|
Args: []string(nil),
|
||||||
WorkingDir: container.WorkingDir,
|
WorkingDir: container.WorkingDir,
|
||||||
Labels: newContainerLabels(container, pod),
|
Labels: newContainerLabels(container, pod, kubecontainer.ContainerTypeRegular),
|
||||||
Annotations: newContainerAnnotations(container, pod, restartCount),
|
Annotations: newContainerAnnotations(container, pod, restartCount),
|
||||||
Devices: makeDevices(opts),
|
Devices: makeDevices(opts),
|
||||||
Mounts: m.makeMounts(opts, container),
|
Mounts: m.makeMounts(opts, container),
|
||||||
@ -259,7 +259,7 @@ func TestGenerateContainerConfig(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
expectedConfig := makeExpectedConfig(m, pod, 0)
|
expectedConfig := makeExpectedConfig(m, pod, 0)
|
||||||
containerConfig, err := m.generateContainerConfig(&pod.Spec.Containers[0], pod, 0, "", pod.Spec.Containers[0].Image)
|
containerConfig, err := m.generateContainerConfig(&pod.Spec.Containers[0], pod, 0, "", pod.Spec.Containers[0].Image, kubecontainer.ContainerTypeRegular)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, expectedConfig, containerConfig, "generate container config for kubelet runtime v1.")
|
assert.Equal(t, expectedConfig, containerConfig, "generate container config for kubelet runtime v1.")
|
||||||
|
|
||||||
@ -288,7 +288,7 @@ func TestGenerateContainerConfig(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = m.generateContainerConfig(&podWithContainerSecurityContext.Spec.Containers[0], podWithContainerSecurityContext, 0, "", podWithContainerSecurityContext.Spec.Containers[0].Image)
|
_, err = m.generateContainerConfig(&podWithContainerSecurityContext.Spec.Containers[0], podWithContainerSecurityContext, 0, "", podWithContainerSecurityContext.Spec.Containers[0].Image, kubecontainer.ContainerTypeRegular)
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
|
|
||||||
imageId, _ := imageService.PullImage(&runtimeapi.ImageSpec{Image: "busybox"}, nil)
|
imageId, _ := imageService.PullImage(&runtimeapi.ImageSpec{Image: "busybox"}, nil)
|
||||||
@ -300,7 +300,7 @@ func TestGenerateContainerConfig(t *testing.T) {
|
|||||||
podWithContainerSecurityContext.Spec.Containers[0].SecurityContext.RunAsUser = nil
|
podWithContainerSecurityContext.Spec.Containers[0].SecurityContext.RunAsUser = nil
|
||||||
podWithContainerSecurityContext.Spec.Containers[0].SecurityContext.RunAsNonRoot = &runAsNonRootTrue
|
podWithContainerSecurityContext.Spec.Containers[0].SecurityContext.RunAsNonRoot = &runAsNonRootTrue
|
||||||
|
|
||||||
_, err = m.generateContainerConfig(&podWithContainerSecurityContext.Spec.Containers[0], podWithContainerSecurityContext, 0, "", podWithContainerSecurityContext.Spec.Containers[0].Image)
|
_, err = m.generateContainerConfig(&podWithContainerSecurityContext.Spec.Containers[0], podWithContainerSecurityContext, 0, "", podWithContainerSecurityContext.Spec.Containers[0].Image, kubecontainer.ContainerTypeRegular)
|
||||||
assert.Error(t, err, "RunAsNonRoot should fail for non-numeric username")
|
assert.Error(t, err, "RunAsNonRoot should fail for non-numeric username")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -424,7 +424,7 @@ func TestLifeCycleHook(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Now try to create a container, which should in turn invoke PostStart Hook
|
// Now try to create a container, which should in turn invoke PostStart Hook
|
||||||
_, err := m.startContainer(fakeSandBox.Id, fakeSandBoxConfig, testContainer, testPod, fakePodStatus, nil, "")
|
_, err := m.startContainer(fakeSandBox.Id, fakeSandBoxConfig, testContainer, testPod, fakePodStatus, nil, "", kubecontainer.ContainerTypeRegular)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("startContainer erro =%v", err)
|
t.Errorf("startContainer erro =%v", err)
|
||||||
}
|
}
|
||||||
|
@ -699,7 +699,7 @@ func (m *kubeGenericRuntimeManager) SyncPod(pod *v1.Pod, _ v1.PodStatus, podStat
|
|||||||
}
|
}
|
||||||
|
|
||||||
glog.V(4).Infof("Creating init container %+v in pod %v", container, format.Pod(pod))
|
glog.V(4).Infof("Creating init container %+v in pod %v", container, format.Pod(pod))
|
||||||
if msg, err := m.startContainer(podSandboxID, podSandboxConfig, container, pod, podStatus, pullSecrets, podIP); err != nil {
|
if msg, err := m.startContainer(podSandboxID, podSandboxConfig, container, pod, podStatus, pullSecrets, podIP, kubecontainer.ContainerTypeInit); err != nil {
|
||||||
startContainerResult.Fail(err, msg)
|
startContainerResult.Fail(err, msg)
|
||||||
utilruntime.HandleError(fmt.Errorf("init container start failed: %v: %s", err, msg))
|
utilruntime.HandleError(fmt.Errorf("init container start failed: %v: %s", err, msg))
|
||||||
return
|
return
|
||||||
@ -723,7 +723,7 @@ func (m *kubeGenericRuntimeManager) SyncPod(pod *v1.Pod, _ v1.PodStatus, podStat
|
|||||||
}
|
}
|
||||||
|
|
||||||
glog.V(4).Infof("Creating container %+v in pod %v", container, format.Pod(pod))
|
glog.V(4).Infof("Creating container %+v in pod %v", container, format.Pod(pod))
|
||||||
if msg, err := m.startContainer(podSandboxID, podSandboxConfig, container, pod, podStatus, pullSecrets, podIP); err != nil {
|
if msg, err := m.startContainer(podSandboxID, podSandboxConfig, container, pod, podStatus, pullSecrets, podIP, kubecontainer.ContainerTypeRegular); err != nil {
|
||||||
startContainerResult.Fail(err, msg)
|
startContainerResult.Fail(err, msg)
|
||||||
// known errors that are logged in other places are logged at higher levels here to avoid
|
// known errors that are logged in other places are logged at higher levels here to avoid
|
||||||
// repetitive log spam
|
// repetitive log spam
|
||||||
|
@ -70,6 +70,7 @@ type sandboxTemplate struct {
|
|||||||
type containerTemplate struct {
|
type containerTemplate struct {
|
||||||
pod *v1.Pod
|
pod *v1.Pod
|
||||||
container *v1.Container
|
container *v1.Container
|
||||||
|
containerType kubecontainer.ContainerType
|
||||||
sandboxAttempt uint32
|
sandboxAttempt uint32
|
||||||
attempt int
|
attempt int
|
||||||
createdAt int64
|
createdAt int64
|
||||||
@ -142,7 +143,7 @@ func makeFakeContainer(t *testing.T, m *kubeGenericRuntimeManager, template cont
|
|||||||
sandboxConfig, err := m.generatePodSandboxConfig(template.pod, template.sandboxAttempt)
|
sandboxConfig, err := m.generatePodSandboxConfig(template.pod, template.sandboxAttempt)
|
||||||
assert.NoError(t, err, "generatePodSandboxConfig for container template %+v", template)
|
assert.NoError(t, err, "generatePodSandboxConfig for container template %+v", template)
|
||||||
|
|
||||||
containerConfig, err := m.generateContainerConfig(template.container, template.pod, template.attempt, "", template.container.Image)
|
containerConfig, err := m.generateContainerConfig(template.container, template.pod, template.attempt, "", template.container.Image, template.containerType)
|
||||||
assert.NoError(t, err, "generateContainerConfig for container template %+v", template)
|
assert.NoError(t, err, "generateContainerConfig for container template %+v", template)
|
||||||
|
|
||||||
podSandboxID := apitest.BuildSandboxName(sandboxConfig.Metadata)
|
podSandboxID := apitest.BuildSandboxName(sandboxConfig.Metadata)
|
||||||
|
@ -23,6 +23,8 @@ import (
|
|||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
"k8s.io/api/core/v1"
|
"k8s.io/api/core/v1"
|
||||||
kubetypes "k8s.io/apimachinery/pkg/types"
|
kubetypes "k8s.io/apimachinery/pkg/types"
|
||||||
|
utilfeature "k8s.io/apiserver/pkg/util/feature"
|
||||||
|
"k8s.io/kubernetes/pkg/features"
|
||||||
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
|
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
|
||||||
"k8s.io/kubernetes/pkg/kubelet/types"
|
"k8s.io/kubernetes/pkg/kubelet/types"
|
||||||
"k8s.io/kubernetes/pkg/kubelet/util/format"
|
"k8s.io/kubernetes/pkg/kubelet/util/format"
|
||||||
@ -56,6 +58,7 @@ type annotatedPodSandboxInfo struct {
|
|||||||
|
|
||||||
type labeledContainerInfo struct {
|
type labeledContainerInfo struct {
|
||||||
ContainerName string
|
ContainerName string
|
||||||
|
ContainerType kubecontainer.ContainerType
|
||||||
PodName string
|
PodName string
|
||||||
PodNamespace string
|
PodNamespace string
|
||||||
PodUID kubetypes.UID
|
PodUID kubetypes.UID
|
||||||
@ -94,12 +97,15 @@ func newPodAnnotations(pod *v1.Pod) map[string]string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// newContainerLabels creates container labels from v1.Container and v1.Pod.
|
// newContainerLabels creates container labels from v1.Container and v1.Pod.
|
||||||
func newContainerLabels(container *v1.Container, pod *v1.Pod) map[string]string {
|
func newContainerLabels(container *v1.Container, pod *v1.Pod, containerType kubecontainer.ContainerType) map[string]string {
|
||||||
labels := map[string]string{}
|
labels := map[string]string{}
|
||||||
labels[types.KubernetesPodNameLabel] = pod.Name
|
labels[types.KubernetesPodNameLabel] = pod.Name
|
||||||
labels[types.KubernetesPodNamespaceLabel] = pod.Namespace
|
labels[types.KubernetesPodNamespaceLabel] = pod.Namespace
|
||||||
labels[types.KubernetesPodUIDLabel] = string(pod.UID)
|
labels[types.KubernetesPodUIDLabel] = string(pod.UID)
|
||||||
labels[types.KubernetesContainerNameLabel] = container.Name
|
labels[types.KubernetesContainerNameLabel] = container.Name
|
||||||
|
if utilfeature.DefaultFeatureGate.Enabled(features.DebugContainers) {
|
||||||
|
labels[types.KubernetesContainerTypeLabel] = string(containerType)
|
||||||
|
}
|
||||||
|
|
||||||
return labels
|
return labels
|
||||||
}
|
}
|
||||||
@ -169,11 +175,16 @@ func getPodSandboxInfoFromAnnotations(annotations map[string]string) *annotatedP
|
|||||||
|
|
||||||
// getContainerInfoFromLabels gets labeledContainerInfo from labels.
|
// getContainerInfoFromLabels gets labeledContainerInfo from labels.
|
||||||
func getContainerInfoFromLabels(labels map[string]string) *labeledContainerInfo {
|
func getContainerInfoFromLabels(labels map[string]string) *labeledContainerInfo {
|
||||||
|
var containerType kubecontainer.ContainerType
|
||||||
|
if utilfeature.DefaultFeatureGate.Enabled(features.DebugContainers) {
|
||||||
|
containerType = kubecontainer.ContainerType(getStringValueFromLabel(labels, types.KubernetesContainerTypeLabel))
|
||||||
|
}
|
||||||
return &labeledContainerInfo{
|
return &labeledContainerInfo{
|
||||||
PodName: getStringValueFromLabel(labels, types.KubernetesPodNameLabel),
|
PodName: getStringValueFromLabel(labels, types.KubernetesPodNameLabel),
|
||||||
PodNamespace: getStringValueFromLabel(labels, types.KubernetesPodNamespaceLabel),
|
PodNamespace: getStringValueFromLabel(labels, types.KubernetesPodNamespaceLabel),
|
||||||
PodUID: kubetypes.UID(getStringValueFromLabel(labels, types.KubernetesPodUIDLabel)),
|
PodUID: kubetypes.UID(getStringValueFromLabel(labels, types.KubernetesPodUIDLabel)),
|
||||||
ContainerName: getStringValueFromLabel(labels, types.KubernetesContainerNameLabel),
|
ContainerName: getStringValueFromLabel(labels, types.KubernetesContainerNameLabel),
|
||||||
|
ContainerType: containerType,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,6 +23,7 @@ import (
|
|||||||
"k8s.io/api/core/v1"
|
"k8s.io/api/core/v1"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/util/intstr"
|
"k8s.io/apimachinery/pkg/util/intstr"
|
||||||
|
utilfeature "k8s.io/apiserver/pkg/util/feature"
|
||||||
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
|
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -63,19 +64,92 @@ func TestContainerLabels(t *testing.T) {
|
|||||||
TerminationGracePeriodSeconds: &terminationGracePeriod,
|
TerminationGracePeriodSeconds: &terminationGracePeriod,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
expected := &labeledContainerInfo{
|
|
||||||
|
var tests = []struct {
|
||||||
|
description string
|
||||||
|
featuresCreated string // Features enabled when container is created
|
||||||
|
featuresStatus string // Features enabled when container status is read
|
||||||
|
typeLabel kubecontainer.ContainerType
|
||||||
|
expected *labeledContainerInfo
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
"Debug containers disabled",
|
||||||
|
"DebugContainers=False",
|
||||||
|
"DebugContainers=False",
|
||||||
|
"ignored",
|
||||||
|
&labeledContainerInfo{
|
||||||
PodName: pod.Name,
|
PodName: pod.Name,
|
||||||
PodNamespace: pod.Namespace,
|
PodNamespace: pod.Namespace,
|
||||||
PodUID: pod.UID,
|
PodUID: pod.UID,
|
||||||
ContainerName: container.Name,
|
ContainerName: container.Name,
|
||||||
|
ContainerType: "",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Regular containers",
|
||||||
|
"DebugContainers=True",
|
||||||
|
"DebugContainers=True",
|
||||||
|
kubecontainer.ContainerTypeRegular,
|
||||||
|
&labeledContainerInfo{
|
||||||
|
PodName: pod.Name,
|
||||||
|
PodNamespace: pod.Namespace,
|
||||||
|
PodUID: pod.UID,
|
||||||
|
ContainerName: container.Name,
|
||||||
|
ContainerType: kubecontainer.ContainerTypeRegular,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Init containers",
|
||||||
|
"DebugContainers=True",
|
||||||
|
"DebugContainers=True",
|
||||||
|
kubecontainer.ContainerTypeInit,
|
||||||
|
&labeledContainerInfo{
|
||||||
|
PodName: pod.Name,
|
||||||
|
PodNamespace: pod.Namespace,
|
||||||
|
PodUID: pod.UID,
|
||||||
|
ContainerName: container.Name,
|
||||||
|
ContainerType: kubecontainer.ContainerTypeInit,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Created without type label",
|
||||||
|
"DebugContainers=False",
|
||||||
|
"DebugContainers=True",
|
||||||
|
"ignored",
|
||||||
|
&labeledContainerInfo{
|
||||||
|
PodName: pod.Name,
|
||||||
|
PodNamespace: pod.Namespace,
|
||||||
|
PodUID: pod.UID,
|
||||||
|
ContainerName: container.Name,
|
||||||
|
ContainerType: "",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Created with type label, subsequently disabled",
|
||||||
|
"DebugContainers=True",
|
||||||
|
"DebugContainers=False",
|
||||||
|
kubecontainer.ContainerTypeRegular,
|
||||||
|
&labeledContainerInfo{
|
||||||
|
PodName: pod.Name,
|
||||||
|
PodNamespace: pod.Namespace,
|
||||||
|
PodUID: pod.UID,
|
||||||
|
ContainerName: container.Name,
|
||||||
|
ContainerType: "",
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test whether we can get right information from label
|
// Test whether we can get right information from label
|
||||||
labels := newContainerLabels(container, pod)
|
for _, test := range tests {
|
||||||
|
utilfeature.DefaultFeatureGate.Set(test.featuresCreated)
|
||||||
|
labels := newContainerLabels(container, pod, test.typeLabel)
|
||||||
|
utilfeature.DefaultFeatureGate.Set(test.featuresStatus)
|
||||||
containerInfo := getContainerInfoFromLabels(labels)
|
containerInfo := getContainerInfoFromLabels(labels)
|
||||||
if !reflect.DeepEqual(containerInfo, expected) {
|
if !reflect.DeepEqual(containerInfo, test.expected) {
|
||||||
t.Errorf("expected %v, got %v", expected, containerInfo)
|
t.Errorf("%v: expected %v, got %v", test.description, test.expected, containerInfo)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
utilfeature.DefaultFeatureGate.Set("DebugContainers=False")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestContainerAnnotations(t *testing.T) {
|
func TestContainerAnnotations(t *testing.T) {
|
||||||
|
@ -21,6 +21,7 @@ const (
|
|||||||
KubernetesPodNamespaceLabel = "io.kubernetes.pod.namespace"
|
KubernetesPodNamespaceLabel = "io.kubernetes.pod.namespace"
|
||||||
KubernetesPodUIDLabel = "io.kubernetes.pod.uid"
|
KubernetesPodUIDLabel = "io.kubernetes.pod.uid"
|
||||||
KubernetesContainerNameLabel = "io.kubernetes.container.name"
|
KubernetesContainerNameLabel = "io.kubernetes.container.name"
|
||||||
|
KubernetesContainerTypeLabel = "io.kubernetes.container.type"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetContainerName(labels map[string]string) string {
|
func GetContainerName(labels map[string]string) string {
|
||||||
|
Loading…
Reference in New Issue
Block a user