# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


- name: "Hydrate and copy templates Anthos Network Gateway"
  template:
    src: "{{ item.template }}"
    dest: "{{ acm_config_files }}/{{ item.final_name }}"
  loop:
  - {"template": "network-gateway-group.yaml.j2", "final_name": "network-gateway-group.yaml"}
  - {"template": "ang-multi-cluster-connect-config.yaml.j2", "final_name": "ang-multi-cluster-connect-config.yaml"}
  - {"template": "egress-nat-config.yaml.j2", "final_name": "egress-nat-config.yaml"}
  when:
  - primary_cluster_machine is defined
  - primary_cluster_machine == true
  tags:
  - ang-install
  - post-acm

- name: "Create IKE secret"
  shell: |
    kubectl create secret -n kube-system generic ike-key --from-literal=psk={{ ike_key_secret_value }} || true
  environment:
    KUBECONFIG: "{{ kubeconfig_shared_location }}"
  when:
  - primary_cluster_machine is defined
  - primary_cluster_machine == true
  tags:
  - ang-install
  - post-acm

- name: "Apply ANG Configurations"
  shell: |
    kubectl apply -f {{ acm_config_files }}/{{ item.final_name }}
  environment:
    KUBECONFIG: "{{ kubeconfig_shared_location }}"
  loop:
  - {"template": "network-gateway-group.yaml.j2", "final_name": "network-gateway-group.yaml"}
  - {"template": "ang-multi-cluster-connect-config.yaml.j2", "final_name": "ang-multi-cluster-connect-config.yaml"}
  - {"template": "egress-nat-config.yaml.j2", "final_name": "egress-nat-config.yaml"}
  when:
  - primary_cluster_machine is defined
  - primary_cluster_machine == true
  tags:
  - ang-install
  - post-acm
