#
# 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.
#
# Source: openwhisk/templates/controller-pod.yaml
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: controller
  namespace: nuvolaris
  labels:
    name: controller
    app: controller
spec:
  serviceName: controller
  podManagementPolicy: "Parallel"
  replicas: {{controller_replicas}}
  selector:
    matchLabels:
      name: controller
  template:
    metadata:
      labels:
        name: controller
        app: controller
      annotations:
        whisks.nuvolaris.org/annotate-version: "true" 
    spec:
      serviceAccountName: openwhisk-core
      restartPolicy: Always
      initContainers:
      - name: check-kafka
        image: busybox:1.36.0
        command: ['sh', '-c', "until nslookup kafka.$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace).svc.cluster.local; do echo waiting for kafka; sleep 2; done"]       
      containers:
      - name: controller
        imagePullPolicy: "IfNotPresent"
        image: "ghcr.io/nuvolaris/openwhisk-controller:0.3.0-morpheus.22122609"      
        command: ["/bin/bash", "-c", "/init.sh `hostname | awk -F '-' '{print $NF}'`"]     
        ports:
        - name: controller
          containerPort: 8080   
        - name: akka-remoting
          containerPort: 25520
        - name: akka-mgmt-http
          containerPort: 19999

        livenessProbe:
          httpGet:
            path: "/ping"
            port: 8080
            scheme: "HTTP"
          initialDelaySeconds: 30
          periodSeconds: 120
          timeoutSeconds: 60
        readinessProbe:
          httpGet:
            path: "/ping"
            port: 8080
            scheme: "HTTP"
          initialDelaySeconds: 10        
          periodSeconds: 10
          timeoutSeconds: 30
        {% if container_manage_resources %}
        resources:
          requests:
            memory: "{{container_mem_req}}"
            cpu : "{{container_cpu_req}}"
          limits:
            memory: "{{container_mem_lim}}"
            cpu : "{{container_cpu_lim}}"
        {% endif %}          
        env:

        - name: "PORT"
          value: "8080"

        - name: "TZ"
          value: "UTC"

        - name: "POD_IP"
          valueFrom:
            fieldRef:
              fieldPath: status.podIP

        # Java options
        - name: "JAVA_OPTS"
          value: "{{controller_java_opts}} "

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

        # action runtimes
        - name: "RUNTIMES_MANIFEST"
          valueFrom:   
            configMapKeyRef:                 
              name: openwhisk-runtimes              
              key: runtimes.json

        # Action limits

        - name:  "CONFIG_whisk_info_date"
          value:  "01-01-2023"

        - name: "CONFIG_whisk_info_buildNo"
          value: "development-unknown"

        - name: "LIMITS_ACTIONS_INVOKES_PERMINUTE"
          value: "{{actions_invokes_perMinute}}"
        - name: "LIMITS_ACTIONS_INVOKES_CONCURRENT"
          value: "{{actions_invokes_concurrent}}"
        - name: "LIMITS_TRIGGERS_FIRES_PERMINUTE"
          value: "{{triggers_fires_perMinute}}"
        - name: "LIMITS_ACTIONS_SEQUENCE_MAXLENGTH"
          value: "{{actions_sequence_maxLength}}"

        - name: "CONFIG_whisk_timeLimit_min"
          value: "{{time_limit_min}}"
        - name: "CONFIG_whisk_timeLimit_max"
          value: "{{time_limit_max}}"
        - name: "CONFIG_whisk_timeLimit_std"
          value: "{{time_limit_std}}"

        - name: "CONFIG_whisk_memory_min"
          value: "{{memory_limit_min}}"
        - name: "CONFIG_whisk_memory_max"
          value:  "{{memory_limit_max}}"
        - name: "CONFIG_whisk_memory_std"
          value: "{{memory_limit_std}}"

        - name: "CONFIG_whisk_concurrencyLimit_min"
          value: "{{concurrency_limit_min}}"
        - name: "CONFIG_whisk_concurrencyLimit_max"
          value: "{{concurrency_limit_max}}"
        - name: "CONFIG_whisk_concurrencyLimit_std"
          value: "{{concurrency_limit_std}}"

        - 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: "{{activation_payload_max}}"

        - name: "CONFIG_whisk_loadbalancer_blackboxFraction"
          value: "{{loadbalancer_blackbox_fraction}}"

        - name: "CONFIG_whisk_loadbalancer_timeoutFactor"
          value: "{{loadbalancer_timeout_factor}}"

        # Kafka properties
        - name: "KAFKA_HOSTS"
          value: "{{kafka_host}}"
        # etcd properties

        # properties for DB connection
        # TODO to be fixed in community edition 1st
        # properties for DB connection
        #- name: "CONFIG_whisk_couchdb_username"
        #  valueFrom:
        #    secretKeyRef:
        #      name: couchdb-auth
        #      key: db_username
        #- name: "CONFIG_whisk_couchdb_password"
        #  valueFrom:
        #    secretKeyRef:
        #      name: couchdb-auth
        #      key: db_password

        - name: "CONFIG_whisk_couchdb_username"
          value: "{{couchdb_admin_user}}"
        - name: "CONFIG_whisk_couchdb_password"
          value: "{{couchdb_admin_password}}"  

        - name: "CONFIG_whisk_couchdb_port"
          value: "{{couchdb_port}}"
        - name: "CONFIG_whisk_couchdb_protocol"
          value: "http"
        - name: "CONFIG_whisk_couchdb_host"
          value: "{{couchdb_host}}"
        - name: "CONFIG_whisk_couchdb_provider"
          value: "CouchDB"
        - name: "CONFIG_whisk_couchdb_databases_WhiskActivation"
          value: "nuvolaris_activations"
        - name: "CONFIG_whisk_couchdb_databases_WhiskEntity"
          value: "nuvolaris_whisks"
        - name: "CONFIG_whisk_couchdb_databases_WhiskAuth"
          value: "nuvolaris_subjects"

        - name: "CONFIG_whisk_couchdb_controller_username"
          value: "{{couchdb_controller_user}}"
        - name: "CONFIG_whisk_couchdb_controller_password"
          value: "{{couchdb_controller_password}}"          

        - name: "CONTROLLER_INSTANCES"
          value: "1"
        - name: "CONFIG_akka_cluster_seedNodes_0"
          value: "akka://controller-actor-system@$(POD_IP):25520"
        - name: "CONFIG_logback_log_level"
          value: "{{controller_logging_level}}"

        # Prometheus activation
        - name: "CONFIG_whisk_metrics_prometheusEnabled"
          value: "true"

        - name: "METRICS_KAMON"
          value: "true"

        - name: "METRICS_KAMON_TAGS"
          value: "true"                         

        # Set this with the same value as the Prometheus scrape interval in seconds
        - name: "CONFIG_kamon_metric_tickInterval"
          value: "30"        

        #- name: "CONFIG_spi_ContainerFactoryProvider"
        #  value: "org.apache.openwhisk.core.containerpool.kubernetes.KubernetesContainerFactoryProvider"
        #- name: "CONFIG_spi_LogStoreProvider"
        #  value:  "org.apache.openwhisk.core.containerpool.logging.DockerToActivationLogStoreProvider"           
        #- name: "CONFIG_spi_ArtifactStoreProvider"
        #  value: "org.apache.openwhisk.core.database.CouchDbStoreProvider"
