Parses the KUBERNETES_EXEC_INFO environment variable and returns the API version. If the environment variable is malformed or invalid, return the v1beta1 response and print a message to stderr. If the v1alpha1 API is specified explicitly, a message is printed to
(self)
| 157 | return 0 |
| 158 | |
| 159 | def discover_api_version(self): |
| 160 | """ |
| 161 | Parses the KUBERNETES_EXEC_INFO environment variable and returns the |
| 162 | API version. If the environment variable is malformed or invalid, |
| 163 | return the v1beta1 response and print a message to stderr. |
| 164 | |
| 165 | If the v1alpha1 API is specified explicitly, a message is printed to |
| 166 | stderr with instructions to update. |
| 167 | |
| 168 | :return: The client authentication API version |
| 169 | :rtype: string |
| 170 | """ |
| 171 | # At the time Kubernetes v1.29 is released upstream (approx Dec 2023), |
| 172 | # "v1beta1" will be removed. At or around that time, EKS will likely |
| 173 | # support v1.22 through v1.28, in which client API version "v1beta1" |
| 174 | # will be supported by all EKS versions. |
| 175 | fallback_api_version = BETA_API |
| 176 | |
| 177 | error_prefixes = { |
| 178 | "error": "Error parsing", |
| 179 | "empty": "Empty", |
| 180 | } |
| 181 | |
| 182 | exec_info_raw = os.environ.get("KUBERNETES_EXEC_INFO", "") |
| 183 | if not exec_info_raw: |
| 184 | # All kube clients should be setting this, but client-go clients |
| 185 | # (kubectl, kubelet, etc) < 1.20 were not setting this if the API |
| 186 | # version defined in the kubeconfig was not v1alpha1. |
| 187 | # |
| 188 | # This was changed in kubernetes/kubernetes#95489 so that |
| 189 | # KUBERNETES_EXEC_INFO is always provided |
| 190 | return fallback_api_version |
| 191 | try: |
| 192 | exec_info = json.loads(exec_info_raw) |
| 193 | except json.JSONDecodeError: |
| 194 | # The environment variable was malformed |
| 195 | uni_print( |
| 196 | ERROR_MSG_TPL.format( |
| 197 | error_prefixes["error"], |
| 198 | fallback_api_version, |
| 199 | ), |
| 200 | sys.stderr, |
| 201 | ) |
| 202 | uni_print("\n", sys.stderr) |
| 203 | return fallback_api_version |
| 204 | |
| 205 | api_version_raw = exec_info.get("apiVersion") |
| 206 | if api_version_raw in FULLY_SUPPORTED_API_VERSIONS: |
| 207 | return api_version_raw |
| 208 | elif api_version_raw in DEPRECATED_API_VERSIONS: |
| 209 | uni_print(DEPRECATION_MSG_TPL.format(api_version_raw), sys.stderr) |
| 210 | uni_print("\n", sys.stderr) |
| 211 | return api_version_raw |
| 212 | else: |
| 213 | uni_print( |
| 214 | UNRECOGNIZED_MSG_TPL.format(fallback_api_version), |
| 215 | sys.stderr, |
| 216 | ) |