apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: {{ .Values.clusterRoleName }}
rules:
- apiGroups:
  - ""
  resources:
  - namespaces
  verbs:
  - create
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: {{ .Values.clusterRoleName }}
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: {{ .Values.clusterRoleName }}
subjects:
- kind: ServiceAccount
  name: default
  namespace: {{ .Release.Namespace }}
---
apiVersion: v1
kind: Secret
metadata:
  name: ssh-key
type: Opaque
data:
  private: {{ .Values.sshPrivateKey }}
---
apiVersion: v1
kind: Service
metadata:
  name: welcome
  namespace: {{ .Release.Namespace }}
spec:
  type: ClusterIP
  selector:
    app: welcome
  ports:
  - name: http
    port: 80
    targetPort: http
    protocol: TCP
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress
  namespace: {{ .Release.Namespace }}
  {{- if .Values.ingress.certificateIssuer }}
  annotations:
    acme.cert-manager.io/http01-edit-in-place: "true"
    cert-manager.io/cluster-issuer: {{ .Values.ingress.certificateIssuer}}
  {{- end }}
spec:
  ingressClassName: {{ .Values.ingress.className }}
  {{- if .Values.ingress.certificateIssuer }}
  tls:
  - hosts:
    - {{ .Values.ingress.domain }}
    secretName: cert-welcome
  {{- end }}
  rules:
  - host: {{ .Values.ingress.domain }}
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: welcome
            port:
              name: http
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: welcome
  namespace: {{ .Release.Namespace }}
spec:
  selector:
    matchLabels:
      app: welcome
  replicas: 1
  template:
    metadata:
      labels:
        app: welcome
    spec:
      volumes:
      - name: ssh-key
        secret:
          secretName: ssh-key
      containers:
      - name: welcome
        image: {{ .Values.image.repository }}:{{ .Values.image.tag }}
        imagePullPolicy: {{ .Values.image.pullPolicy }}
        ports:
        - name: http
          containerPort: 8080
          protocol: TCP
        command:
        - pcloud-installer
        - welcome
        - --repo-addr={{ .Values.repoAddr }}
        - --ssh-key=/pcloud/ssh-key/private
        - --port=8080
        - --create-account-addr={{ .Values.createAccountAddr }}
        - --login-addr={{ .Values.loginAddr }}
        - --memberships-init-addr={{ .Values.membershipsInitAddr }}
        volumeMounts:
        - name: ssh-key
          readOnly: true
          mountPath: /pcloud/ssh-key
