
    VhP                        d dl mZmZmZ eZ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mZmZ 	 d dlmZmZ  G d de      Zd Zd Zedk(  r e        yy# e$ rZ	 i xZxZZY dZ[9dZ[ww xY w# e$ r Y ?w xY w)    )absolute_importdivisionprint_functiona+  
module: openshift_route

short_description: Expose a Service as an OpenShift Route.

version_added: "0.3.0"

author: "Fabian von Feilitzsch (@fabianvf)"

description:
  - Looks up a Service and creates a new Route based on it.
  - Analogous to `oc expose` and `oc create route` for creating Routes, but does not support creating Services.
  - For creating Services from other resources, see kubernetes.core.k8s.

extends_documentation_fragment:
  - kubernetes.core.k8s_auth_options
  - kubernetes.core.k8s_wait_options
  - kubernetes.core.k8s_state_options

requirements:
  - "python >= 3.6"
  - "kubernetes >= 12.0.0"
  - "PyYAML >= 3.11"

options:
  service:
    description:
      - The name of the service to expose.
      - Required when I(state) is not absent.
    type: str
    aliases: ['svc']
  namespace:
    description:
      - The namespace of the resource being targeted.
      - The Route will be created in this namespace as well.
    required: yes
    type: str
  labels:
    description:
      - Specify the labels to apply to the created Route.
      - 'A set of key: value pairs.'
    type: dict
  annotations:
    description:
      - Specify the Route Annotations.
      - 'A set of key: value pairs.'
    type: dict
    version_added: "2.1.0"
  name:
    description:
      - The desired name of the Route to be created.
      - Defaults to the value of I(service)
    type: str
  hostname:
    description:
      - The hostname for the Route.
    type: str
  path:
    description:
      - The path for the Route
    type: str
  wildcard_policy:
    description:
      - The wildcard policy for the hostname.
      - Currently only Subdomain is supported.
      - If not provided, the default of None will be used.
    choices:
      - Subdomain
    type: str
  port:
    description:
      - Name or number of the port the Route will route traffic to.
    type: str
  tls:
    description:
      - TLS configuration for the newly created route.
      - Only used when I(termination) is set.
    type: dict
    suboptions:
      ca_certificate:
        description:
          - Path to a CA certificate file on the target host.
          - Not supported when I(termination) is set to passthrough.
        type: str
      certificate:
        description:
          - Path to a certificate file on the target host.
          - Not supported when I(termination) is set to passthrough.
        type: str
      destination_ca_certificate:
        description:
          - Path to a CA certificate file used for securing the connection.
          - Only used when I(termination) is set to reencrypt.
          - Defaults to the Service CA.
        type: str
      key:
        description:
          - Path to a key file on the target host.
          - Not supported when I(termination) is set to passthrough.
        type: str
      insecure_policy:
        description:
          - Sets the InsecureEdgeTerminationPolicy for the Route.
          - Not supported when I(termination) is set to reencrypt.
          - When I(termination) is set to passthrough, only redirect is supported.
          - If not provided, insecure traffic will be disallowed.
        type: str
        choices:
          - allow
          - redirect
          - disallow
        default: disallow
  termination:
    description:
      - The termination type of the Route.
      - If left empty no termination type will be set, and the route will be insecure.
      - When set to insecure I(tls) will be ignored.
    choices:
      - edge
      - passthrough
      - reencrypt
      - insecure
    default: insecure
    type: str
a  
- name: Create hello-world deployment
  community.okd.k8s:
    definition:
      apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: hello-kubernetes
        namespace: default
      spec:
        replicas: 3
        selector:
          matchLabels:
            app: hello-kubernetes
        template:
          metadata:
            labels:
              app: hello-kubernetes
          spec:
            containers:
              - name: hello-kubernetes
                image: paulbouwer/hello-kubernetes:1.8
                ports:
                  - containerPort: 8080

- name: Create Service for the hello-world deployment
  community.okd.k8s:
    definition:
      apiVersion: v1
      kind: Service
      metadata:
        name: hello-kubernetes
        namespace: default
      spec:
        ports:
          - port: 80
            targetPort: 8080
        selector:
          app: hello-kubernetes

- name: Expose the insecure hello-world service externally
  community.okd.openshift_route:
    service: hello-kubernetes
    namespace: default
    insecure_policy: allow
    annotations:
      haproxy.router.openshift.io/balance: roundrobin
  register: route
a  
result:
  description:
    - The Route object that was created or updated. Will be empty in the case of deletion.
  returned: success
  type: complex
  contains:
    apiVersion:
      description: The versioned schema of this representation of an object.
      returned: success
      type: str
    kind:
      description: Represents the REST resource this object represents.
      returned: success
      type: str
    metadata:
      description: Standard object metadata. Includes name, namespace, annotations, labels, etc.
      returned: success
      type: complex
      contains:
          name:
              description: The name of the created Route
              type: str
          namespace:
              description: The namespace of the create Route
              type: str
    spec:
      description: Specification for the Route
      returned: success
      type: complex
      contains:
          host:
              description: Host is an alias/DNS that points to the service.
              type: str
          path:
              description: Path that the router watches for, to route traffic for to the service.
              type: str
          port:
              description: Defines a port mapping from a router to an endpoint in the service endpoints.
              type: complex
              contains:
                  targetPort:
                      description: The target port on pods selected by the service this route points to.
                      type: str
          tls:
              description: Defines config used to secure a route and provide termination.
              type: complex
              contains:
                  caCertificate:
                      description: Provides the cert authority certificate contents.
                      type: str
                  certificate:
                      description: Provides certificate contents.
                      type: str
                  destinationCACertificate:
                      description: Provides the contents of the ca certificate of the final destination.
                      type: str
                  insecureEdgeTerminationPolicy:
                      description: Indicates the desired behavior for insecure connections to a route.
                      type: str
                  key:
                      description: Provides key file contents.
                      type: str
                  termination:
                      description: Indicates termination type.
                      type: str
          to:
              description: Specifies the target that resolve into endpoints.
              type: complex
              contains:
                  kind:
                      description: The kind of target that the route is referring to. Currently, only 'Service' is allowed.
                      type: str
                  name:
                      description: Name of the service/target that is being referred to. e.g. name of the service.
                      type: str
                  weight:
                      description: Specifies the target's relative weight against other target reference objects.
                      type: int
          wildcardPolicy:
              description: Wildcard policy if any for the route.
              type: str
    status:
      description: Current status details for the Route
      returned: success
      type: complex
      contains:
          ingress:
              description: List of places where the route may be exposed.
              type: complex
              contains:
                conditions:
                    description: Array of status conditions for the Route ingress.
                    type: complex
                    contains:
                        type:
                            description: The type of the condition. Currently only 'Ready'.
                            type: str
                        status:
                            description: The status of the condition. Can be True, False, Unknown.
                            type: str
                host:
                    description: The host string under which the route is exposed.
                    type: str
                routerCanonicalHostname:
                    description: The external host name for the router that can be used as a CNAME for the host requested for this route. May not be set.
                    type: str
                routerName:
                    description: A name chosen by the router to identify itself.
                    type: str
                wildcardPolicy:
                    description: The wildcard policy that was allowed where this route is exposed.
                    type: str
duration:
  description: elapsed time of task in seconds
  returned: when C(wait) is true
  type: int
  sample: 48
N)	to_native)AnsibleOpenshiftModule)perform_action)Waiter)AUTH_ARG_SPECWAIT_ARG_SPECCOMMON_ARG_SPEC)DynamicApiErrorNotFoundErrorc                   V     e Zd Z fdZed        Zd Z	 	 	 	 	 	 	 	 	 	 ddZd Z xZ	S )OpenShiftRoutec                     t         t        |   | j                  d       d| _        d| _        g | _        d | j                  d<   y )NT)argument_specsupports_check_modeF
merge_type)superr   __init__argspecappend_hashapplywarningsparams)self	__class__s    q/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/okd/plugins/modules/openshift_route.pyr   zOpenShiftRoute.__init__T  sG    nd,,, $ 	- 	

 !
$(L!    c                    t        j                  t              }|j                  t        j                  t                     |j                  t        j                  t
                     t        ddg      |d<   t        dd      |d<   t        d	      |d
<   t        d	      |d<   t        d	      |d<   t        d	      |d<   t        dgd      |d<   t        d	      |d<   t        dt        t        d	      t        d	      t        d	      t        dd      t        dg dd                  |d<   t        g dd      |d<   t        d	      |d<   |S )Nstrsvc)typealiasesserviceT)requiredr#   	namespacedict)r#   labelsnamehostnamepath	Subdomain)choicesr#   wildcard_policyportF)r#   no_log)allowredirectdisallowr4   )r#   r.   default)ca_certificatecertificatedestination_ca_certificatekeyinsecure_policy)r#   optionstls)edgepassthrough	reencryptinsecurer@   )r.   r5   terminationannotations)copydeepcopyr
   updater   r   r(   )r   specs     r   r   zOpenShiftRoute.argspec_  s0   }}]+DMM-01DMM/23EE7;Y $U;[6*X'VU+Z'V"&}5"I'V#/ e,+/U+;eE2 $=&!

U #Dj
] #/]r   c                    | j                   j                  d      }| j                   d   }| j                   j                  d      }|dk(  rd }| j                   j                  d      }|dk7  r|s| j                  d       | j                   j                  d      xr# | j                   j                  d	       xr |dk7  }|rd
| j                   d<   | j                   j                  d      xs |}| j                   j                  d      }| j                   j                  d      }| j                   j                  d      }	| j                   j                  d      }
| j                   j                  d      }| j                   j                  d      }|r| j                   j                  d      r| j                   d   j                  d      }| j                   d   j                  d      }| j                   d   j                  d      }| j                   d   j                  d      }| j                   d   j                  d      }|dk(  rd }n
d x}x}x}x}}dd|||di d}|r||d   d<   |dk7  r | j                  ||||
||	||||||      |d<   t	        | j
                  || j                         }| j                   j                  d       }| j                   j                  d!      }|rN| j                  ddd"#      }t        | j                  |t              }|j                  ||||$      \  }|d%<   |d&<    | j                  d'i | y )(Nr%   r'   rA   r@   stateabsentz:If 'state' is not 'absent' then 'service' must be providedwaitwait_conditionFr*   r)   r+   r,   r/   r0   rB   r<   r6   r7   r8   r9   r:   r4   zroute.openshift.io/v1Route)r*   r'   r)   )
apiVersionkindmetadatarF   rO   )
r0   r/   r+   r,   termination_typetls_insecure_policytls_ca_certtls_certtls_keytls_dest_ca_certrF   wait_timeout
wait_sleepTfail)timeoutsleepr*   r'   resultduration )r   get	fail_jsonbuild_route_specr   r"   find_resourcer	   clientwait_predicaterJ   	exit_json)r   service_namer'   rP   rH   custom_wait
route_namer)   r+   r,   r/   r0   rB   rR   rS   rU   rT   rQ   router\   rZ   r[   	v1_routeswaitersuccesss                            r   execute_modulezOpenShiftRoute.execute_module  s-   {{y1KK,	;;??=9z)#(H\NNWX KKOOF# "KKOO$455"! 	
 "'DKK[[__V,<
*;;??:.{{v&++//*;<{{v&kkoom4 6++e,001ABK{{5)--m<H#{{51556RSkk%(,,U3G"&++e"4"8"89J"K"j0&*# K ( %5 # 2"& 
 	
 /:E*m,H 11 /!!1$7'!!1 2 E&M  %=++//.1-**74KRV*WIDKKNCF<BKKu: =H =9GVH%vj'9 	  r   c                 Z   | j                  ddd      }	 |j                  ||      }i d|dd| j                  |      i|d}|rt        |j                               |d<   |re|dk(  r|j                         |d   d<   nQ|dk(  r-|dk7  r| j                  d       |j                         |d   d<   n|dk(  r| j                  d       nd |d   d<   |	r|dk(  r| j                  d       |	|d   d<   |
r|dk(  r| j                  d       |
|d   d<   |r|dk(  r| j                  d       ||d   d<   |r$|dk7  r| j                  d       ||d   d<   nd |d<   |r||d<   |r||d <   |S # t        $ r |s| j                  d       d }Y ct        $ rW}| j                  dj                  |j                        |j                  |j                  |j                  	       Y d }~d }~wt        $ r8}| j                  dj                  t        |            d
d
d
	       Y d }~d }~ww xY w)!NServicev1TrX   )r*   r'   zLYou need to provide the 'port' argument when exposing a non-existent service)msgz-Failed to retrieve service to be exposed: {0})rq   errorstatusreason )rN   r*   
targetPort)r<   tor0   wildcardPolicy)rA   r<   r=   insecureEdgeTerminationPolicyr>   r3   zU'redirect' is the only supported insecureEdgeTerminationPolicy for passthrough routesr?   z<'tls.insecure_policy' is not supported with reencrypt routesz='tls.ca_certificate' is not supported with passthrough routescaCertificatez:'tls.certificate' is not supported with passthrough routesr7   z2'tls.key' is not supported with passthrough routesr9   z<'destination_certificate' is only valid for reencrypt routesdestinationCACertificatehostr,   )rb   r_   r   r`   r   formatbodyrs   rt   	Exceptionr   set_portr(   
capitalize)r   rf   r'   r0   r/   r+   r,   rP   rQ   rR   rS   rT   rU   v1_servicestarget_serviceexc
route_specs                    r   ra   zOpenShiftRoute.build_route_spec  s    ((Dt(D	(__,)_TN2 !$
 dmmNDA .


  $1A1L1L1N OJu"#v- ,668 u%7 &6*j8s
 ,668 u%7 &4NNV FJ
5!"AB#}4NNW 6A
5!/2#}4NNT 4<
5!-0#}4NN#WX+2
5!%(#{2NNV AQ
5!"<= $Ju!)Jv!%Jvm  	"f   "N 	NNCJJ388Tjjzzzz	     	NNCJJcN    	s*   E* *H*H*AG&&H*2-H%%H*c                     |r|S |j                   j                  D ]9  }|j                  dk(  s|j                  |j                  c S |j                  c S  y )NTCP)rF   portsprotocolr*   rv   )r   r%   port_argps       r   r   zOpenShiftRoute.set_port@  sQ    O## 	$AzzU"66%66M||#		$
 r   )
NNNNNNNNNN)
__name__
__module____qualname__r   propertyr   rm   ra   r   __classcell__)r   s   @r   r   r   S  sP    	)    DR!p  hTr   r   c                    | j                   r| j                   j                  sy| j                   j                  D ]E  }|j                  D cg c]  }|j                  dk(  s| }}|s y|d   }|j                   dk7  sE y yc c}w )NFAdmittedr   TrueT)rs   ingress
conditionsr#   )ri   r   xmatchs       r   rd   rd   K  s~    LLU\\11<<'' #..Gq!&&J2FGGa<<6!  Hs   B Bc                  4    t               j                          y )N)r   
run_moduler^   r   r   mainr   X  s    !r   __main__)
__future__r   r   r   r#   __metaclass__DOCUMENTATIONEXAMPLESRETURNrC   ansible.module_utils._textr   Gansible_collections.community.okd.plugins.module_utils.openshift_commonr   Cansible_collections.kubernetes.core.plugins.module_utils.k8s.runnerr   Cansible_collections.kubernetes.core.plugins.module_utils.k8s.waiterr	   Dansible_collections.kubernetes.core.plugins.module_utils.args_commonr
   r   r   ImportErrorekubernetes.dynamic.exceptionsr   r   r   rd   r   r   r^   r   r   <module>r      s    A @||0dv
r  09 	L
u+ up
" zF g  9688M8MO9  		s(   A" A; "A8'A33A8;BB