|
6 | 6 | from sebs.benchmark import Benchmark |
7 | 7 | from sebs.faas.function import Function, FunctionConfig, Runtime |
8 | 8 | from sebs.storage.config import MinioConfig |
9 | | -from sebs.knative.trigger import LibraryTrigger, HTTPTrigger |
10 | | - |
11 | 9 |
|
12 | 10 | @dataclass |
13 | 11 | class KnativeFunctionConfig(FunctionConfig): |
14 | | - """ |
15 | | - Configuration class for Knative function specific configurations. |
16 | | -
|
17 | | - Attributes: |
18 | | - docker_image (str): Docker image for the function. |
19 | | - namespace (str): Kubernetes namespace where the function is deployed (default is 'default'). |
20 | | - storage (Optional[MinioConfig]): Optional MinioConfig object for storage configuration. |
21 | | - """ |
22 | | - |
23 | 12 | docker_image: str = "" |
24 | 13 | namespace: str = "default" |
25 | 14 | storage: Optional[MinioConfig] = None |
| 15 | + url: str = "" |
26 | 16 |
|
27 | 17 | @staticmethod |
28 | 18 | def deserialize(data: dict) -> KnativeFunctionConfig: |
29 | | - """ |
30 | | - Deserialize data from dictionary into KnativeFunctionConfig object. |
31 | | -
|
32 | | - Args: |
33 | | - data (dict): Dictionary containing serialized data. |
34 | | -
|
35 | | - Returns: |
36 | | - KnativeFunctionConfig: Deserialized KnativeFunctionConfig object. |
37 | | - """ |
38 | 19 | keys = list(KnativeFunctionConfig.__dataclass_fields__.keys()) |
39 | 20 | data = {k: v for k, v in data.items() if k in keys} |
40 | 21 | data["runtime"] = Runtime.deserialize(data["runtime"]) |
41 | | - if "storage" in data: |
42 | | - data["storage"] = MinioConfig.deserialize(data["storage"]) |
| 22 | + data["storage"] = MinioConfig.deserialize(data["storage"]) |
43 | 23 | return KnativeFunctionConfig(**data) |
44 | 24 |
|
45 | 25 | def serialize(self) -> dict: |
46 | | - """ |
47 | | - Serialize KnativeFunctionConfig object into dictionary. |
48 | | -
|
49 | | - Returns: |
50 | | - dict: Dictionary containing serialized data. |
51 | | - """ |
52 | 26 | return self.__dict__ |
53 | 27 |
|
54 | 28 | @staticmethod |
55 | 29 | def from_benchmark(benchmark: Benchmark) -> KnativeFunctionConfig: |
56 | | - """ |
57 | | - Create KnativeFunctionConfig object from a benchmark. |
58 | | -
|
59 | | - Args: |
60 | | - benchmark (Benchmark): Benchmark object. |
61 | | -
|
62 | | - Returns: |
63 | | - KnativeFunctionConfig: Initialized KnativeFunctionConfig object. |
64 | | - """ |
65 | 30 | return super(KnativeFunctionConfig, KnativeFunctionConfig)._from_benchmark( |
66 | 31 | benchmark, KnativeFunctionConfig |
67 | 32 | ) |
68 | 33 |
|
69 | | - |
70 | 34 | class KnativeFunction(Function): |
71 | | - """ |
72 | | - Class representing a Knative function. |
73 | | -
|
74 | | - Attributes: |
75 | | - name (str): Name of the function. |
76 | | - benchmark (str): Benchmark associated with the function. |
77 | | - code_package_hash (str): Hash of the code package associated with the function. |
78 | | - cfg (KnativeFunctionConfig): Configuration object for the function. |
79 | | - """ |
80 | | - |
81 | 35 | def __init__( |
82 | 36 | self, name: str, benchmark: str, code_package_hash: str, cfg: KnativeFunctionConfig |
83 | 37 | ): |
84 | | - """ |
85 | | - Initialize KnativeFunction object. |
86 | | -
|
87 | | - Args: |
88 | | - name (str): Name of the function. |
89 | | - benchmark (str): Benchmark associated with the function. |
90 | | - code_package_hash (str): Hash of the code package associated with the function. |
91 | | - cfg (KnativeFunctionConfig): Configuration object for the function. |
92 | | - """ |
93 | 38 | super().__init__(benchmark, name, code_package_hash, cfg) |
94 | 39 |
|
95 | 40 | @property |
96 | 41 | def config(self) -> KnativeFunctionConfig: |
97 | | - """ |
98 | | - Get the configuration object of the function. |
99 | | -
|
100 | | - Returns: |
101 | | - KnativeFunctionConfig: Configuration object of the function. |
102 | | - """ |
103 | 42 | return cast(KnativeFunctionConfig, self._cfg) |
104 | 43 |
|
105 | 44 | @staticmethod |
106 | 45 | def typename() -> str: |
107 | | - """ |
108 | | - Return the typename of the KnativeFunction class. |
109 | | -
|
110 | | - Returns: |
111 | | - str: Typename of the KnativeFunction class. |
112 | | - """ |
113 | 46 | return "Knative.Function" |
114 | 47 |
|
115 | 48 | def serialize(self) -> dict: |
116 | | - """ |
117 | | - Serialize KnativeFunction object into dictionary. |
118 | | -
|
119 | | - Returns: |
120 | | - dict: Dictionary containing serialized data. |
121 | | - """ |
122 | | - serialized_data = super().serialize() |
123 | | - serialized_data["config"] = self._cfg.serialize() |
124 | | - return serialized_data |
| 49 | + return {**super().serialize(), "config": self._cfg.serialize()} |
125 | 50 |
|
126 | 51 | @staticmethod |
127 | 52 | def deserialize(cached_config: dict) -> KnativeFunction: |
128 | | - """ |
129 | | - Deserialize dictionary into KnativeFunction object. |
130 | | -
|
131 | | - Args: |
132 | | - cached_config (dict): Dictionary containing serialized data. |
| 53 | + from sebs.faas.function import Trigger |
| 54 | + from sebs.knative.triggers import KnativeLibraryTrigger, KnativeHTTPTrigger |
133 | 55 |
|
134 | | - Returns: |
135 | | - KnativeFunction: Deserialized KnativeFunction object. |
136 | | - """ |
137 | 56 | cfg = KnativeFunctionConfig.deserialize(cached_config["config"]) |
138 | 57 | ret = KnativeFunction( |
139 | 58 | cached_config["name"], cached_config["benchmark"], cached_config["hash"], cfg |
140 | 59 | ) |
141 | 60 | for trigger in cached_config["triggers"]: |
142 | 61 | trigger_type = cast( |
143 | 62 | Trigger, |
144 | | - {"Library": LibraryTrigger, "HTTP": HTTPTrigger}.get(trigger["type"]), |
| 63 | + {"Library": KnativeLibraryTrigger, "HTTP": KnativeHTTPTrigger}.get(trigger["type"]), |
145 | 64 | ) |
146 | 65 | assert trigger_type, "Unknown trigger type {}".format(trigger["type"]) |
147 | 66 | ret.add_trigger(trigger_type.deserialize(trigger)) |
|
0 commit comments