%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /lib/python3/dist-packages/sos/report/plugins/__pycache__/
Upload File :
Create Path :
Current File : //lib/python3/dist-packages/sos/report/plugins/__pycache__/openshift.cpython-312.pyc

�

i��d�C��L�ddlmZmZmZddlmZddlZddlZGd�dee�Zy)�)�Plugin�RedHatPlugin�	PluginOpt)�	translateNc
��eZdZdZdZdZdZdZdZdZ	e
dd	ed
��e
dd	ed
��e
ddd��e
ddd��e
ddd��e
dded��e
dded��e
dded��gZd�Z
d �Zd!�Zd"�Zd#�Zd$�Zd%�Zd&�Zd'�Zd(�Zy	))�	Openshifta�This is the plugin for OCP 4.x collections. While this product is still
    built ontop of kubernetes, there is enough difference in the collection
    requirements and approach to warrant a separate plugin as opposed to
    further extending the kubernetes plugin (or the OCP 3.x extensions included
    in the Red Hat version of the kube plugin).

    This plugin may collect OCP API information when the `with-api` option is
    enabled. This option is disabled by default.

    When enabled, this plugin will collect cluster information and inspect the
    default namespaces/projects that are created during deployment - i.e. the
    namespaces of the cluster projects matching openshift.* and kube.*. At the
    time of this plugin's creation that number of default projects is already
    north of 50; hence this plugin is expected to take a long time in both the
    setup() and collect() phases. End-user projects may also be collected from
    when those projects are included in the `add-namespaces` or
    `only-namespaces` options.

    It is expected to need to perform an `oc login` command in order for this
    plugin to be able to correctly capture information, as system root is not
    considered cluster root on the cluster nodes in order to access the API.

    Users will need to either:

        1) Accept the use of a well-known stock kubeconfig file provided via a
           static pod resource for the kube-apiserver
        2) Provide the bearer token via the `-k openshift.token` option
        3) Provide the bearer token via the `SOSOCPTOKEN` environment variable
        4) Otherwise ensure that the root user can successfully run `oc` and
           get proper output prior to running this plugin


    It is highly suggested that option #1 be used first, as this uses well
    known configurations and requires the least information from the user. If
    using a token, it is recommended to use option #3 as this will prevent
    the token from being recorded in output saved to the archive. Option #2 may
    be used if this is considered an acceptable risk. It is not recommended to
    rely on option #4, though it will provide the functionality needed.
    z Openshift Container Platform 4.x�	openshifti�)r	)zopenshift-hyperkubezg/etc/kubernetes/static-pod-resources/kube-apiserver-certs/secrets/node-kubeconfigs/localhost.kubeconfig�tokenNz admin token to allow API queries)�default�val_type�desc�
kubeconfigz+Path to a locally available kubeconfig file�hostzhttps://localhost:6443z0host address to use for oc login, including port)rr
�with-apiFzcollect output from the OCP API�podlogsTzcollect logs from each pod�podlogs-filter�z1only collect logs from pods matching this pattern�only-namespacesz2colon-delimited list of namespaces to collect from�add-namespaceszHcolon-delimited list of namespaces to add to the default collection listc�0�|jd�ddk(S)z(Check to see if we can run `oc` commandsz	oc whoami�statusr)�exec_cmd��selfs �>/usr/lib/python3/dist-packages/sos/report/plugins/openshift.py�_check_oc_functionzOpenshift._check_oc_functionWs���}�}�[�)�(�3�q�8�8�c�B�|j|jd��S)z?Check if the localhost.kubeconfig exists with system:admin userr)�path_exists�
get_optionrs r�_check_localhost_kubeconfigz%Openshift._check_localhost_kubeconfig[s��������� =�>�>rc��|j�ry|jd��|jd|j�|j	�rj|jd|jd�i�|j
d�}|ddk(r|j�ry|jd|d�d	|d
���y|jd�xstjd
d�}|rR|j
d|jd��d|�d��}|ddk(r|j�ry|jd�y|jd�y)zuSee if we're logged in to the API service, and if not attempt to do
        so using provided plugin options
        TrN�
KUBECONFIGz8oc login -u system:admin --insecure-skip-tls-verify=Truerrz&The login command failed with status: z and error: �outputFr
�SOSOCPTOKENz	oc login rz	 --token=z  --insecure-skip-tls-verify=TruezIAttempt to login to OCP API failed, will not run or collect `oc` commandszUNot logged in to OCP API, and no login token provided. Will not collect `oc` commands)
rr �
set_option�master_localhost_kubeconfigr!�set_default_cmd_environmentr�	_log_warn�os�getenv)r�oc_resr
s   r�_check_oc_logged_inzOpenshift._check_oc_logged_in_sN���"�"�$���?�?�<�(�0�
�O�O���0�0�
��+�+�-��,�,��d�o�o�l�;�.�
��]�]�2��F��h��1�$��)@�)@�)B���N�N��(�#�V�H�%5�7�
������(�J�B�I�I�m�T�,J����]�]�&*�o�o�f�&=�u�$F�G�F��h��1�$��*�*�,���N�N�6�
7�����9�	:�rc�$�|jd�r.|jd�jd�D�cgc]}|��c}Sddg}|jd�r6|jd�jd�D]}|j|��|Scc}w)z�Combine a set of regexes for collection with any namespaces passed
        to sos via the -k openshift.add-namespaces option. Note that this does
        allow for end users to specify namespace regexes of their own.
        r�:zopenshift.*zkube.*r)r �split�append)r�n�collect_regexes�nsps    r�_setup_namespace_regexesz"Openshift._setup_namespace_regexes�s����?�?�,�-�#���/@�A�G�G��L�M�!�A�M�M�
��
��
�?�?�+�,����'7�8�>�>�s�C�
,���&�&�s�+�
,����Ns�	B
c
����fd�}�j��_tt|D�cgc]
}||�s�|��c}��Scc}w)aReduce the namespace listing returned to just the ones we want to
        collect from. By default, as requested by OCP support personnel, this
        must include all 'openshift' prefixed namespaces

            :param nsps list:            Namespace names from oc output
        c�X���jD]}tj||�s�yy)z�Match a particular namespace for inclusion (or not) in the
            collection phases

                :param namespace str:   The name of a namespace
            TF)r3�re�match)�	namespace�regexrs  �r�_match_namespacez:Openshift._reduce_namespace_list.<locals>._match_namespace�s0����-�-�
 ���8�8�E�9�-��
 �r)r5r3�list�set)r�nspsr<r2s`   r�_reduce_namespace_listz Openshift._reduce_namespace_list�sA���
	� $�<�<�>����C�D�@�q�,<�Q�,?��@�A�B�B��@s
�
A�Ac�h�|jd�|jd�|jddg�|jd�|j	d�r|j�}nd}|r�|j
d�d|_g}|jgd	��|j�|jd
|jz�}|ddk(rE|d
j�ddD�cgc]}|j�d��}}|j|�}|D]}|j|��yycc}w)a�The setup() phase of this plugin will iterate through all default
        projects (namespaces), and/or those specified via the `add-namespaces`
        and `only-namespaces` plugin options. Both of these options accept
        shell-style regexes.

        Cluster-wide information, that is information that is not tied to a
        specific namespace, will be saved in the top-level plugin directory.
        Each namespace will have it's own subdir within the `namespaces` subdir
        to aide in organization. From there, each namespace subdir will have a
        subsequent subdir for each type of API resource the plugin collects.

        In contrast with the `kubernetes` plugin, this plugin will collect
        logs from all pods within each namespace, as well as the previous pod's
        logs, by default. The `-k openshift.podlogs-filter` option can be used
        to greatly reduce the amount of collected information.
        �kubeletz/etc/kubernetes/*.crtz/etc/kubernetes/*.key�/etc/kubernetes/*rFz�Note that the Openshift Container Platform plugin can be expected in most configurations to take 5+ minutes in both the setup and collection phaseszoc get )zoc cluster-infozoc get -A pvz
oc get -A csrz	oc statusz
oc versionz
%s namespacesrrr$�N)�add_journal�add_service_status�add_forbidden_path�
add_copy_specr r-r)�oc_cmd�add_cmd_output�collect_cluster_resources�collect_cmd_output�
splitlinesr0r@�collect_from_namespace)r�
can_run_oc�oc_nsps�_nm_resr2r?r:s       r�setupzOpenshift.setup�sO��(	
����#����	�*����#�#�!
�	�	
���.�/��?�?�:�&��1�1�3�J��J��
�N�N�2�
�$�D�K��G�
���!�
�
�*�*�,��-�-�o����.K�L�G��x� �A�%�*1�(�*;�*F�*F�*H���*L��%&�A�G�G�I�a�L�����5�5�d�;��%�
7�	��+�+�I�6�
7�E��8s�(D/c	��gd�}|D]~}d|z}d|zg}|j|j�d|��||��}|ddk(s�:|dj�d	d
D],}|jd|�d|j	�d��|���.��y
)
zFCollect cluster-level (non-namespaced) resources from the API
        )�clusternetworks�clusteroperators�clusterversions�componentstatuses�configs�containerruntimeconfigs�controllerconfigs�dnses�hostsubnets�infrastructures�machineconfigpools�machineconfigs�
netnamespaces�networks�nodes�proxies�storageclasseszcluster_resources/%s�ocp_%s� ��subdir�tagsrrr$rDNzoc describe �rh)rLrIrMrJr0)r�global_resources�resource�_subdir�_tag�_res�	_res_names       rrKz#Openshift.collect_cluster_resources	s���
��()�
	�H�,�x�7�G��x�'�(�D��*�*�d�k�k�8�+L�29��+�F�D��H�~��"�!%�h��!:�!:�!<�Q�R�!@��I��'�'�/7����9J�1�9M�N�&�(���
	rc��gd�}d|z}|jd|z|��|D]�}|�d|��}d|zd|�d|��|g}|j�d	|�d
|��}|j|||��}|dd
k(s�I|dj�dd}	|	D]2}
|
j	�d
}|j|�d
|�d�|d|z���4|dk(s��|	s��|jd�s��|	D�cgc]}|j	�d
��}
}|j
||
���ycc}w)z�Run through the collection routines for an individual namespace.
        This collection should include all requested resources that exist
        within that namesapce

            :param namespace str:           The name of the namespace
        )�buildconfigs�builds�catalogsourceconfigs�catalogsources�clusterserviceversions�
configmaps�
daemonsets�deploymentconfigs�deployments�events�horizontalpodautoscalers�imagestreams�ingresscontrollers�	ingresses�installplans�limitranges�machines�machinesets�
mcoconfigsznet-attach-def�operatorgroups�operatorsources�pods�pvc�resourcequotas�routes�secrets�services�statefulsets�
subscriptionsz
namespaces/%szoc describe namespace %srj�/re�ocp_�_z
 --namespace=rfrgrrr$rDNz -o yamlz%s.yaml)rh�suggest_filenamer�r)rJrIrLrMr0r �collect_podlogs)rr:�	resourcesrh�resrm�_tags�_get_cmd�_res_out�
_instances�	_instance�_instance_name�p�pod_lists              rrNz Openshift.collect_from_namespace,sR�� 
�	�F!�9�,��	
���6��B�#)�	�	+��	>�C�!'��-�G��3��(�#�.���E�
26���i��M�H��.�.����/��H���!�Q�&�%�h�/�:�:�<�Q�R�@�
�!+��I�%.�_�_�%6�q�%9�N��'�'�+3�^�D�&�)2�^�)C�(����&�=�Z�D�O�O�I�4N�6@�A�����	�!��A�H�A��(�(��H�=�;	>��8 Bs�Dc���d|z}|jd�rt|jd��}nd}|D];}|rtj||�s�d|�d|��}|j	||dzg|���=y)z�For any namespace that has active pods in it, collect the current
        and previous pod's logs

            :param pod_list list:       A list of pod names
        znamespaces/%s/pods/podlogsrNzoc logs --namespace=rfz -prj)r rr8r9rJ)rr:r��_log_dirr;�pod�_log_cmds       rr�zOpenshift.collect_podlogs}s���0�)�;���?�?�+�,��d�o�o�.>�?�@�E��E��	 �C��R�X�X�e�S�1���6?��E�H������5� �!��
 �
 �		 rc���|jd�|jd�gd�}ddj|�z}|jd|d�|j	d|d�d}|j	d	|d
�y)Nzoc rC)z.*.crtzclient-certificate-datazclient-key-datazcertificate-authority-dataz.*.keyr
z.*token.*.valuez(\s*(%s):)(.*)�|z
\1 *******r�z((?P<var>(.*\\n)?Source:\s(.*),)((.*?))\nzoc describez\g<var> *******\n)�do_cmd_private_sub�do_file_private_sub�join�do_path_regex_sub�do_cmd_output_sub)r�_fieldsr;s   r�postproczOpenshift.postproc�s|��	
����&�� � �!4�5�
��"�C�H�H�W�$5�5�����2�E�=�I����y�%��?�<�����}�e�5I�Jr)�__name__�
__module__�__qualname__�__doc__�
short_desc�plugin_name�plugin_timeout�profiles�packagesr'r�str�option_listrr!r-r5r@rRrKrNr�r��rrrrs���&�P4�J��K��N��H�'�H�	M� �	�'�4�#�9�	;��,��s�D�	F��&�":�I�	K��*�e�8�	:��)�T�0L�M��"�B��J�	L��#�R�#�K�	M��"�B��2�	4��K�&9�?�5�n�(C�0E7�N!�FO>�b �0Krr)	�sos.report.pluginsrrr�fnmatchrr*r8rr�rr�<module>r�s(��?�>��	�	�dK���dKr

Zerion Mini Shell 1.0