ops.interface-kube-control 0.2.0__tar.gz → 0.3.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ops_interface_kube_control-0.3.0/MANIFEST.in +1 -0
- ops_interface_kube_control-0.3.0/PKG-INFO +43 -0
- ops_interface_kube_control-0.3.0/README.md +22 -0
- {ops_interface_kube_control-0.2.0 → ops_interface_kube_control-0.3.0}/ops/interface_kube_control/model.py +42 -27
- {ops_interface_kube_control-0.2.0 → ops_interface_kube_control-0.3.0}/ops/interface_kube_control/provides.py +25 -21
- {ops_interface_kube_control-0.2.0 → ops_interface_kube_control-0.3.0}/ops/interface_kube_control/requires.py +31 -31
- ops_interface_kube_control-0.3.0/ops.interface_kube_control.egg-info/PKG-INFO +43 -0
- {ops_interface_kube_control-0.2.0 → ops_interface_kube_control-0.3.0}/ops.interface_kube_control.egg-info/SOURCES.txt +3 -1
- ops_interface_kube_control-0.3.0/ops.interface_kube_control.egg-info/requires.txt +2 -0
- ops_interface_kube_control-0.3.0/ops.interface_kube_control.egg-info/top_level.txt +3 -0
- ops_interface_kube_control-0.3.0/pyproject.toml +80 -0
- ops_interface_kube_control-0.3.0/tox.ini +56 -0
- ops_interface_kube_control-0.3.0/uv.lock +722 -0
- ops_interface_kube_control-0.2.0/PKG-INFO +0 -27
- ops_interface_kube_control-0.2.0/README.md +0 -0
- ops_interface_kube_control-0.2.0/ops.interface_kube_control.egg-info/PKG-INFO +0 -27
- ops_interface_kube_control-0.2.0/ops.interface_kube_control.egg-info/requires.txt +0 -3
- ops_interface_kube_control-0.2.0/ops.interface_kube_control.egg-info/top_level.txt +0 -1
- ops_interface_kube_control-0.2.0/setup.py +0 -33
- ops_interface_kube_control-0.2.0/tox.ini +0 -35
- {ops_interface_kube_control-0.2.0 → ops_interface_kube_control-0.3.0}/ops/interface_kube_control/__init__.py +0 -0
- {ops_interface_kube_control-0.2.0 → ops_interface_kube_control-0.3.0}/ops.interface_kube_control.egg-info/dependency_links.txt +0 -0
- {ops_interface_kube_control-0.2.0 → ops_interface_kube_control-0.3.0}/ops.interface_kube_control.egg-info/zip-safe +0 -0
- {ops_interface_kube_control-0.2.0 → ops_interface_kube_control-0.3.0}/setup.cfg +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
prune tests
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
Metadata-Version: 2.2
|
|
2
|
+
Name: ops.interface_kube_control
|
|
3
|
+
Version: 0.3.0
|
|
4
|
+
Summary: Kube-Control Interface for Charmed Operators
|
|
5
|
+
Author-email: Adam Dyess <adam.dyess@canonical.com>, Mateo Florido <mateo.florido@canonical.com>
|
|
6
|
+
License: Apache
|
|
7
|
+
Project-URL: Repository, https://github.com/charmed-kubernetes/interface-kube-control
|
|
8
|
+
Keywords: juju,charming,kubernetes,ops,framework,interface
|
|
9
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
10
|
+
Classifier: Programming Language :: Python
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Requires-Python: >3.8
|
|
18
|
+
Description-Content-Type: text/markdown
|
|
19
|
+
Requires-Dist: ops
|
|
20
|
+
Requires-Dist: pydantic<3,>=2
|
|
21
|
+
|
|
22
|
+
# ops.interface_kube_control
|
|
23
|
+
|
|
24
|
+
## Rationale for this library
|
|
25
|
+
Many project need credentials in a kubernetes cluster to apply their workloads
|
|
26
|
+
using credentials from the kubernetes cluster. Outside the cluster, these charms
|
|
27
|
+
need access to the cluster's API and credentials within that API.
|
|
28
|
+
|
|
29
|
+
This library supports the `kube-control` relation to relate to a kubernetes
|
|
30
|
+
control-plane application, to provide access to that endpoint.
|
|
31
|
+
|
|
32
|
+
## Contributing
|
|
33
|
+
This library provides an interface for `ops` charms to use to connect to this interface.
|
|
34
|
+
|
|
35
|
+
* Make adjustments
|
|
36
|
+
* test the changes by running tox
|
|
37
|
+
|
|
38
|
+
```sh
|
|
39
|
+
tox
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
* Raise your PR and contact maintainers
|
|
43
|
+
* Once merged, tag with an `ops-vX.X.X` to publish a new pypi package
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# ops.interface_kube_control
|
|
2
|
+
|
|
3
|
+
## Rationale for this library
|
|
4
|
+
Many project need credentials in a kubernetes cluster to apply their workloads
|
|
5
|
+
using credentials from the kubernetes cluster. Outside the cluster, these charms
|
|
6
|
+
need access to the cluster's API and credentials within that API.
|
|
7
|
+
|
|
8
|
+
This library supports the `kube-control` relation to relate to a kubernetes
|
|
9
|
+
control-plane application, to provide access to that endpoint.
|
|
10
|
+
|
|
11
|
+
## Contributing
|
|
12
|
+
This library provides an interface for `ops` charms to use to connect to this interface.
|
|
13
|
+
|
|
14
|
+
* Make adjustments
|
|
15
|
+
* test the changes by running tox
|
|
16
|
+
|
|
17
|
+
```sh
|
|
18
|
+
tox
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
* Raise your PR and contact maintainers
|
|
22
|
+
* Once merged, tag with an `ops-vX.X.X` to publish a new pypi package
|
|
@@ -1,30 +1,45 @@
|
|
|
1
|
-
from pydantic import
|
|
1
|
+
from pydantic import (
|
|
2
|
+
ConfigDict,
|
|
3
|
+
Field,
|
|
4
|
+
AnyHttpUrl,
|
|
5
|
+
BaseModel,
|
|
6
|
+
GetCoreSchemaHandler,
|
|
7
|
+
Json,
|
|
8
|
+
)
|
|
9
|
+
|
|
2
10
|
import json
|
|
3
|
-
import ops
|
|
4
|
-
from typing import List, Dict, Optional
|
|
5
11
|
import re
|
|
12
|
+
from typing import Any, ClassVar, List, Dict, Optional, Pattern
|
|
13
|
+
|
|
14
|
+
import ops
|
|
15
|
+
|
|
16
|
+
from pydantic_core import core_schema
|
|
6
17
|
|
|
7
18
|
|
|
8
19
|
class _ValidatedStr:
|
|
9
|
-
|
|
20
|
+
REGEX: ClassVar[Pattern[str]]
|
|
21
|
+
|
|
22
|
+
def __init__(self, value: str, *groups: str) -> None:
|
|
10
23
|
self._str = value
|
|
11
24
|
self.groups = groups
|
|
12
25
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
yield cls.validate
|
|
26
|
+
def __repr__(self) -> str:
|
|
27
|
+
return f"{type(self).__name__}({self._str!r}, groups={self.groups})"
|
|
16
28
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
if not isinstance(v, str):
|
|
20
|
-
raise TypeError("string required")
|
|
21
|
-
m = cls.REGEX.match(v)
|
|
22
|
-
if not m:
|
|
23
|
-
raise ValueError("invalid format")
|
|
24
|
-
return cls(v, *m.groups())
|
|
29
|
+
def __str__(self) -> str:
|
|
30
|
+
return self._str
|
|
25
31
|
|
|
26
|
-
|
|
27
|
-
|
|
32
|
+
@classmethod
|
|
33
|
+
def __get_pydantic_core_schema__(cls, source: Any, handler: GetCoreSchemaHandler) -> Any:
|
|
34
|
+
def validate(value: str) -> "_ValidatedStr":
|
|
35
|
+
if match := cls.REGEX.match(value):
|
|
36
|
+
return cls(value, *match.groups())
|
|
37
|
+
raise ValueError(f"Invalid {cls.__name__} format")
|
|
38
|
+
|
|
39
|
+
return core_schema.no_info_plain_validator_function(
|
|
40
|
+
validate,
|
|
41
|
+
serialization=core_schema.to_string_ser_schema(),
|
|
42
|
+
)
|
|
28
43
|
|
|
29
44
|
@property
|
|
30
45
|
def key(self) -> str:
|
|
@@ -59,13 +74,13 @@ class AuthRequest(BaseModel):
|
|
|
59
74
|
group str: alias for auth_group
|
|
60
75
|
"""
|
|
61
76
|
|
|
62
|
-
kubelet_user: Optional[str]
|
|
63
|
-
auth_group: Optional[str]
|
|
77
|
+
kubelet_user: Optional[str] = None
|
|
78
|
+
auth_group: Optional[str] = None
|
|
64
79
|
schema_vers: Json[List[int]] = Field(default_factory=list)
|
|
65
|
-
unit: Optional[str]
|
|
80
|
+
unit: Optional[str] = None
|
|
66
81
|
|
|
67
|
-
def
|
|
68
|
-
d = super().
|
|
82
|
+
def model_dump(self, **kwargs):
|
|
83
|
+
d = super().model_dump(**kwargs)
|
|
69
84
|
if schema_vers := d.pop("schema_vers", None):
|
|
70
85
|
d["schema_vers"] = json.dumps(schema_vers)
|
|
71
86
|
return d
|
|
@@ -87,6 +102,8 @@ class AuthRequest(BaseModel):
|
|
|
87
102
|
|
|
88
103
|
|
|
89
104
|
class Creds(BaseModel):
|
|
105
|
+
model_config = ConfigDict(populate_by_name=True)
|
|
106
|
+
|
|
90
107
|
client_token: str
|
|
91
108
|
kubelet_token: str
|
|
92
109
|
proxy_token: str
|
|
@@ -115,9 +132,9 @@ class Creds(BaseModel):
|
|
|
115
132
|
|
|
116
133
|
class Data(BaseModel):
|
|
117
134
|
api_endpoints: Json[List[AnyHttpUrl]] = Field(alias="api-endpoints")
|
|
118
|
-
ca_certificate_secret_id: Optional[str] = Field(alias="ca-certificate-secret-id")
|
|
135
|
+
ca_certificate_secret_id: Optional[str] = Field(None, alias="ca-certificate-secret-id")
|
|
119
136
|
cluster_tag: str = Field(alias="cluster-tag")
|
|
120
|
-
cohort_keys: Optional[Json[Dict[str, str]]] = Field(alias="cohort-keys")
|
|
137
|
+
cohort_keys: Optional[Json[Dict[str, str]]] = Field(None, alias="cohort-keys")
|
|
121
138
|
creds: Json[Dict[str, Creds]] = Field(alias="creds")
|
|
122
139
|
default_cni: Json[str] = Field(alias="default-cni")
|
|
123
140
|
domain: str = Field(alias="domain")
|
|
@@ -133,9 +150,7 @@ class Data(BaseModel):
|
|
|
133
150
|
if not self.ca_certificate_secret_id:
|
|
134
151
|
return None
|
|
135
152
|
try:
|
|
136
|
-
secret = model.get_secret(
|
|
137
|
-
id=self.ca_certificate_secret_id, label="ca-certificate"
|
|
138
|
-
)
|
|
153
|
+
secret = model.get_secret(id=self.ca_certificate_secret_id, label="ca-certificate")
|
|
139
154
|
return secret.get_content(refresh=True)["ca-certificate"].encode()
|
|
140
155
|
except ops.SecretNotFoundError:
|
|
141
156
|
return None
|
|
@@ -1,9 +1,12 @@
|
|
|
1
|
+
import ipaddress
|
|
1
2
|
import json
|
|
2
3
|
import logging
|
|
4
|
+
from typing import Generator, List, Tuple
|
|
5
|
+
|
|
6
|
+
from pydantic import TypeAdapter
|
|
3
7
|
|
|
4
8
|
from .model import AuthRequest, Creds, Label, Taint
|
|
5
9
|
from ops import CharmBase, Relation, SecretNotFoundError, Unit
|
|
6
|
-
from typing import Generator, List, Tuple
|
|
7
10
|
|
|
8
11
|
log = logging.getLogger("KubeControlProvides")
|
|
9
12
|
|
|
@@ -14,6 +17,8 @@ class KubeControlProvides:
|
|
|
14
17
|
def __init__(self, charm: CharmBase, endpoint: str = "kube-control", schemas="0,1"):
|
|
15
18
|
self.charm = charm
|
|
16
19
|
self.endpoint = endpoint
|
|
20
|
+
self.label_adapter = TypeAdapter(Label)
|
|
21
|
+
self.taint_adapter = TypeAdapter(Taint)
|
|
17
22
|
|
|
18
23
|
@property
|
|
19
24
|
def auth_requests(self) -> List[AuthRequest]:
|
|
@@ -31,8 +36,7 @@ class KubeControlProvides:
|
|
|
31
36
|
|
|
32
37
|
def clear_creds(self) -> None:
|
|
33
38
|
"""Clear creds from the relation. This is used by non-leader units to
|
|
34
|
-
stop advertising creds so that the leader can assume full control of
|
|
35
|
-
them.
|
|
39
|
+
stop advertising creds so that the leader can assume full control of them.
|
|
36
40
|
"""
|
|
37
41
|
for relation in self.relations:
|
|
38
42
|
relation.data[self.unit]["creds"] = ""
|
|
@@ -40,14 +44,14 @@ class KubeControlProvides:
|
|
|
40
44
|
@property
|
|
41
45
|
def ingress_addresses(self) -> List[str]:
|
|
42
46
|
"""Ingress addresses for this endpoint."""
|
|
47
|
+
binding = self.charm.model.get_binding(self.endpoint)
|
|
43
48
|
return [
|
|
44
49
|
# RFC 5280 section 4.2.1.6: "For IP version 6 ... the octet string
|
|
45
50
|
# MUST contain exactly sixteen octets." We'll use .exploded to be
|
|
46
51
|
# safe.
|
|
47
|
-
|
|
48
|
-
for addr in
|
|
49
|
-
|
|
50
|
-
).network.ingress_addresses
|
|
52
|
+
ip.exploded
|
|
53
|
+
for addr in (binding and binding.network.ingress_addresses or [])
|
|
54
|
+
if (ip := ipaddress.ip_address(addr))
|
|
51
55
|
]
|
|
52
56
|
|
|
53
57
|
@property
|
|
@@ -65,7 +69,7 @@ class KubeControlProvides:
|
|
|
65
69
|
"""Send the CA certificate to the remote units.
|
|
66
70
|
|
|
67
71
|
Args:
|
|
68
|
-
ca_certificate str: The CA certificate in PEM format.
|
|
72
|
+
ca_certificate (str): The CA certificate in PEM format.
|
|
69
73
|
"""
|
|
70
74
|
content = {"ca-certificate": ca_certificate}
|
|
71
75
|
secret = self.refresh_secret_content(
|
|
@@ -128,16 +132,16 @@ class KubeControlProvides:
|
|
|
128
132
|
|
|
129
133
|
def set_labels(self, labels) -> None:
|
|
130
134
|
"""Send the Juju config labels of the control-plane."""
|
|
131
|
-
|
|
132
|
-
dedup = sorted(set(
|
|
135
|
+
valid_labels = [str(self.label_adapter.validate_python(label_str)) for label_str in labels]
|
|
136
|
+
dedup = sorted(set(valid_labels))
|
|
133
137
|
value = json.dumps(dedup)
|
|
134
138
|
for relation in self.relations:
|
|
135
139
|
relation.data[self.unit]["labels"] = value
|
|
136
140
|
|
|
137
141
|
def set_taints(self, taints) -> None:
|
|
138
142
|
"""Send the Juju config taints of the control-plane."""
|
|
139
|
-
|
|
140
|
-
dedup = sorted(set(
|
|
143
|
+
valid_taints = [str(self.taint_adapter.validate_python(taint_str)) for taint_str in taints]
|
|
144
|
+
dedup = sorted(set(valid_taints))
|
|
141
145
|
value = json.dumps(dedup)
|
|
142
146
|
for relation in self.relations:
|
|
143
147
|
relation.data[self.unit]["taints"] = value
|
|
@@ -150,9 +154,7 @@ class KubeControlProvides:
|
|
|
150
154
|
secret.set_content(content)
|
|
151
155
|
secret.set_info(description=description, label=label)
|
|
152
156
|
except SecretNotFoundError:
|
|
153
|
-
secret = self.charm.app.add_secret(
|
|
154
|
-
content, label=label, description=description
|
|
155
|
-
)
|
|
157
|
+
secret = self.charm.app.add_secret(content, label=label, description=description)
|
|
156
158
|
return secret
|
|
157
159
|
|
|
158
160
|
def closed_auth_creds(self) -> Generator[Tuple[str, Creds], None, None]:
|
|
@@ -171,12 +173,12 @@ class KubeControlProvides:
|
|
|
171
173
|
"""
|
|
172
174
|
creds, unit_names = {}, []
|
|
173
175
|
|
|
174
|
-
# Collect creds from all relations
|
|
176
|
+
# Collect creds from all relations.
|
|
175
177
|
for relation in self.relations:
|
|
176
178
|
creds.update(json.loads(relation.data[self.unit].get("creds", "{}")))
|
|
177
179
|
unit_names += [u.name for u in relation.units]
|
|
178
180
|
|
|
179
|
-
# Revoke creds for units that have been removed
|
|
181
|
+
# Revoke creds for units that have been removed.
|
|
180
182
|
for user, cred in dict(**creds).items():
|
|
181
183
|
data = Creds(**cred)
|
|
182
184
|
if data.scope not in unit_names:
|
|
@@ -187,7 +189,7 @@ class KubeControlProvides:
|
|
|
187
189
|
secret = self.charm.model.get_secret(id=data.secret_id)
|
|
188
190
|
secret.remove_all_revisions()
|
|
189
191
|
|
|
190
|
-
# Publish the updated creds without the revoked units
|
|
192
|
+
# Publish the updated creds without the revoked units.
|
|
191
193
|
value = json.dumps(creds)
|
|
192
194
|
for relation in self.relations:
|
|
193
195
|
relation.data[self.unit]["creds"] = value
|
|
@@ -197,6 +199,8 @@ class KubeControlProvides:
|
|
|
197
199
|
) -> None:
|
|
198
200
|
"""Send authorization tokens to the requesting unit."""
|
|
199
201
|
creds, request_relation = {}, None
|
|
202
|
+
if not request.unit:
|
|
203
|
+
raise ValueError("Request unit is required")
|
|
200
204
|
request_unit = self.charm.model.get_unit(request.unit)
|
|
201
205
|
|
|
202
206
|
for relation in self.relations:
|
|
@@ -212,7 +216,7 @@ class KubeControlProvides:
|
|
|
212
216
|
)
|
|
213
217
|
|
|
214
218
|
if 1 in request.schema_vers and request_relation:
|
|
215
|
-
# Requesting unit can use schema 1, use
|
|
219
|
+
# Requesting unit can use schema 1, use Juju secrets.
|
|
216
220
|
content = {
|
|
217
221
|
"client-token": client_token,
|
|
218
222
|
"kubelet-token": kubelet_token,
|
|
@@ -232,9 +236,9 @@ class KubeControlProvides:
|
|
|
232
236
|
tokens.kubelet_token = ""
|
|
233
237
|
tokens.proxy_token = ""
|
|
234
238
|
tokens.secret_id = secret.id
|
|
235
|
-
creds[request.user] = tokens.
|
|
239
|
+
creds[request.user] = tokens.model_dump(by_alias=True, exclude_none=True)
|
|
236
240
|
else:
|
|
237
|
-
creds[request.user] = tokens.
|
|
241
|
+
creds[request.user] = tokens.model_dump(by_alias=True, exclude_none=True)
|
|
238
242
|
|
|
239
243
|
value = json.dumps(creds)
|
|
240
244
|
for relation in self.relations:
|
|
@@ -8,30 +8,33 @@ style rather than the reactive style.
|
|
|
8
8
|
|
|
9
9
|
import base64
|
|
10
10
|
import logging
|
|
11
|
+
from functools import cached_property
|
|
11
12
|
from os import PathLike
|
|
12
13
|
from pathlib import Path
|
|
13
|
-
from typing import Dict,
|
|
14
|
+
from typing import Any, Dict, List, Mapping, Optional
|
|
14
15
|
|
|
15
16
|
import yaml
|
|
16
|
-
from backports.cached_property import cached_property
|
|
17
|
-
from .model import AuthRequest, Creds, Data, Taint, Label
|
|
18
17
|
from pydantic import ValidationError
|
|
19
18
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
from
|
|
19
|
+
import ops
|
|
20
|
+
|
|
21
|
+
from .model import AuthRequest, Creds, Data, Label, Taint
|
|
23
22
|
|
|
24
23
|
log = logging.getLogger("KubeControlRequirer")
|
|
25
24
|
JUJU_CLUSTER = "juju-cluster"
|
|
26
25
|
JUJU_CONTEXT = "juju-context"
|
|
27
26
|
|
|
28
27
|
|
|
29
|
-
class
|
|
28
|
+
class RelationNotReady(Exception):
|
|
29
|
+
"""Exception raised when the relation is not ready."""
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class KubeControlRequirer(ops.Object):
|
|
30
33
|
"""
|
|
31
34
|
Implements the requirer side of the kube-control interface.
|
|
32
35
|
"""
|
|
33
36
|
|
|
34
|
-
def __init__(self, charm: CharmBase, endpoint: str = "kube-control", schemas="0"):
|
|
37
|
+
def __init__(self, charm: ops.CharmBase, endpoint: str = "kube-control", schemas="0"):
|
|
35
38
|
super().__init__(charm, f"relation-{endpoint}")
|
|
36
39
|
self.endpoint = endpoint
|
|
37
40
|
# comma-separated set of schemas to advertise support
|
|
@@ -40,23 +43,23 @@ class KubeControlRequirer(Object):
|
|
|
40
43
|
self.schema_ver = [int(v) for v in schemas.split(",")]
|
|
41
44
|
|
|
42
45
|
@cached_property
|
|
43
|
-
def relation(self) -> Optional[Relation]:
|
|
46
|
+
def relation(self) -> Optional[ops.Relation]:
|
|
44
47
|
"""The lone relation endpoint or None."""
|
|
45
48
|
return self.model.get_relation(self.endpoint)
|
|
46
49
|
|
|
47
50
|
@cached_property
|
|
48
|
-
def _data(self) ->
|
|
51
|
+
def _data(self) -> Data:
|
|
49
52
|
if self.relation and self.relation.units:
|
|
50
|
-
rx = {}
|
|
53
|
+
rx: Dict[str, str] = {}
|
|
51
54
|
for unit in self.relation.units:
|
|
52
55
|
rx.update(self.relation.data[unit])
|
|
53
|
-
return Data(
|
|
54
|
-
|
|
56
|
+
return Data.model_validate(rx)
|
|
57
|
+
raise RelationNotReady("No unit data available")
|
|
55
58
|
|
|
56
59
|
def evaluate_relation(self, event) -> Optional[str]:
|
|
57
60
|
"""Determine if relation is ready."""
|
|
58
61
|
no_relation = not self.relation or (
|
|
59
|
-
isinstance(event, RelationBrokenEvent) and event.relation is self.relation
|
|
62
|
+
isinstance(event, ops.RelationBrokenEvent) and event.relation is self.relation
|
|
60
63
|
)
|
|
61
64
|
if not self.is_ready:
|
|
62
65
|
if no_relation:
|
|
@@ -72,14 +75,12 @@ class KubeControlRequirer(Object):
|
|
|
72
75
|
except ValidationError as ve:
|
|
73
76
|
log.error(f"{self.endpoint} relation data not yet valid. ({ve}")
|
|
74
77
|
return False
|
|
75
|
-
|
|
78
|
+
except RelationNotReady:
|
|
76
79
|
log.error(f"{self.endpoint} relation data not yet available.")
|
|
77
80
|
return False
|
|
78
81
|
return True
|
|
79
82
|
|
|
80
|
-
def create_kubeconfig(
|
|
81
|
-
self, ca: PathLike, kubeconfig: PathLike, user: str, k8s_user: str
|
|
82
|
-
):
|
|
83
|
+
def create_kubeconfig(self, ca: PathLike, kubeconfig: PathLike, user: str, k8s_user: str):
|
|
83
84
|
"""Write kubeconfig based on available creds."""
|
|
84
85
|
creds = self.get_auth_credentials(k8s_user)
|
|
85
86
|
endpoints = self.get_api_endpoints()
|
|
@@ -153,7 +154,7 @@ class KubeControlRequirer(Object):
|
|
|
153
154
|
}
|
|
154
155
|
return None
|
|
155
156
|
|
|
156
|
-
def get_dns(self) -> Mapping[str,
|
|
157
|
+
def get_dns(self) -> Mapping[str, Any]:
|
|
157
158
|
"""
|
|
158
159
|
Return DNS info provided by the control-plane.
|
|
159
160
|
"""
|
|
@@ -170,10 +171,7 @@ class KubeControlRequirer(Object):
|
|
|
170
171
|
"""
|
|
171
172
|
keys = ["port", "domain", "sdn-ip", "enable-kube-dns"]
|
|
172
173
|
dns_info = self.get_dns()
|
|
173
|
-
return (
|
|
174
|
-
set(dns_info.keys()) == set(keys)
|
|
175
|
-
and dns_info["enable-kube-dns"] is not None
|
|
176
|
-
)
|
|
174
|
+
return set(dns_info.keys()) == set(keys) and dns_info["enable-kube-dns"] is not None
|
|
177
175
|
|
|
178
176
|
def set_auth_request(self, user, group="system:nodes") -> None:
|
|
179
177
|
"""Notify control-plane that we are requesting auth.
|
|
@@ -187,19 +185,21 @@ class KubeControlRequirer(Object):
|
|
|
187
185
|
cluster via changing to
|
|
188
186
|
system:masters. # wokeignore:rule=master
|
|
189
187
|
"""
|
|
190
|
-
if self.relation:
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
188
|
+
if not self.relation:
|
|
189
|
+
return
|
|
190
|
+
|
|
191
|
+
req = AuthRequest(kubelet_user=user, auth_group=group)
|
|
192
|
+
req.schema_vers = self.schema_ver
|
|
193
|
+
log.info(f"Auth Req for {user} with group {group} on schema {self.schema_ver}")
|
|
194
|
+
self.relation.data[self.model.unit].update(
|
|
195
|
+
req.model_dump(exclude_none=True, by_alias=True)
|
|
196
|
+
)
|
|
197
197
|
|
|
198
198
|
def set_gpu(self, enabled=True):
|
|
199
199
|
"""
|
|
200
200
|
Tell the control-plane that we're gpu-enabled (or not).
|
|
201
201
|
"""
|
|
202
|
-
log("Setting gpu
|
|
202
|
+
log.info("Setting gpu=%s on kube-control relation", enabled)
|
|
203
203
|
for relation in self.model.relations:
|
|
204
204
|
relation.data[self.model.unit].update(dict(gpu=enabled))
|
|
205
205
|
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
Metadata-Version: 2.2
|
|
2
|
+
Name: ops.interface_kube_control
|
|
3
|
+
Version: 0.3.0
|
|
4
|
+
Summary: Kube-Control Interface for Charmed Operators
|
|
5
|
+
Author-email: Adam Dyess <adam.dyess@canonical.com>, Mateo Florido <mateo.florido@canonical.com>
|
|
6
|
+
License: Apache
|
|
7
|
+
Project-URL: Repository, https://github.com/charmed-kubernetes/interface-kube-control
|
|
8
|
+
Keywords: juju,charming,kubernetes,ops,framework,interface
|
|
9
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
10
|
+
Classifier: Programming Language :: Python
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Requires-Python: >3.8
|
|
18
|
+
Description-Content-Type: text/markdown
|
|
19
|
+
Requires-Dist: ops
|
|
20
|
+
Requires-Dist: pydantic<3,>=2
|
|
21
|
+
|
|
22
|
+
# ops.interface_kube_control
|
|
23
|
+
|
|
24
|
+
## Rationale for this library
|
|
25
|
+
Many project need credentials in a kubernetes cluster to apply their workloads
|
|
26
|
+
using credentials from the kubernetes cluster. Outside the cluster, these charms
|
|
27
|
+
need access to the cluster's API and credentials within that API.
|
|
28
|
+
|
|
29
|
+
This library supports the `kube-control` relation to relate to a kubernetes
|
|
30
|
+
control-plane application, to provide access to that endpoint.
|
|
31
|
+
|
|
32
|
+
## Contributing
|
|
33
|
+
This library provides an interface for `ops` charms to use to connect to this interface.
|
|
34
|
+
|
|
35
|
+
* Make adjustments
|
|
36
|
+
* test the changes by running tox
|
|
37
|
+
|
|
38
|
+
```sh
|
|
39
|
+
tox
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
* Raise your PR and contact maintainers
|
|
43
|
+
* Once merged, tag with an `ops-vX.X.X` to publish a new pypi package
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools >= 61.0", "setuptools_scm"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[tool.setuptools_scm]
|
|
6
|
+
version_scheme = "guess-next-dev"
|
|
7
|
+
local_scheme = "node-and-date"
|
|
8
|
+
root = ".."
|
|
9
|
+
|
|
10
|
+
[project]
|
|
11
|
+
name = "ops.interface_kube_control"
|
|
12
|
+
dynamic = ["version"]
|
|
13
|
+
description = "Kube-Control Interface for Charmed Operators"
|
|
14
|
+
readme = "README.md"
|
|
15
|
+
requires-python = ">3.8"
|
|
16
|
+
license = { text = "Apache" }
|
|
17
|
+
authors = [
|
|
18
|
+
{ name= "Adam Dyess", email = "adam.dyess@canonical.com" },
|
|
19
|
+
{ name= "Mateo Florido", email = "mateo.florido@canonical.com" }
|
|
20
|
+
]
|
|
21
|
+
dependencies = [
|
|
22
|
+
"ops",
|
|
23
|
+
"pydantic>=2,<3",
|
|
24
|
+
]
|
|
25
|
+
keywords = [
|
|
26
|
+
"juju",
|
|
27
|
+
"charming",
|
|
28
|
+
"kubernetes",
|
|
29
|
+
"ops",
|
|
30
|
+
"framework",
|
|
31
|
+
"interface",
|
|
32
|
+
]
|
|
33
|
+
classifiers = [
|
|
34
|
+
"License :: OSI Approved :: Apache Software License",
|
|
35
|
+
"Programming Language :: Python",
|
|
36
|
+
"Programming Language :: Python :: 3",
|
|
37
|
+
"Programming Language :: Python :: 3.8",
|
|
38
|
+
"Programming Language :: Python :: 3.9",
|
|
39
|
+
"Programming Language :: Python :: 3.10",
|
|
40
|
+
"Programming Language :: Python :: 3.11",
|
|
41
|
+
"Programming Language :: Python :: 3.12"
|
|
42
|
+
]
|
|
43
|
+
|
|
44
|
+
[project.urls]
|
|
45
|
+
Repository = "https://github.com/charmed-kubernetes/interface-kube-control"
|
|
46
|
+
|
|
47
|
+
[tool.isort]
|
|
48
|
+
line_length = 99
|
|
49
|
+
profile = "black"
|
|
50
|
+
|
|
51
|
+
[tool.mypy]
|
|
52
|
+
explicit_package_bases = true
|
|
53
|
+
namespace_packages = true
|
|
54
|
+
plugins = "pydantic.mypy"
|
|
55
|
+
|
|
56
|
+
[tool.ruff]
|
|
57
|
+
line-length = 99
|
|
58
|
+
extend-exclude = ["__pycache__", "*.egg_info"]
|
|
59
|
+
|
|
60
|
+
[tool.setuptools]
|
|
61
|
+
zip-safe = true
|
|
62
|
+
|
|
63
|
+
[tool.setuptools.packages.find]
|
|
64
|
+
namespaces = true
|
|
65
|
+
|
|
66
|
+
[dependency-groups]
|
|
67
|
+
format = [
|
|
68
|
+
"ruff"
|
|
69
|
+
]
|
|
70
|
+
lint = [
|
|
71
|
+
"mypy",
|
|
72
|
+
"types-PyYAML",
|
|
73
|
+
{include-group = "format"},
|
|
74
|
+
{include-group = "unit"},
|
|
75
|
+
]
|
|
76
|
+
publish = []
|
|
77
|
+
unit = [
|
|
78
|
+
"pytest>=8.3.4",
|
|
79
|
+
"pytest-cov>=5.0.0",
|
|
80
|
+
]
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
[tox]
|
|
2
|
+
envlist = format,lint,unit
|
|
3
|
+
|
|
4
|
+
[vars]
|
|
5
|
+
ops_path = {toxinidir}/ops/
|
|
6
|
+
tst_path = {toxinidir}/tests/
|
|
7
|
+
all_path = {[vars]ops_path} {[vars]tst_path}
|
|
8
|
+
|
|
9
|
+
[testenv]
|
|
10
|
+
basepython = python3
|
|
11
|
+
setenv =
|
|
12
|
+
PYTHONPATH = {toxinidir}
|
|
13
|
+
|
|
14
|
+
[testenv:lint]
|
|
15
|
+
dependency_groups = lint
|
|
16
|
+
commands =
|
|
17
|
+
ruff check {[vars]all_path}
|
|
18
|
+
mypy {[vars]all_path}
|
|
19
|
+
|
|
20
|
+
[testenv:format]
|
|
21
|
+
description = Apply coding style standards to code
|
|
22
|
+
dependency_groups = format
|
|
23
|
+
commands =
|
|
24
|
+
ruff format {[vars]all_path}
|
|
25
|
+
ruff check --fix {[vars]all_path}
|
|
26
|
+
|
|
27
|
+
[testenv:unit]
|
|
28
|
+
description = Run unit tests
|
|
29
|
+
dependency_groups = unit
|
|
30
|
+
commands =
|
|
31
|
+
uv run pytest -vv \
|
|
32
|
+
--cov='{envsitepackagesdir}/ops/interface_kube_control' \
|
|
33
|
+
--cov-report=term-missing \
|
|
34
|
+
--basetemp={envtmpdir} \
|
|
35
|
+
--tb native -s {posargs}
|
|
36
|
+
|
|
37
|
+
[testenv:build]
|
|
38
|
+
dependency_groups = publish
|
|
39
|
+
skip_install = True
|
|
40
|
+
skipsdist = True
|
|
41
|
+
allowlist_externals = rm
|
|
42
|
+
setenv =
|
|
43
|
+
SETUPTOOLS_SCM_DEBUG = 1
|
|
44
|
+
commands=
|
|
45
|
+
rm -rf build/ *.egg-info/ dist/
|
|
46
|
+
uv build
|
|
47
|
+
|
|
48
|
+
[testenv:publish]
|
|
49
|
+
dependency_groups = publish
|
|
50
|
+
skip_install = True
|
|
51
|
+
skipsdist = True
|
|
52
|
+
allowlist_externals = rm
|
|
53
|
+
commands=
|
|
54
|
+
rm -rf build/ *.egg-info/ dist/
|
|
55
|
+
uv build
|
|
56
|
+
uv publish {posargs} {toxinidir}/dist/*
|