Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Admission webhooks for validation & mutation #708

Merged
merged 10 commits into from
Mar 27, 2021
Merged

Admission webhooks for validation & mutation #708

merged 10 commits into from
Mar 27, 2021

Conversation

nolar
Copy link
Owner

@nolar nolar commented Mar 8, 2021

This is an early preview of admission webhooks.

For documentation preview, see:

A brief functional example:

import warnings

import kopf


@kopf.on.startup()
def config(settings: kopf.OperatorSettings, **_):
    settings.admission.managed = 'auto.kopf.dev'
    # settings.admission.server = kopf.WebhookServer()
    # settings.admission.server = kopf.WebhookServer(certfile='../../cert.pem', pkeyfile='../../key.pem', port=1234)
    # settings.admission.server = kopf.WebhookK3dServer(certfile='../../k3d-cert.pem', pkeyfile='../../k3d-key.pem', port=1234)
    # settings.admission.server = kopf.WebhookK3dServer(port=1234, cadump='../../ca.pem', verify_cafile='../../client-cert.pem')
    settings.admission.server = kopf.WebhookNgrokTunnel(binary="/usr/local/bin/ngrok", port=1234)


@kopf.on.validate('kopfexamples')
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.validate('kopfexamples')
def validate2(**_):
    raise kopf.AdmissionError("I'm too lazy anyway. Go away!", status=555)


@kopf.on.validate('kopfexamples')
def authhook(headers, sslpeer, **_):
    print(f'{headers=}')
    print(f'{sslpeer=}')
    if sslpeer is None:
        warnings.warn("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.warn("SSL peer's common name is absent.")
        if common_name is not None:
            warnings.warn(f"SSL peer is {common_name}.")


@kopf.on.mutate('kex', labels={'somelabel': 'somevalue'})
def mutate1(patch: kopf.Patch, **_):
    patch.spec['injected'] = 123

@nolar nolar added the enhancement New feature or request label Mar 8, 2021
@lgtm-com
Copy link

lgtm-com bot commented Mar 8, 2021

This pull request introduces 1 alert when merging 6d32bf3 into 8a08e0a - view on LGTM.com

new alerts:

  • 1 for Unguarded next in generator

Base automatically changed from master to main March 10, 2021 19:45
@lgtm-com
Copy link

lgtm-com bot commented Mar 10, 2021

This pull request introduces 1 alert when merging 6d32bf3 into e71dc60 - view on LGTM.com

new alerts:

  • 1 for Unguarded next in generator

@lgtm-com
Copy link

lgtm-com bot commented Mar 18, 2021

This pull request introduces 3 alerts when merging 7dd0d93 into e90ceaa - view on LGTM.com

new alerts:

  • 1 for Wrong name for an argument in a call
  • 1 for Wrong name for an argument in a class instantiation
  • 1 for Unguarded next in generator

@lgtm-com
Copy link

lgtm-com bot commented Mar 23, 2021

This pull request introduces 3 alerts when merging 8d20d7a into e90ceaa - view on LGTM.com

new alerts:

  • 1 for Wrong name for an argument in a call
  • 1 for Wrong name for an argument in a class instantiation
  • 1 for Unguarded next in generator

@nolar nolar force-pushed the admission-hooks branch 3 times, most recently from 9a35aa7 to 11a52ef Compare March 23, 2021 23:13
@lgtm-com
Copy link

lgtm-com bot commented Mar 23, 2021

This pull request introduces 3 alerts when merging 11a52ef into e90ceaa - view on LGTM.com

new alerts:

  • 1 for Wrong name for an argument in a call
  • 1 for Wrong name for an argument in a class instantiation
  • 1 for Unguarded next in generator

@nolar nolar force-pushed the admission-hooks branch 2 times, most recently from 2b161bb to 6059f3a Compare March 23, 2021 23:42
@lgtm-com
Copy link

lgtm-com bot commented Mar 24, 2021

This pull request introduces 3 alerts when merging 6059f3a into e90ceaa - view on LGTM.com

new alerts:

  • 1 for Wrong name for an argument in a call
  • 1 for Wrong name for an argument in a class instantiation
  • 1 for Unguarded next in generator

@lgtm-com
Copy link

lgtm-com bot commented Mar 24, 2021

This pull request introduces 2 alerts when merging 4a22f40 into e90ceaa - view on LGTM.com

new alerts:

  • 1 for Wrong name for an argument in a class instantiation
  • 1 for Unguarded next in generator

@nolar nolar force-pushed the admission-hooks branch 2 times, most recently from 72ef3be to ebe9ec9 Compare March 24, 2021 07:58
@lgtm-com
Copy link

lgtm-com bot commented Mar 24, 2021

This pull request introduces 1 alert when merging ebe9ec9 into e90ceaa - view on LGTM.com

new alerts:

  • 1 for Unguarded next in generator

@lgtm-com
Copy link

lgtm-com bot commented Mar 24, 2021

This pull request introduces 1 alert when merging 0dc46bf into e90ceaa - view on LGTM.com

new alerts:

  • 1 for Unguarded next in generator

@lgtm-com
Copy link

lgtm-com bot commented Mar 24, 2021

This pull request introduces 1 alert when merging 45f2b68 into e90ceaa - view on LGTM.com

new alerts:

  • 1 for Unguarded next in generator

@lgtm-com
Copy link

lgtm-com bot commented Mar 25, 2021

This pull request introduces 1 alert when merging 24cd933 into e90ceaa - view on LGTM.com

new alerts:

  • 1 for Unguarded next in generator

@lgtm-com
Copy link

lgtm-com bot commented Mar 25, 2021

This pull request introduces 1 alert when merging 77d4d57 into e90ceaa - view on LGTM.com

new alerts:

  • 1 for Unguarded next in generator

@nolar nolar changed the title WIP: Admission webhooks for validation & mutation Admission webhooks for validation & mutation Mar 25, 2021
@nolar nolar marked this pull request as ready for review March 25, 2021 21:16
@lgtm-com
Copy link

lgtm-com bot commented Mar 25, 2021

This pull request introduces 1 alert when merging 405b346 into e90ceaa - view on LGTM.com

new alerts:

  • 1 for Unguarded next in generator

Signed-off-by: Sergey Vasilyev <nolar@nolar.info>
Signed-off-by: Sergey Vasilyev <nolar@nolar.info>
Signed-off-by: Sergey Vasilyev <nolar@nolar.info>
Signed-off-by: Sergey Vasilyev <nolar@nolar.info>
Signed-off-by: Sergey Vasilyev <nolar@nolar.info>
Signed-off-by: Sergey Vasilyev <nolar@nolar.info>
Signed-off-by: Sergey Vasilyev <nolar@nolar.info>
Signed-off-by: Sergey Vasilyev <nolar@nolar.info>
Signed-off-by: Sergey Vasilyev <nolar@nolar.info>
@nolar nolar force-pushed the admission-hooks branch 4 times, most recently from aac2d08 to b4e7b2d Compare March 27, 2021 12:41
Signed-off-by: Sergey Vasilyev <nolar@nolar.info>
@nolar nolar merged commit db2c6cd into main Mar 27, 2021
@nolar nolar deleted the admission-hooks branch March 27, 2021 12:57
@lgtm-com
Copy link

lgtm-com bot commented Mar 27, 2021

This pull request introduces 31 alerts when merging e89ac21 into 0ad6551 - view on LGTM.com

new alerts:

  • 31 for Module-level cyclic import

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant