-
-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Sergey Vasilyev <nolar@nolar.info>
- Loading branch information
Showing
50 changed files
with
4,022 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,7 @@ Kopf: Kubernetes Operators Framework | |
scopes | ||
memos | ||
indexing | ||
admission | ||
|
||
.. toctree:: | ||
:maxdepth: 2 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import pathlib | ||
|
||
import kopf | ||
|
||
|
||
@kopf.on.startup() | ||
def config(settings: kopf.OperatorSettings, **_): | ||
ROOT = (pathlib.Path.cwd() / pathlib.Path(__file__)).parent.parent.parent | ||
settings.admission.managed = 'auto.kopf.dev' | ||
# settings.admission.server = kopf.WebhookServer() | ||
# settings.admission.server = kopf.WebhookServer(certfile=ROOT/'cert.pem', pkeyfile=ROOT/'key.pem', port=1234) | ||
# settings.admission.server = kopf.WebhookK3dServer(certfile=ROOT/'k3d-cert.pem', pkeyfile=ROOT/'k3d-key.pem', port=1234) | ||
# settings.admission.server = kopf.WebhookK3dServer(port=1234, cadump=ROOT/'ca.pem', verify_cafile=ROOT/'client-cert.pem') | ||
settings.admission.server = kopf.WebhookK3dServer(cadump=ROOT/'ca.pem') | ||
# settings.admission.server = kopf.WebhookNgrokTunnel() | ||
# settings.admission.server = kopf.WebhookNgrokTunnel(binary="/usr/local/bin/ngrok", token='...', port=1234) | ||
# settings.admission.server = kopf.WebhookNgrokTunnel(binary="/usr/local/bin/ngrok", port=1234, path='/xyz', region='eu') | ||
# settings.admission.server = kopf.WebhookInletsTunnel(...) | ||
|
||
|
||
@kopf.on.validation('kex', persisted=True, field='spec.field2', value='value') | ||
def validate1(spec, dryrun, **_): | ||
print(f'{dryrun=}') | ||
if spec.get('field') == 'value': | ||
raise kopf.AdmissionError("Meh! I don't like it. Change the field.") | ||
|
||
|
||
@kopf.on.validation('kex') | ||
def authhook(headers, sslpeer, warnings, **_): | ||
print(f'{headers=}') | ||
print(f'{sslpeer=}') | ||
if not sslpeer: | ||
warnings.append("SSL peer is not identified.") | ||
else: | ||
common_name = None | ||
for key, val in sslpeer['subject'][0]: | ||
if key == 'commonName': | ||
common_name = val | ||
break | ||
else: | ||
warnings.append("SSL peer's common name is absent.") | ||
if common_name is not None: | ||
warnings.append(f"SSL peer is {common_name}.") | ||
|
||
|
||
@kopf.on.validation('kex') | ||
def validate2(**_): | ||
raise kopf.AdmissionError("I'm too lazy anyway. Go away!", status=555) | ||
|
||
|
||
@kopf.on.mutation('kex') | ||
def mutate1(patch: kopf.Patch, **_): | ||
patch.spec['injected'] = 123 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from typing import Optional | ||
|
||
from kopf.clients import auth, errors | ||
from kopf.structs import bodies, references | ||
|
||
|
||
@auth.reauthenticated_request | ||
async def create_obj( | ||
*, | ||
resource: references.Resource, | ||
namespace: references.Namespace = None, | ||
name: Optional[str] = None, | ||
body: Optional[bodies.RawBody] = None, | ||
context: Optional[auth.APIContext] = None, # injected by the decorator | ||
) -> Optional[bodies.RawBody]: | ||
""" | ||
TODO | ||
""" | ||
if context is None: | ||
raise RuntimeError("API instance is not injected by the decorator.") | ||
|
||
body = body if body is not None else {} | ||
if namespace is not None: | ||
body.setdefault('metadata', {}).setdefault('namespace', namespace) | ||
if name is not None: | ||
body.setdefault('metadata', {}).setdefault('name', name) | ||
|
||
response = await context.session.post( | ||
url=resource.get_url(server=context.server, namespace=namespace), | ||
json=body, | ||
) | ||
created_body: bodies.RawBody = await errors.parse_response(response) | ||
return created_body |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.