7171 KubeletApiException ,
7272 K8sApiTemporaryError ,
7373 K8sConfigBuilder ,
74- K8sNamespaceFilter ,
75- KubernetesCache ,
74+ K8sNamespaceFilter , KubernetesCache ,
7675)
7776from scalyr_agent .monitor_utils .k8s import (
7877 K8sApiPermanentError ,
297296 "DEPRECATED." ,
298297 convert_to = six .text_type ,
299298 default = "https://kubernetes.default" ,
300- allow_http = False ,
299+ allow_http = False
301300)
302301
303302define_config_option (
425424 convert_to = six .text_type ,
426425 default = "https://${host_ip}:10250" ,
427426 env_aware = True ,
428- allow_http = False ,
427+ allow_http = False
429428)
430429
431430define_config_option (
@@ -2353,6 +2352,7 @@ def _get_containers(
23532352 if not k8s_include_by_default :
23542353 continue
23552354
2355+
23562356 # add this container to the list of results
23572357 result [cid ] = {
23582358 "name" : cname ,
@@ -2534,7 +2534,6 @@ class DockerLog(object):
25342534 """
25352535 Represents a list of log configs for a container
25362536 """
2537-
25382537 def __init__ (self , cid , stream , log_configs ):
25392538 assert len (log_configs ) > 0 , "log_configs must not be empty"
25402539 self .__cid = cid
@@ -2670,7 +2669,7 @@ def __init__(
26702669 "k8s_cache_init_abort_delay"
26712670 )
26722671
2673- self .k8s_cache = None # type: Optional[KubernetesCache]
2672+ self .k8s_cache = None # type: Optional[KubernetesCache]
26742673 self .__k8s_config_builder = None
26752674
26762675 self .__node_name = None
@@ -2686,8 +2685,8 @@ def __init__(
26862685 self .__controlled_warmer = controlled_warmer
26872686
26882687 # give this an initial empty value
2689- self .raw_logs = [] # type: List[DockerLog]
2690- self .docker_logs = [] # type: List[DockerLog]
2688+ self .raw_logs = [] # type: List[DockerLog]
2689+ self .docker_logs = [] # type: List[DockerLog]
26912690
26922691 self .__stopped = False
26932692
@@ -3067,11 +3066,7 @@ def check_containers(self, run_state):
30673066 limit_key = "check-container-pod-info-%s" % cid ,
30683067 )
30693068
3070- namespace = self .k8s_cache .namespace (
3071- pod_namespace ,
3072- current_time = current_time ,
3073- allow_expired = False ,
3074- )
3069+ namespace = self .k8s_cache .namespace (pod_namespace , current_time = current_time , allow_expired = False )
30753070 # start the container if have a container that wasn't running
30763071 if cid not in self .containers :
30773072 self ._logger .log (
@@ -3082,14 +3077,10 @@ def check_containers(self, run_state):
30823077 elif cid in prev_digests or cid in prev_digests_namespaces :
30833078 # container was running and it exists in the previous digest dict, so see if
30843079 # it has changed
3085- if (pod and prev_digests [cid ] != pod .digest ) or (
3086- namespace
3087- and prev_digests_namespaces [cid ] != namespace .digest
3088- ):
3080+ if (pod and prev_digests [cid ] != pod .digest ) or (namespace and prev_digests_namespaces [cid ] != namespace .digest ):
30893081 self ._logger .log (
30903082 scalyr_logging .DEBUG_LEVEL_1 ,
3091- "Pod or namespace digest changed for '%s/%s'"
3092- % (namespace .name , info ["name" ]),
3083+ "Pod or namespace digest changed for '%s/%s'" % (namespace .name , info ["name" ]),
30933084 )
30943085 changed [cid ] = info
30953086
@@ -3137,12 +3128,8 @@ def check_containers(self, run_state):
31373128 "updating config for '%s'" % info ["name" ],
31383129 )
31393130
3140- logger .log_configs = (
3141- self .__log_watcher .update_log_configs_on_path (
3142- info ["log_path" ],
3143- self .__module .module_name ,
3144- new_configs ,
3145- )
3131+ logger .log_configs = self .__log_watcher .update_log_configs_on_path (
3132+ info ["log_path" ], self .__module .module_name , new_configs
31463133 )
31473134 else :
31483135 self ._logger .log (
@@ -3207,11 +3194,9 @@ def __start_docker_logs(self, docker_logs):
32073194 if self .__log_watcher :
32083195 updated_log_configs = []
32093196 for log_config in log .log_configs :
3210- updated_log_configs .append (
3211- self .__log_watcher .add_log_config (
3212- self .__module .module_name , log_config , force_add = True
3213- )
3214- )
3197+ updated_log_configs .append (self .__log_watcher .add_log_config (
3198+ self .__module .module_name , log_config , force_add = True
3199+ ))
32153200 log .log_configs = updated_log_configs
32163201
32173202 self .raw_logs .append (log )
@@ -3525,47 +3510,26 @@ def __get_log_config_for_container(self, cid, info, k8s_cache, base_attributes):
35253510
35263511 self ._logger .log (
35273512 scalyr_logging .DEBUG_LEVEL_0 ,
3528- "log_config_for_container Checking for teams in annotations for container %s(%s), pod %s, namespace %s. Container annotations = %s, Pod annotations = %s, Namespace annotations = %s "
3529- % (
3530- info ["name" ],
3531- short_cid ,
3532- pod_name ,
3533- pod_namespace ,
3534- container_annotations ,
3535- all_annotations ,
3536- namespace_annotations ,
3537- ),
3513+ "log_config_for_container Checking for teams in annotations for container %s(%s), pod %s, namespace %s. Container annotations = %s, Pod annotations = %s, Namespace annotations = %s " \
3514+ % (info ["name" ], short_cid , pod_name , pod_namespace , container_annotations , all_annotations , namespace_annotations )
35383515 )
35393516
35403517 if container_annotations or all_annotations or namespace_annotations :
3541- api_keys = self .__container_api_keys_from_annotations (
3542- k8s_cache ,
3543- pod_namespace ,
3544- container_annotations ,
3545- "container" ,
3546- info ["name" ],
3547- )
3518+ api_keys = self .__container_api_keys_from_annotations (k8s_cache , pod_namespace , container_annotations , "container" , info ["name" ])
35483519 if not api_keys :
3549- api_keys = self .__container_api_keys_from_annotations (
3550- k8s_cache , pod_namespace , all_annotations , "pod" , info ["name" ]
3551- )
3520+ api_keys = self .__container_api_keys_from_annotations (k8s_cache , pod_namespace , all_annotations , "pod" , info ["name" ])
35523521 if not api_keys :
3553- api_keys = self .__container_api_keys_from_annotations (
3554- k8s_cache ,
3555- pod_namespace ,
3556- namespace_annotations ,
3557- "namespace" ,
3558- info ["name" ],
3559- )
3522+ api_keys = self .__container_api_keys_from_annotations (k8s_cache , pod_namespace , namespace_annotations , "namespace" , info ["name" ])
35603523 if api_keys :
35613524 # Multiple matching api keys will result in multiple log configs, which will differ in the api_key field only.
3562- results = [{** result , "api_key" : api_key } for api_key in api_keys ]
3525+ results = [
3526+ {** result , "api_key" : api_key }
3527+ for api_key in api_keys
3528+ ]
35633529
35643530 return results
35653531
3566- def __container_api_keys_from_annotations (
3567- self , k8s_cache , namespace , annotations , annotation_kind , container_name
3568- ):
3532+ def __container_api_keys_from_annotations (self , k8s_cache , namespace , annotations , annotation_kind , container_name ):
35693533 def fetch_secret (name ):
35703534 if not name :
35713535 return None
@@ -3582,45 +3546,48 @@ def fetch_secret(name):
35823546 )
35833547
35843548 self ._logger .warning (
3585- "Failed to fetch secret '%s/%s', ignoring." % (namespace , name ),
3549+ "Failed to fetch secret '%s/%s', ignoring."
3550+ % (namespace , name ),
35863551 limit_once_per_x_secs = 300 ,
35873552 limit_key = "k8s-fetch-secret-%s/%s" % (namespace , name ),
35883553 )
35893554
35903555 def get_secret_api_key (secret ):
35913556 if not secret :
35923557 return None
3593- api_key = base64 .b64decode (secret .data .get ("scalyr-api-key" )).decode (
3594- "utf-8"
3595- )
3558+ api_key = base64 .b64decode (secret .data .get ("scalyr-api-key" )).decode ("utf-8" )
35963559
35973560 if not api_key :
35983561 self ._logger .warning (
35993562 "Secret '%s/%s' does not contain a scalyr-api-key field, ingoring."
36003563 % (namespace , secret .name ),
36013564 limit_once_per_x_secs = 300 ,
3602- limit_key = "k8s-fetch-secret-%s" % secret .name ,
3565+ limit_key = "k8s-fetch-secret-%s" % secret .name
36033566 )
36043567
36053568 return api_key
36063569
3607- secrets_names = [team .get ("secret" ) for team in annotations .get ("teams" , [])]
3570+ secrets_names = [
3571+ team .get ("secret" )
3572+ for team in annotations .get ("teams" , [])
3573+ ]
36083574
3609- secrets = [secret for secret in map (fetch_secret , secrets_names ) if secret ]
3575+ secrets = [
3576+ secret
3577+ for secret in map (fetch_secret , secrets_names )
3578+ if secret
3579+ ]
36103580
3611- api_keys = [api_key for api_key in map (get_secret_api_key , secrets ) if api_key ]
3581+ api_keys = [
3582+ api_key
3583+ for api_key in map (get_secret_api_key , secrets )
3584+ if api_key
3585+ ]
36123586
36133587 self ._logger .log (
36143588 scalyr_logging .DEBUG_LEVEL_0 ,
3615- "log_config_for_container From %s annotations of container %s and namespace %s, got %d non-empty api keys, %d secrets for secret names %s"
3616- % (
3617- annotation_kind ,
3618- container_name ,
3619- namespace ,
3620- len (api_keys ),
3621- len (secrets ),
3622- "," .join (secrets_names ),
3623- ),
3589+ "log_config_for_container From %s annotations of container %s and namespace %s, got %d non-empty api keys, %d secrets for secret names %s" \
3590+ % (annotation_kind , container_name , namespace , len (api_keys ), len (secrets ), "," .join (secrets_names ))
36243591 )
36253592
36263593 return api_keys
0 commit comments