# 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 ))
env: (( &temporary ))

plugins:
  - cleanup
  - <<: (( sum[.iaasSeedsSoils|[]|s,id,v|-> s *.pluginTemplate] ))

pluginTemplate:
  - <<: (( &temporary &template ))
  - kubectl: (( "renderedPluginSpecs[" id "].deploySecrets" ))
  - (( valid( renderedPluginSpecs[id].namespace ) ? { "kubectl" = "renderedPluginSpecs[" id "].namespace" } :~~ ))
  - pinned:
    - helm:
      - (( "renderedPluginSpecs[" id "].gardenlet" ))
      - template
    - kubectl: (( "renderedPluginSpecs[" id "].gardenlet" ))
  - seed-ready: (( "renderedPluginSpecs[" id "].seedReady" ))
  - delete: (( "renderedPluginSpecs[" id "].deleteSeed" ))
  - delete: (( "renderedPluginSpecs[" id "].deleteBootstrapToken" ))

iaasSeedsSoils: (( &temporary ( select[.landscape.iaas|elem|-> elem.mode == "seed" -or elem.mode == "soil"] ) ))

temp:
  <<: (( &temporary ))
  bootstrapNamePrefix: (( &temporary ( |x|-> replace( replace( substr( x, 0, 3 ), "-", "7" ), ".", "0" ) ) ))
bootstrapTokens: (( sum[.iaasSeedsSoils|{}|s,elem|-> s { elem.name = { "id" = temp.bootstrapNamePrefix( elem.name ) rand( "a-z0-9", 3 ), "secret" = temp.bootstrapNamePrefix( elem.name ) rand( "a-z0-9", 13 ) } }] ))

shoot-check:
  kubeconfig: (( imports.kube_apiserver.export.kubeconfig ))
  seeds: (( .iaasSeedsSoils ))

cleanup:
  kubeconfig: (( landscape.clusters.[0].kubeconfig ))
  namespace: (( landscape.namespace ))

providerconfig:
  <<: (( &template &temporary ))
  name: (( v.name ))
  namespace: (( landscape.namespace ))
  dns:
    type: (( landscape.dns.type ))
  iaas: (( read(__ctx.DIR "/../provider/" v.type "/seed.yaml", "import") ))
  config: (( v ))
  isBaseCluster: (( ! valid( v.cluster.kubeconfig ) ))
  bootstrapToken: (( bootstrapTokens[v.name] ))
  kubeconfigs:
    virtual: (( .imports.kube_apiserver.export.kubeconfig ))
    seed: (( v.cluster.kubeconfig || read( env.ROOTDIR "/" landscape.clusters.[0].kubeconfig, "yaml" ) ))
  seed:
    ingressdomain: (( name "." .imports.ingress_dns.export.ingress_domain ))
    networks: (( v.cluster.networks || landscape.clusters.[0].networks ))
    shootDefaultNetworks: (( v.shootDefaultNetworks || ~~ ))
  secretname: (( name "-" v.mode ))

renderedPluginSpecs: (( sum[.iaasSeedsSoils|[]|s,id,v|-> s [ ( "configValues" = *providerconfig ) read( __ctx.DIR "/../manifests/seed_manifests.yaml", "yaml" ).pluginSpecs ]] ))
