3
ܓiC @ sF d dl mZ d dlZd dlZd dlmZmZmZ G dd deeZdS ) ) translateN)PluginRedHatPlugin PluginOptc
@ s e Zd ZdZdZdZdZd4Zd5ZdZ dZ
ed d
edded
d
eddeddddeddddeddddeddeddeddeddeddeddgZ
d d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Zd
S )6 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-hyperkubeopenshift-kubeletzg/etc/kubernetes/static-pod-resources/kube-apiserver-certs/secrets/node-kubeconfigs/localhost.kubeconfigzoc get tokenNz admin token to allow API queries)defaultZval_typedesc
kubeconfigz+Path to a locally available kubeconfig filehostzhttps://localhost:6443z0host address to use for oc login, including port)r r zwith-apiFzcollect output from the OCP APIpodlogsTzcollect 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 dd dkS )z(Check to see if we can run `oc` commandsz oc whoamistatusr )exec_cmd)self r /usr/lib/python3.6/openshift.py_check_oc_functionY s zOpenshift._check_oc_functionc C s | j | jdS )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 | jddkr(| jd| j | j r| jd| jdi | jd}|d dkrf| j rfdS | jd|d d |d
dS | jdptj d
d}|r| jd| jd d| d}|d dkr| j rdS | jd dS | jd 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_optionmaster_localhost_kubeconfigr Zset_default_cmd_environmentr _log_warnosgetenv)r Zoc_resr
r r r _check_oc_logged_ina s4
zOpenshift._check_oc_logged_inc C sX | j drt| j djdS ddg}| j drTx"| j djdD ]}|j| qBW |S )zCombine 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 listsplitappend)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" x|D ]}t j|| rdS qW dS )zMatch a particular namespace for inclusion (or not) in the
collection phases
:param namespace str: The name of a namespace
TF)rematch) namespaceregexesregexr r r _match_namespace s
z:Openshift._reduce_namespace_list.