#######################################################################################
# Copyright ETSI Contributors and Others.
#
# 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.
#######################################################################################

---
# TEMPLATE PARAMETERS:
# ===================
#
# CLUSTER_KUSTOMIZATION_NAME: Name of the cluster in the management cluster (i.e., the `Kustomization`).
# CLUSTER_NAME: Name of the cluster in the target cloud. It may differ from `CLUSTER_KUSTOMIZATION_NAME` since naming restrictions are often different from K8s resource naming restrictions (e.g., hyphens vs. underscores).
# CLUSTER_AGE_SECRET_NAME: Name of the secret in the management cluster that keeps the private key for age/sops in the remote cluster.

# Creates remote `managed-resources.cloud-conf-toml` secret
apiVersion: kustomize.toolkit.fluxcd.io/v1beta2
kind: Kustomization
metadata:
  name: ${CLUSTER_KUSTOMIZATION_NAME}-capo-config-toml
  namespace: managed-resources
  labels:
    cluster: ${CLUSTER_KUSTOMIZATION_NAME}
spec:
  # interval: 1h
  interval: 5m
  retryInterval: 1m
  timeout: 5m
  prune: true
  # wait: true
  force: true
  sourceRef:
    kind: GitRepository
    name: sw-catalogs
    namespace: flux-system
  path: ./cloud-resources/flux-remote-bootstrap/bootstrap/manifests/secret
  kubeConfig:
    secretRef:
      name: ${CLUSTER_KUBECONFIG_SECRET_NAME}
      key: ${CLUSTER_KUBECONFIG_SECRET_KEY}
  patches:
    - patch: |-
        apiVersion: v1
        kind: Secret
        metadata:
          name: ${secret_name}
          namespace: ${secret_namespace}
        stringData:
          cloud.conf: |-
            [Global]
            auth-url=${os_auth_url}
            region=${os_region_name}
            username=${os_username}
            password=${os_password}
            tenant-id=${os_project_id}
            domain-id=${os_project_domain_id}
  # Inputs:
  postBuild:
    substitute:
      secret_name: cloud-config
      secret_namespace: kube-system
    substituteFrom:
      - kind: Secret
        name: ${OPENSTACK_CLOUD_NAME}-capo-config-toml
