idCLddlmZmZmZddlmZddlZddlZGddeeZy))Plugin RedHatPlugin PluginOpt) translateNc eZdZdZdZdZdZdZdZdZ e dd e d e d d e d e ddde ddde ddde dde d e dde d e dde d gZ dZ d Zd!Zd"Zd#Zd$Zd%Zd&Zd'Zd(Zy )) OpenshiftaThis 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.kubeconfigtokenNz admin token to allow API queries)defaultval_typedesc kubeconfigz+Path to a locally available kubeconfig filehostzhttps://localhost:6443z0host address to use for oc login, including port)r r with-apiFzcollect output from the OCP APIpodlogsTzcollect logs from each podpodlogs-filterz1only collect logs from pods matching this patternonly-namespacesz2colon-delimited list of namespaces to collect fromadd-namespaceszHcolon-delimited list of namespaces to add to the default collection listc0|jdddk(S)z(Check to see if we can run `oc` commandsz oc whoamistatusr)exec_cmdselfs >/usr/lib/python3/dist-packages/sos/report/plugins/openshift.py_check_oc_functionzOpenshift._check_oc_functionWs}}[)(3q88cB|j|jdS)z?Check if the localhost.kubeconfig exists with system:admin userr) path_exists get_optionrs r_check_localhost_kubeconfigz%Openshift._check_localhost_kubeconfig[s =>>rc|jry|jd|jd|j|j rj|j d|jdi|j d}|ddk(r|jry|jd|dd |d y |jd xstjd d}|rR|j d|jdd|d}|ddk(r|jry|jdy |jdy )zuSee if we're logged in to the API service, and if not attempt to do so using provided plugin options TrN KUBECONFIGz8oc login -u system:admin --insecure-skip-tls-verify=Truerrz&The login command failed with status: z and error: outputFr SOSOCPTOKENz oc login rz --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) rr set_optionmaster_localhost_kubeconfigr!set_default_cmd_environmentr _log_warnosgetenv)roc_resr s r_check_oc_logged_inzOpenshift._check_oc_logged_in_sN  " " $ ??< ( 0 OO00   + + -  , ,dool;. ]]2Fh1$)@)@)B NN(#VH%57 (JBIImT,J ]]&*oof&=u$FGFh1$**, NN6 7 9 :rc$|jdr.|jdjdDcgc]}|c}Sddg}|jdr6|jdjdD]}|j||Scc}w)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. r:z openshift.*zkube.*r)r splitappend)rncollect_regexesnsps r_setup_namespace_regexesz"Openshift._setup_namespace_regexess ??, -#/@AGGLM!AM M    ??+ ,'78>>sC ,&&s+ ,Ns B c fd}j_tt|Dcgc] }||s |c}Scc}w)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 cXjD]}tj||syy)zMatch a particular namespace for inclusion (or not) in the collection phases :param namespace str: The name of a namespace TF)r3rematch) namespaceregexrs r_match_namespacez:Openshift._reduce_namespace_list.._match_namespaces0-- 88E9- r)r5r3listset)rnspsr<r2s` r_reduce_namespace_listz Openshift._reduce_namespace_listsA  $<<>CD@q,C!'-G3(#.E 26iMH../H!Q&%h/::8 BsDcd|z}|jdrt|jd}nd}|D];}|rtj||sd|d|}|j ||dzg|=y)zFor 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/podlogsrNzoc logs --namespace=rfz -prj)r rr8r9rJ)rr:r_log_dirr;pod_log_cmds rrzOpenshift.collect_podlogs}s 0); ??+ ,doo.>?@EE CRXXeS16?EH   5 !   rc|jd|jdgd}ddj|z}|jd|d|j d|dd}|j d |d y) Nzoc rC)z.*.crtzclient-certificate-datazclient-key-datazcertificate-authority-dataz.*.keyr z.*token.*.valuez(\s*(%s):)(.*)|z \1 *******rz((?P(.*\\n)?Source:\s(.*),)((.*?))\nz oc describez\g *******\n)do_cmd_private_subdo_file_private_subjoindo_path_regex_subdo_cmd_output_sub)r_fieldsr;s rpostproczOpenshift.postprocs| &   !45 "CHHW$55 2E=I y%?< }e5IJr)__name__ __module__ __qualname____doc__ short_desc plugin_nameplugin_timeoutprofilespackagesr'rstr option_listrr!r-r5r@rRrKrNrrrrrrs&P4JKNH'H M '4#9 ;,sD F&":I K*e8 :)T0LM"BJ L#R#K M"B2 4K&9?5n(C0E7N!FO>b 0Krr) sos.report.pluginsrrrfnmatchrr*r8rrrrrs(?> dK dKr