save-config.sh 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #!/usr/bin/env bash
  2. # Copyright 2018 The Kubernetes Authors.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. set -o errexit
  16. set -o nounset
  17. set -o pipefail
  18. # Runs tests for --save-config tests.
  19. run_save_config_tests() {
  20. set -o nounset
  21. set -o errexit
  22. kube::log::status "Testing kubectl --save-config"
  23. ## Configuration annotations should be set when --save-config is enabled
  24. ## 1. kubectl create --save-config should generate configuration annotation
  25. # Pre-Condition: no POD exists
  26. create_and_use_new_namespace
  27. kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" ''
  28. # Command: create a pod "test-pod"
  29. kubectl create -f hack/testdata/pod.yaml --save-config "${kube_flags[@]}"
  30. # Post-Condition: pod "test-pod" has configuration annotation
  31. [[ "$(kubectl get pods test-pod -o yaml "${kube_flags[@]}" | grep kubectl.kubernetes.io/last-applied-configuration)" ]]
  32. # Clean up
  33. kubectl delete -f hack/testdata/pod.yaml "${kube_flags[@]}"
  34. ## 2. kubectl edit --save-config should generate configuration annotation
  35. # Pre-Condition: no POD exists, then create pod "test-pod", which shouldn't have configuration annotation
  36. create_and_use_new_namespace
  37. kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" ''
  38. kubectl create -f hack/testdata/pod.yaml "${kube_flags[@]}"
  39. ! [[ "$(kubectl get pods test-pod -o yaml "${kube_flags[@]}" | grep kubectl.kubernetes.io/last-applied-configuration)" ]]
  40. # Command: edit the pod "test-pod"
  41. temp_editor="${KUBE_TEMP}/tmp-editor.sh"
  42. echo -e "#!/usr/bin/env bash\n${SED} -i \"s/test-pod-label/test-pod-label-edited/g\" \$@" > "${temp_editor}"
  43. chmod +x "${temp_editor}"
  44. EDITOR=${temp_editor} kubectl edit pod test-pod --save-config "${kube_flags[@]}"
  45. # Post-Condition: pod "test-pod" has configuration annotation
  46. [[ "$(kubectl get pods test-pod -o yaml "${kube_flags[@]}" | grep kubectl.kubernetes.io/last-applied-configuration)" ]]
  47. # Clean up
  48. kubectl delete -f hack/testdata/pod.yaml "${kube_flags[@]}"
  49. ## 3. kubectl replace --save-config should generate configuration annotation
  50. # Pre-Condition: no POD exists, then create pod "test-pod", which shouldn't have configuration annotation
  51. create_and_use_new_namespace
  52. kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" ''
  53. kubectl create -f hack/testdata/pod.yaml "${kube_flags[@]}"
  54. ! [[ "$(kubectl get pods test-pod -o yaml "${kube_flags[@]}" | grep kubectl.kubernetes.io/last-applied-configuration)" ]]
  55. # Command: replace the pod "test-pod"
  56. kubectl replace -f hack/testdata/pod.yaml --save-config "${kube_flags[@]}"
  57. # Post-Condition: pod "test-pod" has configuration annotation
  58. [[ "$(kubectl get pods test-pod -o yaml "${kube_flags[@]}" | grep kubectl.kubernetes.io/last-applied-configuration)" ]]
  59. # Clean up
  60. kubectl delete -f hack/testdata/pod.yaml "${kube_flags[@]}"
  61. ## 4. kubectl run --save-config should generate configuration annotation
  62. # Pre-Condition: no RC exists
  63. kube::test::get_object_assert rc "{{range.items}}{{$id_field}}:{{end}}" ''
  64. # Command: create the rc "nginx" with image nginx
  65. kubectl run nginx "--image=$IMAGE_NGINX" --save-config --generator=run/v1 "${kube_flags[@]}"
  66. # Post-Condition: rc "nginx" has configuration annotation
  67. [[ "$(kubectl get rc nginx -o yaml "${kube_flags[@]}" | grep kubectl.kubernetes.io/last-applied-configuration)" ]]
  68. ## 5. kubectl expose --save-config should generate configuration annotation
  69. # Pre-Condition: no service exists
  70. kube::test::get_object_assert svc "{{range.items}}{{$id_field}}:{{end}}" ''
  71. # Command: expose the rc "nginx"
  72. kubectl expose rc nginx --save-config --port=80 --target-port=8000 "${kube_flags[@]}"
  73. # Post-Condition: service "nginx" has configuration annotation
  74. [[ "$(kubectl get svc nginx -o yaml "${kube_flags[@]}" | grep kubectl.kubernetes.io/last-applied-configuration)" ]]
  75. # Clean up
  76. kubectl delete rc,svc nginx
  77. ## 6. kubectl autoscale --save-config should generate configuration annotation
  78. # Pre-Condition: no RC exists, then create the rc "frontend", which shouldn't have configuration annotation
  79. kube::test::get_object_assert rc "{{range.items}}{{$id_field}}:{{end}}" ''
  80. kubectl create -f hack/testdata/frontend-controller.yaml "${kube_flags[@]}"
  81. ! [[ "$(kubectl get rc frontend -o yaml "${kube_flags[@]}" | grep kubectl.kubernetes.io/last-applied-configuration)" ]]
  82. # Command: autoscale rc "frontend"
  83. kubectl autoscale -f hack/testdata/frontend-controller.yaml --save-config "${kube_flags[@]}" --max=2
  84. # Post-Condition: hpa "frontend" has configuration annotation
  85. [[ "$(kubectl get hpa frontend -o yaml "${kube_flags[@]}" | grep kubectl.kubernetes.io/last-applied-configuration)" ]]
  86. # Ensure we can interact with HPA objects in lists through autoscaling/v1 APIs
  87. output_message=$(kubectl get hpa -o=jsonpath='{.items[0].apiVersion}' 2>&1 "${kube_flags[@]}")
  88. kube::test::if_has_string "${output_message}" 'autoscaling/v1'
  89. output_message=$(kubectl get hpa.autoscaling -o=jsonpath='{.items[0].apiVersion}' 2>&1 "${kube_flags[@]}")
  90. kube::test::if_has_string "${output_message}" 'autoscaling/v1'
  91. # tests kubectl group prefix matching
  92. output_message=$(kubectl get hpa.autoscal -o=jsonpath='{.items[0].apiVersion}' 2>&1 "${kube_flags[@]}")
  93. kube::test::if_has_string "${output_message}" 'autoscaling/v1'
  94. # Clean up
  95. # Note that we should delete hpa first, otherwise it may fight with the rc reaper.
  96. kubectl delete hpa frontend "${kube_flags[@]}"
  97. kubectl delete rc frontend "${kube_flags[@]}"
  98. set +o nounset
  99. set +o errexit
  100. }