apparmor.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package upgrades
  14. import (
  15. api "k8s.io/api/core/v1"
  16. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  17. "k8s.io/kubernetes/test/e2e/common"
  18. "k8s.io/kubernetes/test/e2e/framework"
  19. e2elog "k8s.io/kubernetes/test/e2e/framework/log"
  20. "github.com/onsi/ginkgo"
  21. "github.com/onsi/gomega"
  22. "github.com/onsi/gomega/gstruct"
  23. )
  24. // AppArmorUpgradeTest tests that AppArmor profiles are enforced & usable across upgrades.
  25. type AppArmorUpgradeTest struct {
  26. pod *api.Pod
  27. }
  28. // Name returns the tracking name of the test.
  29. func (AppArmorUpgradeTest) Name() string { return "apparmor-upgrade" }
  30. // Skip returns true when this test can be skipped.
  31. func (AppArmorUpgradeTest) Skip(upgCtx UpgradeContext) bool {
  32. supportedImages := make(map[string]bool)
  33. for _, d := range common.AppArmorDistros {
  34. supportedImages[d] = true
  35. }
  36. for _, vCtx := range upgCtx.Versions {
  37. if !supportedImages[vCtx.NodeImage] {
  38. return true
  39. }
  40. }
  41. return false
  42. }
  43. // Setup creates a secret and then verifies that a pod can consume it.
  44. func (t *AppArmorUpgradeTest) Setup(f *framework.Framework) {
  45. ginkgo.By("Loading AppArmor profiles to nodes")
  46. common.LoadAppArmorProfiles(f)
  47. // Create the initial test pod.
  48. ginkgo.By("Creating a long-running AppArmor enabled pod.")
  49. t.pod = common.CreateAppArmorTestPod(f, false, false)
  50. // Verify initial state.
  51. t.verifyNodesAppArmorEnabled(f)
  52. t.verifyNewPodSucceeds(f)
  53. }
  54. // Test waits for the upgrade to complete, and then verifies that a
  55. // pod can still consume the secret.
  56. func (t *AppArmorUpgradeTest) Test(f *framework.Framework, done <-chan struct{}, upgrade UpgradeType) {
  57. <-done
  58. if upgrade == MasterUpgrade {
  59. t.verifyPodStillUp(f)
  60. }
  61. t.verifyNodesAppArmorEnabled(f)
  62. t.verifyNewPodSucceeds(f)
  63. }
  64. // Teardown cleans up any remaining resources.
  65. func (t *AppArmorUpgradeTest) Teardown(f *framework.Framework) {
  66. // rely on the namespace deletion to clean up everything
  67. ginkgo.By("Logging container failures")
  68. framework.LogFailedContainers(f.ClientSet, f.Namespace.Name, e2elog.Logf)
  69. }
  70. func (t *AppArmorUpgradeTest) verifyPodStillUp(f *framework.Framework) {
  71. ginkgo.By("Verifying an AppArmor profile is continuously enforced for a pod")
  72. pod, err := f.PodClient().Get(t.pod.Name, metav1.GetOptions{})
  73. framework.ExpectNoError(err, "Should be able to get pod")
  74. gomega.Expect(pod.Status.Phase).To(gomega.Equal(api.PodRunning), "Pod should stay running")
  75. gomega.Expect(pod.Status.ContainerStatuses[0].State.Running).NotTo(gomega.BeNil(), "Container should be running")
  76. gomega.Expect(pod.Status.ContainerStatuses[0].RestartCount).To(gomega.BeZero(), "Container should not need to be restarted")
  77. }
  78. func (t *AppArmorUpgradeTest) verifyNewPodSucceeds(f *framework.Framework) {
  79. ginkgo.By("Verifying an AppArmor profile is enforced for a new pod")
  80. common.CreateAppArmorTestPod(f, false, true)
  81. }
  82. func (t *AppArmorUpgradeTest) verifyNodesAppArmorEnabled(f *framework.Framework) {
  83. ginkgo.By("Verifying nodes are AppArmor enabled")
  84. nodes, err := f.ClientSet.CoreV1().Nodes().List(metav1.ListOptions{})
  85. framework.ExpectNoError(err, "Failed to list nodes")
  86. for _, node := range nodes.Items {
  87. gomega.Expect(node.Status.Conditions).To(gstruct.MatchElements(conditionType, gstruct.IgnoreExtras, gstruct.Elements{
  88. "Ready": gstruct.MatchFields(gstruct.IgnoreExtras, gstruct.Fields{
  89. "Message": gomega.ContainSubstring("AppArmor enabled"),
  90. }),
  91. }))
  92. }
  93. }
  94. func conditionType(condition interface{}) string {
  95. return string(condition.(api.NodeCondition).Type)
  96. }