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
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
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)
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
def create_pod(pod_yaml_file): return execute_local_command( 'kubectl create -f {}'.format(pod_yaml_file))
def pod_logs(pod_name): return execute_local_command('kubectl logs {}'.format(pod_name))
def delete_pod(pod_name): return execute_local_command( 'kubectl delete pods {} --force --grace-period=0'.format( pod_name))
def get_pods(): return execute_local_command('kubectl get pods')
def get_pod(pod): return execute_local_command('kubectl get pods {}'.format(pod))
def describe_pod(pod_name): return execute_local_command( 'kubectl describe pod {}'.format(pod_name))
def get_contexts(): return execute_local_command('kubectl config get-contexts')
def get_clusters(): return execute_local_command('kubectl config get-clusters')
def view_config(): return execute_local_command('kubectl config view')
def describe_node(edge): return execute_local_command('kubectl describe nodes {}'.format( edge.device_id))