def create_cron_job( pod_name: Text, schedule: Text, dry_run: bool, job_spec: client.V1JobSpec, ) -> Text: cron_job = client.V1beta1CronJob( api_version="batch/v1beta1", kind="CronJob", metadata=client.V1ObjectMeta(name=_cronjob_name(pod_name)), spec=client.V1beta1CronJobSpec( schedule=schedule, concurrency_policy="Forbid", successful_jobs_history_limit=1, failed_jobs_history_limit=1, job_template=client.V1beta1JobTemplateSpec(spec=job_spec, ), ), ) options = {"namespace": "default", "body": cron_job, "pretty": "true"} _set_dry_run(options, dry_run) try: api_response = client.BatchV1beta1Api().patch_namespaced_cron_job( **gamla.add_key_value("name", _cronjob_name(pod_name))(options)) except rest.ApiException: logging.info( f"CronJob {options.get('name')} doesn't exist, creating...") api_response = client.BatchV1beta1Api().create_namespaced_cron_job( **options) logging.info(f"CronJob updated: {api_response}.") return pod_name
def create_workflow_cronjob(cron_job: k8s.V1Job) -> None: """Create a cron-job on a k8s cluster. :param cron_job: A configured cron-job object. """ k8s.BatchV1beta1Api().create_namespaced_cron_job( body=cron_job, namespace=cron_job.metadata.namespace)
def __init__(self, bearer_token=None): ''' Initialize connection to Kubernetes ''' self.bearer_token = bearer_token api_client = None try: config.load_incluster_config() except config.config_exception.ConfigException: config.load_kube_config() if self.bearer_token: # Configure API key authorization: Bearer Token configuration = client.Configuration() configuration.api_key_prefix['authorization'] = 'Bearer' configuration.api_key['authorization'] = self.bearer_token api_client = client.ApiClient(configuration) self.client = client.CoreV1Api(api_client) self.batch_api = client.BatchV1Api(api_client) self.batch_v1beta1_api = client.BatchV1beta1Api(api_client) self.custom_objects = client.CustomObjectsApi(api_client) self.api_extensions = client.ApiextensionsV1beta1Api(api_client) self.extension_api = client.ExtensionsV1beta1Api(api_client) self.apps_v1_api = client.AppsV1Api(api_client)
def create_cronjob(username, namespace, dbhost): try: config.load_kube_config() except: config.load_incluster_config() api = client.BatchV1beta1Api() body = client.V1beta1CronJob( metadata=client.V1ObjectMeta(name=namespace), spec=client.V1beta1CronJobSpec( job_template=client.V1beta1JobTemplateSpec( spec=client.V1JobSpec(template=client.V1PodTemplateSpec( spec=client.V1PodSpec( containers=[ client.V1Container(name="scheduler", image="sahandha/lsstscheduler", args=["/bin/bash","-c","python /sched.py {} {} {};".format(username, namespace, dbhost)], resources=client.V1ResourceRequirements( requests={'memory': "200Mi", 'cpu': "100m"}) )], restart_policy="OnFailure" ))) ), schedule = "*/1 * * * *") ) try: api = api.create_namespaced_cron_job(namespace, body) except ApiException as e: print("Exception when calling BatchV1beta1Api->create_namespaced_cron_job: %s\n" % e)
def delete(self, name): DELETE_OPTIONS = client.V1DeleteOptions( propagation_policy='Foreground', grace_period_seconds=5) ignore_404(lambda: client.CoreV1Api().delete_namespaced_secret( secret_id(name), current_namespace(), body=DELETE_OPTIONS)) client.BatchV1beta1Api().delete_namespaced_cron_job( cron_job_id(name), current_namespace(), body=DELETE_OPTIONS)
def _detect_api_object(self, api_version): # Due to https://github.com/kubernetes-client/python/issues/387 if api_version == 'apps/v1beta1': return client.AppsV1beta1Api() if api_version == 'v1': return client.CoreV1Api() if api_version == 'extensions/v1beta1': return client.ExtensionsV1beta1Api() if api_version == 'batch/v1': return client.BatchV1Api() if api_version == 'batch/v2alpha1': return client.BatchV2alpha1Api() if api_version == 'batch/v1beta1': return client.BatchV1beta1Api() if api_version == 'policy/v1beta1': return client.PolicyV1beta1Api() if api_version == 'storage.k8s.io/v1': return client.StorageV1Api() if api_version == 'apps/v1': return client.AppsV1Api() if api_version == 'autoscaling/v1': return client.AutoscalingV1Api() if api_version == 'rbac.authorization.k8s.io/v1': return client.RbacAuthorizationV1Api() if api_version == 'scheduling.k8s.io/v1alpha1': return client.SchedulingV1alpha1Api() if api_version == 'scheduling.k8s.io/v1beta1': return client.SchedulingV1beta1Api() if api_version == 'test/test': return K8sClientMock(self.name)
def get_api_functions(namespace: str = None) -> K8sFetcherFunctions: v1apps = client.AppsV1Api() v1core = client.CoreV1Api() v1batch = client.BatchV1beta1Api() if namespace: get_replica_set_fn = partial(v1apps.list_namespaced_replica_set, namespace) get_pod_fn = partial(v1core.list_namespaced_pod, namespace) get_deployment_fn = partial(v1apps.list_namespaced_deployment, namespace) get_stateful_set_fn = partial(v1apps.list_namespaced_stateful_set, namespace) get_daemon_set_fn = partial(v1apps.list_namespaced_daemon_set, namespace) get_cronjob_fn = partial(v1batch.list_namespaced_cron_job, namespace) else: get_replica_set_fn = partial(v1apps.list_replica_set_for_all_namespaces) get_pod_fn = partial(v1core.list_pod_for_all_namespaces) get_deployment_fn = partial(v1apps.list_deployment_for_all_namespaces) get_stateful_set_fn = partial(v1apps.list_stateful_set_for_all_namespaces) get_daemon_set_fn = partial(v1apps.list_daemon_set_for_all_namespaces) get_cronjob_fn = partial(v1batch.list_cron_job_for_all_namespaces) return K8sFetcherFunctions( get_deployment_fn, get_pod_fn, get_replica_set_fn, get_stateful_set_fn, get_daemon_set_fn, get_cronjob_fn, )
def fetch_corn_jobs(self, ns=None): api = client.BatchV1beta1Api(self.client) if not ns: result = api.list_cron_job_for_all_namespaces().items else: result = api.list_namespaced_cron_job(ns).items return result
def create_cronjob_client(self, path, project_name): """ method to create a cronjob in kubernetes :param path: path of the yaml file :param project_name: project name of which the cronjob is a part :return: status (True/Error code) """ self.logger.info('Entering create_cronjob_client') try: with open(path) as f: dep = yaml.safe_load(f) k8s_beta = client.BatchV1beta1Api() # collecting response from API r = k8s_beta.create_namespaced_cron_job( namespace=project_utils.modify_string_for_deployment( project_name), body=dep) self.logger.debug("Cron Job created. Details : {}".format( str(r))) self.logger.info('exiting create_cronjob_client') return True except ApiException as e: if e.status == 409: # in case of conflict, patch the cronjob self.patch_cronjob_client(path, project_name) return True self.logger.error('Creation of cron job failed. Exiting.') raise CronjobCreationFailedException
def update_cron_job(sender, instance, created, **kwargs): context = Context({ 'schedule': instance.schedule, 'command': mark_safe(instance.command.split(' ')), 'disabled': not instance.enabled, 'name': instance.name }) cron_definition = yaml.load( render_to_string('cron_test/sample.yml', context)) # Load the default configuration of the cluster config.load_incluster_config() kube_cron_job_client = client.BatchV1beta1Api() cron_job = client.V1beta1CronJob() for key, value in cron_definition.items(): setattr(cron_job, key, value) if created: try: kube_cron_job_client.create_namespaced_cron_job( 'cron-poc', cron_job) except Exception as e: logger.info("Error creating cron job:%s" % e) else: try: kube_cron_job_client.replace_namespaced_cron_job( instance.name, 'cron-poc', cron_job) except Exception as e: logger.info("Error updating cron job: %s" % e)
def list(self): batch_v1beta1 = client.BatchV1beta1Api() cron_jobs = batch_v1beta1.list_namespaced_cron_job( current_namespace(), label_selector='type=carconnector') return [ cron_job.metadata.labels['name'] for cron_job in cron_jobs.items ]
def sync_cron_jobs(): config.load_incluster_config() kube_cron_job_client = client.BatchV1beta1Api() cron_jobs = kube_cron_job_client.list_namespaced_cron_job('cron-poc') new_jobs = [] for cron_job in cron_jobs['items']: name = cron_job['metadata']['name'] if not CronJob.objects.filter(name=name).exists(): command = " ".join( cron_job['spec']['job_template']['spec']['template']['spec'] ['containers'][0]['command']) schedule = cron_job['spec']['schedule'] enabled = not cron_job['spec']['suspend'] minute, hour, day_of_month, month, day_of_week = schedule.split( ' ') job = { 'name': name, 'command': command, 'enabled': enabled, 'minute': minute, 'hour': hour, 'day_of_month': day_of_month, 'day_of_week': day_of_week } new_jobs.append(CronJob(**job))
def __init__(self, namespace: str): self.client = client self.appsV1Api = client.AppsV1Api() self.coreV1Api = client.CoreV1Api() self.batchV1Api = client.BatchV1Api() self.namespace = namespace self.batchV1beta1Api = client.BatchV1beta1Api()
def add_to_cronjobs(projects): api = client.BatchV1beta1Api() add_to( projects, "cronjob.batc", api.list_cron_job_for_all_namespaces, api.patch_namespaced_cron_job, )
def init_api(): """Creates instances of the incluster config and client API and stores them in global""" g.configuration = config.load_incluster_config() g.apps_v1_api_instance = client.AppsV1Api(client.ApiClient( g.configuration)) g.batch_v1beta1_instance = client.BatchV1beta1Api( client.ApiClient(g.configuration)) g.PD_REGISTRY = current_app.config['PD_REGISTRY']
def get_resource_api(api_client: client.ApiClient = None, **kwargs) -> "client.BatchV1beta1Api": """ Returns an instance of the kubernetes API client associated with this object. """ if api_client: kwargs["apl_client"] = api_client return client.BatchV1beta1Api(**kwargs)
def api_client_from_version(api_version): return { "v1": kube_client.CoreV1Api(), "apps/v1": kube_client.AppsV1Api(), "batch/v1": kube_client.BatchV1Api(), "batch/v1beta1": kube_client.BatchV1beta1Api(), "extensions/v1beta1": kube_client.ExtensionsV1beta1Api(), "rbac.authorization.k8s.io/v1": kube_client.RbacAuthorizationV1Api(), }[api_version]
def confirm_cron_job(context, namespace): if context is None: raise SystemExit("invalid empty context for CronJob given") if namespace is None: raise SystemExit("invalid empty namespace for CronJob given") load_kube(context) api = client.BatchV1beta1Api() return general_confirm( "CronJob", lambda: api.list_namespaced_cron_job(namespace=namespace), lambda i: i.metadata.name)
def delete_cronjob(namespace: str, name: str) -> None: """Delete a cron-job on a k8s cluster. :param namespace: Namespace in which to look for the secret to delete. :param name: The name of the secret to be deleted. """ k8s.BatchV1beta1Api().delete_namespaced_cron_job( name=name, namespace=namespace, body=k8s.V1DeleteOptions(propagation_policy='Background'))
def get(self, name): batch_v1beta1 = client.BatchV1beta1Api() print("here comes to get cronjob") print(cron_job_id(name)) cron_job = batch_v1beta1.read_namespaced_cron_job( cron_job_id(name), current_namespace()) print("show me the cronjob") print(cron_job) secret = ignore_404(lambda: client.CoreV1Api().read_namespaced_secret( secret_id(name), current_namespace())) return ConnectorConfig(cron_job=cron_job, secret_env_vars=secret)
def __init__(self): # https://github.com/kubernetes-client/python/issues/309 warnings.simplefilter("ignore", ResourceWarning) self.config = config.load_kube_config() self.k8s_client = client.ApiClient() self.core_v1 = client.CoreV1Api() self.apps_v1 = client.AppsV1Api() self.batch_v1_beta1 = client.BatchV1beta1Api() self.custom_objects_api = client.CustomObjectsApi()
def __init__(self, namespace: str = "default"): if ENVIRONMENT == "production": config.load_incluster_config() else: config.load_kube_config() self.core_v1 = client.CoreV1Api() self.batch_v1 = client.BatchV1Api() self.batch_v1beta1 = client.BatchV1beta1Api() self.namespace = namespace self.cronjobs = None self.jobs = None self.pods = None self.sync_resouces()
def remove_cron_job(context, namespace, name): if context is None: raise SystemExit("invalid empty context for CronJob given") if namespace is None: raise SystemExit("invalid empty namespace for CronJob given") if name is None: raise SystemExit("invalid empty name for CronJob given") load_kube(context) api = client.BatchV1beta1Api() ret, status, _ = api.delete_namespaced_cron_job_with_http_info( name, namespace=namespace) handle_status(ret, status, "CronJob", namespace, name)
def __init__(self): ''' Initialize connection to Kubernetes ''' try: config.load_incluster_config() except config.config_exception.ConfigException: config.load_kube_config() self.client = client.CoreV1Api() self.batch_api = client.BatchV1Api() self.batch_v1beta1_api = client.BatchV1beta1Api() self.extension_api = client.ExtensionsV1beta1Api()
def delete_cronjob(namespace): try: config.load_kube_config() except: config.load_incluster_config() api = client.BatchV1beta1Api() body = client.V1DeleteOptions(propagation_policy="Foreground") try: api = api.delete_namespaced_cron_job(namespace, namespace, body) except ApiException as e: print("Exception when calling BatchV1beta1Api->delete_namespaced_cron_job: %s\n" % e)
def wait_for_cron_job_is_up(context, namespace, name): if name is None: raise SystemExit("invalid empty name for CronJob given") if context is None: raise SystemExit("invalid empty name context given") if namespace is None: raise SystemExit("invalid empty name namespace given") load_kube(context) print("check availability of", "CronJob", name, "in namespace", namespace) api = client.BatchV1beta1Api() return general_up_check( namespace, "CronJob", name, lambda: api.read_namespaced_cron_job_status_with_http_info( name, namespace=namespace))
def __init__(self): """ Note that in cluster you'll use the in cluster config, meanwhile when you try it on your local, you'll use your own kubeconfig. """ try: config.load_incluster_config() except: config.load_kube_config() self.configuration = client.Configuration() self.api_instance = client.BatchV1Api( client.ApiClient(self.configuration)) self.api_instance_v1_beta = client.BatchV1beta1Api( client.ApiClient(self.configuration))
def run(self, args): if args.context: context = args.context else: context = confirm_context() load_kube(context) api = client.BatchV1beta1Api() print("Listing cronjobs in all namespaces of context:", context) ret = api.list_cron_job_for_all_namespaces() print("results:", len(ret.items)) for i in ret.items: print("%s\t%s\t%s\t" % (i.status.active, i.metadata.namespace, i.metadata.name))
def __init__(self,run_date="",configpath="/mnt/consumerhub/config/kubernetes/config.ini"): """ Initiates the K8s object :param configpath: path for the k8s config """ config.load_kube_config(configpath) self.api_instance = client.BatchV1Api() self.batch_instance= client.BatchV1beta1Api() if run_date: self.run_date=run_date else: self.run_date=datetime.date.today().isoformat() self.logger=loggerfunc(servicename="kubernetes",run_date=self.run_date) self.api_pods = client.CoreV1Api()
def get_cronjob(cronjob_name, namespace): configuration = client.Configuration() # API client for cronjobs batch = client.BatchV1beta1Api(client.ApiClient(configuration)) try: cronjobs = batch.list_namespaced_cron_job(namespace).items for job in cronjobs: # cronjob names must be unique if job.metadata.name == cronjob_name: return job except ApiException as e: logging.critical( "Exception when calling BatchV1Api->list_namespaced_cron_job: %s\n" % e) return False