Esempio n. 1
0
def reset_kubeadm():
    """Uninstall kubernetes on a node.

    Runs `kubeadm reset` on the specified machine in order to remove the
    kubernetes services and undo all configuration set by `kubeadm init`.

    """
    # Get script from path.
    script = os.path.join(os.path.dirname(__file__), 'mega-reset.sh')
    ctx.download_resource(
        os.path.join('scripts', 'mega-reset.sh'), script
    )

    # Get worker.
    conn = MistConnectionClient()
    machine = conn.get_machine(
        cloud_id=ctx.instance.runtime_properties['cloud_id'],
        machine_id=ctx.instance.runtime_properties['machine_id'],
    )

    ctx.logger.info('Running "kubeadm reset" on %s', machine)

    _add_run_remove_script(
        cloud_id=machine.cloud.id,
        machine_id=machine.id,
        script_path=os.path.abspath(script),
        script_name='kubeadm_reset_%s' % random_string(length=4)
    )
Esempio n. 2
0
def configure_kubernetes_master():
    """Configure the kubernetes master.

    Sets up the master node and stores the necessary settings inside the node
    instance's runtime properties, which are required by worker nodes in order
    to join the kubernetes cluster.

    """
    ctx.logger.info('Setting up kubernetes master node')
    prepare_kubernetes_script()

    conn = MistConnectionClient()
    machine = conn.get_machine(
        cloud_id=ctx.instance.runtime_properties['cloud_id'],
        machine_id=ctx.instance.runtime_properties['machine_id'],
    )

    # Token for secure master-worker communication.
    token = '%s.%s' % (random_string(length=6), random_string(length=16))
    ctx.instance.runtime_properties['master_token'] = token.lower()

    # Store kubernetes dashboard credentials in runtime properties.
    ctx.instance.runtime_properties.update({
        'auth_user':
        ctx.node.properties['auth_user'],
        'auth_pass':
        ctx.node.properties['auth_pass'] or random_string(10),
    })

    ctx.logger.info('Installing kubernetes on master node')

    # Prepare script parameters.
    params = "-u '%s' " % ctx.instance.runtime_properties['auth_user']
    params += "-p '%s' " % ctx.instance.runtime_properties['auth_pass']
    params += "-t '%s' " % ctx.instance.runtime_properties['master_token']
    params += "-r 'master'"

    # Run the script.
    script = conn.client.run_script(
        script_id=ctx.instance.runtime_properties['script_id'],
        su=True,
        machine_id=machine.id,
        cloud_id=machine.cloud.id,
        script_params=params,
    )
    ctx.instance.runtime_properties['job_id'] = script['job_id']
Esempio n. 3
0
def configure_kubernetes_master():
    """Configure the kubernetes master.

    Sets up the master node and stores the necessary settings inside the node
    instance's runtime properties, which are required by worker nodes in order
    to join the kubernetes cluster.

    """
    ctx.logger.info('Setting up kubernetes master node')
    prepare_kubernetes_script()

    conn = MistConnectionClient()
    machine = conn.get_machine(
        cloud_id=ctx.instance.runtime_properties['cloud_id'],
        machine_id=ctx.instance.runtime_properties['machine_id'],
    )

    # Token for secure master-worker communication.
    token = '%s.%s' % (random_string(length=6), random_string(length=16))
    ctx.instance.runtime_properties['master_token'] = token.lower()

    # Store kubernetes dashboard credentials in runtime properties.
    ctx.instance.runtime_properties.update({
        'auth_user': ctx.node.properties['auth_user'],
        'auth_pass': ctx.node.properties['auth_pass'] or random_string(10),
    })

    ctx.logger.info('Installing kubernetes on master node')

    # Prepare script parameters.
    params = "-u '%s' " % ctx.instance.runtime_properties['auth_user']
    params += "-p '%s' " % ctx.instance.runtime_properties['auth_pass']
    params += "-t '%s' " % ctx.instance.runtime_properties['master_token']
    params += "-r 'master'"

    # Run the script.
    script = conn.client.run_script(
        script_id=ctx.instance.runtime_properties['script_id'], su=True,
        machine_id=machine.id,
        cloud_id=machine.cloud.id,
        script_params=params,
    )
    ctx.instance.runtime_properties['job_id'] = script['job_id']
Esempio n. 4
0
def configure_kubernetes_worker():
    """Configure a new kubernetes node.

    Configures a new worker node and connects it to the kubernetes master.

    """
    # Get master node from relationships schema.
    master = ctx.instance.relationships[0]._target.instance
    ctx.instance.runtime_properties.update({
        'script_id':
        master.runtime_properties.get('script_id', ''),
        'master_ip':
        master.runtime_properties.get('master_ip', ''),
        'master_token':
        master.runtime_properties.get('master_token', ''),
    })

    ctx.logger.info('Setting up kubernetes worker')
    prepare_kubernetes_script()

    conn = MistConnectionClient()
    machine = conn.get_machine(
        cloud_id=ctx.instance.runtime_properties['cloud_id'],
        machine_id=ctx.instance.runtime_properties['machine_id'],
    )

    ctx.logger.info('Configuring kubernetes node')

    # Prepare script parameters.
    params = "-m '%s' " % ctx.instance.runtime_properties['master_ip']
    params += "-t '%s' " % ctx.instance.runtime_properties['master_token']
    params += "-r 'node'"

    # Run the script.
    script = conn.client.run_script(
        script_id=ctx.instance.runtime_properties['script_id'],
        su=True,
        machine_id=machine.id,
        cloud_id=machine.cloud.id,
        script_params=params,
    )
    ctx.instance.runtime_properties['job_id'] = script['job_id']
Esempio n. 5
0
def drain_and_remove():
    """Mark the node as unschedulable, evict all pods, and remove it.

    Runs `kubectl drain` and `kubectl delete nodes` on the kubernetes
    master in order to drain and afterwards remove the specified node
    from the cluster.

    """
    if ctx.node.properties['master']:  # FIXME Is this necessary?
        return

    # Get master instance.
    master = ctx.instance.relationships[0]._target.instance

    # Render script.
    script = os.path.join(os.path.dirname(__file__), 'drain-node.sh')
    ctx.download_resource_and_render(
        os.path.join('scripts', 'drain-node.sh'), script,
        template_variables={
            'server_ip': master.runtime_properties.get('server_ip',''),
            'auth_user': master.runtime_properties['auth_user'],
            'auth_pass': master.runtime_properties['auth_pass'],
            'hostname': ctx.instance.runtime_properties.get('machine_name', '').lower()
        },
    )

    conn = MistConnectionClient()
    machine = conn.get_machine(
        cloud_id=master.runtime_properties['cloud_id'],
        machine_id=master.runtime_properties['machine_id'],
    )

    ctx.logger.info('Running "kubectl drain && kubectl delete" on %s', machine)

    _add_run_remove_script(
        cloud_id=machine.cloud.id,
        machine_id=machine.id,
        script_path=os.path.abspath(script),
        script_name='kubectl_drain_%s' % random_string(length=4)
    )
Esempio n. 6
0
def configure_kubernetes_worker():
    """Configure a new kubernetes node.

    Configures a new worker node and connects it to the kubernetes master.

    """
    # Get master node from relationships schema.
    master = ctx.instance.relationships[0]._target.instance
    ctx.instance.runtime_properties.update({
        'script_id': master.runtime_properties.get('script_id', ''),
        'master_ip': master.runtime_properties.get('master_ip', ''),
        'master_token': master.runtime_properties.get('master_token', ''),
    })

    ctx.logger.info('Setting up kubernetes worker')
    prepare_kubernetes_script()

    conn = MistConnectionClient()
    machine = conn.get_machine(
        cloud_id=ctx.instance.runtime_properties['cloud_id'],
        machine_id=ctx.instance.runtime_properties['machine_id'],
    )

    ctx.logger.info('Configuring kubernetes node')

    # Prepare script parameters.
    params = "-m '%s' " % ctx.instance.runtime_properties['master_ip']
    params += "-t '%s' " % ctx.instance.runtime_properties['master_token']
    params += "-r 'node'"

    # Run the script.
    script = conn.client.run_script(
        script_id=ctx.instance.runtime_properties['script_id'], su=True,
        machine_id=machine.id,
        cloud_id=machine.cloud.id,
        script_params=params,
    )
    ctx.instance.runtime_properties['job_id'] = script['job_id']