4444# A regex for splitting a container id and runtime
4545_CID_RE = re .compile ("^(.+)://(.+)$" )
4646
47- # endpoints used by the agent for querying the k8s api. Having this mapping allows
48- # us to avoid special casing the logic for each different object type. We can just
49- # look up the appropriate endpoint in this dict and query objects however we need.
47+ # Endpoints used for querying the Kubernetes api
5048#
51- # The dict is keyed by object kind, and or each object kind, there are 3 endpoints:
52- # single, list and list all.
49+ # Keyed by object kind with the following endpoint specifiers:
50+ # - `single` is for querying a single object of a specific kind
51+ # - Requires substitutions for ${namespace} and ${name}
52+ # - `list` is for querying all objects of a given kind in a specific namespace
53+ # - Requires substitution for ${namespace}
54+ # - `list-all` is for querying all objects of a given kind in the entire cluster
5355#
54- # `single` is for querying a single object of a specific type
55- # `list` is for querying all objects of a given type in a specific namespace
56- # `list-all` is for querying all objects of a given type in the entire cluster
56+ # Each endpoint specifier may be a list and are attempted in order until successful.
57+ # The use case is to support changes, say promotions from /v1beta1 to /v1 and similar.
5758#
58- # the `single` and `list` endpoints are Templates that require the caller to substitute
59- # in the appropriate values for ${namespace} and ${name}.
60- #
61- # For each key we also allow values to be a list. This way multiple URLs can be specific. We will
62- # first try with the first URL with the list and then continue with the next one in case it returns
63- # 404.
64- # The use case here is to support URL paths which have changed, e.g. API endpoint which has been
65- # promoted from /v1beta to /v1 and similar. Keep in mind that this is not a full blown robust
66- # solution and not a replacement for proper API version management which is a larger and a longer
67- # term project. It's a short term workaround / compromise to handle some of those scenarios where
68- # an API endpoint has been promoted from beta to stable.
59+ # TODO Implement proper Kubernetes api version management
6960_OBJECT_ENDPOINTS = {
7061 "CronJob" : {
71- "single" : [
72- Template ("/apis/batch/v1beta1/namespaces/${namespace}/cronjobs/${name}" ),
73- # In Kubernetes v1.25.0 this API endpoint has been promoted from v1beta to v1
74- Template ("/apis/batch/v1/namespaces/${namespace}/cronjobs/${name}" ),
75- ],
76- "list" : [
77- Template ("/apis/batch/v1beta1/namespaces/${namespace}/cronjobs" ),
78- Template ("/apis/batch/v1/namespaces/${namespace}/cronjobs" ),
79- ],
80- "list-all" : [
81- "/apis/batch/v1beta1/cronjobs" ,
82- "/apis/batch/v1/cronjobs" ,
83- ],
62+ "single" : Template ("/apis/batch/v1/namespaces/${namespace}/cronjobs/${name}" ),
63+ "list" : Template ("/apis/batch/v1/namespaces/${namespace}/cronjobs" ),
64+ "list-all" : "/apis/batch/v1/cronjobs" ,
8465 },
8566 "DaemonSet" : {
8667 "single" : Template ("/apis/apps/v1/namespaces/${namespace}/daemonsets/${name}" ),
@@ -1962,9 +1943,6 @@ def create_instance(
19621943 "token_file" : global_config .k8s_service_account_token ,
19631944 "namespace_file" : global_config .k8s_service_account_namespace ,
19641945 "token_re_read_interval" : global_config .k8s_token_re_read_interval ,
1965- "enable_fallback_urls" : not (
1966- global_config .k8s_fallback_urls_disable
1967- ),
19681946 }
19691947 )
19701948 return KubernetesApi (** kwargs )
@@ -1985,7 +1963,6 @@ def __init__(
19851963 token_file = "/var/run/secrets/kubernetes.io/serviceaccount/token" ,
19861964 namespace_file = "/var/run/secrets/kubernetes.io/serviceaccount/namespace" ,
19871965 token_re_read_interval = 300 ,
1988- enable_fallback_urls = True ,
19891966 ):
19901967 """Init the kubernetes object"""
19911968 self .log_api_responses = log_api_responses
@@ -2005,7 +1982,6 @@ def __init__(
20051982 self ._ca_file = ca_file
20061983 self ._token_file = token_file
20071984 self ._token_re_read_interval = int (token_re_read_interval )
2008- self ._enable_fallback_urls = enable_fallback_urls
20091985
20101986 # We create a few headers ahead of time so that we don't have to recreate them each time we need them.
20111987 self ._standard_headers = {
@@ -2491,15 +2467,6 @@ def query_object(self, kind, namespace, name, query_options=None):
24912467 if not isinstance (object_endpoints , list ):
24922468 object_endpoints = [object_endpoints ]
24932469
2494- if not self ._enable_fallback_urls :
2495- global_log .info (
2496- "k8s_fallback_urls_disable option is set to true. Won't fall back to "
2497- "the next URL in the list if API returns 404 response" ,
2498- limit_once_per_x_secs = 3600 ,
2499- limit_key = "k8s_get_object_fallback_url_disabled" ,
2500- )
2501- object_endpoints = object_endpoints [:1 ]
2502-
25032470 object_endpoints_count = len (object_endpoints )
25042471
25052472 for index , object_endpoint in enumerate (object_endpoints ):
@@ -2565,15 +2532,6 @@ def query_objects(self, kind, namespace=None, filter=None):
25652532 if not isinstance (objects_endpoints , list ):
25662533 objects_endpoints = [objects_endpoints ]
25672534
2568- if not self ._enable_fallback_urls :
2569- global_log .info (
2570- "k8s_fallback_urls_disable option is set to true. Won't fall back to "
2571- "the next URL in the list if API returns 404 response" ,
2572- limit_once_per_x_secs = 3600 ,
2573- limit_key = "k8s_list_objects_fallback_url_disabled" ,
2574- )
2575- objects_endpoints = objects_endpoints [:1 ]
2576-
25772535 objects_endpoints_count = len (objects_endpoints )
25782536
25792537 for index , objects_endpoint in enumerate (objects_endpoints ):
0 commit comments