Ejemplo n.º 1
0
    def test_fails_if_no_zone(self):

        v = MagicMock(annotations={
            'pv.kubernetes.io/provisioned-by': 'kubernetes.io/gce-pd'
        },
                      labels={})
        with pytest.raises(AnnotationError):
            rule_from_pv(volume=v, api=None, deltas_annotation_key='test')
Ejemplo n.º 2
0
def test_rule_from_volume_with_claim(
    deltas_annotation_key,
    fx_deltas,
    volume_zone_label,
    provisioner_annotation,
):
    ctx = Context({'deltas_annotation_key': deltas_annotation_key})

    pv, pvc = make_volume_and_claim(
        ctx=ctx,
        volume_annotations=provisioner_annotation,
        claim_annotations={
            ctx.config['deltas_annotation_key']: fx_deltas,
        },
        volume_zone_label=volume_zone_label,
    )

    if pvc.namespace and not pvc.namespace == 'default':
        expected_rule_name = f'pvc-{pvc.namespace}-{pvc.name}'
    else:
        expected_rule_name = f'pvc-{pvc.name}'

    loop = asyncio.get_event_loop()
    with mock_kube([pv, pvc]) as _mocked:
        deltas_annotation_key = ctx.config['deltas_annotation_key']
        rule = loop.run_until_complete(
            rule_from_pv(ctx, pv, deltas_annotation_key))
        assert rule.source == pvc.obj['metadata']['selfLink']
        assert deltas_annotation_key in pvc.annotations
        assert rule.name == expected_rule_name
        assert rule.deltas == parse_deltas(fx_deltas)
Ejemplo n.º 3
0
def sync_get_rules(ctx):
    rules = {}
    api = ctx.make_kubeclient()

    _logger.debug('volume-events.watch')
    stream = pykube.objects.PersistentVolume.objects(api).watch().object_stream()

    for event in stream:
        volume_name = event.object.name
        _log = _logger.new(
            volume_name=volume_name,
            volume_event_type=event.type,
            volume=event.object.obj,
        )

        _log.debug('volume-event.received')

        if event.type == 'ADDED' or event.type == 'MODIFIED':
            rule = None
            try:
                rule = rule_from_pv(
                    event.object,
                    api,
                    ctx.config.get('deltas_annotation_key'),
                    use_claim_name=ctx.config.get('use_claim_name'))
            except AnnotationNotFound as exc:
                _log.info(
                    events.Annotation.NOT_FOUND,
                    exc_info=exc,
                )
            except AnnotationError:
                _log.exception(events.Annotation.ERROR)

            if rule:
                _log.bind(
                    rule=rule
                )
                if event.type == 'ADDED' or volume_name not in rules:
                    _log.info(events.Rule.ADDED)
                else:
                    _log.info(events.Rule.UPDATED)
                rules[volume_name] = rule
            else:
                if volume_name in rules:
                    _log.info(events.Rule.REMOVED)
                    rules.pop(volume_name, False)
        elif event.type == 'DELETED':
            _log.info(events.Rule.REMOVED)
            rules.pop(volume_name, False)
        else:
            _log.warning('Unhandled event')

        yield list(rules.values())

    _logger.debug('sync-get-rules.done')
Ejemplo n.º 4
0
def test_rule_from_volume(fx_context: Context, fx_deltas: str,
                          label_zone: Optional[Dict[str, str]],
                          annotation_provisioned_by: Optional[Dict[str, str]],
                          _spec_gce_persistent_disk: Optional[Dict[str, Any]]):
    annotations = {}

    annotations.update({
        fx_context.config['deltas_annotation_key']: fx_deltas,
    })

    if annotation_provisioned_by is not None:
        annotations.update(annotation_provisioned_by)

    labels = {}

    if label_zone is not None:
        labels.update(label_zone)

    spec = {}

    if _spec_gce_persistent_disk is not None:
        spec.update(_spec_gce_persistent_disk)

    source_pv = make_resource(
        pykube.objects.PersistentVolume,
        'source-pv',
        annotations=annotations,
        labels=labels,
        spec=spec,
    )

    expected_rule_name = f'pv-{source_pv.name}'

    loop = asyncio.get_event_loop()

    with mock_kube([source_pv]):
        rule: Rule = loop.run_until_complete(
            rule_from_pv(fx_context, source_pv,
                         fx_context.config['deltas_annotation_key']))
        assert rule.name == expected_rule_name
        assert rule.deltas == parse_deltas(fx_deltas)