
    VhFm                         d Z dZdZ	 ddlZddlmZ ddlmZ ddlm	Z	 ddl
mZ dd	lmZ dd
lmZ d Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zedk(  r e        yy# e$ r Y ]w xY w)a  
---
module: eks_nodegroup
version_added: 5.3.0
short_description: Manage EKS Nodegroup module
description:
  - Manage EKS Nodegroup.
author:
  - Tiago Jarra (@tjarra)
options:
  name:
    description: Name of EKS Nodegroup.
    required: True
    type: str
  cluster_name:
    description: Name of EKS Cluster.
    required: True
    type: str
  node_role:
    description: ARN of IAM role used by the EKS cluster Nodegroup.
    type: str
  subnets:
    description: list of subnet IDs for the Kubernetes cluster.
    type: list
    elements: str
  scaling_config:
    description: The scaling configuration details for the Auto Scaling group that is created for your node group.
    type: dict
    default:
      min_size: 1
      max_size: 2
      desired_size: 1
    suboptions:
      min_size:
        description: The minimum number of nodes that the managed node group can scale in to.
        type: int
      max_size:
        description: The maximum number of nodes that the managed node group can scale out to.
        type: int
      desired_size:
        description: The current number of nodes that the managed node group should maintain.
        type: int
  disk_size:
    description:
      - Size of disk in nodegroup nodes.
        If you specify I(launch_template), then don't specify I(disk_size), or the node group deployment will fail.
    type: int
  instance_types:
    description:
      - Specify the instance types for a node group.
        If you specify I(launch_template), then don't specify I(instance_types), or the node group deployment will fail.
    type: list
    elements: str
  ami_type:
    description: The AMI type for your node group.
    type: str
    choices:
      - AL2_x86_64
      - AL2_x86_64_GPU
      - AL2_ARM_64
      - CUSTOM
      - BOTTLEROCKET_ARM_64
      - BOTTLEROCKET_x86_64
  remote_access:
    description:
      - The remote access (SSH) configuration to use with your node group.
        If you specify I(launch_template), then don't specify I(remote_access), or the node group deployment will fail.
    type: dict
    suboptions:
      ec2_ssh_key:
        description: The Amazon EC2 SSH key that provides access for SSH communication with the nodes in the managed node group.
        type: str
      source_sg:
        description: The security groups that are allowed SSH access (port 22) to the nodes.
        type: list
        elements: str
  update_config:
    description: The node group update configuration.
    type: dict
    default:
      max_unavailable: 1
    suboptions:
      max_unavailable:
        description: The maximum number of nodes unavailable at once during a version update.
        type: int
      max_unavailable_percentage:
        description: The maximum percentage of nodes unavailable during a version update.
        type: int
  labels:
    description: The Kubernetes labels to be applied to the nodes in the node group when they are created.
    type: dict
    default: {}
  taints:
    description: The Kubernetes taints to be applied to the nodes in the node group.
    type: list
    elements: dict
    default: []
    suboptions:
      key:
        description: The key of the taint.
        type: str
      value:
        description: The value of the taint.
        type: str
      effect:
        description: The effect of the taint.
        type: str
        choices:
          - NO_SCHEDULE
          - NO_EXECUTE
          - PREFER_NO_SCHEDULE
  launch_template:
    description:
      - An object representing a node group's launch template specification.
      - If specified, then do not specify I(instanceTypes), I(diskSize), or I(remoteAccess).
    type: dict
    suboptions:
      name:
        description: The name of the launch template.
        type: str
      version:
        description:
          - The version of the launch template to use.
          - If no version is specified, then the template's default version is used.
        type: str
      id:
        description: The ID of the launch template.
        type: str
  capacity_type:
    description: The capacity type for your node group.
    default: ON_DEMAND
    type: str
    choices:
      - ON_DEMAND
      - SPOT
  release_version:
    description: The AMI version of the Amazon EKS optimized AMI to use with your node group.
    type: str
  state:
    description: Create or delete the Nodegroup.
    choices:
      - absent
      - present
    default: present
    type: str
  tags:
    description: A dictionary of resource tags.
    type: dict
    aliases: ['resource_tags']
  purge_tags:
    description:
      - Purge existing tags that are not found in the nodegroup.
    type: bool
    default: true
  wait:
    description: Specifies whether the module waits until the profile is created or deleted before moving on.
    type: bool
    default: false
  wait_timeout:
    description: The duration in seconds to wait for the nodegroup to become active. Defaults to C(1200) seconds.
    default: 1200
    type: int
extends_documentation_fragment:
  - amazon.aws.common.modules
  - amazon.aws.region.modules
a  
# Note: These examples do not set authentication details, see the AWS Guide for details.

- name: create nodegroup
  community.aws.eks_nodegroup:
    name: test_nodegroup
    state: present
    cluster_name: test_cluster
    node_role: arn:aws:eks:us-east-1:1231231123:role/asdf
    subnets:
      - subnet-qwerty123
      - subnet-asdfg456
    scaling_config:
      min_size: 1
      max_size: 2
      desired_size: 1
    disk_size: 20
    instance_types: 't3.micro'
    ami_type: 'AL2_x86_64'
    labels:
      'teste': 'test'
    taints:
      - key: 'test'
        value: 'test'
        effect: 'NO_SCHEDULE'
    capacity_type: 'ON_DEMAND'

- name: Remove an EKS Nodegrop
  community.aws.eks_nodegroup:
    name: test_nodegroup
    cluster_name: test_cluster
    wait: true
    state: absent
a  
nodegroup_name:
  description: The name associated with an Amazon EKS managed node group.
  returned: when state is present
  type: str
  sample: test_cluster
nodegroup_arn:
  description: The Amazon Resource Name (ARN) associated with the managed node group.
  returned: when state is present
  type: str
  sample: arn:aws:eks:us-east-1:1231231123:safd
cluster_name:
  description: Name of EKS Cluster
  returned: when state is present
  type: str
  sample: test_cluster
version:
  description: The Kubernetes version of the managed node group.
  returned: when state is present
  type: str
  sample: need_validate
release_version:
  description: This is the version of the Amazon EKS optimized AMI that the node group was deployed with.
  returned: when state is present
  type: str
  sample: need_validate
created_at:
  description: Nodegroup creation date and time.
  returned: when state is present
  type: str
  sample: '2022-01-18T20:00:00.111000+00:00'
modified_at:
  description: Nodegroup modified date and time.
  returned: when state is present
  type: str
  sample: '2022-01-18T20:00:00.111000+00:00'
status:
  description: status of the EKS Nodegroup.
  returned: when state is present
  type: str
  sample:
  - CREATING
  - ACTIVE
capacity_type:
  description: The capacity type of your managed node group.
  returned: when state is present
  type: str
  sample: need_validate
scaling_config:
  description: The scaling configuration details for the Auto Scaling group that is associated with your node group.
  returned: when state is present
  type: dict
  sample: need_validate
instance_types:
  description: This is the instance type that is associated with the node group.
  returned: when state is present
  type: list
  sample: need_validate
subnets:
  description: List of subnets used in Fargate Profile.
  returned: when state is present
  type: list
  sample:
  - subnet-qwerty123
  - subnet-asdfg456
remote_access:
  description: This is the remote access configuration that is associated with the node group.
  returned: when state is present
  type: dict
  sample: need_validate
ami_type:
  description: This is the AMI type that was specified in the node group configuration.
  returned: when state is present
  type: str
  sample: need_validate
node_role:
  description: ARN of the IAM Role used by Nodegroup.
  returned: when state is present
  type: str
  sample: arn:aws:eks:us-east-1:1231231123:role/asdf
labels:
  description: The Kubernetes labels applied to the nodes in the node group.
  returned: when state is present
  type: dict
  sample: need_validate
taints:
  description: The Kubernetes taints to be applied to the nodes in the node group when they are created.
  returned: when state is present
  type: list
  sample: need_validate
resources:
  description: The resources associated with the node group.
  returned: when state is present
  type: complex
  contains:
    autoScalingGroups:
      description: The Auto Scaling groups associated with the node group.
      returned: when state is present
      type: list
      elements: dict
    remoteAccessSecurityGroup:
      description: The remote access security group associated with the node group.
      returned: when state is present
      type: str
diskSize:
  description: This is the disk size in the node group configuration.
  returned:  when state is present
  type: int
  sample: 20
health:
  description: The health status of the node group.
  returned: when state is present
  type: dict
  sample: need_validate
update_config:
  description: The node group update configuration.
  returned: when state is present
  type: dict
  contains:
    maxUnavailable:
      description: The maximum number of nodes unavailable at once during a version update.
      type: int
    maxUnavailablePercentage:
      description: The maximum percentage of nodes unavailable during a version update.
      type: int
launch_template:
  description: If a launch template was used to create the node group, then this is the launch template that was used.
  returned: when state is present
  type: dict
  sample: need_validate
tags:
  description: Nodegroup tags.
  returned: when state is present
  type: dict
  sample:
    foo: bar
    N)camel_dict_to_snake_dict)snake_dict_to_camel_dict)is_boto3_error_code)compare_aws_tags)
get_waiter)AnsibleCommunityAWSModulec                 *   d}|j                   j                  d      }|y	 | j                  |d         d   }t        |||j                   j                  d            \  }}r&|j                  sd
}	 | j                  d
|d   |       r(|j                  sd
}	 | j                  d
|d   |       |S |S # t        j
                  j                  t        j
                  j                  f$ r:}|j                  |d|j                   j                  d       d	       Y d }~d }~ww xY w# t        j
                  j                  t        j
                  j                  f$ r;}|j                  |d|j                   j                  d       d	       Y d }~d }~ww xY w# t        j
                  j                  t        j
                  j                  f$ r;}|j                  |d|j                   j                  d       d	       Y d }~|S d }~ww xY w)NFtagsnodegroupArn)resourceArn
purge_tagsz-Unable to list or compare tags for Nodegroup name.msgT)	aws_retryResourceArntagKeysz!Unable to set tags for Nodegroup )r   r   r
   )paramsgetlist_tags_for_resourcer   botocore
exceptionsClientErrorBotoCoreErrorfail_json_aws
check_modeuntag_resourcetag_resource)	clientmodule	nodegroupchangeddesired_tagsexisting_tagstags_to_addtags_to_removees	            o/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/aws/plugins/modules/eks_nodegroup.pyvalidate_tagsr*   j  s   G==$$V,Lr55)NB[5\]cd&6}lTZTaTaTeTefrTs&t#^   Gn%%)NB[es%t   Gn##d	.@Y`k#l N7N# ++X-@-@-N-NO rQ&STZTaTaTeTeflTmSnno$pqqr ''33X5H5H5V5VW n$$Q.OPVP]P]PaPabhPiOjjk,l$mmn ''33X5H5H5V5VW n$$Q.OPVP]P]PaPabhPiOjjk,l$mmNnsH   AB7 4D+ F  77D(.0D##D(+7F"0FF 7H0HHc                     g }g }| D ]  }||vs|j                  |        |D ]  }|| vs|j                  |        ||fS N)append)nodegroup_taintsparam_taintstaints_to_unsettaints_to_add_or_updatetaints        r)   compare_taintsr3     sg    O ! *$""5)*  2((#**512 #O33    c                    d}t               }|d   |d<   |d   |d<   g |d<   d|vrg |d<   t        |d   |      \  }}|r||d   d<   |r||d   d<   |d   r#|j                  sd}	  | j                  di | |S |S # t        j
                  j                  t        j
                  j                  f$ r%}|j                  |d|d    d	
       Y d }~|S d }~ww xY w)NFclusterNamenodegroupNametaintsaddOrUpdateTaintsremoveTaintsTz#Unable to set taints for Nodegroup r   r    )	dictr3   r   update_nodegroup_configr   r   r   r   r   )	r    r!   r"   r/   r#   r   r1   r0   r(   s	            r)   validate_taintsr>     s   GVF%m4F='8F?F8y  	(/=i>QS_/`,_0Gx,-+:x(h  Gn...88 N7N ''33X5H5H5V5VW n$$Q.QRXYhRiQjjk,l$mmNns   %A; ;7C2CCc                     g }i }| j                         D ]  }||vs|j                  |        |j                         D ]  \  }}|| j                         vs|||<    ||fS r,   )keysr-   items)nodegroup_labelsparam_labelslabels_to_unsetlabels_to_add_or_updatelabelkeyvalues          r)   compare_labelsrI     s    O !&&( *$""5)* #((* 1
U&++--+0#C(1 #O33r4   c                    d}t               }|d   |d<   |d   |d<   i |d<   t        |d   |      \  }}|r||d   d<   |r||d   d<   |d   r#|j                  sd}	  | j                  di | |S |S # t        j
                  j                  t        j
                  j                  f$ r%}|j                  |d|d    d	
       Y d }~|S d }~ww xY w)NFr6   r7   labelsaddOrUpdateLabelsremoveLabelsTz#Unable to set labels for Nodegroup r   r   r;   )	r<   rI   r   r=   r   r   r   r   r   )	r    r!   r"   rC   r#   r   rE   rD   r(   s	            r)   validate_labelsrN     s   GVF%m4F='8F?F8/=i>QS_/`,_0Gx,-+:x(h  Gn...88 N7N ''33X5H5H5V5VW n$$Q.QRXYhRiQjjk,l$mmNns   A2 27C)C		Cc                     dD ].  }||v s||v s||   ||   k7  s| j                  d| d       0 d|vrd|v r| j                  d       |d   |d   k7  ry|d	   |d	   k7  ryy
)N)nodeRolesubnetsdiskSizeinstanceTypesamiTypesremoteAccesscapacityTypezCannot modify parameter r   r   launchTemplatez-Cannot add Launch Template in this Nodegroup.updateConfigTscalingConfigF	fail_json)r!   r   r"   params       r)   compare_paramsr]     s    q JYUf_6%=0  '?wa%H IJ 		)0@F0JLM F>$::!VO%<<r4   c                     d|vr| j                  d       y	dD ]2  }||d   v s|d   |   |d   |   k7  s| j                  d| d       4 d|d   v r|d   d   |d   d   k7  ryy	)
NrW   z1Cannot exclude Launch Template in this Nodegroup.r   )r   idzCannot modify Launch Template r   versionTFrZ   )r!   r   r"   rG   s       r)   compare_params_launch_templatera     s    v%PQ  " 	NCv.//'(-;K1LS1QQ  'EcU!%L M		N
  011#$Y/9=M3Ny3YYr4   c                    d}t               }|j                  d   |d<   |j                  d   |d<   |j                  d   |d<   |j                  d   |d<   |j                  d	   xs i |d	<   |j                  d
   |j                  d
   |d<   |j                  d   |j                  d   |d<   |j                  d   |j                  d   |d<   |j                  d   t               |d<   |j                  d   d   |j                  d   d   |d   d<   |j                  d   d   |j                  d   d   |d   d<   |j                  d   d   |j                  d   d   |d   d<   |j                  d   |j                  d   |d<   |j                  d   at               |d<   |j                  d   d   |j                  d   d   |d   d<   |j                  d   d   |j                  d   d   |d   d<   |j                  d    |j                  d   j                         |d<   |j                  d   |j                  d   |d<   |j                  d   |j                  d   |d<   |j                  d    at               |d!<   |j                  d    d"   |j                  d    d"   |d!   d#<   |j                  d    d$   |j                  d    d$   |d!   d%<   |j                  d&   t        |j                  d&         |d'<   |j                  j	                  d(      }t        | ||d   |d         }|r1t               }|d   |d<   |d   |d<   d|v r9t        |||      r,|d   |d<   |j                  s	  | j                  d1i | |d+z  }t        |||      rI	 d|v r|j                  d       |d'   |d'<   |d!   |d!<   |j                  s | j                   d1i | |d+z  }|t#        | ||      z  }|t%        | |||d         z  }d|v r|t'        | |||d         z  }|rt)        | |d,|d   |d          t        | ||d   |d         } |j*                  d1d-|it-        |       |j                  r|j+                  d+.       	  | j.                  d1i |}|r)t)        | |d,|d   |d          t        | ||d   |d         } |j*                  d1d-d+it-        |       y # t        j                  j                  t        j                  j                  f$ r}|j                  |d)*       Y d }~d }~ww xY w# t        j                  j                  t        j                  j                  f$ r}|j                  |d)*       Y d }~d }~ww xY w# t        j                  j                  t        j                  j                  f$ r%}|j                  |d/|d    d0*       Y d }~Pd }~ww xY w)2NFr   r7   cluster_namer6   	node_rolerP   rQ   r
   ami_typeamiType	disk_sizerR   instance_typesrS   launch_templaterW   r_   r`   release_versionreleaseVersionremote_accessrU   ec2_ssh_key	ec2SshKey	source_sgsourceSecurityGroupscapacity_typerV   rK   r8   update_configrX   max_unavailablemaxUnavailablemax_unavailable_percentagemaxUnavailablePercentagescaling_configrY   waitzCouldn't update nodegroup.r   Tnodegroup_activer#   )r#   zCouldn't create Nodegroup r   r;   )r<   r   upperr   r   get_nodegroupra   r   update_nodegroup_versionr   r   r   r   r   r]   popr=   r*   rN   r>   
wait_until	exit_jsonr   create_nodegroup)r    r!   r#   r   rx   r"   update_paramsr(   s           r)   create_or_update_nodegroupsr     s   GVF$mmF3F?"MM.9F={3F:i0F9]]6*0bF6N}}Z ,"MM*5y}}[!-#]];7z}}%&2"(--0@"A}}&'3#'6 ==*+D1=-3]];L-Md-SF#$T*==*+I6B28--@Q2RS\2]F#$Y/==*+F3?/5}}=N/OPV/WF#$V,}}&'3#)==1B#C }}_%1!%~==)-8D28--2PQ^2_F>";/==)+6B=C]]?=[\g=hF>"#9:}}_%1!'!?!E!E!G~}}X*!==2x}}X*!==2x}}_%1!%~==)*;<H7=}}_7UVg7hF>"#34==)*FGSAGA_,BF>"#=> }}%&2":6==IY;Z"[==V$Dfff_.EvmG\]I'-m'<m$)/)@o&y(-ffiH289I2J./((R777H-H 4&&)4J#}4!%%&67171Ho.06~0Fn-((2F22C]C4
 	=;;?669fX>NOOy vvy&BRSSGvv'96/;RTZ[hTij!&&&2I6R_K`a	PP,DY,OP&]+F++5f5	 66#5vo7NPVWdPef!&&&2I6R_K`a	FITI%=i%HI[ %//==x?R?R?^?^_ R,,Q4P,QQR  ''55x7J7J7V7VW J$$Q,H$IIJ, --x/B/B/N/NO ]Q&@AX@YYZ$[\\]sI   6S! AT9 V !7T6T11T697V0V		V7W-W((W-c                    |j                   j                  d      }|j                   d   }t        | |||      }|j                   j                  d      }|s|j                  dd       |d   dk(  r7|r"t	        | |d	||       |j                  dd
       |j                  dd       |j
                  r|j                  dd       	 | j                  ||       |r"t	        | |d	||       |j                  dd
       |j                  dd       y # t        j                  j                  t        j                  j                  f$ r!}|j                  |d| d       Y d }~d }~ww xY w)Nr   rc   rx   Fz!Nodegroup '{name}' does not existr#   r   statusDELETINGnodegroup_deletedz$Nodegroup '{name}' deletion completez,Nodegroup '{name}' already in DELETING stateTz9Nodegroup '{name}' deletion would be started (check mode)r6   r7   zCouldn't delete Nodegroup 'z'.r   z#Nodegroup '{name}' deletion started)r   r   r{   r   r~   r   delete_nodegroupr   r   r   r   r   )r    r!   r   r6   existingrx   r(   s          r)   delete_nodegroupsr   ]  sm   ==V$D--/KVVT;?H==V$D,OPZ'vv':D+NU0VW,Z[+fgLKtL 66#6kJ+QR
T'LM --x/B/B/N/NO LQ&A$r$JKKLs   D 7E+
E&&E+c                    	 | j                  ||      d   S # t        d      $ r Y y t        j                  j                  t        j                  j
                  f$ r!}|j                  |d| d       Y d }~y d }~ww xY w)Nr   r"   ResourceNotFoundExceptionzCouldn't get Nodegroup r   r   )describe_nodegroupr   r   r   r   r   r   )r    r!   nodegroup_namerc   r(   s        r)   r{   r{   {  s    Q((\Q_(`almm:; ))'' Q 	Q&=n=MQ$OPP	Qs    A?6A?A::A?c                 D   |j                   j                  d      }t        | |      }dt        ||j                  j
                  z        z   }	 |j                  ||d|i       y # t        j                  j                  $ r}|j                  |d       Y d }~y d }~ww xY w)Nwait_timeout   MaxAttempts)r6   r7   WaiterConfigzAn error occurred waitingr   )r   r   r   intconfigdelayrx   r   r   WaiterErrorr   )	r    r!   waiter_namer   rc   r   waiterattemptsr(   s	            r)   r~   r~     s    ==$$^4L,F3|fmm&9&99::HANZgiqYrs** AQ$?@@As   A% %BBBc                     t        dEi dt        dd      dt        dd      dt               dt        dd	      d
t        dddddt        t        d      t        d      t        d                  dt        d      dt        dd	      dt        g d      dt        dt        t        d      t        dd	                  dt        dddit        t        d      t        d                  dt        di       d t        ddg t        t        dd!"      t        d      t        dg d#$      %      &      d't        dt        t        d      t        d      t        d      (            d)t        d*d+gd*,      d-t               d.t        dd/g0      d1t        d2d      d3t        d4d5gd5,      d6t        d!d27      d8t        d9d7      } t        | d3d5ddgggg d:d;      }|j                  d'   \|j                  d   d<|j                  d<   |j                  d   d=|j                  d<   |j                  d   Hd>g|j                  d<   n7|j                  d'   d?   %|j                  d'   d   |j                  d!d@A       	 |j	                  dB      }|j                  j                  d3      d5k(  rt        |       y t        |       y # t
        j                  j                  t
        j                  j                  f$ r}|j                  |dCD       Y d }~d }~ww xY w)FNr   strT)typerequiredrc   rd   rQ   list)r   elementsrw   r<   r      )min_sizemax_sizedesired_sizer   )r   )r   defaultoptionsrg   rh   re   )
AL2_x86_64AL2_x86_64_GPU
AL2_ARM_64CUSTOMBOTTLEROCKET_ARM_64BOTTLEROCKET_x86_64)choicesrl   )no_log)rm   ro   )r   r   rr   rs   )rs   ru   rK   )r   r   r8   F)r   r   )NO_SCHEDULE
NO_EXECUTEPREFER_NO_SCHEDULE)r   r   )rG   rH   effect)r   r   r   r   ri   )r   r`   r_   rq   	ON_DEMANDSPOT)r   r   rj   r
   resource_tags)r   aliasesr   boolstateabsentpresentrx   )r   r   r   i  ))ri   rh   )ri   rg   )ri   rl   )ri   re   )argument_specrequired_ifmutually_exclusivesupports_check_mode   r   z	t3.mediumr_   zATo use launch_template, it is necessary to inform the id or name.r   ekszCouldn't connect to AWS.r   r;   )r<   AnsibleAWSModuler   r   r    r   r   r   r   r   r   r   r   )r   r!   r    r(   s       r)   mainr     s    Fut,Fut4F &F &51	F
 !"1E5)5)!u-
F E"F %8F  	
!F4  -FU;
5FB &* $% 0+/U+;
CFR ,SFT   &0cd	
UFn u%%(U#
oF~ K#8+NF@ AFB v'89CFD VT2EFF Hi0)DGFH %f-IFJ $U3KFMP #y;	*BCD
 !
F }}&'/==%-)+FMM+&==$,(4FMM*%==)*2/:mFMM*+MM+,T2:O`AabhAiAqU0st@u% }}!Y.#FF3&&) --x/B/B/N/NO @Q$>??@s   &K/ /7M&L>>M__main__)DOCUMENTATIONEXAMPLESRETURNr   ImportError0ansible.module_utils.common.dict_transformationsr   r   <ansible_collections.amazon.aws.plugins.module_utils.botocorer   ;ansible_collections.amazon.aws.plugins.module_utils.taggingr   ;ansible_collections.amazon.aws.plugins.module_utils.waitersr   >ansible_collections.community.aws.plugins.module_utils.modulesr   r   r*   r3   r>   rI   rN   r]   ra   r   r   r{   r~   r   __name__r;   r4   r)   <module>r      s   eN!FH
T	 V U \ X R x<
42
4. jJZN<	QAg*T zF }  		s   A" "A*)A*