3 ܓi @@s@ddlZddlmZddlmZddlmZGdddeZdS)N)quote)Cluster) is_executablecseZdZdZdZd4ZdZdZdZdZ dZ d5d6d7d8d9d:gZ e ddZ ddZddZfddZddZd d!Zd"d#Zd$d%Zd&d'Zd(d)Zd*d+Zd,d-Zd.d/Zd0d1Zd2d3ZZS);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 v4openshift-hyperkubeopenshift-clientsFNzsos-collect-tmplabel3Colon delimited list of labels to select nodes withrolemaster*Colon delimited list of roles to filter on kubeconfigPath to the kubeconfig filetoken1Service account token to use for oc authorizationwith-api'Collect OCP API data from a master nodeapi-url.Alternate API URL of an external control-planecCs|jsd|_|jjjrt|jjd}|ddkrVtjj|jjj|dj j d|_n|j d|j d|d|j d r|jd |j d 7_|j d |j|jS) Noczwhich ocstatusroutput/zHUnable to to determine PATH for 'oc' command, node enumeration may fail.zLocating 'oc' failed: rz --kubeconfig zoc base command set to )_oc_cmdprimaryhostZ in_container run_commandospathjoinsysrootstriplstriplog_warn log_debug get_option)selfZ_oc_pathr(/usr/lib/python3.6/ocp.pyoc_cmdLs"   z ocp.oc_cmdcCs|jd|S)zcFormat the oc command to optionall include the kubeconfig file if one is specified  )r*)r'cmdr(r(r) fmt_oc_cmdfszocp.fmt_oc_cmdcCs0|j|jd|jd|jd}|ddkS)zXAttempt to login to the API using the oc command using a provided token z.login --insecure-skip-tls-verify=True --token=r+zapi-urlrr)exec_primary_cmdr-rr&)r'Z_resr(r(r)_attempt_oc_loginls zocp._attempt_oc_logincsPtjrdS|jdp"tjdd|_|jr4|j|jd}|j|ddkS)NTr SOSOCPTOKENZwhoamirr) super check_enabledr&rgetenvrr/r-r.)r'Z_who) __class__r(r)r2ws  zocp.check_enabledcCs|jdksdS|j|jd}|ddk|_|jsF|jdtd|jd|jd |j|jd |j}|ddkr|jd S|jd |d tddS)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 ''z new-project TzFailed to create project: rz?Failed to create temporary project for collection. Aborting...) set_transport_typer.r-oc_cluster_adminr% Exceptionlog_infoproject_label_sos_project)r'outretr(r(r)setups   z ocp.setupcCsXddg}xJ|D]B}|j|jd|jd|d}|ddkstd|d qWd S) 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:r8)r'labelsr r=r(r(r)r;s  zocp._label_sos_projectcCs|jryz|j|jd|jdd}|ddksD|jd|d|j|jd|jd }|ddks~|jd |dWn4tk r}z|jd |d WYd d }~XnX|j|jdd |_dS)z8Remove the project we created to execute within zdelete project )Ztimeoutrrz"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-Z log_errorr8)r'r=errr(r(r)cleanups(   z ocp.cleanupc Csi}d|dkr|jdj}i}x8dD]0}y|j|j||<Wq(tk rVYq(Xq(WxJ|D]B}|j}i||d<x(|jD]\}} || ||d|<qWqbW|S)aFrom the output of get_nodes(), construct an easier-to-reference dict of nodes that will be used in determining labels, primary status, etc... :param nodelist: The split output of `oc get nodes` :type nodelist: ``list`` :returns: A dict of nodes with `get nodes` columns as keys :rtype: ``dict`` NAMErrrolesversionos-image)rrDrErF)popsplitindexupperr8items) r'ZnodelistnodesZstatlineidxstatenodeZ_nodecolumnvaluer(r(r) _build_dicts      zocp._build_dictcCsT|jjdkr|jjStd|jjjdr*dS|jd|jjd|jj sPt ddS)Nautor)r!z]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.Zcontrol_persist) ZoptsZ transportrrrr!r9ui_logZwarningZbatchinput)r'r(r(r)r6s  zocp.set_transport_typec Csg}i|_d}|jdr@dj|jdjd}|dt|7}|j|j|}|ddkr|jdd krt|jd t|jdjd}|j |d j |_xl|jj D]>\}}|rx0|D]}||d kr|j |PqWq|j |qWnd } d|d krd} t | |S)Nzget nodes -o wider ,:z -l rrr 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'rrDz'oc' command failedzMissing or incompleteza'oc' failed due to missing kubeconfig on primary node. Specify one via '-c ocp.kubeconfig=') node_dictr&r rHrr.r-r$listrR splitlinesrKappendr8) r'rLr,r?resrDZ node_namerOr msgr(r(r) get_nodess0      z ocp.get_nodescCs:|j|jkrdSx$dD]}||j|jdkr|SqWdS)Nrr workerrD)r r_)addressrX)r'rOr r(r(r)set_node_labels   zocp.set_node_labelcCs$|j|jkrdSd|j|jdkS)NFr rD)r`rX)r'rOr(r(r)check_node_is_primary%s zocp.check_node_is_primarycCsH|jdrd}|rdnd}nd}|r(dnd}|jjd|d|dS) 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-16zwith-apiZonZoffzno-ocz openshift.=N)Zcheck_sos_versionplugoptsr[)r'rOZuse_apiZ_optZ_valr(r(r)_toggle_api_opt*s   zocp._toggle_api_optcCs\|jjd|jds&|j|ddS|jr<|j|dnd}|jd}|rb|jd rbd|}|ph|}d}|jjrd }|jd d d r|d 7}|j |d|jjd d}|ddkr|j|d d |_n`|j r|j |j d<|j|d d |_n:|j|r*||kr|j jd||j|d d |_|jrXd|j d}|jj||jj|dS)NZ openshiftzwith-apiFzl/host/etc/kubernetes/static-pod-resources/kube-apiserver-certs/secrets/node-kubeconfigs/localhost.kubeconfigrz/hostz/host/rz /host/bin/ocz/root/.kube/configT) need_rootz% --kubeconfig /host/root/.kube/configz whoami)Z use_containerrfrrr0zopenshift.kubeconfig=z%API collections will be performed on z< Note: API collections may extend runtime by 10s of minutes )Zenable_pluginsr[r&reapi_collect_enabled startswithrZ containerizedZ file_existsrrZ sos_env_varsrdr`ZsosloginforT)r'rOZ master_kubeZ _optconfigZ _kubeconfigrZcan_ocr]r(r(r)set_primary_optionsCsH              zocp.set_primary_optionscCs|j|ddS)NF)re)r'rOr(r(r)set_node_optionsszocp.set_node_options)rr)r rr )r r r )rrr)rrr)rFr)rrr)__name__ __module__ __qualname____doc__Z cluster_nameZpackagesrgrr:r7rZ option_listpropertyr*r-r/r2r>r;rBrRr6r^rarbrerjrk __classcell__r(r()r4r)rs:&  #=r)rZshlexrZsos.collector.clustersrZ sos.utilitiesrrr(r(r(r) s