Coverage for ocp_resources/task_run.py: 0%

28 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2025-01-30 10:48 +0200

1# API reference: https://tekton.dev/docs/pipelines/taskruns/ 

2 

3from ocp_resources.resource import MissingRequiredArgumentError, NamespacedResource 

4 

5 

6class TaskRun(NamespacedResource): 

7 api_group = NamespacedResource.ApiGroup.TEKTON_DEV 

8 

9 def __init__( 

10 self, 

11 task_ref=None, 

12 task_spec=None, 

13 params=None, 

14 service_account_name=None, 

15 taskrun_timeout=None, 

16 **kwargs, 

17 ): 

18 """ 

19 Create and manage TaskRun which allows you to instantiate and execute a Task on cluster 

20 

21 Args: 

22 task_ref (str): Base task to run taskrun. Mandatory if task_spec is not provided. 

23 task_spec (str): Base task to run taskrun. Mandatory if task_ref is not provided. 

24 params (dict, optional): Params to add during triggering a run. 

25 params can be set/changed based on task_ref. 

26 example : params={"param_name1":"param_value1", "param_name2":"param_value2"} 

27 service_account_name (str, optional): Provide service account 

28 taskrun_timeout (str, optional): Specifies the taskrun_timeout before the taskrun fails 

29 """ 

30 super().__init__( 

31 **kwargs, 

32 ) 

33 self.task_ref = task_ref 

34 self.task_spec = task_spec 

35 self.params = params 

36 self.service_account_name = service_account_name 

37 self.taskrun_timeout = taskrun_timeout 

38 

39 def to_dict(self) -> None: 

40 super().to_dict() 

41 if not self.kind_dict and not self.yaml_file: 

42 if not (self.task_ref or self.task_spec): 

43 raise MissingRequiredArgumentError(argument="'task_ref' or 'task_spec'") 

44 

45 if self.task_ref and self.task_spec: 

46 raise ValueError("Validation failed: expected exactly one either task_ref or task_spec, got both") 

47 

48 self.res["spec"] = {} 

49 if self.task_ref: 

50 self.res["spec"]["taskRef"] = {"name": self.task_ref} 

51 

52 if self.task_spec: 

53 self.res["spec"]["taskSpec"] = {"name": self.task_spec} 

54 

55 if self.params: 

56 self.res["spec"]["params"] = [ 

57 {"name": params_name, "value": params_value} for params_name, params_value in self.params.items() 

58 ] 

59 

60 if self.taskrun_timeout: 

61 self.res["spec"]["taskrun_timeout"] = self.taskrun_timeout 

62 

63 if self.service_account_name: 

64 self.res["spec"]["serviceAccountName"] = self.service_account_name