관리-도구
편집 파일: ocp.cpython-39.opt-1.pyc
a ����-�_g.@����������������������@���s@���d�dl�Z�d�dlmZ�d�dlmZ�d�dlmZ�G�dd��de�ZdS�)�����N)�quote)�Cluster)� is_executablec�����������������������s����e�Zd�ZdZdZdZdZdZdZdZ dZ g�d�Zed d ���Z dd��Zd d��Z��fdd�Zdd��Zdd��Zdd��Zdd��Zdd��Zdd��Zdd��Zdd ��Zd!d"��Zd#d$��Zd%d&��Z���ZS�)'�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��))�labelr���z3Colon delimited list of labels to select nodes with)�role�masterz*Colon delimited list of roles to filter on)� kubeconfigr���zPath to the kubeconfig file)�tokenr���z1Service account token to use for oc authorization)�with-apiFz'Collect OCP API data from a master node)�api-urlr���z.Alternate API URL of an external control-planec�����������������C���s����|�j�s�d|�_�|�jj���r~|�jjd|�jjjd�}|d�dkr`tj�|�jjj|d�� ��� d��|�_�n|��d��|��d |d������|�� d �r�|��j�d|�� d ����7��_�|��d|�j������|�j�S�) N�oczwhich oc)�chroot�statusr����output�/zHUnable to to determine PATH for 'oc' command, node enumeration may fail.zLocating 'oc' failed: r ���z --kubeconfig zoc base command set to )�_oc_cmd�primary�hostZin_container�run_command�sysroot�os�path�join�strip�lstrip�log_warn� log_debug� get_option)�selfZ_oc_path��r!����>/usr/lib/python3.9/site-packages/sos/collector/clusters/ocp.py�oc_cmdL���s.���� ���� � z ocp.oc_cmdc�����������������C���s���|�j���d|���S�)zcFormat the oc command to optionall include the kubeconfig file if one is specified � )r#���)r ����cmdr!���r!���r"���� fmt_oc_cmdf���s����zocp.fmt_oc_cmdc�������������� ���C���s0���|���|��d|�j��d|��d������}|d�dkS�)zXAttempt to login to the API using the oc command using a provided token z.login --insecure-skip-tls-verify=True --token=r$���r ���r���r���)�exec_primary_cmdr&���r���r���)r ���Z_resr!���r!���r"����_attempt_oc_loginl���s�������zocp._attempt_oc_loginc��������������������sP���t������rdS�|��d�p"t�dd��|�_|�jr4|�����|��d�}|��|�d�dkS�)NTr����SOSOCPTOKENZwhoamir���r���) �super� check_enabledr���r����getenvr���r(���r&���r'���)r ���Z_who�� __class__r!���r"���r+���w���s���� zocp.check_enabledc�����������������C���s����|�����dksdS�|��|��d��}|d�dk|�_|�jsF|��d��td��|��d|�j��d ���|��|��d |�j�����}|d�dkr�|�����dS�|��d|d ������td��dS�)zpCreate the project that we will be executing in for any nodes' collection via a container image r���Nzauth can-i '*' '*'r���r���zWCheck for cluster-admin privileges returned false, cannot create project in OCP clusterzLInsufficient permissions to create temporary collection project. Aborting...z Creating new temporary project '�'znew-project TzFailed to create project: r���z?Failed to create temporary project for collection. Aborting...) �set_transport_typer'���r&����oc_cluster_adminr���� Exception�log_info�project�_label_sos_project)r ����out�retr!���r!���r"����setup����s ���� �z ocp.setupc�������������� ���C���sT���ddg}|D�]B}|���|��d|�j��d|��d���}|d�dkstd|d ������qd S�)z�Add 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 --overwriter���r���z!Error applying namespace labels: r���N)r'���r&���r4���r2���)r ����labelsr���r7���r!���r!���r"���r5�������s��������zocp._label_sos_projectc�������������� ���C���s����|�j�r�zz|�j|��d|�j�����dd�}|d�dksD|��d|d������|��|��d|�j���d ���}|d�dks~|��d |d������W�n6�ty��}�z|��d|��d���W�Y�d }~n d }~0�0�|��|��d���d |�_�dS�)z8Remove the project we created to execute within zdelete project ����)�timeoutr���r���z"Error deleting temporary project: r���zwait 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)r4���r'���r&���Z log_errorr2���)r ���r7����errr!���r!���r"����cleanup����s<������������zocp.cleanupc����������� ��� ���C���s����i�}d|d�v�r�|��d����}i�}dD�].}z|�|����||<�W�q&�tyR���Y�q&0�q&|D�]>}|���}i�||d�<�|���D�]\}} || �||d��|<�qzqZ|S�)a���From 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`` �NAMEr���)r����roles�versionzos-image)�pop�split�index�upperr2����items) r ���Znodelist�nodesZstatline�idx�state�nodeZ_node�column�valuer!���r!���r"����_build_dict����s����zocp._build_dictc�����������������C���sT���|�j�jdkr|�j�jS�td|�jjjd�r*dS�|��d��|�j�d��|�j�j sPt d��dS�)N�autor���)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 transportr���r���r���r���r3����ui_logZwarningZbatch�input)r ���r!���r!���r"���r0�������s���� �zocp.set_transport_typec����������� ������C���s��g�}i�|�_�d}|��d�r@d�|��d��d��}|dt|����7�}|��|��|��}|d�dkr�|��d�d krt|��d ��t|��d��d��}|�� |d�� ���|�_�|�j����D�]<\}}|r�|D�]}||d�v�r�|�|���q�q�q�|�|��q�nd } d|d�v�r�d} t | ��|S�)Nzget nodes -o wider����,�:z -l r���r���r���r ���z�NOTE: By default, only master nodes are listed. To collect from all/more nodes, override the role option with '-c ocp.role=role1:role2'r���r?���z'oc' command failedzMissing or incompleteza'oc' failed due to missing kubeconfig on primary node. Specify one via '-c ocp.kubeconfig=<path>')� node_dictr���r���rB���r���r'���r&���r����listrL���� splitlinesrE����appendr2���) r ���rF���r%���r9����resr?���Z node_namerI���r����msgr!���r!���r"���� get_nodes����s0���� z ocp.get_nodesc�����������������C���s:���|j�|�jvrdS�dD�] }||�j|j��d�v�r|��S�qdS�)Nr���)r ���Zworkerr?�����addressrR���)r ���rI���r���r!���r!���r"����set_node_label��s���� zocp.set_node_labelc�����������������C���s$���|j�|�jvrdS�d|�j|j��d�v�S�)NFr ���r?���rY����r ���rI���r!���r!���r"����check_node_is_primary%��s����zocp.check_node_is_primaryc�����������������C���sH���|��d�rd}|rdnd}nd}|r(dnd}|j�d|��d|�����dS�) a���In 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-16r���ZonZoffzno-ocz openshift.�=N)Zcheck_sos_version�plugoptsrU���)r ���rI���Zuse_apiZ_optZ_valr!���r!���r"����_toggle_api_opt*��s���� zocp._toggle_api_optc�����������������C���sZ��|j��d��|��d�s&|��|d��d�S�|�jr<|��|d���nd}|��d�}|r`|�d�s`d|�}|pf|}d}|jjr�d }|jd dd�r�|d 7�}|j |��d�|jjdd�}|d�dkr�|��|d��d|�_n`|�j r�|�j |jd<�|��|d��d|�_n:|�|��r(||k�r|j�d|�����|��|d��d|�_|�j�rVd|j ��d�}|�j�|��|�j�|��d�S�)NZ openshiftr���Fzl/host/etc/kubernetes/static-pod-resources/kube-apiserver-certs/secrets/node-kubeconfigs/localhost.kubeconfigr ���z/hostz/host/r���z/host/bin/ocz/root/.kube/configT)� need_rootz% --kubeconfig /host/root/.kube/configz whoami)Z use_containerra���r���r���r)���zopenshift.kubeconfig=z%API collections will be performed on z< Note: API collections may extend runtime by 10s of minutes )Zenable_pluginsrU���r���r`����api_collect_enabled� startswithr���Z containerizedZfile_existsr���r���Zsos_env_varsr_���rZ���Zsoslog�inforN���)r ���rI���Zmaster_kubeZ _optconfigZ_kubeconfigr���Zcan_ocrW���r!���r!���r"����set_primary_optionsC��sN���� � � �zocp.set_primary_optionsc�����������������C���s���|���|d��d�S�)NF)r`���r\���r!���r!���r"����set_node_options���s����zocp.set_node_options)�__name__� __module__�__qualname__�__doc__Zcluster_nameZpackagesrb���r���r4���r1���r���Zoption_list�propertyr#���r&���r(���r+���r8���r5���r=���rL���r0���rX���r[���r]���r`���re���rf���� __classcell__r!���r!���r-���r"���r������s2���' #=r���)r���Zshlexr���Zsos.collector.clustersr���Z sos.utilitiesr���r���r!���r!���r!���r"����<module>���s���