---
# Source: openwhisk/templates/controller-pod.yaml
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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.
#

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: RELEASE-NAME-controller
  labels:
    name: RELEASE-NAME-controller
    heritage: "Helm"
    release: "RELEASE-NAME"
    chart: "openwhisk-1.0.1"
    app: RELEASE-NAME-openwhisk
spec:
  serviceName: RELEASE-NAME-controller
  podManagementPolicy: "Parallel"
  replicas: 1
  selector:
    matchLabels:
      name: RELEASE-NAME-controller
  template:
    metadata:
      labels:
        name: RELEASE-NAME-controller
        heritage: "Helm"
        release: "RELEASE-NAME"
        chart: "openwhisk-1.0.1"
        app: RELEASE-NAME-openwhisk

    spec:
      serviceAccountName: RELEASE-NAME-core
      restartPolicy: Always
      affinity:
        # prefer to not run on an invoker node (only prefer because of single node clusters)
        nodeAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 100
            preference:
              matchExpressions:
              - key: openwhisk-role
                operator: NotIn
                values:
                - invoker
          - weight: 80
            preference:
              matchExpressions:
              - key: openwhisk-role
                operator: In
                values:
                - core
        # Fault tolerance: prevent multiple instances of RELEASE-NAME-controller from running on the same node
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchExpressions:
              - key: name
                operator: In
                values:
                - RELEASE-NAME-controller
            topologyKey: "kubernetes.io/hostname"
      tolerations:
        - key: "openwhisk-role"
          operator: "Equal"
          value: core
          effect: "NoSchedule"

      initContainers:
      # The controller must wait for kafka and/or couchdb to be ready before it starts
      - name: "wait-for-kafka"
        image: "openwhisk/ow-utils:3e6138d"
        imagePullPolicy: "IfNotPresent"
        command: ["sh", "-c", 'cacert="/var/run/secrets/kubernetes.io/serviceaccount/ca.crt"; token="$(cat /var/run/secrets/kubernetes.io/serviceaccount/token)"; while true; do rc=$(curl -sS --cacert $cacert --header "Authorization: Bearer $token" https://kubernetes.default.svc/api/v1/namespaces/default/endpoints/RELEASE-NAME-kafka | jq -r ".subsets[].addresses | length"); echo "num ready kafka endpoints is $rc"; if [ $rc -gt 0 ]; then echo "Success: ready kafka endpoint!"; break; fi; echo "kafka not ready yet; sleeping for 3 seconds"; sleep 3; done;']
      - name: "wait-for-couchdb"
        image: "busybox:latest"
        imagePullPolicy: "IfNotPresent"
        env:
        - name: "READINESS_URL"
          value: http://RELEASE-NAME-couchdb.default.svc.cluster.local:5984/ow_kube_couchdb_initialized_marker
        command: ["sh", "-c", "while true; do echo 'checking CouchDB readiness'; wget -T 5 --spider $READINESS_URL --header=\"Authorization: Basic d2hpc2tfYWRtaW46c29tZV9wYXNzdzByZA==\"; result=$?; if [ $result -eq 0 ]; then echo 'Success: CouchDB is ready!'; break; fi; echo '...not ready yet; sleeping 3 seconds before retry'; sleep 3; done;"]
      # The lean controller requires invoker volumes mounts
      

      containers:
      - name: controller
        imagePullPolicy: "IfNotPresent"
        image: "openwhisk/controller:3e6138d"
        command: ["/bin/bash", "-c", "/init.sh `hostname | awk -F '-' '{print $NF}'`"]
        ports:
        - name: controller
          containerPort: 8080
        - name: akka-remoting
          containerPort: 2552
        - name: akka-mgmt-http
          containerPort: 19999
        livenessProbe:
          httpGet:
            path: "/ping"
            port: 8080
            scheme: "HTTP"
          initialDelaySeconds: 10
          periodSeconds: 10
          timeoutSeconds: 1
        readinessProbe:
          httpGet:
            path: "/ping"
            port: 8080
            scheme: "HTTP"
          initialDelaySeconds: 10
          periodSeconds: 10
          timeoutSeconds: 1
        env:
        - name: "PORT"
          value: "8080"

        - name: "TZ"
          value: "UTC"

        - name:  "CONFIG_whisk_info_date"
          valueFrom:
            configMapKeyRef:
              name: RELEASE-NAME-whisk.config
              key: whisk_info_date
        - name: "CONFIG_whisk_info_buildNo"
          valueFrom:
            configMapKeyRef:
              name: RELEASE-NAME-whisk.config
              key: whisk_info_buildNo

        # Java options
        - name: "JAVA_OPTS"
          value: "-Xmx1024M "

        # specific controller arguments
        - name: "CONTROLLER_OPTS"
          value: " "

        # action runtimes
        - name: "RUNTIMES_MANIFEST"
          value: "{\n    \"runtimes\": {\n        \"nodejs\": [\n            {\n                \"kind\": \"nodejs:12\",\n                \"default\": false,\n                \"image\": {\n                    \"prefix\": \"openwhisk\",\n                    \"name\": \"action-nodejs-v12\",\n                    \"tag\": \"1.19.0\"\n                },\n                \"deprecated\": false,\n                \"attached\": {\n                    \"attachmentName\": \"codefile\",\n                    \"attachmentType\": \"text/plain\"\n                }\n            },\n            {\n                \"kind\": \"nodejs:14\",\n                \"default\": true,\n                \"image\": {\n                    \"prefix\": \"openwhisk\",\n                    \"name\": \"action-nodejs-v14\",\n                    \"tag\": \"1.19.0\"\n                },\n                \"deprecated\": false,\n                \"attached\": {\n                    \"attachmentName\": \"codefile\",\n                    \"attachmentType\": \"text/plain\"\n                },\n                \"stemCells\": [\n                    {\n                        \"initialCount\": 2,\n                        \"memory\": \"256 MB\",\n                        \"reactive\": {\n                            \"minCount\": 1,\n                            \"maxCount\": 4,\n                            \"ttl\": \"2 minutes\",\n                            \"threshold\": 1,\n                            \"increment\": 1\n                        }\n                    }\n                ]\n            }\n        ],\n        \"python\": [\n            {\n                \"kind\": \"python:3\",\n                \"default\": true,\n                \"image\": {\n                    \"prefix\": \"openwhisk\",\n                    \"name\": \"action-python-v3.7\",\n                    \"tag\": \"1.17.0\"\n                },\n                \"deprecated\": false,\n                \"attached\": {\n                    \"attachmentName\": \"codefile\",\n                    \"attachmentType\": \"text/plain\"\n                }\n            }\n        ],\n        \"swift\": [\n            {\n                \"kind\": \"swift:4.2\",\n                \"default\": true,\n                \"image\": {\n                    \"prefix\": \"openwhisk\",\n                    \"name\": \"action-swift-v4.2\",\n                    \"tag\": \"1.17.0\"\n                },\n                \"deprecated\": false,\n                \"attached\": {\n                    \"attachmentName\": \"codefile\",\n                    \"attachmentType\": \"text/plain\"\n                }\n            },\n            {\n                \"kind\": \"swift:5.1\",\n                \"default\": false,\n                \"image\": {\n                    \"prefix\": \"openwhisk\",\n                    \"name\": \"action-swift-v5.1\",\n                    \"tag\": \"1.17.0\"\n                },\n                \"deprecated\": false,\n                \"attached\": {\n                    \"attachmentName\": \"codefile\",\n                    \"attachmentType\": \"text/plain\"\n                }\n            },\n            {\n                \"kind\": \"swift:5.3\",\n                \"default\": false,\n                \"image\": {\n                    \"prefix\": \"openwhisk\",\n                    \"name\": \"action-swift-v5.3\",\n                    \"tag\": \"1.17.0\"\n                },\n                \"deprecated\": false,\n                \"attached\": {\n                    \"attachmentName\": \"codefile\",\n                    \"attachmentType\": \"text/plain\"\n                }\n            },\n            {\n                \"kind\": \"swift:5.4\",\n                \"default\": false,\n                \"image\": {\n                    \"prefix\": \"openwhisk\",\n                    \"name\": \"action-swift-v5.4\",\n                    \"tag\": \"1.17.0\"\n                },\n                \"deprecated\": false,\n                \"attached\": {\n                    \"attachmentName\": \"codefile\",\n                    \"attachmentType\": \"text/plain\"\n                }\n            }\n        ],\n        \"java\": [\n            {\n                \"kind\": \"java:8\",\n                \"default\": true,\n                \"image\": {\n                    \"prefix\": \"openwhisk\",\n                    \"name\": \"java8action\",\n                    \"tag\": \"1.17.0\"\n                },\n                \"deprecated\": false,\n                \"attached\": {\n                    \"attachmentName\": \"jarfile\",\n                    \"attachmentType\": \"application/java-archive\"\n                },\n                \"requireMain\": true\n            }\n        ],\n        \"php\": [\n            {\n                \"kind\": \"php:7.3\",\n                \"default\": false,\n                \"deprecated\": false,\n                \"image\": {\n                    \"prefix\": \"openwhisk\",\n                    \"name\": \"action-php-v7.3\",\n                    \"tag\": \"1.17.0\"\n                },\n                \"attached\": {\n                    \"attachmentName\": \"codefile\",\n                    \"attachmentType\": \"text/plain\"\n                }\n            },\n            {\n                \"kind\": \"php:7.4\",\n                \"default\": true,\n                \"deprecated\": false,\n                \"image\": {\n                    \"prefix\": \"openwhisk\",\n                    \"name\": \"action-php-v7.4\",\n                    \"tag\": \"1.17.0\"\n                },\n                \"attached\": {\n                    \"attachmentName\": \"codefile\",\n                    \"attachmentType\": \"text/plain\"\n                }\n            },\n            {\n                \"kind\": \"php:8.0\",\n                \"default\": false,\n                \"deprecated\": false,\n                \"image\": {\n                    \"prefix\": \"openwhisk\",\n                    \"name\": \"action-php-v7.4\",\n                    \"tag\": \"1.17.0\"\n                },\n                \"attached\": {\n                    \"attachmentName\": \"codefile\",\n                    \"attachmentType\": \"text/plain\"\n                }\n            }\n        ],\n        \"ruby\": [\n            {\n                \"kind\": \"ruby:2.5\",\n                \"default\": true,\n                \"deprecated\": false,\n                \"attached\": {\n                    \"attachmentName\": \"codefile\",\n                    \"attachmentType\": \"text/plain\"\n                },\n                \"image\": {\n                    \"prefix\": \"openwhisk\",\n                    \"name\": \"action-ruby-v2.5\",\n                    \"tag\": \"1.17.0\"\n                }\n            }\n        ],\n        \"go\": [\n            {\n                \"kind\": \"go:1.15\",\n                \"default\": true,\n                \"deprecated\": false,\n                \"attached\": {\n                    \"attachmentName\": \"codefile\",\n                    \"attachmentType\": \"text/plain\"\n                },\n                \"image\": {\n                    \"prefix\": \"openwhisk\",\n                    \"name\": \"action-golang-v1.15\",\n                    \"tag\": \"1.18.0\"\n                }\n            }\n        ],\n        \"rust\": [\n            {\n                \"kind\": \"rust:1.34\",\n                \"default\": true,\n                \"deprecated\": false,\n                \"attached\": {\n                    \"attachmentName\": \"codefile\",\n                    \"attachmentType\": \"text/plain\"\n                },\n                \"image\": {\n                    \"prefix\": \"openwhisk\",\n                    \"name\": \"action-rust-v1.34\",\n                    \"tag\": \"1.3.0\"\n                }\n            }\n        ],\n        \"dotnet\": [\n            {\n                \"kind\": \"dotnet:2.2\",\n                \"default\": true,\n                \"deprecated\": false,\n                \"requireMain\": true,\n                \"image\": {\n                    \"prefix\": \"openwhisk\",\n                    \"name\": \"action-dotnet-v2.2\",\n                    \"tag\": \"1.16.0\"\n                },\n                \"attached\": {\n                    \"attachmentName\": \"codefile\",\n                    \"attachmentType\": \"text/plain\"\n                }\n            },\n            {\n                \"kind\": \"dotnet:3.1\",\n                \"default\": false,\n                \"deprecated\": false,\n                \"requireMain\": true,\n                \"image\": {\n                    \"prefix\": \"openwhisk\",\n                    \"name\": \"action-dotnet-v3.1\",\n                    \"tag\": \"1.16.0\"\n                },\n                \"attached\": {\n                    \"attachmentName\": \"codefile\",\n                    \"attachmentType\": \"text/plain\"\n                }\n            }\n        ],\n        \"ballerina\": [\n            {\n                \"kind\": \"ballerina:0.990\",\n                \"default\": true,\n                \"image\": {\n                    \"prefix\": \"openwhisk\",\n                    \"name\": \"action-ballerina-v0.990.2\",\n                    \"tag\": \"nightly\"\n                },\n                \"deprecated\": false,\n                \"attached\": {\n                    \"attachmentName\": \"codefile\",\n                    \"attachmentType\": \"text/plain\"\n                }\n            }\n        ]\n    },\n    \"blackboxes\": [\n        {\n            \"prefix\": \"openwhisk\",\n            \"name\": \"dockerskeleton\",\n            \"tag\": \"1.14.0\"\n        }\n    ]\n}\n"

        # Action limits
        - name: "LIMITS_ACTIONS_INVOKES_PERMINUTE"
          value: "60"
        - name: "LIMITS_ACTIONS_INVOKES_CONCURRENT"
          value: "30"
        - name: "LIMITS_TRIGGERS_FIRES_PERMINUTE"
          value: "60"
        - name: "LIMITS_ACTIONS_SEQUENCE_MAXLENGTH"
          value: "50"
        - name: "CONFIG_whisk_timeLimit_min"
          value: "100ms"
        - name: "CONFIG_whisk_timeLimit_max"
          value: "5m"
        - name: "CONFIG_whisk_timeLimit_std"
          value: "1m"
        - name: "CONFIG_whisk_memory_min"
          value: "128m"
        - name: "CONFIG_whisk_memory_max"
          value: "512m"
        - name: "CONFIG_whisk_memory_std"
          value: "256m"
        - name: "CONFIG_whisk_concurrencyLimit_min"
          value: "1"
        - name: "CONFIG_whisk_concurrencyLimit_max"
          value: "1"
        - name: "CONFIG_whisk_concurrencyLimit_std"
          value: "1"
        - name: "CONFIG_whisk_logLimit_min"
          value: "0m"
        - name: "CONFIG_whisk_logLimit_max"
          value: "10m"
        - name: "CONFIG_whisk_logLimit_std"
          value: "10m"
        - name: "CONFIG_whisk_activation_payload_max"
          value: "1048576"

        - name: "CONFIG_whisk_loadbalancer_blackboxFraction"
          value: "10%"

        - name: "CONFIG_whisk_loadbalancer_timeoutFactor"
          value: "2"

        # Kafka properties
        - name: "KAFKA_HOSTS"
          value: "RELEASE-NAME-kafka-0.RELEASE-NAME-kafka.default.svc.cluster.local:9092"
        - name: "CONFIG_whisk_kafka_replicationFactor"
          value: ""
        - name: "CONFIG_whisk_kafka_topics_cacheInvalidation_retentionBytes"
          value: ""
        - name: "CONFIG_whisk_kafka_topics_cacheInvalidation_retentionMs"
          value: ""
        - name: "CONFIG_whisk_kafka_topics_cacheInvalidation_segmentBytes"
          value: ""
        - name: "CONFIG_whisk_kafka_topics_completed_retentionBytes"
          value: ""
        - name: "CONFIG_whisk_kafka_topics_completed_retentionMs"
          value: ""
        - name: "CONFIG_whisk_kafka_topics_completed_segmentBytes"
          value: ""
        - name: "CONFIG_whisk_kafka_topics_events_retentionBytes"
          value: ""
        - name: "CONFIG_whisk_kafka_topics_events_retentionMs"
          value: ""
        - name: "CONFIG_whisk_kafka_topics_events_segmentBytes"
          value: ""
        - name: "CONFIG_whisk_kafka_topics_health_retentionBytes"
          value: ""
        - name: "CONFIG_whisk_kafka_topics_health_retentionMs"
          value: ""
        - name: "CONFIG_whisk_kafka_topics_health_segmentBytes"
          value: ""
        - name: "CONFIG_whisk_kafka_topics_invoker_retentionBytes"
          value: ""
        - name: "CONFIG_whisk_kafka_topics_invoker_retentionMs"
          value: ""
        - name: "CONFIG_whisk_kafka_topics_invoker_segmentBytes"
          value: ""

        # properties for DB connection
        - name: "CONFIG_whisk_couchdb_username"
          valueFrom:
            secretKeyRef:
              name: RELEASE-NAME-db.auth
              key: db_username
        - name: "CONFIG_whisk_couchdb_password"
          valueFrom:
            secretKeyRef:
              name: RELEASE-NAME-db.auth
              key: db_password
        - name: "CONFIG_whisk_couchdb_port"
          valueFrom:
            configMapKeyRef:
              name: RELEASE-NAME-db.config
              key: db_port
        - name: "CONFIG_whisk_couchdb_protocol"
          valueFrom:
            configMapKeyRef:
              name: RELEASE-NAME-db.config
              key: db_protocol
        - name: "CONFIG_whisk_couchdb_host"
          value: "RELEASE-NAME-couchdb.default.svc.cluster.local"
        - name: "CONFIG_whisk_couchdb_provider"
          valueFrom:
            configMapKeyRef:
              name: RELEASE-NAME-db.config
              key: db_provider
        - name: "CONFIG_whisk_couchdb_databases_WhiskActivation"
          valueFrom:
            configMapKeyRef:
              name: RELEASE-NAME-db.config
              key: db_whisk_activations
        - name: "CONFIG_whisk_couchdb_databases_WhiskEntity"
          valueFrom:
            configMapKeyRef:
              name: RELEASE-NAME-db.config
              key: db_whisk_actions
        - name: "CONFIG_whisk_couchdb_databases_WhiskAuth"
          valueFrom:
            configMapKeyRef:
              name: RELEASE-NAME-db.config
              key: db_whisk_auths

        - name: "CONTROLLER_INSTANCES"
          value: "1"



        - name: "CONFIG_logback_log_level"
          value: "INFO"
        # properties for lean messaging provider
