Exemplo n.º 1
0
async def rule_from_persistent_volume_claim(
    ctx: Context,
    volume_claim: pykube.objects.PersistentVolumeClaim
) -> Optional[Rule]:
    """
    If a `PersistentVolumeClaim` is annotated, we create a rule
    based on those annotations, for the disk that the claim is bound to.

    If the claim is currently unbound, we return `None`. We do not have
    have to worry about being notified of any future binding, since
    Kubernetes will update the `PersistentVolumeClaim` resource when
    that happens, so we will see that update.
    """
    _log = _logger.new(resource=volume_claim, volume_claim=volume_claim.obj)

    try:
        _log.debug('Checking volume claim for deltas')
        deltas = get_deltas(
            volume_claim.annotations, ctx.config.get('deltas_annotation_key'))
    except AnnotationNotFound as exc:
        _log.exception(
            events.Annotation.NOT_FOUND,
            key_hints=['volume_claim.metadata.name'],
        )
        return
    except AnnotationError:
        _log.exception(
            events.Annotation.ERROR,
            key_hints=['volume_claim.metadata.name'],
        )
        return

    try:
        volume = await volume_from_pvc(ctx, volume_claim)
    except VolumeNotFound:
        _log.warning(
            events.Rule.PENDING,
            reason='Volume claim is not bound',
            key_hints=['volume_claim.metadata.name'],
        )
        return

    return await rule_from_pv(
        ctx,
        volume,
        deltas=deltas,
        source=volume_claim
    )
Exemplo n.º 2
0
async def rule_from_persistent_volume(
    ctx: Context,
    volume: pykube.objects.PersistentVolume
) -> Optional[Rule]:
    _log = _logger.new(resource=volume)

    volume_name = volume.name
    _log = _log.bind(
        volume_name=volume_name,
        volume=volume.obj,
    )

    try:
        _log.debug('Checking volume for deltas')
        deltas = get_deltas(volume.annotations,
                            ctx.config.get('deltas_annotation_key'))
    except AnnotationNotFound as exc:
        _log.info(
            events.Annotation.NOT_FOUND,
            key_hints=['volume.metadata.name'],
            exc_info=exc,
        )
        return
    except AnnotationError:
        _log.exception(
            events.Annotation.ERROR,
            key_hints=['volume.metadata.name'],
        )
        return

    try:
        return await rule_from_pv(ctx, volume, deltas, source=volume)
    except UnsupportedVolume as exc:
        _log.info(
            events.Volume.UNSUPPORTED,
            key_hints=['volume.metadata.name'],
            exc_info=exc,
        )