
    '                         d Z ddlmZ ddlmZ ddlmZ ddlmZ g dZg dZg dZ	d	 Z
d
 Zd Zd Zd Zd Zd Zd Zd Zd Zy)a  Handles the updating of PolicyControllerPolicyControllerDeploymentConfig.

Each function updates a single value, diving to the appropriate depth, updating
and returning the updated object. Note that while client-side validation could
occur here, it is deferred to the API layer.
    )absolute_import)division)unicode_literals)
exceptions)z	cpu-limitzcpu-requestzmemory-limitzmemory-request
tolerationzreplica-countzpod-affinity)
NoSchedulePreferNoSchedule	NoExecute)	admissionauditmutationc                     | j                   j                  j                  j                  D ci c]  }|j                  |j
                   c}S c c}w )zExtracts a dictionary of deployment configuration by component name.

  Args:
    spec: A hub membership spec.

  Returns:
    A dictionary mapping component name to configuration object.
  )policycontrollerpolicyControllerHubConfigdeploymentConfigsadditionalPropertieskeyvalue)speccfgs     Ulib/googlecloudsdk/command_lib/container/fleet/policycontroller/deployment_configs.pyget_configurationsr   5   sN     &&@@RRgg
g# 
ggsyyg
  
s   Ac                 <    |	d| _         | S t        |      | _         | S )zDConfigures a replica count for the current deployment configuration.N)replicaCountint)currentr   s     r   update_replica_countr   D   s)    
]G 
. u:G	.    c                 8   | j                         }|j                  |j                  }| j                         }|j                  |j                  }||_        |j                  |j
                  d}||_        |j                  |j                  d}||_        |S )a  Configures a cpu limit for the current deployment configuration.

  Args:
    messages: the set of proto messages for this feature.
    current: the deployment configuration object being modified.
    value: The value to set the cpu limit to. If None, the limit will be
      removed. If this is the only limit, limit requirements will be removed. If
      this is the only requirement, requirements will be removed.

  Returns:
    The modified deployment configuration object.
  N)$PolicyControllerResourceRequirementscontainerResourcesPolicyControllerResourceListlimitscpumemoryrequestsmessagesr   r   requirementsresource_lists        r   update_cpu_limitr+   M   s     >>@,+--L779-$ ''M-=#7#7#?M%, \%:%:%BL+'	.r   c                 :   |j                   |j                   }n| j                         }| j                         }|j                  |j                  }||_        |j
                  |j                  d}||_        |j                  |j                  d}||_         |S )a  Configures a memory limit for the current deployment configuration.

  Args:
    messages: the set of proto messages for this feature.
    current: the deployment configuration object being modified.
    value: The value to set the memory limit to. If None, the limit will be
      removed. If this is the only limit, limit requirements will be removed. If
      this is the only requirement, requirements will be removed.

  Returns:
    The modified deployment configuration object.
  N)r!   r    r"   r#   r%   r$   r&   r'   s        r   update_mem_limitr-   j   s     +--L@@BL779-$ ''M-=#7#7#?M%, \%:%:%BL+'	.r   c                 :   |j                   |j                   }n| j                         }| j                         }|j                  |j                  }||_        |j                  |j
                  d}||_        |j                  |j                  d}||_         |S )a  Configures a cpu request for the current deployment configuration.

  Args:
    messages: the set of proto messages for this feature.
    current: the deployment configuration object being modified.
    value: The value to set the cpu request to. If None, the request will be
      removed. If this is the only request, request requirements will be
      removed. If this is the only requirement, requirements will be removed.

  Returns:
    The modified deployment configuration object.
  N)r!   r    r"   r&   r$   r%   r#   r'   s        r   update_cpu_requestr/      s     +--L@@BL779-& ))M-=#7#7#?M', \%:%:%BL+'	.r   c                 :   |j                   |j                   }n| j                         }| j                         }|j                  |j                  }||_        |j
                  |j                  d}||_        |j                  |j                  d}||_         |S )a  Configures a memory request for the current deployment configuration.

  Args:
    messages: the set of proto messages for this feature.
    current: the deployment configuration object being modified.
    value: The value to set the memory request to. If None, the request will be
      removed. If this is the only request, request requirements will be
      removed. If this is the only requirement, requirements will be removed.

  Returns:
    The modified deployment configuration object.
  N)r!   r    r"   r&   r%   r$   r#   r'   s        r   update_mem_requestr1      s     +--L@@BL779-& ))M-=#7#7#?M', \%:%:%BL+'	.r   c                     | j                  d      }t        |      dkD  r$t        j                  dj	                  |             |d   }t        |      dk(  r|d   nd }t        |      dk(  rdnd}|||fS )N=   z)Illegal value for toleration key-value={}r      ExistsEqual)splitlenr   Errorformat)	key_valuesplit_key_valuer   r   operators        r   _parse_key_valuer?      s    OOC(/A


3::9E  	# #O 4 9/!
t%_-2X(	eX	r   c                     | j                         }t        |      \  }}}||_        ||_        |||_        |||_        g }|j                  |j                  }|j                  |       ||_        |S )a  Adds a toleration to the current deployment configuration.

  Args:
    messages: the set of proto messages for this feature.
    current: the deployment configuration object being modified.
    key_value: the key-and-optional-value string specifying the toleration key
      and value.
    effect: Optional. If included, will set the effect value on the toleration.

  Returns:
    The modified deployment configuration object.
  )PolicyControllerTolerationr?   r>   r   r   effectpodTolerationsappend)	r(   r   r<   rB   r   r   r   r>   tolerationss	            r   add_tolerationrF      s     224*))4#uh **.
JJ+'((KZ &'	.r   c                     | j                   }t        |      \  fd}|D cg c]  } ||      r| c}| _         | S c c}w )a   Removes a toleration from the current deployment configuration.

  A toleration must match exactly to be removed - it is not enough to match the
  key, or even key-value. The effect must also match the toleration being
  removed.

  Args:
    current: the deployment configuration object being modified.
    key_value: the key-and-optional-value string specifying the toleration key
      and value.
    effect: Optional. If included, will set the effect value on the toleration.

  Returns:
    The modified deployment configuration object.
  c                     | j                   k(  xr1 | j                  k(  xr  | j                  k(  xr | j                  k(  S )N)r   r   r>   rB   )r   rB   r   r>   r   s    r   matchz remove_toleration.<locals>.match  sO    	3	 	*&	*  H,	* &(	r   )rC   r?   )	r   r<   rB   current_tolerationsrI   tr   r>   r   s	     `   @@@r   remove_tolerationrL      sS       ..))4#uh (;K':!%(A':K'	. Ls
   AAc                     |dk(  r'| j                   j                  j                  |_        |S ||dk(  r'| j                   j                  j                  |_        |S t        j                  dj                  |            )a  Configures the pod affinity for the current deployment configuration.

  Args:
    messages: the set of proto messages for this feature.
    current: the deployment configuration object being modified.
    value: The value to set the pod affinity to. If the value is the string
      "none" or value `None`, the pod affinity will be NO_AFFINITY.

  Returns:
    The modified deployment configuration object.
  antinonez)invalid pod affinity option {} specified.)0PolicyControllerPolicyControllerDeploymentConfigPodAffinityValueValuesEnumANTI_AFFINITYpodAffinityNO_AFFINITYr   r:   r;   )r(   r   r   s      r   update_pod_affinityrU     s     f_AA\\jj  
. }AA\\hh  
. 

3::5A r   N)__doc__
__future__r   r   r   googlecloudsdk.corer   SUPPORTED_PROPERTIESK8S_SCHEDULING_OPTIONSG8R_COMPONENTSr   r   r+   r-   r/   r1   r?   rF   rL   rU    r   r   <module>r]      sb    '  ' *  :<<<	>Br   