|
1 | | -from typing import List, Self, Union |
| 1 | +from typing import Any, List, Union |
2 | 2 |
|
3 | 3 | from kubernetes import client |
4 | 4 |
|
5 | 5 | from transpire.resources.base import Resource |
| 6 | +from transpire.resources.podspec import PodSpec |
6 | 7 |
|
7 | 8 |
|
8 | 9 | class Deployment(Resource[client.V1Deployment]): |
@@ -45,98 +46,12 @@ def __init__( |
45 | 46 | ), |
46 | 47 | ) |
47 | 48 | super().__init__() |
48 | | - |
49 | | - def _init_env(self, container_id: int) -> client.V1Container: |
50 | | - container = self.obj.spec.template.spec.containers[container_id] |
51 | | - |
52 | | - if container.env_from is None: |
53 | | - container.env_from = [] |
54 | | - if container.env is None: |
55 | | - container.env = [] |
56 | | - |
57 | | - return container |
58 | | - |
59 | | - def with_configmap_env( |
60 | | - self, name: str, *, mapping: dict[str, str] | None = None, container_id: int = 0 |
61 | | - ) -> Self: |
62 | | - container = self._init_env(container_id) |
63 | | - if mapping is None: |
64 | | - container.env_from.append( |
65 | | - client.V1EnvFromSource( |
66 | | - config_map_ref=client.V1ConfigMapEnvSource( |
67 | | - name=name, |
68 | | - ) |
69 | | - ) |
70 | | - ) |
71 | | - else: |
72 | | - container.env.extend( |
73 | | - client.V1EnvVar( |
74 | | - name=envvar_name, |
75 | | - value_from=client.V1EnvVarSource( |
76 | | - config_map_key_ref=client.V1ConfigMapKeySelector( |
77 | | - name=name, |
78 | | - key=cm_key, |
79 | | - ) |
80 | | - ), |
81 | | - ) |
82 | | - for envvar_name, cm_key in mapping.items() |
83 | | - ) |
84 | | - return self |
85 | | - |
86 | | - def with_secrets_env( |
87 | | - self, name: str, *, mapping: dict[str, str] | None = None, container_id: int = 0 |
88 | | - ) -> Self: |
89 | | - container = self._init_env(container_id) |
90 | | - if mapping is None: |
91 | | - container.env_from.append( |
92 | | - client.V1EnvFromSource( |
93 | | - secret_ref=client.V1SecretEnvSource( |
94 | | - name=name, |
95 | | - ) |
96 | | - ) |
97 | | - ) |
98 | | - else: |
99 | | - container.env.extend( |
100 | | - client.V1EnvVar( |
101 | | - name=envvar_name, |
102 | | - value_from=client.V1EnvVarSource( |
103 | | - secret_key_ref=client.V1SecretKeySelector( |
104 | | - name=name, |
105 | | - key=secret_key, |
106 | | - ) |
107 | | - ), |
108 | | - ) |
109 | | - for envvar_name, secret_key in mapping.items() |
110 | | - ) |
111 | | - return self |
112 | | - |
113 | | - def get_container( |
114 | | - self, name: str | None = None, *, remove: bool = False |
115 | | - ) -> client.V1Container: |
116 | | - container_list = self.obj.spec.template.spec.containers |
117 | | - |
118 | | - if name is None: |
119 | | - if len(container_list) != 1: |
120 | | - raise ValueError("If multiple containers, must pass name.") |
121 | | - return container_list[0] |
122 | | - |
123 | | - for i, container in enumerate(container_list): |
124 | | - if container.name == name: |
125 | | - if remove: |
126 | | - return container_list.pop(i) |
127 | | - return container |
128 | | - |
129 | | - raise ValueError(f"No such container: {name}") |
130 | | - |
131 | | - def add_container(self, container: client.V1Container) -> int: |
132 | | - container_list = self.obj.spec.template.spec.containers |
133 | | - |
134 | | - names = set(c.name for c in container_list) |
135 | | - if container.name in names: |
136 | | - raise ValueError(f"Can't use name {container.name}, already in use.") |
137 | | - |
138 | | - container_list.append(container) |
139 | | - return len(container_list) - 1 |
| 49 | + |
| 50 | + def pod_spec(self) -> PodSpec: |
| 51 | + return PodSpec(self.obj.spec.template.spec) |
140 | 52 |
|
141 | 53 | def get_selector(self) -> dict[str, str]: |
142 | 54 | return {self.SELECTOR_LABEL: self.obj.metadata.name} |
| 55 | + |
| 56 | + def __getattr__(self, name: str) -> Any: |
| 57 | + return getattr(self.pod_spec(), name) |
0 commit comments