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

# Patch to install troubleshooting tools and protobuf definitions using an init container.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: bastion
spec:
  template:
    spec:
      containers:
      - name: app
        volumeMounts:
        - name: protos
          mountPath: /opt/protos
        - name: tools
          mountPath: /usr/local/bin
      initContainers:
      - name: install-tools
        image: golang
        command:
        - /bin/sh
        - -c
        - |
          apt-get update --assume-yes
          apt-get install --assume-yes ca-certificates curl jq
          #
          go install github.com/fullstorydev/grpcurl/cmd/grpcurl@latest
          #
          go install github.com/grpc-ecosystem/grpcdebug@latest
          #
          curl -L "https://github.com/mikefarah/yq/releases/download/$(curl -sL https://api.github.com/repos/mikefarah/yq/releases/latest | jq -r .tag_name)/yq_$(go env GOOS)_$(go env GOARCH).tar.gz" | tar zx -C /opt/tools ./yq_$(go env GOOS)_$(go env GOARCH)
          mv /opt/tools/yq_$(go env GOOS)_$(go env GOARCH) /opt/tools/yq
          chmod +x /opt/tools/yq
          #
          curl -L -o /opt/tools/kubectl "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/$(go env GOOS)/$(go env GOARCH)/kubectl"
          chmod +x /opt/tools/kubectl
          #
          curl -L -o /opt/tools/jq "https://github.com/jqlang/jq/releases/download/$(curl -sSL https://api.github.com/repos/jqlang/jq/releases | grep '"tag_name":' | head -n1 | grep -o 'jq-[0-9]*\.[0-9]*\.*[0-9]*')/jq-$(go env GOOS)-$(go env GOARCH)"
          chmod +x /opt/tools/jq
          #
          # Clone the Google RPC type definition protos, such as ErrorInfo, to /opt/protos/google/rpc:
          git clone --branch=master --depth=1 --no-checkout --single-branch https://github.com/googleapis/googleapis /opt/protos
          cd /opt/protos
          git sparse-checkout init --cone
          git sparse-checkout set google/rpc
          git checkout --quiet @
          #
          # Download the `helloworld.Greeter` gRPC service proto definition to /opt/protos/helloworld/greeter.proto:
          mkdir -p /opt/protos/helloworld
          curl -sSLo /opt/protos/helloworld/greeter.proto https://raw.githubusercontent.com/grpc/grpc/v1.68.2/examples/protos/helloworld.proto
        env:
        - name: DEBIAN_FRONTEND
          value: noninteractive
        - name: GOBIN
          value: /opt/tools
        volumeMounts:
        - name: protos
          mountPath: /opt/protos          
        - name: tools
          mountPath: /opt/tools
      volumes:
      - name: protos
        emptyDir: {}
      - name: tools
        emptyDir: {}
