Merge pull request #72280 from verb/kubelet-no-containertype

Remove container type from kubelet runtime labels
This commit is contained in:
Kubernetes Prow Robot 2019-01-10 01:18:41 -08:00 committed by GitHub
commit a6d7527ac3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 20 additions and 107 deletions

View File

@ -254,13 +254,6 @@ 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 {

View File

@ -90,7 +90,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, containerType kubecontainer.ContainerType) (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) (string, error) {
// Step 1: pull the image. // Step 1: pull the image.
imageRef, msg, err := m.imagePuller.EnsureImageExists(pod, container, pullSecrets, podSandboxConfig) imageRef, msg, err := m.imagePuller.EnsureImageExists(pod, container, pullSecrets, podSandboxConfig)
if err != nil { if err != nil {
@ -112,7 +112,7 @@ func (m *kubeGenericRuntimeManager) startContainer(podSandboxID string, podSandb
restartCount = containerStatus.RestartCount + 1 restartCount = containerStatus.RestartCount + 1
} }
containerConfig, cleanupAction, err := m.generateContainerConfig(container, pod, restartCount, podIP, imageRef, containerType) containerConfig, cleanupAction, err := m.generateContainerConfig(container, pod, restartCount, podIP, imageRef)
if cleanupAction != nil { if cleanupAction != nil {
defer cleanupAction() defer cleanupAction()
} }
@ -188,7 +188,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, containerType kubecontainer.ContainerType) (*runtimeapi.ContainerConfig, func(), error) { func (m *kubeGenericRuntimeManager) generateContainerConfig(container *v1.Container, pod *v1.Pod, restartCount int, podIP, imageRef string) (*runtimeapi.ContainerConfig, func(), error) {
opts, cleanupAction, err := m.runtimeHelper.GenerateRunContainerOptions(pod, container, podIP) opts, cleanupAction, err := m.runtimeHelper.GenerateRunContainerOptions(pod, container, podIP)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
@ -221,7 +221,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, containerType), Labels: newContainerLabels(container, pod),
Annotations: newContainerAnnotations(container, pod, restartCount, opts), Annotations: newContainerAnnotations(container, pod, restartCount, opts),
Devices: makeDevices(opts), Devices: makeDevices(opts),
Mounts: m.makeMounts(opts, container), Mounts: m.makeMounts(opts, container),

View File

@ -25,7 +25,6 @@ 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"
runtimeapi "k8s.io/kubernetes/pkg/kubelet/apis/cri/runtime/v1alpha2" runtimeapi "k8s.io/kubernetes/pkg/kubelet/apis/cri/runtime/v1alpha2"
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
) )
func makeExpectedConfig(m *kubeGenericRuntimeManager, pod *v1.Pod, containerIndex int) *runtimeapi.ContainerConfig { func makeExpectedConfig(m *kubeGenericRuntimeManager, pod *v1.Pod, containerIndex int) *runtimeapi.ContainerConfig {
@ -46,7 +45,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, kubecontainer.ContainerTypeRegular), Labels: newContainerLabels(container, pod),
Annotations: newContainerAnnotations(container, pod, restartCount, opts), Annotations: newContainerAnnotations(container, pod, restartCount, opts),
Devices: makeDevices(opts), Devices: makeDevices(opts),
Mounts: m.makeMounts(opts, container), Mounts: m.makeMounts(opts, container),
@ -90,7 +89,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, kubecontainer.ContainerTypeRegular) containerConfig, _, err := m.generateContainerConfig(&pod.Spec.Containers[0], pod, 0, "", pod.Spec.Containers[0].Image)
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.")
assert.Equal(t, runAsUser, containerConfig.GetLinux().GetSecurityContext().GetRunAsUser().GetValue(), "RunAsUser should be set") assert.Equal(t, runAsUser, containerConfig.GetLinux().GetSecurityContext().GetRunAsUser().GetValue(), "RunAsUser should be set")
@ -121,7 +120,7 @@ func TestGenerateContainerConfig(t *testing.T) {
}, },
} }
_, _, err = m.generateContainerConfig(&podWithContainerSecurityContext.Spec.Containers[0], podWithContainerSecurityContext, 0, "", podWithContainerSecurityContext.Spec.Containers[0].Image, kubecontainer.ContainerTypeRegular) _, _, err = m.generateContainerConfig(&podWithContainerSecurityContext.Spec.Containers[0], podWithContainerSecurityContext, 0, "", podWithContainerSecurityContext.Spec.Containers[0].Image)
assert.Error(t, err) assert.Error(t, err)
imageID, _ := imageService.PullImage(&runtimeapi.ImageSpec{Image: "busybox"}, nil, nil) imageID, _ := imageService.PullImage(&runtimeapi.ImageSpec{Image: "busybox"}, nil, nil)
@ -133,6 +132,6 @@ 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, kubecontainer.ContainerTypeRegular) _, _, err = m.generateContainerConfig(&podWithContainerSecurityContext.Spec.Containers[0], podWithContainerSecurityContext, 0, "", podWithContainerSecurityContext.Spec.Containers[0].Image)
assert.Error(t, err, "RunAsNonRoot should fail for non-numeric username") assert.Error(t, err, "RunAsNonRoot should fail for non-numeric username")
} }

View File

@ -323,7 +323,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, "", kubecontainer.ContainerTypeRegular) _, err := m.startContainer(fakeSandBox.Id, fakeSandBoxConfig, testContainer, testPod, fakePodStatus, nil, "")
if err != nil { if err != nil {
t.Errorf("startContainer erro =%v", err) t.Errorf("startContainer erro =%v", err)
} }

View File

@ -726,7 +726,7 @@ func (m *kubeGenericRuntimeManager) SyncPod(pod *v1.Pod, _ v1.PodStatus, podStat
} }
klog.V(4).Infof("Creating init container %+v in pod %v", container, format.Pod(pod)) klog.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, kubecontainer.ContainerTypeInit); err != nil { if msg, err := m.startContainer(podSandboxID, podSandboxConfig, container, pod, podStatus, pullSecrets, podIP); 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
@ -750,7 +750,7 @@ func (m *kubeGenericRuntimeManager) SyncPod(pod *v1.Pod, _ v1.PodStatus, podStat
} }
klog.V(4).Infof("Creating container %+v in pod %v", container, format.Pod(pod)) klog.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, kubecontainer.ContainerTypeRegular); err != nil { if msg, err := m.startContainer(podSandboxID, podSandboxConfig, container, pod, podStatus, pullSecrets, podIP); 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

View File

@ -70,7 +70,6 @@ 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
@ -143,7 +142,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, template.containerType) containerConfig, _, err := m.generateContainerConfig(template.container, template.pod, template.attempt, "", template.container.Image)
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)

View File

@ -22,9 +22,7 @@ import (
"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/klog" "k8s.io/klog"
"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"
@ -58,7 +56,6 @@ 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
@ -97,15 +94,12 @@ 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, containerType kubecontainer.ContainerType) map[string]string { func newContainerLabels(container *v1.Container, pod *v1.Pod) 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
} }
@ -181,16 +175,11 @@ 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,
} }
} }

View File

@ -23,9 +23,6 @@ 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"
utilfeaturetesting "k8s.io/apiserver/pkg/util/feature/testing"
"k8s.io/kubernetes/pkg/features"
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container" kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
) )
@ -68,90 +65,27 @@ func TestContainerLabels(t *testing.T) {
} }
var tests = []struct { var tests = []struct {
description string description string
featuresCreated bool // Features enabled when container is created expected *labeledContainerInfo
featuresStatus bool // Features enabled when container status is read
typeLabel kubecontainer.ContainerType
expected *labeledContainerInfo
}{ }{
{
"Debug containers disabled",
false,
false,
"ignored",
&labeledContainerInfo{
PodName: pod.Name,
PodNamespace: pod.Namespace,
PodUID: pod.UID,
ContainerName: container.Name,
ContainerType: "",
},
},
{ {
"Regular containers", "Regular containers",
true,
true,
kubecontainer.ContainerTypeRegular,
&labeledContainerInfo{ &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: kubecontainer.ContainerTypeRegular,
},
},
{
"Init containers",
true,
true,
kubecontainer.ContainerTypeInit,
&labeledContainerInfo{
PodName: pod.Name,
PodNamespace: pod.Namespace,
PodUID: pod.UID,
ContainerName: container.Name,
ContainerType: kubecontainer.ContainerTypeInit,
},
},
{
"Created without type label",
false,
true,
"ignored",
&labeledContainerInfo{
PodName: pod.Name,
PodNamespace: pod.Namespace,
PodUID: pod.UID,
ContainerName: container.Name,
ContainerType: "",
},
},
{
"Created with type label, subsequently disabled",
true,
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
for _, test := range tests { for _, test := range tests {
func() { labels := newContainerLabels(container, pod)
defer utilfeaturetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.DebugContainers, test.featuresCreated)() containerInfo := getContainerInfoFromLabels(labels)
labels := newContainerLabels(container, pod, test.typeLabel) if !reflect.DeepEqual(containerInfo, test.expected) {
defer utilfeaturetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.DebugContainers, test.featuresStatus)() t.Errorf("%v: expected %v, got %v", test.description, test.expected, containerInfo)
containerInfo := getContainerInfoFromLabels(labels) }
if !reflect.DeepEqual(containerInfo, test.expected) {
t.Errorf("%v: expected %v, got %v", test.description, test.expected, containerInfo)
}
}()
} }
} }

View File

@ -21,7 +21,6 @@ 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 {