def cli(ctx): conf = config() cli_plugin_context = dict( conf=conf, kubectl=lambda cluster_name, command: k.discover_conn(cluster_name) (conf).cmd(command, cluster_name)) ctx.obj = cli_plugin_context
def gen_template(templates: List): conf = config() k_ctl = noop_conn()(conf) t_ctx = TemplateContext(conf, k_ctl) file_path = '{}/k8s.yaml'.format(conf.output_dir) conf.fs.delete(file_path) return pipeline(templates, lambda template, cur_ctx: _plan_template(conf, template, cur_ctx, file_path), t_ctx) \ | E.on | dict(success=lambda _: conf.log.info('Writing k8s template to {}'.format(file_path))) \ | E.on | dict(success=lambda _: conf.log.info('DONE'), failure=lambda e: conf.log.error('FAILED: {}'.format(e))) \ | E.on | dict(failure=lambda e: sys.exit(1))
def _run_dsl(_args): project_id = "someId" service_account_key = {"key": "someKey"} service_account_b64 = b64encode( json.dumps(service_account_key).encode('utf-8')).decode('utf-8') conf = config() k_ctl = gke_conn(cluster_regex='.*', project_id=project_id, service_account_b64=service_account_b64)(conf) provider: GKEProvider = k_ctl.get_provider() assert provider.get_project_id() == project_id
def _run_dsl(_args): project_id = "someProjectId" conf = config() with patch('sys.exit', return_value=b64encode('{"error": "RaisedError"}'.encode( 'utf-8')).decode('utf-8')): k_ctl = gke_conn(cluster_regex='.*', project_id=project_id)(conf) provider: GKEProvider = k_ctl.get_provider() assert provider.get_service_account_info( )['error'] == 'RaisedError'
def _run_dsl(_args): project_id = None service_account_key = {"key": "someKey"} service_account_b64 = b64encode( json.dumps(service_account_key).encode('utf-8')).decode('utf-8') conf = config() with patch('sys.exit', return_value="RaisedError"): k_ctl = gke_conn(cluster_regex='.*', project_id=project_id, service_account_b64=service_account_b64)(conf) provider: GKEProvider = k_ctl.get_provider() assert provider.get_project_id() == "RaisedError"
def _run_dsl(args): os: MockOsSystem = args['os_system'] project_id = "someId" os.mock_get_env('KG_PROJECT_ID', return_values=[project_id]) os.mock_get_env('KG_DELETE', return_values=["true"]) os.mock_get_env('KG_TEMPLATE', return_values=["false"]) service_account_key = {'key': 'someKey'} service_account_b64 = b64encode( json.dumps(service_account_key).encode('utf-8')).decode('utf-8') conf = config() k_ctl = gke_conn(cluster_regex='.*', project_id=project_id, service_account_b64=service_account_b64)(conf) provider: GKEProvider = k_ctl.get_provider() assert provider.get_project_id() == project_id
def _run_dsl(args): os: MockOsSystem = args['os_system'] project_id = "someId" service_account_key = {"key": "someKey"} service_account_b64 = b64encode( json.dumps(service_account_key).encode('utf-8')).decode('utf-8') os.mock_get_env('KG_SERVICE_ACCOUNT_B64', return_values=[service_account_b64]) os.mock_get_env('KG_DELETE', return_values=["true"]) os.mock_get_env('KG_TEMPLATE', return_values=["false"]) conf = config() k_ctl = gke_conn(cluster_regex='.*', project_id=project_id)(conf) provider: GKEProvider = k_ctl.get_provider() assert_same_dict(provider.get_service_account_info(), service_account_key)
def run_output(): conf = config() cluster_names = conf.fs.directories_in(conf.output_dir) if len(cluster_names) == 0: print("No cluster directories in output folder") return def _run_in_cluster(cluster_name): template_files = conf.fs.files_in('{}/{}'.format( conf.output_dir, cluster_name)) templates = [conf.fs.read(i) for i in template_files] k_ctl = k.discover_conn(cluster_name)(conf) return k_ctl.apply(templates, cluster_name) chain(cluster_names, lambda cluster_name: _run_in_cluster(cluster_name)) \ | E.on | dict(success=lambda c: conf.log.info('DONE:'), failure=lambda e: conf.log.error('FAILED: {}'.format(e)))
def run_in_cluster(templates: List, conn: Callable[[Config], KubeCtl] = None): conf = config() if conn is None: conn = discover_conn() k_ctl = conn(conf) t_ctx = TemplateContext(conf, k_ctl) conf.log.info("KROGON:") conf.log.info("version: {}".format(conf.krogon_version)) conf.log.info("deleting: {}".format(conf.deleting)) conf.log.info("template_output: {}".format(conf.output_template)) conf.log.info('Kubectl conn: {}'.format(k_ctl)) return _run_in_cluster(conf, templates, t_ctx, k_ctl) \ | E.on | dict(success=lambda _: conf.log.info('DONE'), failure=lambda e: conf.log.error('FAILED: {}'.format(e))) \ | E.on | dict(failure=lambda e: sys.exit(1))