Example #1
0
def create_git_config_synchronizer(spec, namespace):
    if 'git-url' not in spec.keys():
        raise kopf.HandlerFatalError(f"git-url must be set.")
    if 'config-map' not in spec.keys():
        raise kopf.HandlerFatalError(f"config-map must be set.")

    git_url = spec['git-url']
    logger.info(f'git-url = {git_url}')
    config_map = spec['config-map']
    logger.info(f'config-map = {config_map}')

    _kwargs = {}
    for k in {
            'git-branch', 'git-username', 'git-token',
            UPDATE_EVERY_SECOND_PROPERTY
    }:
        if k in spec:
            logger.info(f'{k} = {spec[k]}')
            _kwargs[k.replace('-', '_')] = spec[k]

    config = RemoteGitConfig(git_url, **_kwargs)

    config_map = K8sConfigMap(config_map, namespace, config)

    asyncio.run(config.when_updated(config_map.publish))

    msg = f"configmap {config_map} created from git repo {git_url}"

    return msg
Example #2
0
def create_web_server(server_type, name):
    if server_type in __servers_conf:
        port, image = __servers_conf[server_type]
        return Webserver(name, port, server_type, image)

    raise kopf.HandlerFatalError(
        f"Type must be one of {list(__servers_conf.keys())}")
def create_workflow(**kwargs):
    body = kwargs['body']
    attributes = body['spec']['metadata']['service'][0]['attributes']

    # Make sure type did is provided
    if not attributes:
        raise kopf.HandlerFatalError(f"Workflow error. Got {attributes}.")

    # Pod template
    kopf.info(body,
              reason='workflow with type {}'.format(
                  attributes['main']['type']))
    for stage in attributes['workflow']['stages']:
        logger.info(
            f"Stage {stage['index']} with stageType {stage['stageType']}")
        logger.info(
            f"Running container {stage['requirements']['container']['image']}"
            f":{stage['requirements']['container']['tag']}")

    # Configmap for workflow
    create_configmap_workflow(body, logger)

    # Volume
    create_pvc(body, logger)

    # Configure pod
    create_configure_job(body, logger)
    # Wait configure pod to finish
    while not wait_finish_job(body['metadata']['namespace'],
                              f"{body['metadata']['name']}-configure-job"):
        logger.info("Waiting configure pod to finish")
        time.sleep(10.0)

    # Algorithm job
    create_algorithm_job(body, logger)
    # Wait configure pod to finish
    while not wait_finish_job(body['metadata']['namespace'],
                              f"{body['metadata']['name']}-algorithm-job"):
        logger.info("Waiting algorithm pod to finish")
        time.sleep(10.0)

    # Publish job
    create_publish_job(body, logger)
    while not wait_finish_job(body['metadata']['namespace'],
                              f"{body['metadata']['name']}-publish-job"):
        logger.info("Waiting publish pod to finish")
        time.sleep(10.0)

    return {'message': "Creating workflow finished"}
Example #4
0
def update_fn(spec, status, namespace, logger, **kwargs):

    size = spec.get('size', None)
    if not size:
        raise kopf.HandlerFatalError(f"Size must be set. Got {size!r}.")

    pvc_name = status['create_fn']['pvc-name']
    pvc_patch = {'spec': {'resources': {'requests': {'storage': size}}}}

    api = kubernetes.client.CoreV1Api()
    obj = api.patch_namespaced_persistent_volume_claim(
        namespace=namespace,
        name=pvc_name,
        body=pvc_patch,
    )

    logger.info(f"PVC child is updated: %s", obj)
def create_fn(body, spec, **kwargs):
    log.info("Operator create function called")
    print(f"A handler is called with body: {body}")

    # Get info from resource object
    size = spec['size']
    name = body['metadata']['name']
    namespace = body['metadata']['namespace']
    image = 'nginx'

    if not size:
        raise kopf.HandlerFatalError(f"size must be set. Got {size}.")
    # Pod template
    pod = {
        'apiVersion': 'v1',
        'metadata': {
            'name': name,
            'labels': {
                'app': 'deepak'
            }
        },
        'spec': {
            'containers': [{
                'image': image,
                'name': name
            }]
        }
    }

    # Make the Pod for resource object
    kopf.adopt(pod, owner=body)

    # Object used to communicate with the API Server
    api = kubernetes.client.CoreV1Api()

    # Create Pod
    obj = api.create_namespaced_pod(namespace, pod)
    print(f"Pod {obj.metadata.name} created")

    # Update status
    msg = f"Pod created for resource object {name}"
    return {'message': msg}
Example #6
0
def create_fn(meta, body, spec, namespace, logger, **kwargs):

    name = meta.get('name')
    size = spec.get('size')
    if not size:
        raise kopf.HandlerFatalError(f"Size must be set. Got {size!r}.")

    path = os.path.join(os.path.dirname(__file__), './manifest/pvc.yaml')
    tmpl = open(path, 'rt').read()
    text = tmpl.format(name=name, size=size)
    data = yaml.safe_load(text)

    kopf.adopt(data, owner=body)

    api = kubernetes.client.CoreV1Api()
    obj = api.create_namespaced_persistent_volume_claim(
        namespace=namespace,
        body=data,
    )

    logger.info(f"PVC child is created: %s", obj)

    return {'pvc-name': obj.metadata.name}
Example #7
0
def create_fn(body, spec, meta, status, **kwargs):
    # Get info from Database object
    name = body['metadata']['name']
    namespace = body['metadata']['namespace']
    db_type = spec['type']
    tag = spec['tag'] if spec['tag'] else 'latest'

    # Make sure type is provided
    if not db_type:
        raise kopf.HandlerFatalError(f'Type must be set. Got {db_type}.')

    # Pod template
    pod = {
        'apiVersion': 'v1',
        'metadata': {
            'name': name,
            'labels': {
                'app': 'db'
            }
        }
    }

    # Service template
    svc = {
        'apiVersion': 'v1',
        'metadata': {
            'name': name
        },
        'spec': {
            'selector': {
                'app': 'db'
            },
            'type': 'NodePort'
        }
    }

    # Update templates based on Database specification
    image = f'{db_type}:{tag}'
    pod['spec'] = {'containers': [{'image': image, 'name': db_type}]}
    if db_type == 'mongo':
        port = 27017
    if db_type == 'mysql':
        port = 3306
        pod['spec']['containers'][0]['env'] = [{
            'name': 'MYSQL_ROOT_PASSWORD',
            'value': 'my_passwd'
        }]
    svc['spec']['ports'] = [{'port': port, 'targetPort': port}]

    # Make the Pod and Service the children of the Database object
    kopf.adopt(pod, owner=body)
    kopf.adopt(svc, owner=body)

    # Object used to communicate with the API Server
    api = kubernetes.client.CoreV1Api()

    # Create Pod
    obj = api.create_namespaced_pod(namespace, pod)
    print(f"Pod {obj.metadata.name} created")

    # Create Service
    obj = api.create_namespaced_service(namespace, svc)
    print(
        f"NodePort Service {obj.metadata.name} created, exposing on port {obj.spec.ports[0].node_port}"
    )

    # Update status
    msg = f"Pod and Service created by Database {name}"
    return {'message': msg}
def create_fn(body, spec, **kwargs):

    # Get info from cassandra object
    name = body['metadata']['name']
    namespace = body['metadata']['namespace']
    image = spec['image']
    nodeport = spec['nodeport']
    port = spec['port']
    if not nodeport:
        raise kopf.HandlerFatalError(f"Nodeport must be set. Got {nodeport}.")

    # Pod template
    pod = {
        'apiVersion': 'v1',
        'metadata': {
            'name': name,
            'labels': {
                'app': 'cassandra'
            }
        },
        'spec': {
            'containers': [{
                'image': image,
                'name': name
            }]
        }
    }

    # Service template
    svc = {
        'apiVersion': 'v1',
        'metadata': {
            'name': name
        },
        'spec': {
            'selector': {
                'app': 'nginx'
            },
            'type': 'NodePort',
            'ports': [{
                'port': port,
                'targetPort': port,
                'nodePort': nodeport
            }]
        }
    }

    # Make the Pod and Service the children of the cassandra object
    kopf.adopt(pod, owner=body)
    kopf.adopt(svc, owner=body)

    # Object used to communicate with the API Server
    api = kubernetes.client.CoreV1Api()

    # Create Pod
    obj = api.create_namespaced_pod(namespace, pod)
    print(f"Pod {obj.metadata.name} created")

    # Create Service
    obj = api.create_namespaced_service(namespace, svc)
    print(
        f"NodePort Service {obj.metadata.name} created, exposing on port {obj.spec.ports[0].node_port}"
    )

    # Update status
    msg = f"Pod and Service created for cassandra object {name}"
    return {'message': msg}