%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /usr/lib/python2.7/site-packages/salt/modules/
Upload File :
Create Path :
Current File : //usr/lib/python2.7/site-packages/salt/modules/k8s.pyc

�
���^c@@sdZddlmZmZmZddlZddlZddlZddl	Z	ddl
mZddlm
ZddlZddljjZddlZdZeje�Zd�Zdd�Zd	�Zd
�Zd�Zd�Zd
�Z d�Z!d�Z"d�Z#d�Z$d�Z%ddd�Z&ddd�Z'ddd�Z(ddd�Z)dd�Z*d�Z+dd�Z,ddd�Z-d�Z.d�Z/d�Z0d�Z1d �Z2d!�Z3dde4e4d"�Z5d#�Z6de7d$d%�Z8de4e4d$d&�Z9de7d'�Z:dS((u
Salt module to manage Kubernetes cluster

.. versionadded:: 2016.3.0

Roadmap:

* Add creation of K8S objects (pod, rc, service, ...)
* Add replace of K8S objects (pod, rc, service, ...)
* Add deletion of K8S objects (pod, rc, service, ...)
* Add rolling update
* Add (auto)scalling

i(tabsolute_importtunicode_literalstprint_functionN(tsix(turlparseuk8scC@stS(u*Load load if python-requests is installed.(t__virtualname__(((s4/usr/lib/python2.7/site-packages/salt/modules/k8s.pyt__virtual__&scC@s#d}|d	k	r|Sdtjkr:tjjd�}n�tdd�r]tdd�}n�tjj|�s�tddd�r	tdd|�}tjdtj	�}t
jjj
|��E}x#|j�D]}|j|�}q�W|r|jd�}nWd	QXnd
}tjd|�|S(u^Try to guees the kubemaster url from environ,
    then from `/etc/kubernetes/config` file
    u/etc/kubernetes/configuKUBERNETES_MASTERu
config.getu
k8s:masteru
k8s:configuu!KUBE_MASTER=['"]--master=(.*)['"]iNuhttp://127.0.0.1:8080u$Discoverd k8s API server address: %s(tNonetostenvirontgett__salt__tpathtexiststretcompilet	MULTILINEtsalttutilstfilestfopent	readlinestmatchtgrouptlogtdebug(t
apiserver_urltdefault_configtconfigt
kubeapi_regextfh_k8stlinet
match_line((s4/usr/lib/python2.7/site-packages/salt/modules/k8s.pyt_guess_apiserver+s&%	c	C@s�idd6}tjd||�tj|ddd|dtjjj|��}|jd�rc|Stjjj	|jd	��Sd
S(u. create any object in kubernetes based on URL uapplication/jsonuContent-Typeuurl is: %s, data is: %stmethoduPOSTtheader_dicttdatauerrorubodyN(
RttracethttptqueryRRtjsontdumpsR
tloads(turlR$theaderstret((s4/usr/lib/python2.7/site-packages/salt/modules/k8s.pyt_kpostFs
c	C@spidd6}tj|ddd|dtjjj|��}|jd�rP|Stjjj|jd��Sd	S(
u+ put any object in kubernetes based on URL uapplication/jsonuContent-TypeR"uPUTR#R$uerrorubodyN(R&R'RRR(R)R
R*(R+R$R,R-((s4/usr/lib/python2.7/site-packages/salt/modules/k8s.pyt_kputXs
c	C@s�idd6}tj|ddd|dtjjj|��}|jd�ritjd|jd��|Stjjj	|jd	��Sd
S(u- patch any object in kubernetes based on URL uapplication/json-patch+jsonuContent-TypeR"uPATCHR#R$uerroruGot an error: %subodyN(
R&R'RRR(R)R
RterrorR*(R+R$R,R-((s4/usr/lib/python2.7/site-packages/salt/modules/k8s.pyt_kpatchis
cC@s�t|t�r.|jdi�jdd�gSt|ttf�r�g}x3|D]+}|j|jdi�jdd��qPW|SdSdS(u4Get name or names out of json result from API serverumetadataunameuuUnknown typeN(t
isinstancetdictR
tlistttupletappend(tobjtnamesti((s4/usr/lib/python2.7/site-packages/salt/modules/k8s.pyt_knameys
)cC@sJtjd�}|j|�r2tjd|�tStjd|�tSdS(u� Check that name is DNS subdomain: One or more lowercase rfc1035/rfc1123
    labels separated by '.' with a maximum length of 253 characters u^[a-z0-9\.-]{1,253}$uName: %s is valid DNS subdomainu#Name: %s is not valid DNS subdomainN(RRRRRtTruetFalse(tnamet
dns_subdomain((s4/usr/lib/python2.7/site-packages/salt/modules/k8s.pyt_is_dns_subdomain�scC@s*tjd�}|j|�r"tStSdS(u Check that name is IANA service: An alphanumeric (a-z, and 0-9) string,
    with a maximum length of 15 characters, with the '-' character allowed
    anywhere except the first or the last character or adjacent to another '-'
    character, it must contain at least a (a-z) character u^[a-z0-9]{1,15}$N(RRRR;R<(R=t	port_name((s4/usr/lib/python2.7/site-packages/salt/modules/k8s.pyt
_is_port_name�scC@s*tjd�}|j|�r"tStSdS(u Check that name is DNS label: An alphanumeric (a-z, and 0-9) string,
    with a maximum length of 63 characters, with the '-' character allowed
    anywhere except the first or last character, suitable for use as a hostname
    or segment in a domain name u^[a-z0-9][a-z0-9\.-]{1,62}$N(RRRR;R<(R=t	dns_label((s4/usr/lib/python2.7/site-packages/salt/modules/k8s.pyt
_is_dns_label�scC@s|dkrtdd�S|S(u.Try to guess kube node ID using salt minion IDu
grains.getuidN(RR(tnode((s4/usr/lib/python2.7/site-packages/salt/modules/k8s.pyt_guess_node_id�scC@s�dj||�}tj|�}d|krNtjjj|jd��}n)|jdd�dkrsdj|�S|S|jdi�jdi�S(	u Get all labels from a kube node.u{0}/api/v1/nodes/{1}ubodyustatusii�uNode {0} doesn't existumetadataulabels(tformatR&R'RRR(R*R
(RDRR+R-((s4/usr/lib/python2.7/site-packages/salt/modules/k8s.pyt_get_labels�s!
cC@sedj||�}idd6dd6|d6g}t||�}|jd�dkrad	j|�S|S(
u Replace labels dict by a new oneu{0}/api/v1/nodes/{1}ureplaceuopu/metadata/labelsupathuvalueustatusi�uNode {0} doesn't exist(RFR1R
(RDRtlabelsR+R$R-((s4/usr/lib/python2.7/site-packages/salt/modules/k8s.pyt_set_labels�s
cC@sBt|�}t|�}|dkr(tSt||�}i|d6S(u�
    .. versionadded:: 2016.3.0

    Get labels from the current node

    CLI Example:

    .. code-block:: bash

        salt '*' k8s.get_labels
        salt '*' k8s.get_labels kube-node.cluster.local http://kube-master.cluster.local

    ulabelsN(RER!RR<RG(RDRR-((s4/usr/lib/python2.7/site-packages/salt/modules/k8s.pyt
get_labels�scC@s�i|d6td6dd6id6}t|�}t|�}|dkrJtSt||�}||kr�i||6|d<t|�||<t|||�}|jd�dkr�t	j
d�i|d<d	j|�|d<q�d
j|�|d<n�|j|�t|�kr�it|�|6|d<t|�||<t|||�}|jd�dkr�t	j
d�i|d<dj|�|d<q�dj|�|d<nd
j|�|d<|S(u
    .. versionadded:: 2016.3.0

    Set label to the current node

    CLI Example:

    .. code-block:: bash

        salt '*' k8s.label_present hw/disktype ssd

        salt '*' k8s.label_present hw/disktype ssd kube-node.cluster.local http://kube-master.cluster.local

    unameuresultuucommentuchangesustatusi�uGot 409, will try lateru(Could not create label {0}, please retryuLabel {0} createdu(Could not update label {0}, please retryuLabel {0} updateduLabel {0} already setN(R;RER!RR<RGtstrRIR
RRRF(R=tvalueRDRR-RHtres((s4/usr/lib/python2.7/site-packages/salt/modules/k8s.pyt
label_present�s4"



c	C@s4i|d6td6dd6id6}t|�}t|�}|d
krJtSt||�}tg|j�D]$\}}||kri||f^qi�}||kr�dj|�|d<nxt	|||�}|j
d�dkrtjd	�i|d<d
j|�|d<n$i|d6|d<dj|�|d<|S(u

    .. versionadded:: 2016.3.0

    Delete label to the current node

    CLI Example:

    .. code-block:: bash

        salt '*' k8s.label_absent hw/disktype
        salt '*' k8s.label_absent hw/disktype kube-node.cluster.local http://kube-master.cluster.local

    unameuresultuucommentuchangesuLabel {0} already absentustatusi�uGot 409, will try lateru(Could not delete label {0}, please retryudeleteduLabel {0} absentN(
R;RER!RR<RGR3titemsRFRIR
RR(	R=RDRR-t
old_labelstkeyRLRHRM((s4/usr/lib/python2.7/site-packages/salt/modules/k8s.pytlabel_absent's$"!

c
C@sJ|jd�d}i|d6td6dd6id6}t|�}t|�}|dkr]tSt||�}tg|j�D]'\}}|j	|�s|||f^q|�}||kr�dj
|�|d<nxt|||�}	|	jd�d	kr"t
jd
�i|d<dj
|�|d<n$i|d6|d<d
j
|�|d<|S(u
    .. versionadded:: 2016.3.0

    Delete label folder to the current node

    CLI Example:

    .. code-block:: bash

        salt '*' k8s.label_folder_absent hw
        salt '*' k8s.label_folder_absent hw/ kube-node.cluster.local http://kube-master.cluster.local

    u/unameuresultuucommentuchangesuLabel folder {0} already absentustatusi�uGot 409, will try lateru/Could not delete label folder {0}, please retryudeleteduLabel folder {0} absentN(tstripR;RER!RR<RGR3ROt
startswithRFRIR
RR(
R=RDRtfolderR-RPRQRLRHRM((s4/usr/lib/python2.7/site-packages/salt/modules/k8s.pytlabel_folder_absentVs&"$

ucC@sTdj||�}tj|�}|jd�rLtjjj|jd��SdSdS(uEGet namespace is namespace is defined otherwise return all namespacesu{0}/api/v1/namespaces/{1}ubodyN(	RFR&R'R
RRR(R*R(RR=R+R-((s4/usr/lib/python2.7/site-packages/salt/modules/k8s.pyt_get_namespaces�s
cC@sddj|�}idd6dd6i|d6d6}tjd|�t||�}tjd	|�|S(
u- create namespace on the defined k8s cluster u{0}/api/v1/namespacesu	Namespaceukinduv1u
apiVersionunameumetadataunamespace creation requests: %su
result is: %s(RFRR%R.(t	namespaceRR+R$R-((s4/usr/lib/python2.7/site-packages/salt/modules/k8s.pyt_create_namespace�scC@s�i|d6td6dd6id6}t|�}|dkr>tSt||�szt||�||d<dj|�|d<ndj|�|d<|S(	u�
    .. versionadded:: 2016.3.0

    Create kubernetes namespace from the name, similar to the functionality added to kubectl since v.1.2.0:
    .. code-block:: bash

        kubectl create namespaces namespace-name

    CLI Example:

    .. code-block:: bash

        salt '*' k8s.create_namespace namespace_name

        salt '*' k8s.create_namespace namespace_name http://kube-master.cluster.local

    unameuresultuucommentuchangesuNamespace {0} createduNamespace {0} already presentN(R;R!RR<RWRYRF(R=RR-((s4/usr/lib/python2.7/site-packages/salt/modules/k8s.pytcreate_namespace�s"

cC@s/t|�}|dkrtSt||�}|S(u`
    .. versionadded:: 2016.3.0

    Get one or all kubernetes namespaces.

    If namespace parameter is omitted, all namespaces will be returned back to user, similar to following kubectl example:

    .. code-block:: bash

        kubectl get namespaces -o json

    In case namespace is set by user, the output will be similar to the one from kubectl:

    .. code-block:: bash

        kubectl get namespaces namespace_name -o json


    CLI Example:

    .. code-block:: bash

        salt '*' k8s.get_namespaces
        salt '*' k8s.get_namespaces namespace_name http://kube-master.cluster.local

    N(R!RR<RW(RXRR-((s4/usr/lib/python2.7/site-packages/salt/modules/k8s.pytget_namespaces�s
cC@sWdj|||�}tj|�}|jd�rOtjjj|jd��SdSdS(uGet secrets of the namespace.u%{0}/api/v1/namespaces/{1}/secrets/{2}ubodyN(	RFR&R'R
RRR(R*R(RXR=RR+R-((s4/usr/lib/python2.7/site-packages/salt/modules/k8s.pyt_get_secrets�s	cC@shdj|||�}idd6dd6|d6g}t||�}|jd�dkrdd	j|�S|S(
u!Replace secrets data by a new oneu%{0}/api/v1/namespaces/{1}/secrets/{2}ureplaceuopu/dataupathuvalueustatusi�uNode {0} doesn't exist(RFR1R
(RXR=R$RR+R-((s4/usr/lib/python2.7/site-packages/salt/modules/k8s.pyt_update_secret�s	
cC@sUdj||�}idd6dd6i|d6|d6d6|d	6}t||�}|S(
u- create namespace on the defined k8s cluster u!{0}/api/v1/namespaces/{1}/secretsuv1u
apiVersionuSecretukindunameu	namespaceumetadataudata(RFR.(RXR=R$RR+trequestR-((s4/usr/lib/python2.7/site-packages/salt/modules/k8s.pyt_create_secret
s
cC@sLtjj|�r8tjj|�r8tjd|�tStjd|�tS(NuFile: %s is valid secret fileu$File: %s does not exists or not file(	RRR
tisfileRRR;twarningR<(tfilename((s4/usr/lib/python2.7/site-packages/salt/modules/k8s.pyt_is_valid_secret_files
$cC@sNtjd|�tjjj|d�� }|j�}tj|�SWdQXdS(NuEncoding secret file: %surb(	RR%RRRRtreadtbase64t	b64encode(RbtfR$((s4/usr/lib/python2.7/site-packages/salt/modules/k8s.pyt_file_encode%scC@s�|jdg�}|r�xqt|�D]c\}}tj||�xDtj|jdi��D]'\}}tj|�||d|<q]Wq%W||d<|Sx@tj|jdi��D]#\}}tj|�|d|<q�W|SdS(Nuitemsudata(R
t	enumerateRR%Rt	iteritemsRet	b64decode(tsecretsROR9tsecrettktv((s4/usr/lib/python2.7/site-packages/salt/modules/k8s.pyt_decode_secrets,s(#
(cC@sSt|�}|dkrtS|s7t|||�}ntt|||��}|S(u�
    Get k8s namespaces

    CLI Example:

    .. code-block:: bash

        salt '*' k8s.get_secrets namespace_name
        salt '*' k8s.get_secrets namespace_name secret_name http://kube-master.cluster.local

    N(R!RR<R\Rp(RXR=RtdecodetbriefR-((s4/usr/lib/python2.7/site-packages/salt/modules/k8s.pytget_secrets;s
cC@syt|�}Wntk
r-didfSXd}tjd
|�|js]|jd	kr�tjj|j�}tjj|�}tj	d|j�t
|�rt|�r|t|�fSnX|j|krt
d||�}|s�tjd
|�dStjj|�t|�fSdS(Nuu#Invalid format for source parameterusaltuhttpuhttpsuftpuswiftus3ufileuparsed source looks like: %su"Source is a regular local file: %su
cp.cache_fileu$Source file: %s can not be retrieved(usaltuhttpuhttpsuftpuswiftus3ufile(uu(uu(t	_urlparset	TypeErrorRR%tschemeRRtabspathtbasenameRR?RcRhRRa(tsourcetsaltenvt
source_urltprotosRbtsname((s4/usr/lib/python2.7/site-packages/salt/modules/k8s.pyt_source_encodeTs&
ubasecC@s:t|�}t|||d|d|dtd|�}|S(u�
    .. versionadded:: 2016.3.0

    alias to k8s.create_secret with update=true

    CLI Example:

    .. code-block:: bash

        salt '*' k8s.update_secret namespace_name secret_name sources [apiserver_url] [force=true] [update=false] [saltenv='base']

    sources are either dictionary of {name: path, name1: path} pairs or array of strings defining paths.

    Example of paths array:

    .. code-block:: bash

    ['/full/path/filename', "file:///full/path/filename", "salt://secret/storage/file.txt", "http://user:password@securesite.com/secret-file.json"]

    Example of dictionaries:

    .. code-block:: bash

    {"nameit": '/full/path/fiename', name2: "salt://secret/storage/file.txt"}

    optional parameters accepted:

    force=[true] default value is true
    if the to False, secret will not be created in case one of the files is not
    valid kubernetes secret. e.g. capital letters in secret name or _
    in case force is set to True, wrong files will be skipped but secret will be created any way.

    saltenv=['base'] default value is base
    in case 'salt://' path is used, this parameter can change the visibility of files

    RtforcetupdateRz(R!t
create_secretR;(RXR=tsourcesRRRzR-((s4/usr/lib/python2.7/site-packages/salt/modules/k8s.pyt
update_secretos%c	C@si|d6td6dd6id6}|sHi|d6td6dd6id6St|�}t||�s�|ryt||�q�i|d6td6dd6id6Snt|||�}|r�|r�tjd||�i|d6td6d	j|�d6id6Si}	xC|D];}
tj	d
|
�t
|
t�r�tjd|
�xt
j|
�D]s\}}t||�\}
}|
|ko�dknr�|dcdj|�7<|r�qIq�|Sn||	|<qIWqt
|
t
j�rt|
|�\}
}|
|kodknr2|r+|dcdj|
�7<qq2|Sn||	|
<qqWtjd
j|	��|r�|r�|	s�|dcd7<t|d<q|jd�r�|	|jd�kr�t|||	|�}d|d<d|d<qtj	d�d|d<nt|||	|�}|S(u�
    .. versionadded:: 2016.3.0

    Create k8s secrets in the defined namespace from the list of files

    CLI Example:

    .. code-block:: bash

        salt '*' k8s.create_secret namespace_name secret_name sources

        salt '*' k8s.create_secret namespace_name secret_name sources
        http://kube-master.cluster.local

    sources are either dictionary of {name: path, name1: path} pairs or array of strings defining paths.

    Example of paths array:

    .. code-block:: bash

    ['/full/path/filename', "file:///full/path/filename", "salt://secret/storage/file.txt", "http://user:password@securesite.com/secret-file.json"]

    Example of dictionaries:

    .. code-block:: bash

    {"nameit": '/full/path/fiename', name2: "salt://secret/storage/file.txt"}

    optional parameters accepted:

    update=[false] default value is false
    if set to false, and secret is already present on the cluster - warning will be returned and no changes to the secret will be done.
    In case it is set to "true" and secret is present but data is differ - secret will be updated.

    force=[true] default value is true
    if the to False, secret will not be created in case one of the files is not
    valid kubernetes secret. e.g. capital letters in secret name or _
    in case force is set to True, wrong files will be skipped but secret will be created any way.

    saltenv=['base'] default value is base
    in case 'salt://' path is used, this parameter can change the visibility of files

    unameuresultuucommentuchangesuNo source availableuNamespace doesn't existsu"Secret %s is already present on %suSecret {0} is already presentu
source is: %susource is dictionary: %su0Source file {0} is missing or name is incorrect
usecret data is: {0}u5Could not find source files or your sources are emptyudatauUpdated secretu3Secret has not been changed on cluster, skipping itu,Has not been changed on cluster, skipping it(R;R<R!RWRYR\RtinfoRFRR2R3R%RRjR~tstring_typesR
R]R_(RXR=R�RRR�RzR-RmR$RyRnRoR}tencodedRM((s4/usr/lib/python2.7/site-packages/salt/modules/k8s.pyR��s`," #


$



cC@s�i|d6td6dd6id6}t|�}|dkr>tSt||�smi|d6td6dd6id6Sdj|||�}tj|dd	�}|jd
�r�dj||�|d<n|S(
u^
    .. versionadded:: 2016.3.0

    Delete kubernetes secret in the defined namespace. Namespace is the mandatory parameter as well as name.

    CLI Example:

    .. code-block:: bash

        salt '*' k8s.delete_secret namespace_name secret_name

        salt '*' k8s.delete_secret namespace_name secret_name http://kube-master.cluster.local

    unameuresultuucommentuchangesu5Namespace doesn't exists, can't delete anything thereu%{0}/api/v1/namespaces/{1}/secrets/{2}R"uDELETEubodyu#Removed secret {0} in {1} namespaceN(	R;R!RR<RWRFR&R'R
(RXR=RRR-R+RM((s4/usr/lib/python2.7/site-packages/salt/modules/k8s.pyt
delete_secret	s"		(;t__doc__t
__future__RRRRRtloggingtloggerRetsalt.extRtsalt.ext.six.moves.urllib.parseRRttsalt.utils.filesRtsalt.utils.httpRR&tsalt.utils.jsonRt	getLoggert__name__RRRR!R.R/R1R:R?RARCRERGRIRJRNRRRVRWRYRZR[R\R]R_RcRhRpR<RsR~R;R�R�R�(((s4/usr/lib/python2.7/site-packages/salt/modules/k8s.pyt<module>sV					
	
	
	
			
:/0	#&	
						,n

Zerion Mini Shell 1.0