async def set_snapshot_labels( ctx: Context, snapshot: Dict, labels: Dict, gcloud: Optional[GoogleResource]=None, ): _log = _logger.new( snapshot_name=snapshot['name'], labels=labels, ) gcloud = gcloud or ctx.gcloud() body = { 'labels': labels, 'labelFingerprint': snapshot['labelFingerprint'], } _log.debug( 'snapshot.set-labels', key_hints=['body.labels'], body=body, ) return await run_in_executor( gcloud.snapshots().setLabels( resource=snapshot['name'], project=ctx.config['gcloud_project'], body=body, ).execute )
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)
async def create_snapshot( ctx: Context, disk_name: str, disk_zone: str, snapshot_name: str, snapshot_description: str, *, gcloud: Optional[GoogleResource]=None ) -> Dict: gcloud = gcloud or ctx.gcloud() _log = _logger.new( disk_name=disk_name, disk_zone=disk_zone, snapshot_name=snapshot_name, snapshot_description=snapshot_description ) request_body = { 'name': snapshot_name, 'description': snapshot_description } # TODO _log.info( events.Snapshot.START, key_hints=['snapshot_name', 'rule.name'], request=request_body, ) return await run_in_executor( gcloud.disks().createSnapshot( disk=disk_name, project=ctx.config['gcloud_project'], zone=disk_zone, body=request_body ).execute )
def setUp(self): self.mock_context = Context({}) self.rule = Rule(name='test_rule', deltas=[timedelta(hours=1), timedelta(days=30)], backend='test_backend', disk=self.TEST_DISK)
def get_project_id(ctx: Context): if not ctx.config['gcloud_project']: response = requests.get( 'http://metadata.google.internal/computeMetadata/v1/project/project-id', headers={'Metadata-Flavor': 'Google'}) response.raise_for_status() ctx.config['gcloud_project'] = response.text return ctx.config['gcloud_project']
async def get_snapshot( ctx: Context, snapshot_name: str, *, gcloud: Optional[GoogleResource]=None ) -> Dict: gcloud = gcloud or ctx.gcloud() return await run_in_executor( gcloud.snapshots().get( snapshot=snapshot_name, project=ctx.config['gcloud_project'] ).execute )
async def daemon(config, *, loop=None): """Main app; it runs two tasks; one schedules backups, the other one executes the. """ loop = loop or asyncio.get_event_loop() ctx = Context(config) # Using this channel, we can trigger a refresh of the list of # disk snapshots in the Google Cloud. snapshot_reload_trigger = Channel() # The backup task consumes this channel for the next backup task. scheduling_chan = Channel() schedule_task = asyncio.ensure_future( scheduler(ctx, scheduling_chan, snapshot_reload_trigger)) backup_task = asyncio.ensure_future( backuper(ctx, scheduling_chan, snapshot_reload_trigger)) tasks = [schedule_task, backup_task] _logger.debug('Gathering tasks', tasks=tasks) try: await asyncio.gather(*tasks) except asyncio.CancelledError: _logger.exception( 'Received CancelledError', tasks=tasks ) for task in tasks: task.cancel() _logger.debug('daemon cancelled task', task=task) while True: finished, pending = await asyncio.wait( tasks, return_when=asyncio.FIRST_COMPLETED) _logger.debug( 'task completed', finished=finished, pending=pending) if not pending: _logger.debug('all tasks done') raise
async def get_zone_operation( ctx: Context, zone: str, operation_name: str, *, gcloud: Optional[GoogleResource]=None ): gcloud = gcloud or ctx.gcloud() return await run_in_executor( gcloud.zoneOperations().get( project=ctx.config['gcloud_project'], zone=zone, operation=operation_name ).execute )
def fx_mock_context_kube_config(): with mock.patch('k8s_snapshots.context.Context.load_kube_config', return_value=KUBE_CONFIG) as _mock: assert Context().load_kube_config() == KUBE_CONFIG yield _mock
def fx_context(request): request.getfixturevalue('fx_mock_context_kube_config') request.getfixturevalue('fx_mock_context_kube_client') ctx = Context( {'deltas_annotation_key': 'test.k8s-snapshots.example/deltas'}) return ctx