redeploy-router-certificates.yml 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. ---
  2. - name: Update router certificates
  3. hosts: oo_first_master
  4. vars:
  5. roles:
  6. - lib_openshift
  7. tasks:
  8. - name: Create temp directory for kubeconfig
  9. command: mktemp -d /tmp/openshift-ansible-XXXXXX
  10. register: router_cert_redeploy_tempdir
  11. changed_when: false
  12. - name: Copy admin client config(s)
  13. command: >
  14. cp {{ openshift.common.config_base }}/master//admin.kubeconfig {{ router_cert_redeploy_tempdir.stdout }}/admin.kubeconfig
  15. changed_when: false
  16. - name: Determine if router exists
  17. command: >
  18. {{ openshift.common.client_binary }} get dc/router -o json
  19. --config={{ router_cert_redeploy_tempdir.stdout }}/admin.kubeconfig
  20. -n default
  21. register: l_router_dc
  22. failed_when: false
  23. changed_when: false
  24. - name: Determine if router service exists
  25. command: >
  26. {{ openshift.common.client_binary }} get svc/router -o json
  27. --config={{ router_cert_redeploy_tempdir.stdout }}/admin.kubeconfig
  28. -n default
  29. register: l_router_svc
  30. failed_when: false
  31. changed_when: false
  32. - name: Collect router environment variables and secrets
  33. set_fact:
  34. router_env_vars: "{{ ((l_router_dc.stdout | from_json)['spec']['template']['spec']['containers'][0]['env']
  35. | lib_utils_oo_collect('name'))
  36. | default([]) }}"
  37. router_secrets: "{{ ((l_router_dc.stdout | from_json)['spec']['template']['spec']['volumes']
  38. | lib_utils_oo_collect('secret')
  39. | lib_utils_oo_collect('secretName'))
  40. | default([]) }}"
  41. changed_when: false
  42. when: l_router_dc.rc == 0
  43. - name: Collect router service annotations
  44. set_fact:
  45. router_service_annotations: "{{ (l_router_svc.stdout | from_json)['metadata']['annotations'] if 'annotations' in (l_router_svc.stdout | from_json)['metadata'] else [] }}"
  46. when: l_router_svc.rc == 0
  47. - name: Update router environment variables
  48. shell: >
  49. {{ openshift.common.client_binary }} env dc/router
  50. OPENSHIFT_CA_DATA="$(cat /etc/origin/master/ca.crt)"
  51. OPENSHIFT_CERT_DATA="$(cat /etc/origin/master/openshift-router.crt)"
  52. OPENSHIFT_KEY_DATA="$(cat /etc/origin/master/openshift-router.key)"
  53. --config={{ router_cert_redeploy_tempdir.stdout }}/admin.kubeconfig
  54. -n default
  55. when:
  56. - l_router_dc.rc == 0
  57. - ('OPENSHIFT_CA_DATA' in router_env_vars)
  58. - ('OPENSHIFT_CERT_DATA' in router_env_vars)
  59. - ('OPENSHIFT_KEY_DATA' in router_env_vars)
  60. # When the router service contains service signer annotations we
  61. # will delete the existing certificate secret and allow OpenShift to
  62. # replace the secret.
  63. - block:
  64. - name: Delete existing router certificate secret
  65. oc_secret:
  66. kubeconfig: "{{ router_cert_redeploy_tempdir.stdout }}/admin.kubeconfig"
  67. name: router-certs
  68. namespace: default
  69. state: absent
  70. run_once: true
  71. - name: Remove router service annotations
  72. command: >
  73. {{ openshift.common.client_binary }} annotate service/router
  74. service.alpha.openshift.io/serving-cert-secret-name-
  75. service.alpha.openshift.io/serving-cert-signed-by-
  76. --config={{ router_cert_redeploy_tempdir.stdout }}/admin.kubeconfig
  77. -n default
  78. - name: Add serving-cert-secret annotation to router service
  79. command: >
  80. {{ openshift.common.client_binary }} annotate service/router
  81. service.alpha.openshift.io/serving-cert-secret-name=router-certs
  82. --config={{ router_cert_redeploy_tempdir.stdout }}/admin.kubeconfig
  83. -n default
  84. when:
  85. - l_router_dc.rc == 0
  86. - l_router_svc.rc == 0
  87. - ('router-certs' in router_secrets)
  88. - openshift_hosted_router_certificate is undefined
  89. - ('service.alpha.openshift.io/serving-cert-secret-name') in router_service_annotations
  90. - ('service.alpha.openshift.io/serving-cert-signed-by') in router_service_annotations
  91. # When there are no annotations on the router service we will allow
  92. # the openshift_hosted role to either create a new wildcard
  93. # certificate (since we deleted the original) or reapply a custom
  94. # openshift_hosted_router_certificate.
  95. - file:
  96. path: "{{ item }}"
  97. state: absent
  98. with_items:
  99. - /etc/origin/master/openshift-router.crt
  100. - /etc/origin/master/openshift-router.key
  101. when:
  102. - l_router_dc.rc == 0
  103. - l_router_svc.rc == 0
  104. - ('router-certs' in router_secrets)
  105. - ('service.alpha.openshift.io/serving-cert-secret-name') not in router_service_annotations
  106. - ('service.alpha.openshift.io/serving-cert-signed-by') not in router_service_annotations
  107. - include_role:
  108. name: openshift_hosted
  109. tasks_from: main
  110. vars:
  111. openshift_hosted_manage_registry: false
  112. when:
  113. - l_router_dc.rc == 0
  114. - l_router_svc.rc == 0
  115. - ('router-certs' in router_secrets)
  116. - ('service.alpha.openshift.io/serving-cert-secret-name') not in router_service_annotations
  117. - ('service.alpha.openshift.io/serving-cert-signed-by') not in router_service_annotations
  118. - name: Redeploy router
  119. command: >
  120. {{ openshift.common.client_binary }} deploy dc/router
  121. --latest
  122. --config={{ router_cert_redeploy_tempdir.stdout }}/admin.kubeconfig
  123. -n default
  124. - name: Delete temp directory
  125. file:
  126. name: "{{ router_cert_redeploy_tempdir.stdout }}"
  127. state: absent
  128. changed_when: False