
    ~ g.@                     F    S SK r S SKJr  S SKJr  S SKJr   " S S\5      rg)    N)quote)Cluster)is_executablec                      ^  \ rS rSrSrSrSrSrSrSr	Sr
Sr/ S	Qr\S
 5       rS rS rU 4S jrS rS rS rS rS rS rS rS rS rS rS rSrU =r$ )ocp   a'  
This profile is for use with OpenShift Container Platform (v4) clusters
instead of the kubernetes profile.

This profile will favor using the `oc` transport type, which means it will
leverage a locally installed `oc` binary. This is also how node enumeration
is done. To instead use SSH to connect to the nodes, use the
'--transport=control_persist' option.

Thus, a functional `oc` binary for the user executing sos collect is
required. Functional meaning that the user can run `oc` commands with
clusterAdmin privileges.

If this requires the use of a secondary configuration file, specify that
path with the 'kubeconfig' cluster option. This config file will also be
used on a single master node to perform API collections if the `with-api`
option is enabled (default disabled). If no `kubeconfig` option is given,
but `with-api` is enabled, the cluster profile will attempt to use a
well-known default kubeconfig file if it is available on the host.

Alternatively, provide a clusterAdmin access token either via the 'token'
cluster option or, preferably, the SOSOCPTOKEN environment variable.

By default, this profile will enumerate only master nodes within the
cluster, and this may be changed by overriding the 'role' cluster option.
To collect from all nodes in the cluster regardless of role, use the form
-c ocp.role=''.

Filtering nodes by a label applied to that node is also possible via the
label cluster option, though be aware that this is _combined_ with the role
option mentioned above.

To avoid redundant collections of OCP API information (e.g. 'oc get'
commands), this profile will attempt to enable the API collections on only
a single master node. If the none of the master nodes have a functional
'oc' binary available, *and* the --no-local option is used, that means that
no API data will be collected.
zOpenShift Container Platform v4)zopenshift-hyperkubezopenshift-clientsFNzsos-collect-tmp ))labelr	   z3Colon delimited list of labels to select nodes with)rolemasterz*Colon delimited list of roles to filter on)
kubeconfigr	   zPath to the kubeconfig file)tokenr	   z1Service account token to use for oc authorization)with-apiFz'Collect OCP API data from a master node)api-urlr	   z.Alternate API URL of an external control-planec                    U R                   (       GdX  SU l         U R                  R                  R                  5       (       a  U R                  R	                  SU R                  R                  R
                  S9nUS   S:X  ad  [        R                  R                  U R                  R                  R
                  US   R                  5       R                  S5      5      U l         O(U R                  S5        U R                  S	US    35        U R                  S
5      (       a'  U =R                   SU R                  S
5       3-  sl         U R                  SU R                    35        U R                   $ )Noczwhich oc)chrootstatusr   output/zHUnable to to determine PATH for 'oc' command, node enumeration may fail.zLocating 'oc' failed: r   z --kubeconfig zoc base command set to )_oc_cmdprimaryhostin_containerrun_commandsysrootospathjoinstriplstriplog_warn	log_debug
get_option)self_oc_paths     </usr/lib/python3/dist-packages/sos/collector/clusters/ocp.pyoc_cmd
ocp.oc_cmdL   s3   |||DL||  --//<<33t||'8'8'@'@ 4  H%*#%77<<))11 *00299#>$DL
 MM5 NN0(1C0DEG|,, 0??<89!; ;NN4T\\NCD||    c                 $    U R                    SU 3$ )zSFormat the oc command to optionall include the kubeconfig file if
one is specified
 )r(   )r%   cmds     r'   
fmt_oc_cmdocp.fmt_oc_cmdf   s     ++au%%r*   c           
          U R                  U R                  SU R                   SU R                  S5       35      5      nUS   S:H  $ )zHAttempt to login to the API using the oc command using a provided
token
z.login --insecure-skip-tls-verify=True --token=r,   r   r   r   )exec_primary_cmdr.   r   r$   )r%   _ress     r'   _attempt_oc_loginocp._attempt_oc_loginl   sU     $$OO ''+zzl!#y9:< =

 H~""r*   c                 &  > [         TU ]  5       (       a  gU R                  S5      =(       d    [        R                  " SS 5      U l        U R
                  (       a  U R                  5         U R                  S5      nU R                  U5      S   S:H  $ )NTr   SOSOCPTOKENwhoamir   r   )	supercheck_enabledr$   r   getenvr   r3   r.   r1   )r%   _who	__class__s     r'   r9   ocp.check_enabledw   sp    7 ""__W-O=$1O
::""$x($$T*8499r*   c                    U R                  5       S:X  d  gU R                  U R                  S5      5      nUS   S:H  U l        U R                  (       d  U R	                  S5        [        S5      eU R                  SU R                   S	35        U R                  U R                  S
U R                   35      5      nUS   S:X  a  U R                  5         gU R	                  SUS    35        [        S5      e)z`Create the project that we will be executing in for any nodes'
collection via a container image
r   Nzauth can-i '*' '*'r   r   zWCheck for cluster-admin privileges returned false, cannot create project in OCP clusterzLInsufficient permissions to create temporary collection project.
Aborting...z Creating new temporary project ''znew-project TzFailed to create project: r   z?Failed to create temporary project for collection. 
Aborting...)	set_transport_typer1   r.   oc_cluster_adminr#   	Exceptionlog_infoproject_label_sos_project)r%   outrets      r'   setup	ocp.setup   s     &&(D0##DOO4H$IJ #H 2$$NN C D ? @ @ 	8aHI##OOl4<<.9:
 x=A##%3CM?CD ( ) 	)r*   c           
          SS/nU HN  nU R                  U R                  SU R                   SU S35      5      nUS   S:X  a  M?  [        SUS	    35      e   g
)zpAdd pertinent labels to the temporary project we've created so that
our privileged containers can properly run.
z4security.openshift.io/scc.podSecurityLabelSync=falsez-pod-security.kubernetes.io/enforce=privilegedzlabel namespace r,   z --overwriter   r   z!Error applying namespace labels: r   N)r1   r.   rD   rB   )r%   labelsr
   rG   s       r'   rE   ocp._label_sos_project   sy    
 C;
 E''&t||nAeWLIC
 x=A%7HG  r*   c                    U R                   (       a   U R                  U R                  SU R                    35      SS9nUS   S:X  d  U R                  SUS    35        U R                  U R                  SU R                    S	35      5      nUS   S:X  d  U R                  S
US    35        U R                  U R                  S5      5        SU l         g! [         a  nU R                  SU S35         SnANLSnAff = f)z8Remove the project we created to execute within
        zdelete project    )timeoutr   r   z"Error deleting temporary project: r   zwait namespace/z --for=delete --timeout=30sz3Error waiting for temporary project to be deleted: zAFailed attempting to remove temporary project 'sos-collect-tmp': z.
Please manually remove the temporary project.Nzproject defaultT)rD   r1   r.   	log_errorrB   )r%   rG   errs      r'   cleanupocp.cleanup   s"    <<++OOodll^$DE ,  8})NN<S]OL ++OO)$,, 8( ) 8})NNMx=/+ !!$//2C"DEDL  **- /DE s   BC 
C>C99C>c                 `   0 nSUS   ;   a  UR                  S5      R                  5       n0 nS H%  n UR                  UR                  5       5      XE'   M'     U H?  nUR                  5       n0 X'S   '   UR                  5        H  u  pXy   X'S      U'   M     MA     U$ ! [         a     M}  f = f)aO  From the output of get_nodes(), construct an easier-to-reference
dict of nodes that will be used in determining labels, primary status,
etc...

:param nodelist:        The split output of `oc get nodes`
:type nodelist:         ``list``

:returns:           A dict of nodes with `get nodes` columns as keys
:rtype:             ``dict``
NAMEr   )r   rolesversionzos-image)popsplitindexupperrB   items)
r%   nodelistnodesstatlineidxstatenode_nodecolumnvalues
             r'   _build_dictocp._build_dict   s     Xa[ ||A,,.HCC!)!>CJ D !

"$Ah%(YY[MF.3lE(OF+ &1 !
  !  s   !B
B-,B-c                 d   U R                   R                  S:w  a  U R                   R                  $ [        SU R                  R                  R
                  S9(       a  gU R                  S5        U R                  R                  S5        U R                   R                  (       d  [        S5        g)Nautor   )r   z]Local installation of 'oc' not found or is not correctly configured. Will use ControlPersist.z=Preferred transport 'oc' not available, will fallback to SSH.z?Press ENTER to continue connecting with SSH, or Ctrl+C toabort.control_persist)opts	transportr   r   r   r   rC   ui_logwarningbatchinput)r%   s    r'   r@   ocp.set_transport_type   s    99&(99&&&t||'8'8'@'@A G 	HK	
 yy   r*   c                    / n0 U l         SnU R                  S5      (       a@  SR                  U R                  S5      R                  S5      5      nUS[	        U5       3-  nU R                  U R                  U5      5      nUS   S:X  a  U R                  S5      S	:X  a  U R                  S
5        [        U R                  S5      R                  S5      5      nU R                  US   R                  5       5      U l         U R                   R                  5        HD  u  pgU(       a'  U H  nXS   ;   d  M  UR                  U5          M/     M3  UR                  U5        MF     U$ Sn	SUS   ;   a  Sn	[        U	5      e)Nzget nodes -o wider
   ,:z -l r   r   r   r   zNOTE: By default, only master nodes are listed.
To collect from all/more nodes, override the role option with '-c ocp.role=role1:role2'r   rV   z'oc' command failedzMissing or incompleteza'oc' failed due to missing kubeconfig on primary node. Specify one via '-c ocp.kubeconfig=<path>')	node_dictr$   r   rY   r   r1   r.   r"   listrf   
splitlinesr\   appendrB   )
r%   r^   r-   rK   resrV   	node_namerb   r   msgs
             r'   	get_nodesocp.get_nodes   sZ   !??7##XXdoog6<<SABFT%-))C##DOOC$89x=Av&(2 K L 066s;<E!--c(m.F.F.HIDN#'>>#7#7#9	 %=0!LL3! !&
 LL+ $:  (C&#h-7EC. r*   c                     UR                   U R                  ;  a  gS H&  nX R                  UR                      S   ;   d  M$  Us  $    g)Nr	   )r   workerrV   addressru   )r%   rb   r
   s      r'   set_node_labelocp.set_node_label  sA    <<t~~-)Et||4W== * r*   c                 v    UR                   U R                  ;  a  gSU R                  UR                      S   ;   $ )NFr   rV   r   r%   rb   s     r'   check_node_is_primaryocp.check_node_is_primary%  s2    <<t~~-4>>$,,7@@@r*   c                     UR                  S5      (       a  SnU(       a  SOSnOSnU(       a  SOSnUR                  R                  SU SU 35        g)	a  In earlier versions of sos, the openshift plugin option that is
used to toggle the API collections was called `no-oc` rather than
`with-api`. This older plugin option had the inverse logic of the
current `with-api` option.

Use this to toggle the correct plugin option given the node's sos
version. Note that the use of version 4.2 here is tied to the RHEL
release (the only usecase for this cluster profile) rather than
the upstream version given the backports for that downstream.

:param node:    The node being inspected for API collections
:type node:     ``SoSNode``

:param use_api: Should this node enable API collections?
:type use_api:  ``bool``
z4.2-16r   onoffzno-ocz
openshift.=N)check_sos_versionplugoptsrx   )r%   rb   use_api_opt_vals        r'   _toggle_api_optocp._toggle_api_opt*  sP    " !!(++D"4DD#5Dz$q78r*   c                 8   UR                   R                  S5        U R                  S5      (       d  U R                  US5        g U R                  (       a  U R                  US5        g SnU R                  S5      nU(       a  UR                  S5      (       d  SU-   nU=(       d    UnSnUR                  R                  (       a  S	nUR                  S
SS9(       a  US-  nUR                  U S3UR                  R                  SS9nUS   S:X  a  U R                  US5        SU l        OU R                  (       a3  U R                  UR                  S'   U R                  US5        SU l        ORUR                  U5      (       a<  XB:w  a  UR                  R                  SU 35        U R                  US5        SU l        U R                  (       aG  SUR                   S3nU R                  R                  U5        U R                   R                  U5        g g )N	openshiftr   Fzl/host/etc/kubernetes/static-pod-resources/kube-apiserver-certs/secrets/node-kubeconfigs/localhost.kubeconfigr   z/hostz/host/r   z/host/bin/ocz/root/.kube/configT)	need_rootz% --kubeconfig /host/root/.kube/configz whoami)use_containerr   r   r   r6   zopenshift.kubeconfig=z%API collections will be performed on z<
Note: API collections may extend runtime by 10s of minutes
)enable_pluginsrx   r$   r   api_collect_enabled
startswithr   containerizedfile_existsr   r   sos_env_varsr   r   sosloginform   )r%   rb   master_kube
_optconfig_kubeconfigr   can_ocr{   s           r'   set_primary_optionsocp.set_primary_optionsC  s   "";/z**  u-##   u-' 
 6J*"7"7"@"@%
2
$3KGyy&&( ##$8D#IFFG%%	&948II4K4K 15 & 6F h1$$$T40+/(37::!!-0$$T40+/(!!+.. -MM((/}= $$T40+/(''>t||n M# #   %  % (r*   c                 (    U R                  US5        g )NF)r   r   s     r'   set_node_optionsocp.set_node_options  s    T5)r*   )r   r   ru   rA   rD   r   )__name__
__module____qualname____firstlineno____doc__cluster_namepackagesr   r   rD   rA   r   option_listpropertyr(   r.   r3   r9   rH   rE   rR   rf   r@   r|   r   r   r   r   r   __static_attributes____classcell__)r<   s   @r'   r   r      s    %N 5L;HEGGK  2&	#:)6&!F<!>A
92;&z* *r*   r   )r   shlexr   sos.collector.clustersr   sos.utilitiesr   r    r*   r'   <module>r      s!    
  * 'p*' p*r*   