apiVersion: v1
kind: Service
metadata:
  labels:
    app: metadata
    app.kubernetes.io/name: {{ .Release.Name }}
  name: metadata-grpc-service
spec:
  ports:
    - name: grpc-api
      port: 8080
      protocol: TCP
  selector:
    component: metadata-grpc-server
    app.kubernetes.io/name: {{ .Release.Name }}
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    component: metadata-grpc-server
    app.kubernetes.io/name: {{ .Release.Name }}
  name: metadata-grpc-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      component: metadata-grpc-server
      app.kubernetes.io/name: {{ .Release.Name }}
  template:
    metadata:
      labels:
        component: metadata-grpc-server
        app.kubernetes.io/name: {{ .Release.Name }}
    spec:
      containers:
      - name: container
        image: {{ .Values.images.metadataserver }}
        imagePullPolicy: 'Always'
        env:
        # TODO: merge all into mysql-credential
        {{ if .Values.managedstorage.enabled }}
        - name: DBCONFIG_USER
          valueFrom:
            secretKeyRef:
              name: mysql-credential
              key: username
        - name: DBCONFIG_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-credential
              key: password
        {{ else }}
        - name: DBCONFIG_USER
          value: 'root'
        - name: DBCONFIG_PASSWORD
          value: ''
        {{ end }}
        - name: MYSQL_DATABASE
          valueFrom:
            configMapKeyRef:
              name: metadata-mysql-configmap
              key: MYSQL_DATABASE
        - name: MYSQL_HOST
          valueFrom:
            configMapKeyRef:
              name: metadata-mysql-configmap
              key: MYSQL_HOST
        - name: MYSQL_PORT
          valueFrom:
            configMapKeyRef:
              name: metadata-mysql-configmap
              key: MYSQL_PORT
        command: ["/bin/metadata_store_server"]
        args: ["--grpc_port=8080",
                 "--mysql_config_database=$(MYSQL_DATABASE)",
                 "--mysql_config_host=$(MYSQL_HOST)",
                 "--mysql_config_port=$(MYSQL_PORT)",
                 "--mysql_config_user=$(DBCONFIG_USER)",
                 "--mysql_config_password=$(DBCONFIG_PASSWORD)",
                 "--enable_database_upgrade=true"
                ]
        ports:
        - containerPort: 8080
          name: grpc-api
        livenessProbe:
          tcpSocket:
            port: grpc-api
          initialDelaySeconds: 3
          periodSeconds: 5
          timeoutSeconds: 2
        readinessProbe:
          tcpSocket:
            port: grpc-api
          initialDelaySeconds: 3
          periodSeconds: 5
          timeoutSeconds: 2
---
kind: Service
apiVersion: v1
metadata:
  labels:
    app: metadata
    app.kubernetes.io/name: {{ .Release.Name }}
  name: metadata-envoy-service
spec:
  selector:
    component: metadata-envoy
  type: ClusterIP
  ports:
    - port: 9090
      protocol: TCP
      name: md-envoy
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: metadata-envoy
  labels:
    component: metadata-envoy
    app.kubernetes.io/name: {{ .Release.Name }}
spec:
  replicas: 1
  selector:
    matchLabels:
      component: metadata-envoy
  template:
    metadata:
      labels:
        component: metadata-envoy
    spec:
      containers:
        - name: container
          image: {{ .Values.images.metadataenvoy }}
          ports:
            - name: md-envoy
              containerPort: 9090
            - name: envoy-admin
              containerPort: 9901
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: metadata-mysql-configmap
  labels:
    component: metadata-server
data:
  MYSQL_DATABASE: '{{ tpl .Values.managedstorage.databaseNamePrefix . }}_metadata'
  MYSQL_HOST: "mysql"
  MYSQL_PORT: "3306"
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: metadata-grpc-configmap
  labels:
    component: metadata-grpc-server
data:
  METADATA_GRPC_SERVICE_HOST: "metadata-grpc-service"
  METADATA_GRPC_SERVICE_PORT: "8080"
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: metadata-writer
  labels:
    app: metadata-writer
    app.kubernetes.io/name: {{ .Release.Name }}
spec:
  replicas: 1
  selector:
    matchLabels:
      app: metadata-writer
      app.kubernetes.io/name: {{ .Release.Name }}
  template:
    metadata:
      labels:
        app: metadata-writer
        app.kubernetes.io/name: {{ .Release.Name }}
    spec:
      containers:
      - name: main
        image: {{ .Values.images.metadatawriter }}
        env:
        - name: NAMESPACE_TO_WATCH
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
      serviceAccountName: kubeflow-pipelines-metadata-writer
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  labels:
    app: kubeflow-pipelines-metadata-writer-role
    app.kubernetes.io/name: {{ .Release.Name }}
  name: kubeflow-pipelines-metadata-writer-role
rules:
- apiGroups:
  - ""
  resources:
  - pods
  verbs:
  - get
  - list
  - watch
  - update
  - patch
- apiGroups:
  - ""
  resources:
  - configmaps
  verbs:
  - get
- apiGroups:
  - argoproj.io
  resources:
  - workflows
  verbs:
  - get
  - list
  - watch
  - update
  - patch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: kubeflow-pipelines-metadata-writer-binding
  labels:
    app.kubernetes.io/name: {{ .Release.Name }}
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: kubeflow-pipelines-metadata-writer-role
subjects:
- kind: ServiceAccount
  name: kubeflow-pipelines-metadata-writer
  namespace: {{ .Release.Namespace }}
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: kubeflow-pipelines-metadata-writer
  labels:
    app.kubernetes.io/name: {{ .Release.Name }}
