관리-도구
편집 파일: openshift.cpython-39.opt-1.pyc
a ����-�_g�C����������������������@���sF���d�dl�mZ�d�dlZd�dlZd�dlmZmZmZ�G�dd��dee�ZdS�)�����)� translateN)�Plugin�RedHatPlugin� PluginOptc���������������� ���@���s����e�Zd�ZdZdZdZdZdZdZdZ dZ ed d edd�ed d edd�edddd�edddd�edddd�eddedd�eddedd�eddedd�gZ d d!��Zd"d#��Zd$d%��Zd&d'��Zd(d)��Zd*d+��Zd,d-��Zd.d/��Zd0d1��Zd2d3��Zd S�)4� 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-hyperkubezopenshift-kubeletzg/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 ���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�����������������C���s���|���d�d�dkS�)z(Check to see if we can run `oc` commandsz oc whoami�statusr���)�exec_cmd��self��r����@/usr/lib/python3.9/site-packages/sos/report/plugins/openshift.py�_check_oc_functionY���s����zOpenshift._check_oc_functionc�����������������C���s���|���|��d��S�)z?Check if the localhost.kubeconfig exists with system:admin userr���)Zpath_exists� get_optionr���r���r���r����_check_localhost_kubeconfig]���s����z%Openshift._check_localhost_kubeconfigc�����������������C���s����|�����rdS�|��d�du�r(|��d|�j��|����r�|��d|��d�i��|��d�}|d�dkrf|�����rfdS�|��d|d���d |d ������dS�|��d�p�t� d d�}|r�|��d|��d���d|��d��}|d�dkr�|�����r�dS�|��d��dS�|��d��dS�)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=Truer���r���z&The login command failed with status: z and error: �outputFr���ZSOSOCPTOKENz 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���Z set_option�master_localhost_kubeconfigr���Zset_default_cmd_environmentr���� _log_warn�os�getenv)r���Zoc_resr���r���r���r����_check_oc_logged_ina���sH����� ������ zOpenshift._check_oc_logged_inc�����������������C���sT���|���d�rt|���d��d��S�ddg}|���d�rP|���d��d�D�]}|�|��q@|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. r����:zopenshift.*zkube.*r���)r����list�split�append)r���Zcollect_regexesZnspr���r���r����_setup_namespace_regexes����s���� � z"Openshift._setup_namespace_regexesc��������������������s(���dd����|������t���fdd�|D���S�)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�����������������S���s ���|D�]}t��||��r�dS�qdS�)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�regexr���r���r����_match_namespace����s����z:Openshift._reduce_namespace_list.<locals>._match_namespacec��������������������s���h�|�]}��|��r|�qS�r���r�����.0�n�r,���r*���r���r���� <setcomp>���������z3Openshift._reduce_namespace_list.<locals>.<setcomp>)r&���r#���)r����nspsr���r0���r����_reduce_namespace_list����s����z Openshift._reduce_namespace_listc�����������������C���s����|���d��|��d��|��ddg��|��d��|��d�r@|����}nd}|r�|��d��g�}|��g�d���|�����|�� |�j ��d ��}|d �dkr�dd ��|d����dd��D��}|��|�}|D�]}|�� |��q�dS�)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/*.key�/etc/kubernetes/*r ���Fz�Note that the Openshift Container Platform plugin can be expected in most configurations to take 5+ minutes in both the setup and collection phases)zoc cluster-infozoc get -A pvz oc get -A csrz oc statusz oc versionz namespacesr���r���c�����������������S���s���g�|�]}|����d���qS��r����r$���r-���r���r���r���� <listcomp>��s���z#Openshift.setup.<locals>.<listcomp>r�������N)Zadd_journalZadd_service_statusZadd_forbidden_pathZ add_copy_specr���r!���r����add_cmd_output�collect_cluster_resources�collect_cmd_output�oc_cmd� splitlinesr4����collect_from_namespace)r���Z can_run_ocZoc_nspsZ_nm_resr3���r)���r���r���r����setup����s2���� � � � zOpenshift.setupc�����������������C���s����g�d�}|D�]~}d|���}d|���g}|�j�|�j��d|���||d�}|d�dkr|d����d d ��D�]&}|�jd|��d|���d����|d��qbqd S�) zFCollect cluster-level (non-namespaced) resources from the API )ZclusternetworksZclusteroperatorsZclusterversionsZcomponentstatusesZconfigsZcontainerruntimeconfigsZcontrollerconfigsZdnsesZhostsubnetsZinfrastructuresZmachineconfigpoolsZmachineconfigsZ netnamespacesZnetworksZnodesZproxiesZstorageclasseszcluster_resources/�ocp_� ��subdir�tagsr���r���r���r9���Nzoc describe �rD���)r<���r=���r>���r:���r$���)r���Zglobal_resources�resource�_subdirZ_tagZ_resZ _res_namer���r���r���r;��� ��s���� ��z#Openshift.collect_cluster_resourcesc����������� ������C���s��g�d�}d|���}|�j�d|���|d��|D�]�}|��d|���}d|���d|��d|���|g}|�j��d|��d |���}|�j|||d �}|d�dkr*|d ����dd��} | D�]2} | ���d�}|�j�|��d |��d�||��d�d��q�|dkr*| r*|��d�r*dd��| D��}|��||��q*dS�)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 )ZbuildconfigsZbuildsZcatalogsourceconfigsZcatalogsourcesZclusterserviceversionsZ configmapsZ daemonsetsZdeploymentconfigsZdeploymentsZeventsZhorizontalpodautoscalersZimagestreamsZingresscontrollersZ ingressesZinstallplansZlimitrangesZmachinesZmachinesetsZ mcoconfigsznet-attach-defZoperatorgroupsZoperatorsources�podsZpvcZresourcequotasZroutes�secretsZservicesZstatefulsetsZ subscriptions�namespaces/zoc describe namespace rF����/rA����_z --namespace=rB���rC���r���r���r���r9���Nz -o yamlz.yaml)rD���Zsuggest_filenamerI���r���c�����������������S���s���g�|�]}|����d���qS�r6���r7���)r.����pr���r���r���r8���{��r2���z4Openshift.collect_from_namespace.<locals>.<listcomp>)r:���r=���r<���r>���r$���r����collect_podlogs) r���r)���Z resourcesrD����resrH���Z_tagsZ_get_cmdZ_res_outZ _instancesZ _instanceZ_instance_name�pod_listr���r���r���r?���-��s:���� # ����z Openshift.collect_from_namespacec�����������������C���sp���d|��d�}|���d�r&t|���d��}nd}|D�]<}|rDt�||�sDq.d|��d|���}|�j||d�g|d��q.dS�) 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 rK���z /pods/podlogsr���Nzoc logs --namespace=rB���z -prF���)r���r���r'���r(���r:���)r���r)���rQ���Z_log_dirr+���ZpodZ_log_cmdr���r���r���rO���~��s���� ��zOpenshift.collect_podlogsc�����������������C���s`���|���d��|��d��g�d�}dd�|���d�}|��d|d��|��d|d��d }|��d |d��d�S�)Nzoc r5���)z.*.crtzclient-certificate-datazclient-key-datazcertificate-authority-dataz.*.keyr���z.*token.*.valuez(\s*(�|z):)(.*)z \1 *******rJ���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+���r���r���r����postproc���s���� zOpenshift.postproc)�__name__� __module__�__qualname__�__doc__Z short_descZplugin_nameZplugin_timeoutZprofilesZpackagesr���r=���r����strZoption_listr���r���r!���r&���r4���r@���r;���r?���rO���rU���r���r���r���r���r������sT���(���������7F#Qr���) �fnmatchr���r���r'���Zsos.report.pluginsr���r���r���r���r���r���r���r����<module> ���s���