%PDF- %PDF-
| Direktori : /lib/python3/dist-packages/sos/report/plugins/__pycache__/ | 
| Current File : //lib/python3/dist-packages/sos/report/plugins/__pycache__/openshift.cpython-312.pyc | 
�
    i��d�C  �                   �L   � d dl mZmZmZ d dlmZ d dlZd dlZ G d� dee�      Zy)�    )�Plugin�RedHatPlugin�	PluginOpt)�	translateNc            
       �  � e Zd ZdZdZdZdZdZdZdZ	 e
dd	ed
��       e
dd	ed
��       e
ddd��       e
ddd��       e
ddd��       e
dded��       e
dded��       e
dded��      gZd� 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)r   r
   �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   � | j                  d�      d   dk(  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_functionW   s   � ��}�}�[�)�(�3�q�8�8�    c                 �B   � | j                  | j                  d�      �      S )z?Check if the localhost.kubeconfig exists with system:admin userr   )�path_exists�
get_optionr   s    r   �_check_localhost_kubeconfigz%Openshift._check_localhost_kubeconfig[   s   � �������� =�>�>r   c                 �  � | j                  �       ry| j                  d�      �| j                  d| j                  �       | j	                  �       rj| j                  d| j                  d�      i�       | j
                  d�      }|d   dk(  r| j                  �       ry| j                  d|d   �d	|d
   ���       y| j                  d�      xs t        j                  d
d�      }|rR| j
                  d| j                  d�      �d|�d��      }|d   dk(  r| j                  �       ry| j                  d�       y| j                  d�       y)zuSee if we're logged in to the API service, and if not attempt to do
        so using provided plugin options
        Tr   N�
KUBECONFIGz8oc login -u system:admin --insecure-skip-tls-verify=Truer   r   z&The login command failed with status: z and error: �outputFr
   �SOSOCPTOKENz	oc login r   z	 --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)
r   r    �
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� 	:�r   c                 �$  � | j                  d�      r.| j                  d�      j                  d�      D �cg c]  }|�� c}S ddg}| j                  d�      r6| j                  d�      j                  d�      D ]  }|j                  |�       � |S c c}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                  �       � _        t        t        |D �cg c]
  } ||�      s�|�� c}�      �      S c c}w )a  Reduce 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   �� �j                   D ]  }t        j                  || �      s� y y)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�regexr   s     �r   �_match_namespacez:Openshift._reduce_namespace_list.<locals>._match_namespace�   s0   �� � �-�-� 
 ���8�8�E�9�-��
 � r   )r5   r3   �list�set)r   �nspsr<