How to use the kopf.on.update function in kopf

To help you get started, we’ve selected a few kopf examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github elemental-lf / benji / src / benji / k8s_operator / crd / retention_schedule.py View on Github external
@kopf.on.update(*BenjiRetentionSchedule.group_version_plural())
@kopf.on.resume(*ClusterBenjiRetentionSchedule.group_version_plural())
@kopf.on.create(*ClusterBenjiRetentionSchedule.group_version_plural())
@kopf.on.update(*ClusterBenjiRetentionSchedule.group_version_plural())
def benji_retention_schedule(namespace: str, spec: Dict[str, Any], body: Dict[str, Any], logger,
                             **_) -> Optional[Dict[str, Any]]:
    schedule = spec[K8S_RESTORE_SPEC_SCHEDULE]
    retention_rule = spec[K8S_RESTORE_SPEC_RETENTION_RULE]

    instance_filter = f'labels["{LABEL_INSTANCE}"] == "{benji_instance}"'
    match_versions = spec.get(K8S_RESTORE_SPEC_MATCH_VERSIONS, None)
    if match_versions is not None:
        match_versions = f'({match_versions}) and {instance_filter}'
    else:
        match_versions = instance_filter

    if body['kind'] == BenjiRetentionSchedule.kind:
github elemental-lf / benji / src / benji / k8s_operator / crd / backup_schedule.py View on Github external
@kopf.on.update(CRD_CLUSTER_BACKUP_SCHEDULE.api_group, CRD_CLUSTER_BACKUP_SCHEDULE.api_version,
                CRD_CLUSTER_BACKUP_SCHEDULE.plural)
def benji_backup_schedule(namespace: str, spec: Dict[str, Any], body: Dict[str, Any], logger,
                          **_) -> Optional[Dict[str, Any]]:
    schedule = spec['schedule']
    label_selector = spec['persistentVolumeClaimSelector'].get('matchLabels', None)
    namespace_label_selector = None
    if body['kind'] == CRD_BACKUP_SCHEDULE.name:
        namespace_label_selector = spec['persistentVolumeClaimSelector'].get('matchNamespaceLabels', None)

    job_name = cr_to_job_name(body, 'scheduler')
    benji.k8s_operator.scheduler.add_job(partial(backup_scheduler_job,
                                                 namespace_label_selector=namespace_label_selector,
                                                 namespace=namespace,
                                                 label_selector=label_selector,
                                                 parent_body=body,
                                                 logger=logger),
github Viasat / nhd / nhd / TriadController.py View on Github external
@kopf.on.update('', 'v1', 'nodes')
def TriadNodeUpdate(spec, old, new, meta, **_):
    logger = NHDCommon.GetLogger(__name__)
    NHDTainted = lambda obj: any([x['key'] == 'sigproc.viasat.io/nhd_scheduler' for x in obj['spec']['taints']])

    k8sq = qinst
    # If the NHD taint has been added/removed or the code has been cordoned/uncordoned, detect it here
    if (not NHDTainted(old) and NHDTainted(new)) or (('unschedulable' in old['spec'] and 'unschedulable' not in new['spec']) and NHDTainted(new)): # Uncordon
        logger.info(f'Uncordoning node {meta["name"]}')
        k8sq.put({"type": NHDWatchTypes.NHD_WATCH_TYPE_NODE_UNCORDON, "node": meta["name"]})
    elif (not NHDTainted(new) and NHDTainted(old)) or ('unschedulable' not in old['spec'] and 'unschedulable' in new['spec']): # Cordon:
        logger.info(f'Cordoning node {meta["name"]}')
        k8sq.put({"type": NHDWatchTypes.NHD_WATCH_TYPE_NODE_CORDON, "node": meta["name"]})

    # Detect NHD group changes. If the label didn't exist, or it's now different than the old one, send the new one
    if ('NHD_GROUP' not in old['metadata']['labels'] and 'NHD_GROUP' in new['metadata']['labels']) or \
       ('NHD_GROUP' in old['metadata']['labels'] and 'NHD_GROUP' in new['metadata']['labels'] and old['metadata']['labels'] != new['metadata']['labels']):
github zalando-incubator / kopf / examples / 99-all-at-once / example.py View on Github external
@kopf.on.update('zalando.org', 'v1', 'kopfexamples')
def update(body, meta, spec, status, old, new, diff, **kwargs):
    print('Handling the diff')
    pprint.pprint(list(diff))
github redhat-cop / anarchy / operator / operator.py View on Github external
@kopf.on.update(runtime.operator_domain, runtime.api_version, 'anarchyactions', labels={runtime.run_label: kopf.ABSENT})
def handle_action_activity(body, logger, **_):
    action = AnarchyAction(body)
    if not action.has_owner:
        action.set_owner(runtime)
    elif not action.has_started:
        if action.after_datetime <= datetime.utcnow():
            action.start(runtime)
        else:
            AnarchyAction.cache_put(action)
github elemental-lf / benji / src / benji / k8s_operator / crd / operator_config.py View on Github external
@kopf.on.update(CRD_OPERATOR_CONFIG.api_group, CRD_OPERATOR_CONFIG.api_version, CRD_OPERATOR_CONFIG.plural)
def reload_operator_config(name: str, namespace: str, logger, **_) -> Optional[Dict[str, Any]]:
    if namespace != service_account_namespace() or name != benji.k8s_operator.operator_config_name:
        return

    set_operator_config()
    remove_maintenance_jobs()
    install_maintenance_jobs(parent_body=benji.k8s_operator.operator_config, logger=logger)
github elemental-lf / benji / src / benji / k8s_operator / crd / backup_schedule.py View on Github external
@kopf.on.update(CRD_BACKUP_SCHEDULE.api_group, CRD_BACKUP_SCHEDULE.api_version, CRD_BACKUP_SCHEDULE.plural)
@kopf.on.resume(CRD_CLUSTER_BACKUP_SCHEDULE.api_group, CRD_CLUSTER_BACKUP_SCHEDULE.api_version,
                CRD_CLUSTER_BACKUP_SCHEDULE.plural)
@kopf.on.create(CRD_CLUSTER_BACKUP_SCHEDULE.api_group, CRD_CLUSTER_BACKUP_SCHEDULE.api_version,
                CRD_CLUSTER_BACKUP_SCHEDULE.plural)
@kopf.on.update(CRD_CLUSTER_BACKUP_SCHEDULE.api_group, CRD_CLUSTER_BACKUP_SCHEDULE.api_version,
                CRD_CLUSTER_BACKUP_SCHEDULE.plural)
def benji_backup_schedule(namespace: str, spec: Dict[str, Any], body: Dict[str, Any], logger,
                          **_) -> Optional[Dict[str, Any]]:
    schedule = spec['schedule']
    label_selector = spec['persistentVolumeClaimSelector'].get('matchLabels', None)
    namespace_label_selector = None
    if body['kind'] == CRD_BACKUP_SCHEDULE.name:
        namespace_label_selector = spec['persistentVolumeClaimSelector'].get('matchNamespaceLabels', None)

    job_name = cr_to_job_name(body, 'scheduler')
    benji.k8s_operator.scheduler.add_job(partial(backup_scheduler_job,
github redhat-cop / anarchy / operator / operator.py View on Github external
@kopf.on.update(runtime.operator_domain, runtime.api_version, 'anarchysubjects')
def handle_subject_update(body, old, new, **_):
    try:
        subject = AnarchySubject(body)
        if old['spec'] != new['spec']:
            subject.handle_spec_update(runtime)
    except AssertionError as e:
        operator_logger.warning('AnarchySubject %s invalid: %s', body['metadata']['name'], e)
github zalando-incubator / kopf / examples / 05-handlers / example.py View on Github external
@kopf.on.update('zalando.org', 'v1', 'kopfexamples')
def update_fn(old, new, diff, **kwargs):
    print('UPDATED')