コード例 #1
0
 def delete_all_pods_for_service(dto):
     """
     停止所有的某类容器[根据关键字删]
     :param service: 容器名中的关键字
     :return: 结果
     """
     service = dto["service"]
     result_list = []
     target_host = "10.60.38.181"
     name_list = K8sObserver.get_pod_name_list('sock-shop')
     for pod_name in name_list:
         if service in pod_name and (service + "-db") not in pod_name and (
                 service + "-external") not in pod_name:
             target_inject = Cmd['k8s'] + pod_name
             r = Runner()
             r.run_ad_hoc(hosts=target_host,
                          module='shell',
                          args=target_inject)
             result = r.get_adhoc_result()
             result_list.append(
                 handle_inject_result('k8s', target_host, target_inject,
                                      result,
                                      sys._getframe().f_code.co_name,
                                      dto['open']))
     return result_list
コード例 #2
0
ファイル: injector.py プロジェクト: XLab-Tongji/ChaosToolbox
    def inject_random(dto):

        host = dto['host']

        target = K8sObserver.get_random_name(dto)

        if dto.get('cpu-percent') == None:
            # Inject Pod
            namespace = dto['namespace']
            args = Command.get_command('pod_injection', 'pod_delete') \
                + "--names " +  target \
                + " --namespace " + namespace \
                + Command.get_config()
        else:
            # Inject Node
            cpu_percent = dto['cpu-percent']
            args = Command.get_command('node_injection', 'cpu_load') \
                + "--cpu-percent " + cpu_percent \
                + " --names " + target \
                + Command.get_config()

        r_success_dict = Runner.run_adhoc(host, args)

        # Handle result
        return Handler.get_stdout_info(r_success_dict)
コード例 #3
0
 def chaos_inject_random(dto):
     global Default_cmd
     Default_cmd = DefaultCmd.get_default_cmd()
     find = 0
     timeout = ''
     pod_inject = ''
     (target_host, is_exist) = get_target_host(dto)
     if not is_exist:
         return 'Host: ' + target_host + ' does not exist.'
     j = random.randint(0, len(Default_cmd) - 1)
     inject_type = Default_cmd.keys()[j]
     if inject_type == "k8s":
         pod_list = K8sObserver.get_pod_name_list("sock-shop")
         k = random.randint(0, len(pod_list) - 1)
         pod_inject = pod_list[k]
         target_inject = Default_cmd[Default_cmd.keys()
                                     [j]] + " " + pod_inject
     else:
         target_inject = Default_cmd[Default_cmd.keys()[j]]
     if dto['timeout'] == 'default':
         timeout = ' --timeout 300'
     elif dto['timeout'] != 'no':
         timeout = ' --timeout ' + dto['timeout']
     for i in range(0, len(has_injected)):
         if has_injected[i]['host'] == target_host and has_injected[i][
                 'inject_type'] == target_inject:
             find = 1
     if find == 0:
         if inject_type != "k8s":
             target_inject = target_inject + timeout
         r = Runner()
         r.run_ad_hoc(hosts=target_host, module='shell', args=target_inject)
         result = r.get_adhoc_result()
         return handle_inject_result(inject_type, target_host,
                                     target_inject, result,
                                     sys._getframe().f_code.co_name,
                                     dto['open'])
     else:
         Logger.log(
             "error", "HOST HAS BEEN INJECTED - Method : " +
             sys._getframe().f_code.co_name + "() - - " + str(dto))
         return "The host has been injected by the inject"
コード例 #4
0
def get_pod_log():
    if not request.json or 'service' not in request.json or 'namespace' not in request.json:
        abort(400)
    pod_name = request.json['service']
    namespace = request.json['namespace']
    return jsonify(K8sObserver.get_service_log(pod_name, namespace))
コード例 #5
0
def get_namespace():
    return jsonify(K8sObserver.get_namespace())
コード例 #6
0
def get_pods_status(namespace):
    return jsonify(K8sObserver.get_pods_status(namespace))
コード例 #7
0
def batch_deliver_ssh():
    return jsonify(K8sObserver.batch_deliver_ssh())
コード例 #8
0
def get_information_from_topology():
    return jsonify(K8sObserver.get_information_from_topology())
コード例 #9
0
def get_pods(namespace):
    return jsonify(K8sObserver.get_pods(namespace=namespace.encode('raw_unicode_escape')))
コード例 #10
0
def get_node():
    return jsonify(K8sObserver.get_node())