3434from clusterfuzz ._internal .datastore import feature_flags
3535from clusterfuzz ._internal .datastore import ndb_utils
3636from clusterfuzz ._internal .metrics import logs
37- from clusterfuzz ._internal .remote_task import types
37+ from clusterfuzz ._internal .remote_task import remote_task_types
3838from clusterfuzz ._internal .system import environment
3939
4040CLUSTER_NAME = 'clusterfuzz-cronjobs-gke'
5050])
5151
5252
53- def _get_config_names (remote_tasks : typing .List [types .RemoteTask ]):
53+ def _get_config_names (remote_tasks : typing .List [remote_task_types .RemoteTask ]):
5454 """"Gets the name of the configs for each batch_task. Returns a dict
5555
5656 that is indexed by command and job_type for efficient lookup."""
@@ -62,7 +62,7 @@ def _get_config_names(remote_tasks: typing.List[types.RemoteTask]):
6262 config_map = {}
6363 for task in remote_tasks :
6464 if task .job_type not in job_map :
65- logs .error (f' { task .job_type } doesn\ ' t exist.' )
65+ logs .error (f" { task .job_type } doesn't exist." )
6666 continue
6767 if task .command == 'fuzz' :
6868 suffix = '-PREEMPTIBLE-UNPRIVILEGED'
@@ -88,13 +88,13 @@ def _get_config_names(remote_tasks: typing.List[types.RemoteTask]):
8888
8989
9090def _get_k8s_job_configs (
91- remote_tasks : typing .List [types .RemoteTask ]
91+ remote_tasks : typing .List [remote_task_types .RemoteTask ]
9292) -> typing .Dict [typing .Tuple [str , str ], KubernetesJobConfig ]:
9393 """Gets the configured specifications for a batch workload."""
9494
9595 if not remote_tasks :
9696 return {}
97- #TODO(javanlacerda): Create remote task config
97+ # TODO(javanlacerda): Create remote task config
9898 batch_config = local_config .BatchConfig ()
9999 config_map = _get_config_names (remote_tasks )
100100 configs = {}
@@ -162,7 +162,7 @@ def _create_job_body(config: KubernetesJobConfig, input_url: str,
162162 return yaml .safe_load (rendered_spec )
163163
164164
165- class KubernetesService (types .RemoteTaskInterface ):
165+ class KubernetesService (remote_task_types .RemoteTaskInterface ):
166166 """A remote task execution client for Kubernetes."""
167167
168168 def __init__ (self , k8s_config_loaded : bool = False ):
@@ -178,7 +178,7 @@ def _load_gke_credentials(self):
178178 credentials , _ = google .auth .default ()
179179 project = utils .get_application_id ()
180180 service = discovery .build ('container' , 'v1' , credentials = credentials )
181- parent = f' projects/{ project } /locations/-'
181+ parent = f" projects/{ project } /locations/-"
182182
183183 try :
184184 # pylint: disable=no-member
@@ -188,12 +188,12 @@ def _load_gke_credentials(self):
188188 cluster = next ((c for c in clusters if c ['name' ] == CLUSTER_NAME ), None )
189189
190190 if not cluster :
191- logs .error (f' Cluster { CLUSTER_NAME } not found in project { project } .' )
192- print (f' DEBUG: Cluster { CLUSTER_NAME } not found in project { project } .' )
191+ logs .error (f" Cluster { CLUSTER_NAME } not found in project { project } ." )
192+ print (f" DEBUG: Cluster { CLUSTER_NAME } not found in project { project } ." )
193193 return
194194
195195 except Exception as e :
196- logs .error (f' Failed to list clusters in { project } : { e } ' )
196+ logs .error (f" Failed to list clusters in { project } : { e } " )
197197 return
198198
199199 endpoint = cluster ['endpoint' ]
@@ -214,13 +214,13 @@ def get_token(creds):
214214 request = google_requests .Request ()
215215 if not creds .valid or creds .expired :
216216 creds .refresh (request )
217- return {' authorization' : ' Bearer ' + creds .token }
217+ return {" authorization" : " Bearer " + creds .token }
218218
219219 configuration .refresh_api_key_hook = lambda _ : get_token (credentials )
220220 configuration .api_key = get_token (credentials )
221221
222222 k8s_client .Configuration .set_default (configuration )
223- logs .info (' GKE credentials loaded successfully.' )
223+ logs .info (" GKE credentials loaded successfully." )
224224
225225 def _create_service_account_if_needed (self ,
226226 service_account_email : str ) -> str :
@@ -266,36 +266,39 @@ def _get_pending_jobs_count(self) -> int:
266266 namespace = 'default' ,
267267 label_selector = 'app.kubernetes.io/name=clusterfuzz-kata-job' ,
268268 field_selector = 'status.phase=Pending' )
269- logs .info (f' Found { len (pods .items )} pending jobs.' )
269+ logs .info (f" Found { len (pods .items )} pending jobs." )
270270 return len (pods .items )
271271 except Exception as e :
272- logs .error (f' Failed to list pods: { e } ' )
272+ logs .error (f" Failed to list pods: { e } " )
273273 return 0
274274
275275 def create_utask_main_job (self , module : str , job_type : str ,
276276 input_download_url : str ):
277277 """Creates a single batch job for a uworker main task."""
278278
279279 command = task_utils .get_command_from_module (module )
280- batch_tasks = [types .RemoteTask (command , job_type , input_download_url )]
280+ batch_tasks = [
281+ remote_task_types .RemoteTask (command , job_type , input_download_url )
282+ ]
281283 result = self .create_utask_main_jobs (batch_tasks )
282284
283285 if result is None :
284286 return result
285287 return result [0 ]
286288
287- def create_utask_main_jobs (self , remote_tasks : typing .List [types .RemoteTask ]):
289+ def create_utask_main_jobs (
290+ self , remote_tasks : typing .List [remote_task_types .RemoteTask ]):
288291 """Creates a batch job for a list of uworker main tasks.
289292
290293 This method groups the tasks by their workload specification and creates a
291294 separate batch job for each group. This allows tasks with similar
292295 requirements to be processed together, which can improve efficiency.
293296 """
294- if feature_flags . FeatureFlags . K8S_PENDING_JOBS_LIMITER . enabled and \
295- feature_flags .FeatureFlags .K8S_PENDING_JOBS_LIMITER .content is not None \
296- and self . _get_pending_jobs_count () >= int ( feature_flags
297- . FeatureFlags .
298- K8S_PENDING_JOBS_LIMITER . content ):
297+ limit = None
298+ if feature_flags .FeatureFlags .K8S_PENDING_JOBS_LIMITER .enabled :
299+ limit = feature_flags . FeatureFlags . K8S_PENDING_JOBS_LIMITER . content
300+
301+ if limit is not None and self . _get_pending_jobs_count () >= int ( limit ):
299302 logs .warning (
300303 f'Kubernetes job limit reached. Not acking { len (remote_tasks )} tasks.'
301304 )
@@ -310,11 +313,10 @@ def create_utask_main_jobs(self, remote_tasks: typing.List[types.RemoteTask]):
310313 logs .info (f'Scheduling { remote_task .command } , { remote_task .job_type } .' )
311314 config = configs [(remote_task .command , remote_task .job_type )]
312315 job_specs [config ].append (remote_task .input_download_url )
313- logs .info ('Creating Kubernetes jobs.' )
316+ logs .info ('Creating batch jobs.' )
314317 jobs = []
315- logs .info ('Batching k8s utask_mains.' )
318+ logs .info ('Batching utask_mains.' )
316319 for config , input_urls in job_specs .items ():
317- # TODO(javanlacerda): Batch multiple tasks into a single job.
318320 for input_url in input_urls :
319321 jobs .append (self .create_job (config , input_url ))
320322
0 commit comments