---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: browser-{{ id }}
  namespace: {{ namespace }}
  labels:
    browser: {{ id }}
    role: browser

spec:
  selector:
    matchLabels:
      browser: {{ id }}
      role: browser

  serviceName: browser-{{ id }}
  replicas: 1

  template:
    metadata:
      labels:
        browser: {{ id }}
        role: browser

    spec:
      affinity:
        nodeAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
            - weight: 1
              preference:
                matchExpressions:
                - key: nodeType
                  operator: In
                  values:
                    - "{{ crawler_node_type }}"

        podAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
            - weight: 2
              podAffinityTerm:
                topologyKey: "failure-domain.beta.kubernetes.io/zone"
                labelSelector:
                  matchLabels:
                    job-name: job-{{ id }}
                    crawl: {{ id }}

      tolerations:
        - key: "nodeType"
          operator: "Equal"
          value: "crawling"
          effect: "NoSchedule"

      containers:
        - name: browser
          image: {{ crawler_image }}
          imagePullPolicy: {{ crawler_image_pull_policy }}
          command:
            - create-login-profile
            - --interactive
            - --filename
            - /tmp/profile.tar.gz
            - --url
            - {{ url }}
          {%- if profile_filename %}
            - --profile
            - "@{{ profile_filename }}"
          {%- endif %}   

          envFrom:
            - secretRef:
                name: storage-{{ storage_name }}

          env:
            - name: STORE_PATH
              value: {{ storage_path }}

---
apiVersion: v1
kind: Service
metadata:
  name: browser-{{ id }}
  labels:
    browser: {{ id }}
    role: browser
 
spec:
  clusterIP: None
  selector:
    browser: {{ id }}
    role: browser

  ports:
    - protocol: TCP
      port: 9223
      name: browser-api

    - protocol: TCP
      port: 9222
      name: browser-ws

