# Copyright 2019 Copyright (c) 2019 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file.
#
# 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.

---
landscape: (( &temporary ))
imports: (( &temporary ))
utilities: (( &temporary ))
env: (( &temporary ))

plugins:
  - kubectl: domain-secret
  - parallel: (( sum[.kubectl|[]|s,i,e|-> s {"kubectl" = "kubectl[" i "]"}] ))
  - -echo: "The setup waits until the shoots are ready. This may take up to 60 minutes."
  - <<: (( sum[.shootready|[]|s,i,e|-> s {"shootready" = "shootready[" i "]"}] ))

shootready: (( sum[.shoots|[]|s,e|-> s ( e {"kubeconfig" = imports.kube_apiserver.export.kubeconfig} )] ))

settings:
  domain_secret: internal-domain-seed

domain-secret:
  kubeconfig: (( imports.kube_apiserver.export.kubeconfig ))
  manifests:
    - apiVersion: v1
      kind: Secret
      metadata:
        name: (( .settings.domain_secret ))
        namespace: (( imports.project.export.project_namespace ))
        labels:
          garden.sapcloud.io/role: internal-domain
        annotations:
          dns.gardener.cloud/provider: (( .landscape.dns.type ))
          dns.gardener.cloud/domain: (( "seed." .landscape.domain ))
      type: Opaque
      data: (( sum[landscape.dns.credentials|{}|c,k,v|-> c {k=base64(v)}] ))

shootconfig:
  <<: (( &template &temporary ))
  values:
    name: (( v.name ))
    type: (( v.type ))
    namespace: (( v.namespace || imports.project.export.project_namespace ))
    domain: (( name "." landscape.domain ))
    domain_secret: (( .settings.domain_secret ))
    iaas: (( read(__ctx.DIR "/../provider/" v.type "/shoot.yaml", "import") ))
    cloudprofile: (( v.cloudprofile || v.name ))
    k8sVersion: (( v.k8sVersion || imports.profiles.export.k8sVersions[cloudprofile] ))
    defaultMachineImage: (( imports.profiles.export.machineImages[cloudprofile].name ))
    seed: (( v.seed ))
    secretbinding: (( ( "v" = { "type" = v.type, "name" = cloudprofile }, "settings" = { "project_name" = imports.project.export.project_name } ) *imports.project.export.secret_name_template ))
    config: (( v ))

kubectl: (( sum[.landscape.iaas_shooted_seeds|[]|s,v|-> s utilities.kubectl.generate( __ctx.DIR "/../manifests/shoot_manifest.yaml", imports.kube_apiserver.export.kubeconfig, *shootconfig )] ))

shoot_template:
  <<: (( &template &temporary ))
  name: (( e.metadata.name ))
  namespace: (( e.metadata.namespace ))

shoots: (( sum[.kubectl.[*].manifests[0]|[]|s,e|-> s *.shoot_template] ))
