Sindbad~EG File Manager
3
�f�C � @ sF d dl mZ d dlZd dlZd dlmZmZmZ G dd� dee�ZdS )� )� translateN)�Plugin�RedHatPlugin� PluginOptc
@ s� e Zd ZdZdZdZdZd3Zd4ZdZ dZ
edd ed
d�edd ed
d�edddd�edddd�edddd�eddedd�eddedd�eddedd�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
r zwith-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 listc C s | j d�d dkS )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_functionY s zOpenshift._check_oc_functionc C s | j | jd��S )z?Check if the localhost.kubeconfig exists with system:admin userr )Zpath_exists�
get_option)r r r r �_check_localhost_kubeconfig] s z%Openshift._check_localhost_kubeconfigc C s� | j � rdS | jd�dkr(| jd| j� | j� r�| jd| jd�i� | jd�}|d dkrf| j � rfdS | jd|d |d f � d
S | jd�p�tj dd�}|r�| jd
| jd�|f �}|d dkr�| 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=Truer r z6The 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)
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 s8
zOpenshift._check_oc_logged_inc C sX | j d�rt| j d�jd��S ddg}| j d�rTx"| j d�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)r Zcollect_regexesZnspr r r �_setup_namespace_regexes� s
z"Openshift._setup_namespace_regexesc s( dd� � | j � �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"