k8s.io/kubernetes@v1.29.3/test/instrumentation/stability-utils.sh (about) 1 #!/usr/bin/env bash 2 3 # Copyright 2021 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 KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/../.. 22 source "${KUBE_ROOT}/hack/lib/init.sh" 23 source "${KUBE_ROOT}/hack/lib/util.sh" 24 25 stability_check_setup() { 26 kube::golang::verify_go_version 27 kube::util::ensure-temp-dir 28 cd "${KUBE_ROOT}" 29 kube::golang::setup_env 30 } 31 32 function find_files_to_check() { 33 # Similar to find but faster and easier to understand. We want to include 34 # modified and untracked files because this might be running against code 35 # which is not tracked by git yet. 36 git ls-files -cmo --exclude-standard \ 37 ':!:vendor/*' `# catches vendor/...` \ 38 ':!:*/vendor/*' `# catches any subdir/vendor/...` \ 39 ':!:third_party/*' `# catches third_party/...` \ 40 ':!:*/third_party/*' `# catches third_party/...` \ 41 ':!:hack/*' `# catches hack/...` \ 42 ':!:*/hack/*' `# catches any subdir/hack/...` \ 43 ':!:*/*_test.go' \ 44 ':!:test/instrumentation' \ 45 ':(glob)**/*.go' \ 46 "$@" 47 } 48 49 function find_test_files() { 50 git ls-files -cmo --exclude-standard \ 51 './test/instrumentation' \ 52 "$@" 53 } 54 55 red=$(tput setaf 1) 56 green=$(tput setaf 2) 57 reset=$(tput sgr0) 58 59 function kube::validate::stablemetrics() { 60 stability_check_setup 61 temp_file=$(mktemp) 62 temp_file2=$(mktemp) 63 doValidate=$(find_files_to_check -z \ 64 | sort -z \ 65 | KUBE_ROOT=${KUBE_ROOT} xargs -0 -L 200 \ 66 go run \ 67 "test/instrumentation/main.go" \ 68 "test/instrumentation/decode_metric.go" \ 69 "test/instrumentation/find_stable_metric.go" \ 70 "test/instrumentation/error.go" \ 71 "test/instrumentation/metric.go" \ 72 -- \ 73 1>"${temp_file}") 74 75 if $doValidate; then 76 echo -e "${green}Diffing test/instrumentation/testdata/stable-metrics-list.yaml\n${reset}" 77 fi 78 doSort=$(KUBE_ROOT=${KUBE_ROOT} go run "test/instrumentation/sort/main.go" --sort-file="${temp_file}" 1>"${temp_file2}") 79 if ! $doSort; then 80 echo "${red}!!! sorting metrics has failed! ${reset}" >&2 81 exit 1 82 fi 83 if diff -u "$KUBE_ROOT/test/instrumentation/testdata/stable-metrics-list.yaml" "$temp_file2"; then 84 echo -e "${green}\nPASS metrics stability verification ${reset}" 85 return 0 86 fi 87 echo "${red}!!! Metrics Stability static analysis has failed!${reset}" >&2 88 echo "${red}!!! Please run ./hack/update-generated-stable-metrics.sh to update the golden list.${reset}" >&2 89 exit 1 90 } 91 92 function kube::validate::test::stablemetrics() { 93 stability_check_setup 94 temp_file=$(mktemp) 95 doValidate=$(find_test_files -z \ 96 | sort -z \ 97 | KUBE_ROOT=${KUBE_ROOT} xargs -0 -L 200 \ 98 go run \ 99 "test/instrumentation/main.go" \ 100 "test/instrumentation/decode_metric.go" \ 101 "test/instrumentation/find_stable_metric.go" \ 102 "test/instrumentation/error.go" \ 103 "test/instrumentation/metric.go" \ 104 -- \ 105 1>"${temp_file}") 106 107 if $doValidate; then 108 echo -e "${green}Diffing test/instrumentation/testdata/test-stable-metrics-list.yaml\n${reset}" 109 if diff -u "$KUBE_ROOT/test/instrumentation/testdata/test-stable-metrics-list.yaml" "$temp_file"; then 110 echo -e "${green}\nPASS metrics stability verification ${reset}" 111 return 0 112 fi 113 fi 114 115 echo "${red}!!! Metrics stability static analysis test has failed!${reset}" >&2 116 echo "${red}!!! Please run './test/instrumentation/test-update.sh' to update the golden list.${reset}" >&2 117 exit 1 118 } 119 120 function kube::update::stablemetrics() { 121 stability_check_setup 122 temp_file=$(mktemp) 123 temp_file2=$(mktemp) 124 doCheckStability=$(find_files_to_check -z \ 125 | sort -z \ 126 | KUBE_ROOT=${KUBE_ROOT} xargs -0 -L 200 \ 127 go run \ 128 "test/instrumentation/main.go" \ 129 "test/instrumentation/decode_metric.go" \ 130 "test/instrumentation/find_stable_metric.go" \ 131 "test/instrumentation/error.go" \ 132 "test/instrumentation/metric.go" \ 133 -- \ 134 1>"${temp_file}") 135 136 if ! $doCheckStability; then 137 echo "${red}!!! updating golden list of metrics has failed! ${reset}" >&2 138 exit 1 139 fi 140 mv -f "$temp_file" "${KUBE_ROOT}/test/instrumentation/testdata/stable-metrics-list.yaml" 141 doSort=$(go run "test/instrumentation/sort/main.go" --sort-file="${KUBE_ROOT}/test/instrumentation/testdata/stable-metrics-list.yaml" 1>"${temp_file2}") 142 if ! $doSort; then 143 echo "${red}!!! sorting metrics has failed! ${reset}" >&2 144 exit 1 145 fi 146 mv -f "$temp_file2" "${KUBE_ROOT}/test/instrumentation/testdata/stable-metrics-list.yaml" 147 echo "${green}Updated golden list of stable metrics.${reset}" 148 } 149 150 function kube::update::documentation::list() { 151 stability_check_setup 152 temp_file=$(mktemp) 153 doCheckStability=$(find_files_to_check -z \ 154 | sort -z \ 155 | KUBE_ROOT=${KUBE_ROOT} xargs -0 -L 200 \ 156 go run \ 157 "test/instrumentation/main.go" \ 158 "test/instrumentation/decode_metric.go" \ 159 "test/instrumentation/find_stable_metric.go" \ 160 "test/instrumentation/error.go" \ 161 "test/instrumentation/metric.go" \ 162 --allstabilityclasses \ 163 -- \ 164 1>"${temp_file}") 165 166 if ! $doCheckStability; then 167 echo "${red}!!! updating golden list of metrics has failed! ${reset}" >&2 168 exit 1 169 fi 170 mv -f "$temp_file" "${KUBE_ROOT}/test/instrumentation/documentation/documentation-list.yaml" 171 echo "${green}Updated list of metrics for documentation ${reset}" 172 } 173 174 function kube::update::documentation() { 175 stability_check_setup 176 temp_file=$(mktemp) 177 arg1=$1 178 arg2=$2 179 doUpdateDocs=$(go run "test/instrumentation/documentation/main.go" --major "$arg1" --minor "$arg2" -- 1>"${temp_file}") 180 if ! $doUpdateDocs; then 181 echo "${red}!!! updating documentation has failed! ${reset}" >&2 182 exit 1 183 fi 184 mv -f "$temp_file" "${KUBE_ROOT}/test/instrumentation/documentation/documentation.md" 185 echo "${green}Updated documentation of metrics.${reset}" 186 } 187 188 function kube::update::test::stablemetrics() { 189 stability_check_setup 190 temp_file=$(mktemp) 191 doCheckStability=$(find_test_files -z \ 192 | sort -z \ 193 | KUBE_ROOT=${KUBE_ROOT} xargs -0 -L 200 \ 194 go run \ 195 "test/instrumentation/main.go" \ 196 "test/instrumentation/decode_metric.go" \ 197 "test/instrumentation/find_stable_metric.go" \ 198 "test/instrumentation/error.go" \ 199 "test/instrumentation/metric.go" \ 200 -- \ 201 1>"${temp_file}") 202 203 if ! $doCheckStability; then 204 echo "${red}!!! updating golden list of test metrics has failed! ${reset}" >&2 205 exit 1 206 fi 207 mv -f "$temp_file" "${KUBE_ROOT}/test/instrumentation/testdata/test-stable-metrics-list.yaml" 208 echo "${green}Updated test list of stable metrics.${reset}" 209 }