Debian Codegraph @

magnum_capi_helm/kubernetes.py

raw
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
12
13 import base64
14 import copy
15 import os
16 import pathlib
17 import re
18 import tempfile
19 import yaml
20
21 from oslo_log import log as logging
22 import requests
23
24 from magnum_capi_helm import conf
25
26
27 LOG = logging.getLogger(__name__)
28 CONF = conf.CONF
29
30
31 def ensure_file_cert(obj, file_key):
32 """Returns the path and cleanup requirements of cert.
33
34 Returns a tuple containing the path to a file with the requesteddata,
35 and whether the file must be cleaned up.
36
37 First check if there is a file path already,
38 if the data is there, put it in a file,
39 remember for cleanup and return the created file.
40 """
41 if file_key in obj:
42 return obj[file_key], False
43
44 data_key = file_key + "-data"
45 if data_key in obj:
46 # NOTE(dalees): The created file may contain private key material
47 # but is owned by the current user and mode 0600.
48 # See also: tempfile.mkstemp
49 # In Python 3.12, cleanup may be improved with
50 # delete=True,delete_on_close=False
51 with tempfile.NamedTemporaryFile(delete=False) as fd:
52 fd.write(base64.standard_b64decode(obj[data_key]))
53 return fd.name, True
54 return None, False
55
56
57 class Client(requests.Session):
58 """Object for producing Kubernetes clients."""
59
60 KUBECONFIG_ENV_NAME = "KUBECONFIG"
61
62 def __init__(self, kubeconfig):
63 super().__init__()
64 self._tempfiles = []
65 cluster, user = self._get_cluster_and_user(kubeconfig)
66
67 self.server = cluster["server"].rstrip("/")
68 ca_file, cleanup_file = ensure_file_cert(
69 cluster, "certificate-authority"
70 )
71 if cleanup_file and ca_file:
72 self._tempfiles.append(ca_file)
73 if ca_file:
74 self.verify = ca_file
75
76 # convert certs into files as required by requests
77 # https://requests.readthedocs.io/en/latest/api/#requests.Session.cert
78 client_cert, cleanup_file = ensure_file_cert(
79 user, "client-certificate"
80 )
81 if cleanup_file and client_cert:
82 self._tempfiles.append(client_cert)
83 assert client_cert is not None
84 client_key, cleanup_file = ensure_file_cert(user, "client-key")
85 if cleanup_file and client_key:
86 self._tempfiles.append(client_key)
87 assert client_key is not None
88 self.cert = (client_cert, client_key)
89
90 def __del__(self):
91 # Remove any temporary certificate files this class owns.
92 for file_path in self._tempfiles:
93 try:
94 os.remove(file_path)
95 except (OSError, FileNotFoundError):
96 pass
97 self._tempfiles = []
98
99 def _get_cluster_and_user(self, kubeconfig):
100 # get the context
101 current_context = kubeconfig["current-context"]
102 context = [
103 c["context"]
104 for c in kubeconfig["contexts"]
105 if c["name"] == current_context
106 ][0]
107 # extract cluster and user from context
108 cluster = [
109 c["cluster"]
110 for c in kubeconfig["clusters"]
111 if c["name"] == context["cluster"]
112 ][0]
113 user = [
114 u["user"]
115 for u in kubeconfig["users"]
116 if u["name"] == context["user"]
117 ][0]
118 return cluster, user
119
120 @classmethod
121 def _get_kubeconfig_path(cls):
122 # use config if specified
123 if CONF.capi_helm.kubeconfig_file:
124 return CONF.capi_helm.kubeconfig_file
125 if cls.KUBECONFIG_ENV_NAME in os.environ:
126 return os.environ[cls.KUBECONFIG_ENV_NAME]
127 # the default kubeconfig location
128 return pathlib.Path.home() / ".kube" / "config"
129
130 @classmethod
131 def _load_kubeconfig(cls, path):
132 with open(path) as fd:
133 return yaml.safe_load(fd)
134
135 @classmethod
136 def load(cls):
137 path = cls._get_kubeconfig_path()
138 kubeconfig = cls._load_kubeconfig(path)
139 return Client(kubeconfig)
140
141 def request(self, method, url, *args, **kwargs):
142 # Make sure to add the server to any relative URLs
143 if re.match(r"^http(s)://", url) is None:
144 url = "{}{}".format(self.server, url)
145 response = super().request(method, url, *args, **kwargs)
146 LOG.debug(
147 'Kubernetes API request: "%s %s" %s',
148 method,
149 url,
150 response.status_code,
151 )
152 return response
153
154 def ensure_namespace(self, namespace):
155 Namespace(self).apply(namespace)
156
157 def apply_secret(self, secret_name, data, namespace):
158 Secret(self).apply(secret_name, data, namespace)
159
160 def delete_all_secrets_by_label(self, label, value, namespace):
161 Secret(self).delete_all_by_label(label, value, namespace)
162
163 def get_capi_cluster(self, name, namespace):
164 return Cluster(self).fetch(name, namespace)
165
166 def get_capi_openstackcluster(self, name, namespace):
167 return OpenstackCluster(self).fetch(name, namespace)
168
169 def get_kubeadm_control_plane(self, name, namespace):
170 return KubeadmControlPlane(self).fetch(name, namespace)
171
172 def get_machine_deployment(self, name, namespace):
173 return MachineDeployment(self).fetch(name, namespace)
174
175 def get_manifests_by_label(self, labels, namespace):
176 return list(Manifests(self).fetch_all_by_label(labels, namespace))
177
178 def get_helm_releases_by_label(self, labels, namespace):
179 return list(HelmRelease(self).fetch_all_by_label(labels, namespace))
180
181 def get_addons_by_label(self, labels, namespace):
182 addons = list(self.get_manifests_by_label(labels, namespace))
183 addons.extend(self.get_helm_releases_by_label(labels, namespace))
184 return addons
185
186 def get_all_machines_by_label(self, labels, namespace):
187 return list(Machine(self).fetch_all_by_label(labels, namespace))
188
189
190 class Resource:
191 def __init__(self, client):
192 self.client = client
193 assert hasattr(self, "api_version")
194 self.kind = getattr(self, "kind", type(self).__name__)
195 self.plural_name = getattr(
196 self, "plural_name", self.kind.lower() + "s"
197 )
198 self.namespaced = getattr(self, "namespaced", True)
199
200 def prepare_path(self, name=None, namespace=None):
201 # Begin with either /api or /apis depending whether the api version
202 # is the core API
203 prefix = "/apis" if "/" in self.api_version else "/api"
204 # Include the namespace unless the resource is namespaced
205 path_namespace = f"/namespaces/{namespace}" if namespace else ""
206 # Include the resource name if given
207 path_name = f"/{name}" if name else ""
208 return (
209 f"{prefix}/{self.api_version}{path_namespace}/"
210 f"{self.plural_name}{path_name}"
211 )
212
213 def fetch(self, name, namespace=None):
214 """Fetches specified object from the target Kubernetes cluster.
215
216 If the object is not found, None is returned.
217 """
218 assert self.namespaced == bool(namespace)
219 assert name is not None
220 response = self.client.get(self.prepare_path(name, namespace))
221 if 200 <= response.status_code < 300:
222 return response.json()
223 elif response.status_code == 404:
224 return None
225 else:
226 response.raise_for_status()
227
228 def fetch_all_by_label(self, labels, namespace=None):
229 """Fetches objects matching the labels from the target cluster."""
230 assert self.namespaced == bool(namespace)
231 label_selector = ",".join(f"{k}={v}" for k, v in labels.items())
232 continue_token = ""
233 while True:
234 params = {"labelSelector": label_selector}
235 if continue_token:
236 params["continue"] = continue_token
237 response = self.client.get(
238 self.prepare_path(namespace=namespace), params=params
239 )
240 response.raise_for_status()
241 response_data = response.json()
242 yield from response_data["items"]
243 continue_token = response_data["metadata"]["continue"]
244 if not continue_token:
245 break
246
247 def apply(self, name, data=None, namespace=None):
248 """Applies the given object to the target Kubernetes cluster."""
249 assert self.namespaced == bool(namespace)
250 body_data = copy.deepcopy(data) if data else {}
251 body_data["apiVersion"] = self.api_version
252 body_data["kind"] = self.kind
253 body_data.setdefault("metadata", {})["name"] = name
254 if namespace:
255 body_data["metadata"]["namespace"] = namespace
256 response = self.client.patch(
257 self.prepare_path(name, namespace),
258 json=body_data,
259 headers={"Content-Type": "application/apply-patch+yaml"},
260 params={"fieldManager": "magnum", "force": "true"},
261 )
262 response.raise_for_status()
263 return response.json()
264
265 def delete_all_by_label(self, label, value, namespace=None):
266 """Deletes all objects with the specified label from cluster."""
267 assert self.namespaced == bool(namespace)
268 response = self.client.delete(
269 self.prepare_path(namespace=namespace),
270 params={"labelSelector": f"{label}={value}"},
271 )
272 response.raise_for_status()
273
274
275 class Namespace(Resource):
276 api_version = "v1"
277 namespaced = False
278
279
280 class Secret(Resource):
281 api_version = "v1"
282
283
284 class Cluster(Resource):
285 api_version = "cluster.x-k8s.io/v1beta1"
286
287
288 class OpenstackCluster(Resource):
289 api_version = "infrastructure.cluster.x-k8s.io/v1alpha6"
290
291
292 class MachineDeployment(Resource):
293 api_version = "cluster.x-k8s.io/v1beta1"
294
295
296 class KubeadmControlPlane(Resource):
297 api_version = "controlplane.cluster.x-k8s.io/v1beta1"
298
299
300 class Machine(Resource):
301 api_version = "cluster.x-k8s.io/v1beta1"
302
303
304 class Manifests(Resource):
305 api_version = "addons.stackhpc.com/v1alpha1"
306 plural_name = "manifests"
307
308
309 class HelmRelease(Resource):
310 api_version = "addons.stackhpc.com/v1alpha1"