# Copyright 2021 DoiT International.
#
# 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.

#
# --
# @spec: https://kubernetes.io/docs/reference/kubernetes-api/workload-resources/pod-v1/
#
# @warn: please be aware of the limits of pod-resources in GKE/k8s regarding declarative updates!
#        pod updates may not change fields other than `spec.containers[*].image`,
#                                                     `spec.initContainers[*].image`,
#                                                     `spec.activeDeadlineSeconds`,
#                                                     `spec.tolerations` (only additions to existing toleration's)
#
# @info: Here we create a simple pod resource that describes a nginx-based static web application
#        and provisions it within the cluster. You can access this pod from your local environment
#        by kubectl's port-forwarding command and access the web-application on localhost:8080 ...
#
#        $ kubectl port-forward pod/static-web-app-advanced 8080:80 -n doit-lab-02
# --
#
apiVersion: v1
kind: Pod
metadata:
  name: static-web-app-advanced
  namespace: doit-lab-02
  labels:
    k8s-app: static-web-app-advanced
    k8s-scope: gke-ws-doit-lab-02
spec:
  #
  # These are our two primary container definitions within the pod. both containers share the same namespaces, cgroups
  # and potentially other facets of isolation - the same things that isolate a Docker container. Within a Pod's context,
  # the individual applications may have further sub-isolations applied. In terms of Docker concepts, a Pod is similar
  # to a group of Docker containers with shared namespaces and shared filesystem volumes.
  # --
  containers:
    - name: 001-static-web-app-c
      image: doitlabs/web-app-static:1.0.5
      env:
      - name: C_MESSAGE
        value: "Stay Awhile & Listen; Lab-2"
      ports:
      - name: http-80
        containerPort: 80
        protocol: TCP

    - name: 002-static-web-app-sidecar-c
      image: busybox:1.28
      command: [ "sh", "-c"]
      args:
      - while true; do
          echo "I'm really busy right now ... please come back later!";
          sleep 30;
        done;

  #
  # An Init-container in Kubernetes is a special type of container that runs to completion before the main app’s container
  # and the pod will repeatedly restart until all of them succeed. It’s sometimes necessary to prepare the primary container
  # which will be running our application or the main logic, and it's accomplished through init containers.
  # --
  initContainers:
    - name: 000-static-web-app-advanced-init-c
      image: busybox:1.28
      command:
        - sysctl
        - -w
        - vm.max_map_count=262144
      securityContext:
        privileged: true
