apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: default
  name: micro-network
  labels:
    micro: service
    name: micro-network
spec:
  replicas: 3
  selector:
    matchLabels:
      name: micro-network
      micro: service
  strategy:
    rollingUpdate:
      maxSurge: 0
      maxUnavailable: 1
  template:
    metadata:
      labels:
        name: micro-network
        micro: service
    spec:
      affinity:
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 100
            podAffinityTerm:
              labelSelector:
                matchExpressions:
                - key: name
                  operator: In
                  values:
                  - micro-network
              topologyKey: "kubernetes.io/hostname"
      volumes:
      - name: shared-data
        emptyDir: {}
      containers:
      - name: network
        env:
        - name: MICRO_LOG_LEVEL
          value: "debug"
        - name: MICRO_BROKER
          value: "nats"
        - name: MICRO_BROKER_ADDRESS
          value: "nats-cluster"
        - name: MICRO_REGISTRY
          value: "etcd"
        - name: MICRO_REGISTRY_ADDRESS
          value: "etcd-cluster-client"
        - name: MICRO_SERVER_ADDRESS
          value: "0.0.0.0:8080"
        - name: MICRO_NETWORK_NODES
          value: "network.micro.mu:30038"
        - name: MICRO_NETWORK_SVC_NODE_PORT
          value: "30038"
        - name: MICRO_NETWORK_DNS_REMOVE_DOMAIN
          value: "network.micro.mu"
        - name: MICRO_NETWORK_DNS_REMOVE_TOKEN
          valueFrom:
            secretKeyRef:
              key: DNS_SHARED_SECRET
              name: dns-shared-secret
        - name: MICRO_NETWORK_DNS_ADVERTISE_TOKEN
          valueFrom:
            secretKeyRef:
              key: DNS_SHARED_SECRET
              name: dns-shared-secret
        - name: MICRO_NETWORK_DNS_ADVERTISE_DOMAIN
          value: "network.micro.mu"
        command: ['sh', '-c']
        args:
        - "export NODE_IP=$(cat /node_info/ip); /micro network --advertise=\"$NODE_IP:$MICRO_NETWORK_SVC_NODE_PORT\""
        image: micro/micro
        imagePullPolicy: Always
        volumeMounts:
        - name: shared-data
          mountPath: /node_info
        ports:
        - containerPort: 8080
          name: service-port
        - containerPort: 8085
          name: network-port
        lifecycle:
          preStop:
            exec:
              command:
              - /bin/sh
              - -c
              - /micro network dns remove --address $(cat /node_info/ip)
          postStart:
            exec:
              command:
              - /bin/sh
              - -c
              - /micro network dns advertise --address $(cat /node_info/ip)
      initContainers:
      - name: init-network-env
        env:
        - name: NODE_NAME
          valueFrom:
            fieldRef:
              fieldPath: spec.nodeName
        image: microhq/curljq
        imagePullPolicy: Always
        volumeMounts:
        - name: shared-data
          mountPath: /node_info
        command: ['sh', '-c']
        args:
        - "TOKEN=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token); curl -k -X GET -H \"Authorization: Bearer $TOKEN\" https://$KUBERNETES_SERVICE_HOST/api/v1/nodes/$NODE_NAME | jq -r '.status.addresses[] | select (.type==\"ExternalIP\").address' > /node_info/ip"

