Add support to deploy DevOps portal on K8s

Change-Id: I23766e4b88f36606e930769e424afbfe7174d68f
diff --git a/devops_portal/files/kubernetes/devops_portal-cm.yaml b/devops_portal/files/kubernetes/devops_portal-cm.yaml
new file mode 100644
index 0000000..589b434
--- /dev/null
+++ b/devops_portal/files/kubernetes/devops_portal-cm.yaml
@@ -0,0 +1,12 @@
+{%- macro include_by_filename(filename) -%}
+  {%- include filename -%}
+{%- endmacro -%}
+apiVersion: v1
+kind: ConfigMap
+metadata:
+  name: devops-portal
+data:
+  config.json: |
+{{ include_by_filename('/srv/oss/devops_portal/config/config.json')|indent(width=4, indentfirst=True) }}
+  nginx.conf: |
+{{ include_by_filename('/srv/oss/devops_portal/nginx/nginx.conf')|indent(width=4, indentfirst=True) }}
diff --git a/devops_portal/files/kubernetes/devops_portal-deploy.yaml b/devops_portal/files/kubernetes/devops_portal-deploy.yaml
new file mode 100644
index 0000000..c6c1c1e
--- /dev/null
+++ b/devops_portal/files/kubernetes/devops_portal-deploy.yaml
@@ -0,0 +1,52 @@
+{%- from "devops_portal/map.jinja" import control with context %}
+{%- set container = control.container %}
+apiVersion: extensions/v1beta1
+kind: Deployment
+metadata:
+  name: devops-portal
+spec:
+  replicas: 1
+  template:
+    metadata:
+      labels:
+        app: devops-portal
+    spec:
+      containers:
+        - name: nginx
+          image: {{ container.image }}
+          command:
+            - nginx
+            - -c
+            - /etc/nginx/config/nginx.conf
+          env:
+            - name: CONFIGMAP_HASH
+              value: {{ salt['hashutil.digest_file']('/srv/oss/devops_portal/kubernetes/devops_portal-cm.yaml') }}
+          volumeMounts:
+            - name: nginx-conf
+              mountPath: /etc/nginx/config
+              readOnly: true
+            - name: config-json
+              mountPath: /opt/devops-portal/config
+              readOnly: true
+          ports:
+            - containerPort: {{ container.container_port }}
+          livenessProbe:
+            httpGet:
+              path: /
+              port: {{ container.container_port }}
+            initialDelaySeconds: 10
+            timeoutSeconds: 1
+      volumes:
+        - name: nginx-conf
+          configMap:
+            name: devops-portal
+            items:
+              - key: nginx.conf
+                path: nginx.conf
+        - name: config-json
+          configMap:
+            name: devops-portal
+            items:
+              - key: config.json
+                path: config.json
+      restartPolicy: Always
diff --git a/devops_portal/files/kubernetes/devops_portal-svc.yaml b/devops_portal/files/kubernetes/devops_portal-svc.yaml
new file mode 100644
index 0000000..cde5657
--- /dev/null
+++ b/devops_portal/files/kubernetes/devops_portal-svc.yaml
@@ -0,0 +1,16 @@
+{%- from "devops_portal/map.jinja" import control with context %}
+{%- set container = control.container %}
+apiVersion: v1
+kind: Service
+metadata:
+  name: devops-portal
+spec:
+  selector:
+    app: devops-portal
+  ports:
+    - name: http
+      protocol: TCP
+      port: {{ container.external_port }}
+      targetPort: {{ container.container_port }}
+  externalIPs:
+    - {{ container.external_ip }}
diff --git a/devops_portal/setup/init.sls b/devops_portal/setup/init.sls
index 1b43923..7e644c6 100644
--- a/devops_portal/setup/init.sls
+++ b/devops_portal/setup/init.sls
@@ -1 +1,2 @@
 include:
+  - devops_portal.setup.kubernetes
diff --git a/devops_portal/setup/kubernetes.sls b/devops_portal/setup/kubernetes.sls
new file mode 100644
index 0000000..375d183
--- /dev/null
+++ b/devops_portal/setup/kubernetes.sls
@@ -0,0 +1,59 @@
+{%- from "devops_portal/map.jinja" import control with context %}
+{%- if control.setup.engine == "kubernetes" %}
+{%- set namespace = control.setup.kubernetes.namespace|default("oss") %}
+
+devops_portal_kubernetes_dir:
+  file.directory:
+    - name: /srv/oss/devops_portal/kubernetes
+    - user: root
+    - group: root
+    - mode: 0755
+    - makedirs: true
+
+devops_portal_k8s_svc:
+  file.managed:
+    - name: /srv/oss/devops_portal/kubernetes/devops_portal-svc.yaml
+    - source: salt://devops_portal/files/kubernetes/devops_portal-svc.yaml
+    - template: jinja
+    - require:
+      - file: devops_portal_kubernetes_dir
+
+devops_portal_k8s_cm:
+  file.managed:
+    - name: /srv/oss/devops_portal/kubernetes/devops_portal-cm.yaml
+    - source: salt://devops_portal/files/kubernetes/devops_portal-cm.yaml
+    - template: jinja
+    - require:
+      - file: devops_portal_kubernetes_dir
+      - file: devops_portal_nginx_conf
+      - file: devops_portal_config
+
+devops_portal_k8s_deploy:
+  file.managed:
+    - name: /srv/oss/devops_portal/kubernetes/devops_portal-deploy.yaml
+    - source: salt://devops_portal/files/kubernetes/devops_portal-deploy.yaml
+    - template: jinja
+    - require:
+      - file: devops_portal_kubernetes_dir
+      - file: devops_portal_k8s_cm
+
+devop_portal_namespace:
+  cmd.run:
+    - name: hyperkube kubectl create namespace {{ namespace }}
+    - unless: hyperkube kubectl get namespaces {{ namespace }}
+
+apply_devops_portal:
+  cmd.run:
+    - name: hyperkube kubectl apply
+            --filename /srv/oss/devops_portal/kubernetes/devops_portal-svc.yaml
+            --filename /srv/oss/devops_portal/kubernetes/devops_portal-cm.yaml
+            --filename /srv/oss/devops_portal/kubernetes/devops_portal-deploy.yaml
+            --namespace={{ namespace }}
+            --record
+    - require:
+      - cmd: devop_portal_namespace
+      - file: devops_portal_k8s_svc
+      - file: devops_portal_k8s_cm
+      - file: devops_portal_k8s_deploy
+
+{%- endif %}
diff --git a/metadata/service/control/single.yml b/metadata/service/control/single.yml
index 6d43062..2651b49 100644
--- a/metadata/service/control/single.yml
+++ b/metadata/service/control/single.yml
@@ -2,11 +2,17 @@
   - devops_portal
 parameters:
   _param:
+    devops_portal_setup_engine: kubernetes
+    devops_portal_k8s_namespace: oss
     devops_portal_image: docker-sandbox.sandbox.mirantis.net/ikharin/oss/devops-portal:latest
     devops_portal_external_port: 8000
   devops_portal:
     control:
       enabled: true
+      setup:
+        engine: ${_param:devops_portal_setup_engine}
+        kubernetes:
+          namespace: ${_param:devops_portal_k8s_namespace}
       container:
         image: ${_param:devops_portal_image}
         external_ip: ${_param:devops_portal_external_ip}