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

Add default container annotation to Pod #7288

Merged
merged 1 commit into from
Mar 4, 2022
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
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ require (
k8s.io/client-go v12.0.0+incompatible
k8s.io/kube-aggregator v0.23.1
k8s.io/kube-openapi v0.0.0-20211115234752-e816edb12b65
k8s.io/kubectl v0.0.0-00010101000000-000000000000
k8s.io/utils v0.0.0-20210930125809-cb0fa318a74b
kubevirt.io/api v0.0.0-00010101000000-000000000000
kubevirt.io/client-go v0.0.0-00010101000000-000000000000
Expand Down
31 changes: 30 additions & 1 deletion go.sum

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pkg/virt-controller/services/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ go_library(
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
"//vendor/k8s.io/client-go/tools/cache:go_default_library",
"//vendor/k8s.io/kubectl/pkg/cmd/util/podcmd:go_default_library",
],
)

Expand Down
4 changes: 4 additions & 0 deletions pkg/virt-controller/services/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (
"strconv"
"strings"

"k8s.io/kubectl/pkg/cmd/util/podcmd"

k8sv1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -2176,6 +2178,8 @@ func generatePodAnnotations(vmi *v1.VirtualMachineInstance) (map[string]string,
annotationsSet[k] = v
}

annotationsSet[podcmd.DefaultContainerAnnotationName] = "compute"

multusAnnotation, err := generateMultusCNIAnnotation(vmi)
if err != nil {
return nil, err
Expand Down
119 changes: 64 additions & 55 deletions pkg/virt-controller/services/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ var _ = Describe("Template", func() {

})
Context("launch template with correct parameters", func() {
table.DescribeTable("should check annotations", func(vmiAnnotation, podExpectedAnnotation map[string]string) {
table.DescribeTable("should contain tested annotations", func(vmiAnnotation, podExpectedAnnotation map[string]string) {
config, kvInformer, svc = configFactory(defaultArch)
pod, err := svc.RenderLaunchManifest(&v1.VirtualMachineInstance{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -242,46 +242,17 @@ var _ = Describe("Template", func() {
})

Expect(err).ToNot(HaveOccurred())
Expect(pod.ObjectMeta.Annotations).To(Equal(podExpectedAnnotation))
for key, expectedValue := range podExpectedAnnotation {
Expect(pod.ObjectMeta.Annotations).To(HaveKeyWithValue(key, expectedValue))
}

},
table.Entry("and don't contain kubectl annotation",
map[string]string{
"kubectl.kubernetes.io/last-applied-configuration": "open",
},
map[string]string{
"kubevirt.io/domain": "testvmi",
"pre.hook.backup.velero.io/container": "compute",
"pre.hook.backup.velero.io/command": "[\"/usr/bin/virt-freezer\", \"--freeze\", \"--name\", \"testvmi\", \"--namespace\", \"testns\"]",
"post.hook.backup.velero.io/container": "compute",
"post.hook.backup.velero.io/command": "[\"/usr/bin/virt-freezer\", \"--unfreeze\", \"--name\", \"testvmi\", \"--namespace\", \"testns\"]",
"kubevirt.io/migrationTransportUnix": "true",
},
),
table.Entry("and don't contain kubevirt annotation added by apiserver",
map[string]string{
"kubevirt.io/latest-observed-api-version": "source",
"kubevirt.io/storage-observed-api-version": ".com",
},
map[string]string{
"kubevirt.io/domain": "testvmi",
"pre.hook.backup.velero.io/container": "compute",
"pre.hook.backup.velero.io/command": "[\"/usr/bin/virt-freezer\", \"--freeze\", \"--name\", \"testvmi\", \"--namespace\", \"testns\"]",
"post.hook.backup.velero.io/container": "compute",
"post.hook.backup.velero.io/command": "[\"/usr/bin/virt-freezer\", \"--unfreeze\", \"--name\", \"testvmi\", \"--namespace\", \"testns\"]",
"kubevirt.io/migrationTransportUnix": "true",
},
),
table.Entry("and contain kubevirt domain annotation",
map[string]string{
"kubevirt.io/domain": "fedora",
},
map[string]string{
"kubevirt.io/domain": "fedora",
"pre.hook.backup.velero.io/container": "compute",
"pre.hook.backup.velero.io/command": "[\"/usr/bin/virt-freezer\", \"--freeze\", \"--name\", \"testvmi\", \"--namespace\", \"testns\"]",
"post.hook.backup.velero.io/container": "compute",
"post.hook.backup.velero.io/command": "[\"/usr/bin/virt-freezer\", \"--unfreeze\", \"--name\", \"testvmi\", \"--namespace\", \"testns\"]",
"kubevirt.io/migrationTransportUnix": "true",
"kubevirt.io/domain": "fedora",
},
),
table.Entry("and contain kubernetes annotation",
Expand All @@ -290,12 +261,6 @@ var _ = Describe("Template", func() {
},
map[string]string{
"cluster-autoscaler.kubernetes.io/safe-to-evict": "true",
"kubevirt.io/domain": "testvmi",
"pre.hook.backup.velero.io/container": "compute",
"pre.hook.backup.velero.io/command": "[\"/usr/bin/virt-freezer\", \"--freeze\", \"--name\", \"testvmi\", \"--namespace\", \"testns\"]",
"post.hook.backup.velero.io/container": "compute",
"post.hook.backup.velero.io/command": "[\"/usr/bin/virt-freezer\", \"--unfreeze\", \"--name\", \"testvmi\", \"--namespace\", \"testns\"]",
"kubevirt.io/migrationTransportUnix": "true",
},
),
table.Entry("and contain kubevirt ignitiondata annotation",
Expand All @@ -307,17 +272,60 @@ var _ = Describe("Template", func() {
}`,
},
map[string]string{
"kubevirt.io/domain": "testvmi",
"kubevirt.io/ignitiondata": `{
"ignition" : {
"version": "3"
},
}`,
"pre.hook.backup.velero.io/container": "compute",
"pre.hook.backup.velero.io/command": "[\"/usr/bin/virt-freezer\", \"--freeze\", \"--name\", \"testvmi\", \"--namespace\", \"testns\"]",
"post.hook.backup.velero.io/container": "compute",
"post.hook.backup.velero.io/command": "[\"/usr/bin/virt-freezer\", \"--unfreeze\", \"--name\", \"testvmi\", \"--namespace\", \"testns\"]",
"kubevirt.io/migrationTransportUnix": "true",
},
),
table.Entry("and contain default container for logging and exec",
map[string]string{},
map[string]string{
"kubectl.kubernetes.io/default-container": "compute",
},
),
)

table.DescribeTable("should not contain tested annotations", func(vmiAnnotation, podExpectedAnnotation map[string]string) {
config, kvInformer, svc = configFactory(defaultArch)
pod, err := svc.RenderLaunchManifest(&v1.VirtualMachineInstance{
ObjectMeta: metav1.ObjectMeta{
Name: "testvmi",
Namespace: "testns",
UID: "1234",
Annotations: vmiAnnotation,
},
Spec: v1.VirtualMachineInstanceSpec{
Domain: v1.DomainSpec{
Devices: v1.Devices{
DisableHotplug: true,
},
},
},
})
Expect(err).ToNot(HaveOccurred())

for key := range podExpectedAnnotation {
Expect(pod.ObjectMeta.Annotations).To(Not(HaveKey(key)))
}
},
table.Entry("and don't contain kubectl annotation",
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: s/don't/not/

map[string]string{
"kubectl.kubernetes.io/last-applied-configuration": "open",
},
map[string]string{
"kubectl.kubernetes.io/last-applied-configuration": "open",
},
),
table.Entry("and don't contain kubevirt annotation added by apiserver",
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: s/don't/not/

Copy link
Contributor

Choose a reason for hiding this comment

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

Note: these are really minor spelling observations, it's ok if they don't get fixed.
Let's have someone else review and LGTM this PR.

map[string]string{
"kubevirt.io/latest-observed-api-version": "source",
"kubevirt.io/storage-observed-api-version": ".com",
},
map[string]string{
"kubevirt.io/latest-observed-api-version": "source",
"kubevirt.io/storage-observed-api-version": ".com",
},
),
)
Expand Down Expand Up @@ -355,14 +363,15 @@ var _ = Describe("Template", func() {
v1.VirtualMachineNameLabel: "testvmi",
}))
Expect(pod.ObjectMeta.Annotations).To(Equal(map[string]string{
v1.DomainAnnotation: "testvmi",
"test": "shouldBeInPod",
hooks.HookSidecarListAnnotationName: `[{"image": "some-image:v1", "imagePullPolicy": "IfNotPresent"}]`,
"pre.hook.backup.velero.io/container": "compute",
"pre.hook.backup.velero.io/command": "[\"/usr/bin/virt-freezer\", \"--freeze\", \"--name\", \"testvmi\", \"--namespace\", \"testns\"]",
"post.hook.backup.velero.io/container": "compute",
"post.hook.backup.velero.io/command": "[\"/usr/bin/virt-freezer\", \"--unfreeze\", \"--name\", \"testvmi\", \"--namespace\", \"testns\"]",
"kubevirt.io/migrationTransportUnix": "true",
v1.DomainAnnotation: "testvmi",
"test": "shouldBeInPod",
hooks.HookSidecarListAnnotationName: `[{"image": "some-image:v1", "imagePullPolicy": "IfNotPresent"}]`,
"pre.hook.backup.velero.io/container": "compute",
"pre.hook.backup.velero.io/command": "[\"/usr/bin/virt-freezer\", \"--freeze\", \"--name\", \"testvmi\", \"--namespace\", \"testns\"]",
"post.hook.backup.velero.io/container": "compute",
"post.hook.backup.velero.io/command": "[\"/usr/bin/virt-freezer\", \"--unfreeze\", \"--name\", \"testvmi\", \"--namespace\", \"testns\"]",
"kubevirt.io/migrationTransportUnix": "true",
"kubectl.kubernetes.io/default-container": "compute",
}))
Expect(pod.ObjectMeta.OwnerReferences).To(Equal([]metav1.OwnerReference{{
APIVersion: v1.VirtualMachineInstanceGroupVersionKind.GroupVersion().String(),
Expand Down
23 changes: 23 additions & 0 deletions tests/kubectl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"strings"
"time"

"kubevirt.io/kubevirt/tests/libvmi"

"kubevirt.io/kubevirt/tests/console"
cd "kubevirt.io/kubevirt/tests/containerdisk"

Expand Down Expand Up @@ -205,4 +207,25 @@ var _ = Describe("[sig-compute]oc/kubectl integration", func() {
})
})

Describe("oc/kubectl logs", func() {
var (
vm *v1.VirtualMachineInstance
)

It("oc/kubectl logs <vmi-pod> return default container log", func() {
vm = libvmi.NewCirros()
vm = tests.RunVMIAndExpectLaunch(vm, 30)

k8sClient := tests.GetK8sCmdClient()

output, _, err := tests.RunCommand(k8sClient, "logs", tests.GetPodByVirtualMachineInstance(vm).Name)
Expect(err).NotTo(HaveOccurred())

Expect(output).To(ContainSubstring("component"))
Expect(output).To(ContainSubstring("level"))
Expect(output).To(ContainSubstring("msg"))
Expect(output).To(ContainSubstring("pos"))
Expect(output).To(ContainSubstring("timestamp"))
})
})
})