go.ligato.io/vpp-agent/v3@v3.5.0/tests/robot/libraries/kubernetes/KubeCtl.robot (about) 1 *** Settings *** 2 Documentation This is a library to handle kubectl commands on the remote machine, towards which 3 ... ssh connection is opened. 4 Resource ${CURDIR}/../all_libs.robot 5 6 *** Keywords *** 7 Apply_F 8 [Arguments] ${ssh_session} ${file_path} 9 [Documentation] Execute "kubectl apply -f" with given local file. 10 BuiltIn.Log_Many ${ssh_session} ${file_path} 11 SshCommons.Switch_And_Execute_With_Copied_File ${ssh_session} ${file_path} kubectl apply -f 12 13 Apply_F_Url 14 [Arguments] ${ssh_session} ${url} 15 [Documentation] Execute "kubectl apply -f" with given \${url}. 16 BuiltIn.Log_Many ${ssh_session} ${url} 17 SshCommons.Switch_And_Execute_Command ${ssh_session} kubectl apply -f ${url} 18 19 Delete_F 20 [Arguments] ${ssh_session} ${file_path} ${expected_rc}=0 ${ignore_stderr}=${False} 21 [Documentation] Execute "kubectl delete -f" with given local file. 22 BuiltIn.Log_Many ${ssh_session} ${file_path} ${expected_rc} ${ignore_stderr} 23 SshCommons.Switch_And_Execute_With_Copied_File ${ssh_session} ${file_path} kubectl delete -f expected_rc=${expected_rc} ignore_stderr=${ignore_stderr} 24 25 Delete_F_Url 26 [Arguments] ${ssh_session} ${url} ${expected_rc}=0 ${ignore_stderr}=${False} 27 [Documentation] Execute "kubectl delete -f" with given \${url}. 28 BuiltIn.Log_Many ${ssh_session} ${url} ${expected_rc} ${ignore_stderr} 29 SshCommons.Switch_And_Execute_Command ${ssh_session} kubectl delete -f ${url} expected_rc=${expected_rc} ignore_stderr=${ignore_stderr} 30 31 Get_Pod 32 [Arguments] ${ssh_session} ${pod_name} ${namespace}=default 33 [Documentation] Execute "kubectl get pod -n" for given \${namespace} and \${pod_name}, parse, log and return the parsed result. 34 Builtin.Log_Many ${ssh_session} ${pod_name} ${namespace} 35 ${stdout} = SshCommons.Switch_And_Execute_Command ${ssh_session} kubectl get pod -n ${namespace} ${pod_name} 36 ${output} = kube_parser.parse_kubectl_get_pods ${stdout} 37 BuiltIn.Log ${output} 38 [Return] ${output} 39 40 Get_Pods 41 [Arguments] ${ssh_session} ${namespace}=default 42 [Documentation] Execute "kubectl get pods -n" for given \${namespace} tolerating zero resources, parse, log and return the parsed output. 43 BuiltIn.Log_Many ${ssh_session} ${namespace} 44 ${status} ${message} = BuiltIn.Run_Keyword_And_Ignore_Error SshCommons.Switch_And_Execute_Command ${ssh_session} kubectl get pods -n ${namespace} 45 BuiltIn.Run_Keyword_If """${status}""" == """FAIL""" and """No resources found""" not in """${message}""" BuiltIn.Fail msg=${message} 46 ${output} = kube_parser.parse_kubectl_get_pods ${message} 47 BuiltIn.Log ${output} 48 [Return] ${output} 49 50 Get_Pods_Wide 51 [Arguments] ${ssh_session} 52 [Documentation] Execute "kubectl get pods -o wide", parse, log and return the parsed outpt. 53 Builtin.Log_Many ${ssh_session} 54 ${stdout} = SshCommons.Switch_And_Execute_Command ${ssh_session} kubectl get pods -o wide 55 ${output} = kube_parser.parse_kubectl_get_pods ${stdout} 56 BuiltIn.Log ${output} 57 [Return] ${output} 58 59 Get_Pods_All_Namespaces 60 [Arguments] ${ssh_session} 61 [Documentation] Execute "kubectl get pods --all-namespaces", parse, log and return the parsed outpt. 62 Builtin.Log_Many ${ssh_session} 63 ${stdout} = SshCommons.Switch_And_Execute_Command ${ssh_session} kubectl get pods --all-namespaces 64 ${output} = kube_parser.parse_kubectl_get_pods ${stdout} 65 BuiltIn.Log ${output} 66 [Return] ${output} 67 68 Get_Nodes 69 [Arguments] ${ssh_session} 70 [Documentation] Execute "kubectl get nodes", parse, log and return the parsed outpt. 71 Builtin.Log_Many ${ssh_session} 72 ${stdout} = SshCommons.Switch_And_Execute_Command ${ssh_session} kubectl get nodes 73 ${output} = kube_parser.parse_kubectl_get_nodes ${stdout} 74 BuiltIn.Log ${output} 75 [Return] ${output} 76 77 Logs 78 [Arguments] ${ssh_session} ${pod_name} ${container}=${EMPTY} ${namespace}=${EMPTY} ${compress}=${False} 79 [Documentation] Execute "kubectl logs" with given params, log output into a result file. 80 BuiltIn.Log_Many ${ssh_session} ${pod_name} ${container} ${namespace} ${compress} 81 ${nsparam} = BuiltIn.Set_Variable_If """${namespace}""" != """${EMPTY}""" --namespace ${namespace} ${EMPTY} 82 ${cntparam} = BuiltIn.Set_Variable_If """${container}""" != """${EMPTY}""" ${container} ${EMPTY} 83 SshCommons.Switch_Execute_And_Log_To_File ${ssh_session} kubectl logs ${nsparam} ${pod_name} ${cntparam} compress=${compress} 84 85 Describe_Pod 86 [Arguments] ${ssh_session} ${pod_name} 87 [Documentation] Execute "kubectl describe pod" with given \${pod_name}, parse, log and return the parsed details. 88 BuiltIn.Log_Many ${ssh_session} ${pod_name} 89 ${output} = SshCommons.Switch_And_Execute_Command ${ssh_session} kubectl describe pod ${pod_name} 90 ${details} = kube_parser.parse_kubectl_describe_pod ${output} 91 BuiltIn.Log ${details} 92 [Return] ${details} 93 94 Taint 95 [Arguments] ${ssh_session} ${cmd_parameters} 96 [Documentation] Execute "kubectl taint" with given \${cmd_parameters}, return the result. 97 Builtin.Log_Many ${ssh_session} ${cmd_parameters} 98 BuiltIn.Run_Keyword_And_Return SshCommons.Switch_And_Execute_Command ${ssh_session} kubectl taint ${cmd_parameters} 99 100 Label_Nodes 101 [Arguments] ${ssh_session} ${node_name} ${label_key} ${label_value} 102 [Documentation] Execute "kubectl label nodes" with given parameters, return the result. 103 Builtin.Log_Many ${ssh_session} ${node_name} ${label_key} ${label_value} 104 BuiltIn.Run_Keyword_And_Return SshCommons.Switch_And_Execute_Command ${ssh_session} kubectl label nodes ${node_name} ${label_key}=${label_value} 105 106 Get_Container_Id 107 [Arguments] ${ssh_session} ${pod_name} ${container}=${EMPTY} 108 [Documentation] Return \${container} or describe pod, parse for first container ID, log and return that. 109 ... As kubectl is usually only present only on master node, switch to \${ssh_session} is done after. 110 BuiltIn.Log_Many ${ssh_session} ${pod_name} ${container} 111 Builtin.Return_From_Keyword_If """${container}""" ${container} 112 ${output} = SshCommons.Switch_And_Execute_Command ${VM_SSH_ALIAS_PREFIX}1 kubectl describe pod ${pod_name} 113 SSHLibrary.Switch_Connection ${ssh_session} 114 ${id} = kube_parser.parse_for_first_container_id ${output} 115 Builtin.Log ${id} 116 [Return] ${id} 117 118 Execute_On_Pod 119 [Arguments] ${ssh_session} ${pod_name} ${cmd} ${container}=${EMPTY} ${tty}=${False} ${stdin}=${False} ${privileged}=${True} ${ignore_stderr}=${False} ${ignore_rc}=${False} 120 [Documentation] Execute "docker exec" with given parameters, return the result. 121 ... Container ID is autodetected if empty. This only works if \${ssh_session} points to the correct host. 122 Builtin.Log_Many ${ssh_session} ${pod_name} ${cmd} ${container} ${tty} ${stdin} ${privileged} ${ignore_stderr} ${ignore_rc} 123 ${container_id} = Get_Container_Id ${ssh_session} ${pod_name} ${container} 124 ${t_param} = BuiltIn.Set_Variable_If ${tty} -t ${EMPTY} 125 ${i_param} = BuiltIn.Set_Variable_If ${stdin} -i ${EMPTY} 126 ${p_param} = BuiltIn.Set_Variable_If ${privileged} --privileged=true --privileged=false 127 ${docker} = BuiltIn.Set_Variable ${KUBE_CLUSTER_${CLUSTER_ID}_DOCKER_COMMAND} 128 BuiltIn.Run_Keyword_And_Return SshCommons.Switch_And_Execute_Command ${ssh_session} ${docker} exec ${i_param} ${t_param} ${p_param} ${container_id} ${cmd} ignore_stderr=${ignore_stderr} ignore_rc=${ignore_rc} 129 130 # TODO: Add more commands like Get Deployment, Get statefulset describe deployment, describe statefulset etc...