redeploy-router-certificates.yml 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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_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_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_client_binary }} set 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_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_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. - file:
  92. path: "{{ item }}"
  93. state: absent
  94. with_items:
  95. - /etc/origin/master/openshift-router.crt
  96. - /etc/origin/master/openshift-router.key
  97. when:
  98. - l_router_dc.rc == 0
  99. - l_router_svc.rc == 0
  100. - ('router-certs' in router_secrets)
  101. - import_role:
  102. name: openshift_hosted
  103. tasks_from: router.yml
  104. when:
  105. - l_router_dc.rc == 0
  106. - l_router_svc.rc == 0
  107. - ('router-certs' in router_secrets)
  108. - name: Delete temp directory
  109. file:
  110. name: "{{ router_cert_redeploy_tempdir.stdout }}"
  111. state: absent
  112. changed_when: False