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