Sindbad~EG File Manager

Current Path : /lib/python3.6/site-packages/sos/report/plugins/__pycache__/
Upload File :
Current File : //lib/python3.6/site-packages/sos/report/plugins/__pycache__/openshift.cpython-36.opt-1.pyc

3

�f�C�@sFddlmZddlZddlZddlmZmZmZGdd�dee�ZdS)�)�	translateN)�Plugin�RedHatPlugin�	PluginOptc
@s�eZdZdZdZdZdZd3Zd4ZdZ	dZ
edd	ed
d�edd	ed
d�edddd�edddd�edddd�eddedd�eddedd�eddedd�gZ
dd �Zd!d"�Zd#d$�Zd%d&�Zd'd(�Zd)d*�Zd+d,�Zd-d.�Zd/d0�Zd1d2�Zd	S)5�	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��openshift-hyperkubezg/etc/kubernetes/static-pod-resources/kube-apiserver-certs/secrets/node-kubeconfigs/localhost.kubeconfigzoc get �tokenNz admin token to allow API queries)�defaultZval_type�desc�
kubeconfigz+Path to a locally available kubeconfig file�hostzhttps://localhost:6443z0host address to use for oc login, including port)r
rzwith-apiFzcollect output from the OCP API�podlogsTzcollect logs from each podzpodlogs-filter�z1only collect logs from pods matching this patternzonly-namespacesz2colon-delimited list of namespaces to collect fromzadd-namespaceszHcolon-delimited list of namespaces to add to the default collection listcCs|jd�ddkS)z(Check to see if we can run `oc` commandsz	oc whoami�statusr)�exec_cmd)�self�r�/usr/lib/python3.6/openshift.py�_check_oc_functionYszOpenshift._check_oc_functioncCs|j|jd��S)z?Check if the localhost.kubeconfig exists with system:admin userr)Zpath_exists�
get_option)rrrr�_check_localhost_kubeconfig]sz%Openshift._check_localhost_kubeconfigcCs�|j�rdS|jd�dkr(|jd|j�|j�r�|jd|jd�i�|jd�}|ddkrf|j�rfdS|jd|d|d	f�d
S|jd�p�tj	dd�}|r�|jd
|jd�|f�}|ddkr�|j�r�dS|jd�d
S|jd�d
S)zuSee if we're logged in to the API service, and if not attempt to do
        so using provided plugin options
        TrNZ
KUBECONFIGz8oc login -u system:admin --insecure-skip-tls-verify=Truerrz6The login command failed with status: %s and error: %s�outputFr	ZSOSOCPTOKENz6oc login %s --token=%s --insecure-skip-tls-verify=Truer
zIAttempt 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)
rrZ
set_option�master_localhost_kubeconfigrZset_default_cmd_environmentr�	_log_warn�os�getenv)rZoc_resr	rrr�_check_oc_logged_inas8

zOpenshift._check_oc_logged_incCsX|jd�rt|jd�jd��Sddg}|jd�rTx"|jd�jd�D]}|j|�qBW|S)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.
        zonly-namespaces�:zopenshift.*zkube.*zadd-namespaces)r�list�split�append)rZcollect_regexesZnsprrr�_setup_namespace_regexes�s

z"Openshift._setup_namespace_regexescs(dd��|j��t��fdd�|D��S)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
        cSs"x|D]}tj||�rdSqWdS)z�Match a particular namespace for inclusion (or not) in the
            collection phases

                :param namespace str:   The name of a namespace
            TF)�re�match)�	namespace�regexes�regexrrr�_match_namespace�s
z:Openshift._reduce_namespace_list.<locals>._match_namespacecsh|]}�|��r|�qSrr)�.0�n)r(r&rr�	<setcomp>�sz3Openshift._reduce_namespace_list.<locals>.<setcomp>)r"r)r�nspsr)r(r&r�_reduce_namespace_list�sz Openshift._reduce_namespace_listcCs�|jd�|jd�|jddg�|jd�|jd�r@|j�}nd}|r�|jd�g}|jdd	d
ddg�|j�|j	d
|j
�}|ddkr�dd�|dj�dd�D�}|j|�}x|D]}|j
|�q�WdS)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.
        Zkubeletz/etc/kubernetes/*.crtz/etc/kubernetes/*.keyz/etc/kubernetes/*zwith-apiFz�Note that the Openshift Container Platform plugin can be expected in most configurations to take 5+ minutes in both the setup and collection phaseszoc cluster-infozoc get -A pvz
oc get -A csrz	oc statusz
oc versionz
%s namespacesrrcSsg|]}|j�d�qS)r)r )r)r*rrr�
<listcomp>sz#Openshift.setup.<locals>.<listcomp>r�N)Zadd_journalZadd_service_statusZadd_forbidden_pathZ
add_copy_specrrr�add_cmd_output�collect_cluster_resources�collect_cmd_output�oc_cmd�
splitlinesr-�collect_from_namespace)rZ
can_run_ocZoc_nspsZ_nm_resr,r%rrr�setup�s4






zOpenshift.setupcCs�ddddddddd	d
ddd
ddddg}x�|D]x}d|}d|g}|jd|j|f||d�}|ddkr,x:|dj�dd�D]"}|jd||j�df|d�q~Wq,WdS)zFCollect cluster-level (non-namespaced) resources from the API
        ZclusternetworksZclusteroperatorsZclusterversionsZcomponentstatusesZconfigsZcontainerruntimeconfigsZcontrollerconfigsZdnsesZhostsubnetsZinfrastructuresZmachineconfigpoolsZmachineconfigsZ
netnamespacesZnetworksZnodesZproxiesZstorageclasseszcluster_resources/%szocp_%sz%s %s)�subdir�tagsrrrr/Nzoc describe %s %s)r7)r2r3r4r0r )rZglobal_resourcesZresource�_subdirZ_tagZ_resZ	_res_namerrrr1
s6


z#Openshift.collect_cluster_resourcesc
Cs4ddddddddd	d
ddd
dddddddddddddddddg}d|}|jd ||d!�x�|D]�}d"||f}d#|d$||f|g}d%|j||f}|j|||d&�}|d'd(kr`|d)j�d*d+�}	x4|	D],}
|
j�d(}|jd,||f|d-|d.�q�W|dkr`|	r`|jd/�r`d0d1�|	D�}|j||�q`Wd+S)2z�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
        ZbuildconfigsZbuildsZcatalogsourceconfigsZcatalogsourcesZclusterserviceversionsZ
configmapsZ
daemonsetsZdeploymentconfigsZdeploymentsZeventsZhorizontalpodautoscalersZimagestreamsZingresscontrollersZ	ingressesZinstallplansZlimitrangesZmachinesZmachinesetsZ
mcoconfigsznet-attach-defZoperatorgroupsZoperatorsourcesZpodsZpvcZresourcequotasZroutes�secretsZservicesZstatefulsetsZ
subscriptionsz
namespaces/%szoc describe namespace %s)r7z%s/%szocp_%sz	ocp_%s_%sz%s --namespace=%s %s)r7r8rrrr/Nz
%s %s -o yamlz%s.yaml)r7Zsuggest_filenamercSsg|]}|j�d�qS)r)r )r)�prrrr.{sz4Openshift.collect_from_namespace.<locals>.<listcomp>)r0r3r2r4r r�collect_podlogs)
rr%Z	resourcesr7�resr9Z_tagsZ_get_cmdZ_res_outZ
_instancesZ	_instanceZ_instance_name�pod_listrrrr5-sl





z Openshift.collect_from_namespacecCsnd|}|jd�r"t|jd��}nd}xB|D]:}|rDtj||�rDq,d||f}|j||dg|d�q,WdS)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/podlogszpodlogs-filterNzoc logs --namespace=%s %sz -p)r7)rrr#r$r0)rr%r>Z_log_dirr'ZpodZ_log_cmdrrrr<~s

zOpenshift.collect_podlogscCsf|jd�|jd�ddddddd	g}d
dj|�}|jd|d�|jd
|d�d}|jd|d�dS)Nzoc z/etc/kubernetes/*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)Zdo_cmd_private_subZdo_file_private_sub�joinZdo_path_regex_subZdo_cmd_output_sub)r�_fieldsr'rrr�postproc�s

zOpenshift.postproc)r)r)�__name__�
__module__�__qualname__�__doc__Z
short_descZplugin_nameZplugin_timeoutZprofilesZpackagesrr3r�strZoption_listrrrr"r-r6r1r5r<rBrrrrrsB'
7F#Qr)	Zfnmatchrrr#Zsos.report.pluginsrrrrrrrr�<module>	s

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists