コード例 #1
0
 def kubectl_installed(assert_errors=True):
     response = execute_local_command('kubectl version')
     installed = 'not found' not in response
     if assert_errors and installed is False:
         assert False, 'kubectl not installed..{}'.format(response)
     elif installed is False:
         log.warning('Kubectl is not installed.. {}'.format(response))
         return False
     else:
         return True
コード例 #2
0
 def execute_with_retry(command,
                        assert_text,
                        retry_count=20,
                        delay_in_sec=5):
     response = ''
     for i in range(retry_count + 1):
         if i > 0:
             log.info('{}/{} retry: {}'.format(i, retry_count - 1, command))
         response = execute_local_command(command, False)
         if assert_text in response:
             break
         time.sleep(delay_in_sec)
     return response
コード例 #3
0
def kube_config_file_path(tc_config_data):
    """
    Create temporary configuration file
    :param tc_config_data:
    :return:
    """
    kube_config_path = tc_config_data.get('kubernetes_configuration_file', '')
    is_temporary = False

    # If kube configuration file is missing, add it to the disk
    if 'edge_k8s_url' in tc_config_data \
            and 'api_key' in tc_config_data \
            and not kube_config_path:
        log.debug('Creating kubernetes configuration file')
        kube_config_path = Kubectl().write_kubectl_config(
            server_url=tc_config_data['edge_k8s_url'],
            api_key=tc_config_data['api_key'])
        if not os.environ.get('JENKINS_URL', False):
            is_temporary = True

        # Set kubectl to use new configuration file
        os.environ['KUBECONFIG'] = kube_config_path
        log.debug('KUBECONFIG={}'.format(os.environ['KUBECONFIG']))

        # Take configuration in use
        utils.execute_local_command('kubectl config use-context edge-k8s')
        log.debug(
            'Temporary kubernetes configuration successfully created and in use'
        )
    else:
        log.debug('No need to create temporary kubernetes konfiguration')
    yield kube_config_path

    # Remove temporary configuration when running outside Jenkins
    if is_temporary:
        log.debug(
            'Removing temporary kube config: {}'.format(kube_config_path))
        os.remove(kube_config_path)
コード例 #4
0
    def get_pod_details(pod):
        pod_details = {
            'NAME': None,
            'READY': None,
            'STATUS': None,
            'RESTARTS': None,
            'AGE': None
        }

        response = execute_local_command(
            'kubectl get pods {}'.format(pod)).split()

        if len(response) == 10:
            pod_details['NAME'] = response[5]
            pod_details['READY'] = response[6]
            pod_details['STATUS'] = response[7]
            pod_details['RESTARTS'] = response[8]
            pod_details['AGE'] = response[9]

        return pod_details
コード例 #5
0
 def create_pod(pod_yaml_file):
     return execute_local_command(
         'kubectl create -f {}'.format(pod_yaml_file))
コード例 #6
0
 def pod_logs(pod_name):
     return execute_local_command('kubectl logs {}'.format(pod_name))
コード例 #7
0
 def delete_pod(pod_name):
     return execute_local_command(
         'kubectl delete pods {}  --force --grace-period=0'.format(
             pod_name))
コード例 #8
0
 def get_pods():
     return execute_local_command('kubectl get pods')
コード例 #9
0
 def get_pod(pod):
     return execute_local_command('kubectl get pods {}'.format(pod))
コード例 #10
0
 def describe_pod(pod_name):
     return execute_local_command(
         'kubectl describe pod {}'.format(pod_name))
コード例 #11
0
 def get_contexts():
     return execute_local_command('kubectl config get-contexts')
コード例 #12
0
 def get_clusters():
     return execute_local_command('kubectl config get-clusters')
コード例 #13
0
 def view_config():
     return execute_local_command('kubectl config view')
コード例 #14
0
 def describe_node(edge):
     return execute_local_command('kubectl describe nodes {}'.format(
         edge.device_id))