id@?FddlZddlmZddlmZddlmZGddeZy)N)quote)Cluster) is_executableceZdZdZdZdZdZdZdZdZ dZ gdZ e d Z d Zd Zfd Zd ZdZdZdZdZdZdZdZdZdZdZxZS)ocpa This profile is for use with OpenShift Container Platform (v4) clusters instead of the kubernetes profile. This profile will favor using the `oc` transport type, which means it will leverage a locally installed `oc` binary. This is also how node enumeration is done. To instead use SSH to connect to the nodes, use the '--transport=control_persist' option. Thus, a functional `oc` binary for the user executing sos collect is required. Functional meaning that the user can run `oc` commands with clusterAdmin privileges. If this requires the use of a secondary configuration file, specify that path with the 'kubeconfig' cluster option. This config file will also be used on a single master node to perform API collections if the `with-api` option is enabled (default disabled). If no `kubeconfig` option is given, but `with-api` is enabled, the cluster profile will attempt to use a well-known default kubeconfig file if it is available on the host. Alternatively, provide a clusterAdmin access token either via the 'token' cluster option or, preferably, the SOSOCPTOKEN environment variable. By default, this profile will enumerate only master nodes within the cluster, and this may be changed by overriding the 'role' cluster option. To collect from all nodes in the cluster regardless of role, use the form -c ocp.role=''. Filtering nodes by a label applied to that node is also possible via the label cluster option, though be aware that this is _combined_ with the role option mentioned above. To avoid redundant collections of OCP API information (e.g. 'oc get' commands), this profile will attempt to enable the API collections on only a single master node. If the none of the master nodes have a functional 'oc' binary available, *and* the --no-local option is used, that means that no API data will be collected. zOpenShift Container Platform v4)zopenshift-hyperkubezopenshift-clientsFNzsos-collect-tmp))labelrz3Colon delimited list of labels to select nodes with)rolemasterz*Colon delimited list of roles to filter on) kubeconfigrzPath to the kubeconfig file)tokenrz1Service account token to use for oc authorization)with-apiFz'Collect OCP API data from a master nodec|jsPd|_|jjjr|jj d|jjj }|ddk(rdt jj|jjj |djjd|_n(|jd|jd |dz|jd r'|xjd |jd zz c_|jd |jz|jS) Noczwhich oc)chrootstatusroutput/zHUnable to to determine PATH for 'oc' command, node enumeration may fail.zLocating 'oc' failed: %sr z --config %szoc base command set to %s)_oc_cmdprimaryhost in_container run_commandsysrootospathjoinstriplstriplog_warn log_debug get_option)self_oc_paths $DL MM5NN#=%-h%7$89|, 1N NN NN6E F||c$|jd|S)zcFormat the oc command to optionall include the kubeconfig file if one is specified  )r&)r#cmds r% fmt_oc_cmdzocp.fmt_oc_cmdds++s++r'cl|j|jd|jz}|ddk(S)zXAttempt to login to the API using the oc command using a provided token z0login --insecure-skip-tls-verify=True --token=%srr)exec_primary_cmdr+r )r#_ress r%_attempt_oc_loginzocp._attempt_oc_loginjs@$$ OON"jj) * H~""r'ctt| ry|jdxst j dd|_|j r|j|jd}|j|ddk(S)NTr SOSOCPTOKENwhoamirr) superr check_enabledr"rgetenvr r/r+r-)r#_who __class__s r%r4zocp.check_enabledtso d ) +__W-O=$1O ::  " " $x($$T*8499r'c|jdk(sy|j|jd}|ddk(|_|js|j dt d|j d|jz|j|jd |jz}|ddk(r|jy |j d |d zt d )zpCreate the project that we will be executing in for any nodes' collection via a container image rNzauth can-i '*' '*'rrzWCheck for cluster-admin privileges returned false, cannot create project in OCP clusterzLInsufficient permissions to create temporary collection project. Aborting...z#Creating new temporary project '%s'znew-project %sTzFailed to create project: %srz?Failed to create temporary project for collection. Aborting...) set_transport_typer-r+oc_cluster_adminr! Exceptionlog_infoproject_label_sos_project)r#outrets r%setupz ocp.setup}s&&(D0 ##DOO4H$IJ #H  2$$ NNC D?@ @ ;dllJK## OO,t||; <  x=A   # # % 5H EF() )r'c ddg}|D]L}|j|jd|jd|d}|ddk(r=td|d y ) zAdd pertinent labels to the temporary project we've created so that our privileged containers can properly run. z4security.openshift.io/scc.podSecurityLabelSync=falsez-pod-security.kubernetes.io/enforce=privilegedzlabel namespace r)z --overwriterrz!Error applying namespace labels: rN)r-r+r=r;)r#labelsr r@s r%r>zocp._label_sos_projects~ C ;  E''&t||nAeWLIC x=A%7H G r'c|jr |j|jd|jd}|ddk(s|jd|d|j|jd|jd }|ddk(s|jd |d|j|jdd |_y#t$r}|jd |d Yd }~Kd }~wwxYw)z8Remove the project we created to execute within zdelete project )timeoutrrz"Error deleting temporary project: rzwait namespace/z --for=delete --timeout=30sz3Error waiting for temporary project to be deleted: zAFailed attempting to remove temporary project 'sos-collect-tmp': z. Please manually remove the temporary project.Nzproject defaultT)r=r-r+ log_errorr;)r#r@errs r%cleanupz ocp.cleanups" << ++OOodll^$DE,8})NNCJ  ! A "$eAh!AF.3CK.@E%(OF+A A  !s"B BBcT|jjdk7r|jjStd|jjj ry|j d|jjd|jjs tdy)Nautor)rz]Local installation of 'oc' not found or is not correctly configured. Will use ControlPersist.z=Preferred transport 'oc' not available, will fallback to SSH.z?Press ENTER to continue connecting with SSH, or Ctrl+C toabort.control_persist) opts transportrrrrr<ui_logwarnbatchinput)r#s r%r9zocp.set_transport_types 99  & (99&& & t||'8'8'@'@ A G H  K yy   r'cg}i|_d}|jdr@dj|jdjd}|dt |zz }|j |j |}|ddk(r|jdd k(r|jd |jdjdDcgc]}|}}|j|d j|_|jjD]:\}}|r"|D]} | |d vs |j|(*|j|<|Sd } d|d vrd} t| cc}w)Nzget nodes -o wider ,:z -l %srrr r zNOTE: By default, only master nodes are listed. To collect from all/more nodes, override the role option with '-c ocp.role=role1:role2'rrLz'oc' command failedzMissing or incompleteza'oc' failed due to missing kubeconfig on primary node. Specify one via '-c ocp.kubeconfig=') node_dictr"rrOrr-r+r rZ splitlinesitemsappendr;) r#rSr*rCresrrL node_namerWr msgs r% get_nodesz ocp.get_nodessr! ??7 #XXdoog6<>#7#7#9 , 4 %"4=0!LL3!" LL+ , (C&#h-7EC. Ds E-c|j|jvrydD]$}||j|jdvs"|cSy)Nr)r workerrLaddressrg)r#rWr s r%set_node_labelzocp.set_node_labelsH <>'//:7CCCr'c|jdr d}|rdnd}nd}|rdnd}|jjd|d|y) aIn earlier versions of sos, the openshift plugin option that is used to toggle the API collections was called `no-oc` rather than `with-api`. This older plugin option had the inverse logic of the current `with-api` option. Use this to toggle the correct plugin option given the node's sos version. Note that the use of version 4.2 here is tied to the RHEL release (the only usecase for this cluster profile) rather than the upstream version given the backports for that downstream. :param node: The node being inspected for API collections :type node: ``SoSNode`` :param use_api: Should this node enable API collections? :type use_api: ``bool`` z4.2-16ronoffzno-ocz openshift.=N)check_sos_versionplugoptsrj)r#rWuse_api_opt_vals r%_toggle_api_optzocp._toggle_api_opt%sE"  ! !( +D"4DD#5D $=>r'c|jjd|jds|j|dy|jr|j|dyd}|jd}|r|j dsd|z}|xs|}d}|j jrd }|jd d r|d z }|jd|z|j jd }|ddk(r|j|d d |_n|jr3|j|jd<|j|d d |_nM|j|r<||k(s|jjd|z|j|d d |_|jrFd|jz}|jj||j j|yy)N openshiftrFzl/host/etc/kubernetes/static-pod-resources/kube-apiserver-certs/secrets/node-kubeconfigs/localhost.kubeconfigr z/hostz/host/rz /host/bin/ocz/root/.kube/configT) need_rootz% --kubeconfig /host/root/.kube/configz %s whoami) use_containerrrrr1zopenshift.kubeconfig=%szcAPI collections will be performed on %s Note: API collections may extend runtime by 10s of minutes )enable_pluginsrjr"rapi_collect_enabled startswithr containerized file_existsrr sos_env_varsr}rssosloginfor`)r#rW master_kube _optconfig _kubeconfigrcan_ocrns r%set_primary_optionszocp.set_primary_options>s "";/z*  u -   # #  u -'  6J*"7"7"@% 2 $3 KGyy&&( ##$8D#IFFG%%kG&;48II4K4K15 &6F h1$$$T40+/(37::!!-0$$T40+/(!!+.#k1MM((1K?$$T40+/(''L&   %   % (r'c(|j|dy)NF)r)r#rWs r%set_node_optionszocp.set_node_options{s T5)r')__name__ __module__ __qualname____doc__ cluster_namepackagesrr r=r:r option_listpropertyr&r+r/r4rAr>rIrZr9rortrwrrr __classcell__)r7s@r%rrs%N5L;H EGGK0, #:)6&!F8 !>D ?2;&z*r'r)rpipesrsos.collector.clustersr sos.utilitiesrrr'r%rs! *'k*'k*r'