apiVersion: v1
kind: Service
metadata:
  name: api
  labels:
    name: api
spec:
  type: LoadBalancer
  ports:
  - name: http
    port: 3000
    targetPort: 3000
  selector:
    name: heroes-api
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name:  heroes-api-deploy
  labels:
    name:  heroes-api
spec:
  replicas: 1
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
    type: RollingUpdate
  template:
    metadata:
      labels:
        name:  heroes-api
    spec:
      containers:
      - image: sonofjorel/rating-api:v1
        name:  heroes-api-cntnr
        resources:
          requests:
            cpu: "20m"
            memory: "55M"
        env:
        - name:  MONGODB_URI
          value:  mongodb://mongodb:27017/webratings
        ports:
        - containerPort: 3000
          name:  heroes-api
        volumeMounts:
          - name: datadir
            mountPath: /usr/tmp
        imagePullPolicy: Always
      restartPolicy: Always
      initContainers:
      - name: get-hero-data
        image: busybox  
        command: ['sh', '-c', 'for i in {1..100}; do sleep 1; if [ ! -f "./data-dir/heroes.json" ]; then wget -O ./data-dir/heroes.json https://raw.githubusercontent.com/Azure/blackbelt-aks-hackfest/master/app/db/heroes.json; cat ./data-dir/heroes.json; exit 1; fi; done; exit 0']
        volumeMounts:
        - name: datadir
          mountPath: "/data-dir"
      - name: get-site-data
        image: busybox  
        command: ['sh', '-c', 'for i in {1..100}; do sleep 1; if [ ! -f "./data-dir/sites.json" ]; then wget -O ./data-dir/sites.json https://raw.githubusercontent.com/Azure/blackbelt-aks-hackfest/master/app/db/sites.json; cat ./data-dir/sites.json; exit 1; fi; done; exit 0']
        volumeMounts:
        - name: datadir
          mountPath: "/data-dir"
      - name: get-ratings-data
        image: busybox  
        command: ['sh', '-c', 'for i in {1..100}; do sleep 1; if [ ! -f "./data-dir/ratings.json" ]; then wget -O ./data-dir/ratings.json https://raw.githubusercontent.com/Azure/blackbelt-aks-hackfest/master/app/db/ratings.json; cat ./data-dir/ratings.json; exit 1; fi; done; exit 0']
        volumeMounts:
        - name: datadir
          mountPath: "/data-dir"
      - name: import-hero-data
        image: mongo:3.6.1-jessie
        command: ['sh', '-c', 'for i in {1..100}; do sleep 1; if [ -f "./data-dir/heroes.json" ]; then mongoimport --host mongodb:27017 --db webratings --collection heroes --file /data-dir/heroes.json --jsonArray; exit 0; fi; done; exit 1']
        volumeMounts:
        - name: datadir
          mountPath: "/data-dir"
      - name: import-site-data
        image: mongo:3.6.1-jessie
        command: ['sh', '-c', 'for i in {1..100}; do sleep 1; if [ -f "./data-dir/sites.json" ]; then mongoimport --host mongodb:27017 --db webratings --collection sites --file /data-dir/sites.json --jsonArray; exit 0; fi; done; exit 1']
        volumeMounts:
        - name: datadir
          mountPath: "/data-dir"
      - name: import-ratings-data
        image: mongo:3.6.1-jessie
        command: ['sh', '-c', 'for i in {1..100}; do sleep 1; if [ -f "./data-dir/ratings.json" ]; then mongoimport --host mongodb:27017 --db webratings --collection ratings --file /data-dir/ratings.json --jsonArray; exit 0; fi; done; exit 1']
        volumeMounts:
        - name: datadir
          mountPath: "/data-dir"
      volumes:
        - name: datadir
          emptyDir: {}
---
apiVersion: v1
kind: Service
metadata:
  name: web
  labels:
    name: web
spec:
  type: LoadBalancer
  ports:
  - name: http
    port: 8080
    targetPort: 8080
  selector:
    name: heroes-web
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name:  heroes-web-deploy
  labels:
    name:  heroes-web
spec:
  replicas: 1
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
    type: RollingUpdate
  template:
    metadata:
      labels:
        name:  heroes-web
    spec:
      containers:
      - image: sonofjorel/rating-web:v1
        name:  heroes-web-cntnr
        resources:
          requests:
            cpu: "0.5"
            memory: "1Gi"
        env:
        - name:  API
          value:  http://api:3000/
        - name: KUBE_NODE_NAME
          valueFrom:
            fieldRef:
              fieldPath: spec.nodeName
        - name: KUBE_POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        - name: KUBE_POD_IP
          valueFrom:
            fieldRef:
              fieldPath: status.podIP
        ports:
        - containerPort:  8080
          name:  heroes-web
        imagePullPolicy: Always
      restartPolicy: Always