# 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.

---
settings: (( &temporary ))
state: (( &temporary ))
landscape: (( &temporary ))
env: (( &temporary ))

temp:
  <<: (( &temporary ))
  command:
    - kubectl
    - --kubeconfig
    - (( lookup_file(landscape.clusters.[0].kubeconfig, env.ROOTDIR).[0] ))
    - "-n"
    - (( landscape.namespace ))
    - get
    - secret
    - garden-kubeconfig-for-admin
    - "-o"
    - "jsonpath={.data.kubeconfig}"

export:
  gardener_dns: (( .settings.gardener_dns ))
  apiserver_dns: (( .settings.apiserver_dns ))
  apiserver_url: (( "https://" apiserver_dns ))
  apiserver_url_internal: (( .settings.apiserver_url_internal ))
  kube_apiserver_ca: (( .state.kube_apiserver_ca.value ))
  kubeconfig: (( parse(base64_decode(exec( temp.command ))) || "" ))
  kubeconfig_internal_merge_snippet: (( .files.kubeconfig_internal_merge_snippet.data ))

files:
  kubeconfig:
    data: (( asyaml(export.kubeconfig) ))
  kubeconfig_internal_merge_snippet:
    data: (( .kubeconfig_internal_merge.snippet ))

kubeconfig_internal_merge:
  <<: (( &temporary ))
  snippet: (( replace( raw_snippet, "internal-domain-here", .settings.apiserver_url_internal ) ))
  raw_snippet: |
    ---
    <<: (( &template ))
    apiVersion:
    kind:
    current-context:
    contexts:
    clusters: (( merge none // [ stub(clusters)[0] { "cluster" = merge( stub(clusters)[0].cluster, { "server" = "internal-domain-here" } ) } ] ))
    users: