# Copyright 2018 Google Inc.
#
# 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
#
#     https://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:
  labels:
    'app.kubernetes.io/component': discovery-server
    'app.kubernetes.io/instance': eureka
  name: discovery-server
spec:
  serviceName: "discovery-instance"
  podManagementPolicy: Parallel
  replicas: 3 # Update annotation
  selector:
    matchLabels:
      'app.kubernetes.io/component': discovery-server
      'app.kubernetes.io/instance': eureka
  template:
    metadata:
      labels:
        'app.kubernetes.io/component': discovery-server
        'app.kubernetes.io/instance': eureka
      annotations:
        replicas: '3' # For the downward API
    spec:
      terminationGracePeriodSeconds: 10
      containers:
        - command: ['/bin/sh']
          args:
            - '-c'
            - |
              ./dockerize -wait=tcp://$(CONFIG_SERVER_NAME):8888 -timeout=60s -- echo "found config-service"

              # Calculate the default zone based on the number of replicas
              export EUREKA_CLIENT_SERVICEURL_DEFAULTZONE=""
              for i in $(seq 0 $(expr $REPLICAS - 1));
              do
                  EUREKA_CLIENT_SERVICEURL_DEFAULTZONE="http://$(DISCOVERY_STATEFULSET_NAME)-$i.$(DISCOVERY_INSTANCE_NAME):8761/eureka/,$EUREKA_CLIENT_SERVICEURL_DEFAULTZONE"
              done
              EUREKA_CLIENT_SERVICEURL_DEFAULTZONE=$(echo $EUREKA_CLIENT_SERVICEURL_DEFAULTZONE | sed 's/,$//')

              # Calculate the hostname from the Pod and Server namnes
              export EUREKA_INSTANCE_HOSTNAME="$(POD_NAME).$(DISCOVERY_INSTANCE_NAME)"

              # Run the program
              java -XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap -Djava.security.egd=file:/dev/./urandom -jar /app.jar --server.port=8761 --eureka.client.serviceUrl.defaultZone=$EUREKA_CLIENT_SERVICEURL_DEFAULTZONE --eureka.instance.hostname=$EUREKA_INSTANCE_HOSTNAME --eureka.client.enableSelfPreservation=false --eureka.client.fetchRegistry=true --eureka.client.registerWithEureka=true
          env:
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: REPLICAS
              valueFrom:
                fieldRef:
                  fieldPath: "metadata.annotations['replicas']"
            - name: SPRING_PROFILES_ACTIVE
              value: docker
          envFrom:
            - configMapRef:
                name: spring-config
            - secretRef:
                name: spring-secret
          readinessProbe:
            httpGet:
              path: '/'
              port: 8761
            initialDelaySeconds: 10
            timeoutSeconds: 1
            periodSeconds: 5
          image: spring-petclinic-discovery-server
          name: discovery-server
          ports:
            - containerPort: 8761
          resources:
            limits:
              memory: "536870912"
      restartPolicy: Always
  volumeClaimTemplates:
    - metadata:
        name: mysql
      spec:
        accessModes: [ "ReadWriteOnce" ]
        resources:
          requests:
            storage: 20Gi
