예제 #1
0
def run_module():
    # define available arguments/parameters a user can pass to the module
    module_args = dict(
        name=dict(type='str', required=True),
        key=dict(type='str', required=True),
        value=dict(type='str', required=True),
    )

    module = AnsibleModule(
        argument_spec=module_args,
        supports_check_mode=True
    )

    out = ""
    err = ""
    rc = 0

    load_kubernetes_config()
    configuration = client.Configuration()
    configuration.assert_hostname = False
    client.api_client.ApiClient(configuration=configuration)

    module.log(msg='test!!!!!!!!!!!!!!!!!')

    name = module.params['name']
    key = module.params['key']
    value = module.params['value']

    result = dict(
        changed=True,
        stdout=out,
        stderr=err,
        rc=rc,
    )

    patch_cr(name, key, value)

    if module.check_mode:
        return result

    module.exit_json(**result)
예제 #2
0
def run_module():
    # define available arguments/parameters a user can pass to the module
    module_args = dict(
        namespace=dict(type='str', required=True),
        pod=dict(type='str', required=True),
        amount=dict(type='int', required=True),
        duration=dict(type='int', required=True),
    )

    module = AnsibleModule(argument_spec=module_args, supports_check_mode=True)

    rc = 0
    stderr = "err"
    stderr_lines = ["errl1", "errl2"]
    stdout = "out"
    stdout_lines = ["outl1", "outl1"]

    module.log(msg='test!!!!!!!!!!!!!!!!!')

    namespace = module.params['namespace']
    amount = module.params['amount']

    result = dict(
        changed=True,
        stdout=stdout,
        stdout_lines=stdout_lines,
        stderr=stderr,
        stderr_lines=stderr_lines,
        rc=rc,
    )

    result['fact'] = random.choice(FACTS).format(
        name=module.params['namespace'])

    # random numbers from poisson distribution
    n = amount
    a = 0
    duration = module.params['duration']
    load_kubernetes_config()
    configuration = client.Configuration()
    configuration.assert_hostname = False
    client.api_client.ApiClient(configuration=configuration)

    podName = module.params['pod']
    if (podName == 'random poisson'):
        data_poisson = poisson.rvs(mu=10, size=n, loc=a)
        counts, bins, bars = plt.hist(data_poisson)
        plt.close()
        for experiment in counts:
            pod_list = get_pods(namespace=namespace)
            aux_li = []
            for fil in pod_list.items:
                if fil.status.phase == "Running":
                    aux_li.append(fil)
            pod_list = aux_li

            # From the Running pods I randomly choose those to die
            # based on the histogram length
            print("-------")
            print("Pod list length: " + str(len(pod_list)))
            print("Number of pods to get: " + str(int(experiment)))
            print("-------")
            # In the case of the experiment being longer than the pod list,
            # then the maximum will be the lenght of the pod list
            if (int(experiment) > len(pod_list)):
                to_be_memory = random.sample(pod_list, len(pod_list))
            else:
                to_be_memory = random.sample(pod_list, int(experiment))

            for pod in to_be_memory:
                inyect_memory(pod.metadata.name, pod.metadata.namespace,
                              module, duration)
            global_kill.append((datetime.datetime.now(), int(experiment)))
            # time.sleep(10)
            print(datetime.datetime.now())
    else:
        pod = get_pod_by_name(namespace=namespace, name=podName)
        inyect_memory(pod.metadata.name, pod.metadata.namespace, module,
                      duration)
    print("Ending histogram execution")

    if module.check_mode:
        return result

    module.exit_json(**result)
예제 #3
0
def run_module():
    # define available arguments/parameters a user can pass to the module
    module_args = dict(
        nodes=dict(type='json', required=True),
        amount=dict(type='int', required=True),
        duration=dict(type='int', required=True),
    )

    global module
    module = AnsibleModule(argument_spec=module_args, supports_check_mode=True)

    rc = 0
    stderr = "err"
    stderr_lines = ["errl1", "errl2"]
    stdout = "out"
    stdout_lines = ["outl1", "outl1"]

    nodes = module.params['nodes']
    amount = module.params['amount']
    duration = module.params['duration']

    result = dict(
        changed=True,
        stdout=stdout,
        stdout_lines=stdout_lines,
        stderr=stderr,
        stderr_lines=stderr_lines,
        rc=rc,
    )

    # Load the Kubernetes configuration
    load_kubernetes_config()
    configuration = client.Configuration()
    configuration.assert_hostname = False
    client.api_client.ApiClient(configuration=configuration)

    nodes_list = json.loads(nodes)
    module.log(msg='Nodes list size:' + str(len(nodes_list)))

    # We get the final list of nodes to drain
    all_workers = get_worker_nodes()
    if len(nodes_list) == 0:
        # This means that there are no specific nodes, get get random nodes
        module.log(msg='Nodes list is empty, we get random nodes')
        if amount >= len(all_workers):
            amount = len(all_workers)
        nodes_list = sample(all_workers, amount)
    else:
        aux_nodes = []
        for node in nodes_list:
            if node in all_workers:
                aux_nodes.append(node)
        nodes_list = aux_nodes

    module.log(msg='Nodes to drain: ' + str(nodes_list))

    # We cordon the nodes and drain them
    for node_name in nodes_list:
        module.log(msg='Node to cordon:' + node_name)
        if cordon_node(node_name):
            module.log(msg='Cordon OK - Node to drain:' + node_name)
            drain_node(node_name)
        else:
            module.log(msg='Cordon NOT OK')

    # We wait until the duration pass
    module.log(msg='Waiting for: ' + str(duration))
    time.sleep(duration)

    # We restore the nodes
    for node_name in nodes_list:
        module.log(msg='Node to uncordon:' + node_name)
        if uncordon_node(node_name):
            module.log(msg='Uncordon OK')
        else:
            module.log(msg='Uncordon NOT OK')

    if module.check_mode:
        return result

    module.exit_json(**result)