sysctl.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. "context"
  16. "fmt"
  17. "github.com/onsi/ginkgo"
  18. "k8s.io/api/core/v1"
  19. apierrors "k8s.io/apimachinery/pkg/api/errors"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. "k8s.io/apimachinery/pkg/util/uuid"
  22. "k8s.io/kubernetes/pkg/kubelet/sysctl"
  23. "k8s.io/kubernetes/test/e2e/framework"
  24. imageutils "k8s.io/kubernetes/test/utils/image"
  25. )
  26. // SysctlUpgradeTest tests that a pod with sysctls runs before and after an upgrade. During
  27. // a master upgrade, the exact pod is expected to stay running. A pod with unsafe sysctls is
  28. // expected to keep failing before and after the upgrade.
  29. type SysctlUpgradeTest struct {
  30. validPod *v1.Pod
  31. invalidPod *v1.Pod
  32. }
  33. // Setup creates two pods: one with safe sysctls, one with unsafe sysctls. It checks that the former
  34. // launched and the later is rejected.
  35. func (t *SysctlUpgradeTest) Setup(f *framework.Framework) {
  36. t.validPod = t.verifySafeSysctlWork(f)
  37. t.invalidPod = t.verifyUnsafeSysctlsAreRejected(f)
  38. }
  39. // Test waits for the upgrade to complete, and then verifies that a
  40. // pod can still consume the ConfigMap.
  41. func (t *SysctlUpgradeTest) Test(f *framework.Framework, done <-chan struct{}, upgrade UpgradeType) {
  42. <-done
  43. switch upgrade {
  44. case MasterUpgrade, ClusterUpgrade:
  45. ginkgo.By("Checking the safe sysctl pod keeps running on master upgrade")
  46. pod, err := f.ClientSet.CoreV1().Pods(t.validPod.Namespace).Get(context.TODO(), t.validPod.Name, metav1.GetOptions{})
  47. framework.ExpectNoError(err)
  48. framework.ExpectEqual(pod.Status.Phase, v1.PodRunning)
  49. }
  50. ginkgo.By("Checking the old unsafe sysctl pod was not suddenly started during an upgrade")
  51. pod, err := f.ClientSet.CoreV1().Pods(t.invalidPod.Namespace).Get(context.TODO(), t.invalidPod.Name, metav1.GetOptions{})
  52. if err != nil && !apierrors.IsNotFound(err) {
  53. framework.ExpectNoError(err)
  54. }
  55. if err == nil {
  56. framework.ExpectNotEqual(pod.Status.Phase, v1.PodRunning)
  57. }
  58. t.verifySafeSysctlWork(f)
  59. t.verifyUnsafeSysctlsAreRejected(f)
  60. }
  61. // Teardown cleans up any remaining resources.
  62. func (t *SysctlUpgradeTest) Teardown(f *framework.Framework) {
  63. // rely on the namespace deletion to clean up everything
  64. }
  65. func (t *SysctlUpgradeTest) verifySafeSysctlWork(f *framework.Framework) *v1.Pod {
  66. ginkgo.By("Creating a pod with safe sysctls")
  67. safeSysctl := "net.ipv4.ip_local_port_range"
  68. safeSysctlValue := "1024 1042"
  69. sysctlTestPod("valid-sysctls", map[string]string{safeSysctl: safeSysctlValue})
  70. validPod := f.PodClient().Create(t.validPod)
  71. ginkgo.By("Making sure the valid pod launches")
  72. _, err := f.PodClient().WaitForErrorEventOrSuccess(t.validPod)
  73. framework.ExpectNoError(err)
  74. f.TestContainerOutput("pod with safe sysctl launched", t.validPod, 0, []string{fmt.Sprintf("%s = %s", safeSysctl, safeSysctlValue)})
  75. return validPod
  76. }
  77. func (t *SysctlUpgradeTest) verifyUnsafeSysctlsAreRejected(f *framework.Framework) *v1.Pod {
  78. ginkgo.By("Creating a pod with unsafe sysctls")
  79. invalidPod := sysctlTestPod("valid-sysctls-"+string(uuid.NewUUID()), map[string]string{
  80. "fs.mount-max": "1000000",
  81. })
  82. invalidPod = f.PodClient().Create(invalidPod)
  83. ginkgo.By("Making sure the invalid pod failed")
  84. ev, err := f.PodClient().WaitForErrorEventOrSuccess(invalidPod)
  85. framework.ExpectNoError(err)
  86. framework.ExpectEqual(ev.Reason, sysctl.ForbiddenReason)
  87. return invalidPod
  88. }
  89. func sysctlTestPod(name string, sysctls map[string]string) *v1.Pod {
  90. sysctlList := []v1.Sysctl{}
  91. keys := []string{}
  92. for k, v := range sysctls {
  93. sysctlList = append(sysctlList, v1.Sysctl{Name: k, Value: v})
  94. keys = append(keys, k)
  95. }
  96. return &v1.Pod{
  97. ObjectMeta: metav1.ObjectMeta{
  98. Name: name,
  99. },
  100. Spec: v1.PodSpec{
  101. Containers: []v1.Container{
  102. {
  103. Name: "test-container",
  104. Image: imageutils.GetE2EImage(imageutils.BusyBox),
  105. Command: append([]string{"/bin/sysctl"}, keys...),
  106. },
  107. },
  108. RestartPolicy: v1.RestartPolicyNever,
  109. SecurityContext: &v1.PodSecurityContext{
  110. Sysctls: sysctlList,
  111. },
  112. },
  113. }
  114. }