apiVersion: service.aep.asag.io/v1
kind: Definition
metadata:
  namespace: asag
  name: defaults-0.2.8
spec:
  name: defaults
  version: 0.2.8
  onlyinclude: true
  includes:
    - vendor: asag
      name: lxc
      version: 0.2.8
  settings:
    - name: clusterid
      from:
        - uid
        - user
        - event
      required: true
      schema:
        type: string
        description: for use as clusterid
    - name: environment
      from:
        - env
        - user
      required: true
      schema:
        type: string
        description: the puppet environment
    - name: puppet_timeout
      from:
        - user
      schema:
        type: integer
        default: 300
        description: timeout for puppet agent during initial runs
  flags:
    - key: v1_clusterid
      from: setting
      setting: clusterid
      to: label
    - key: v1_puppetmaster
      from: env
      to: annotation
    - key: v1_environment
      from: setting
      setting: environment
      to: annotation
    - key: v1_datacenter
      from: env
      to: label
    - key: v1_customer_env
      from: env
      to: annotation
    - key: v1_level
      from: env
      to: label
    - key: puppet_timeout
      from: setting
      setting: puppet_timeout
      to: annotation
  limits:
    cpu: '2'
    ram: 2Gi
    rootfs: 10Gi
  clusters:
    - name: '*'
      sizing:
        specific:
          initial:
            - name: '*'
              labels:
                ops.aep.asag.io/maintenance: 'false'
          additional:
            labels:
              ops.aep.asag.io/maintenance: 'false'
      topologyqueries:
        - name: members
          format: array
          scope: instance
          groupby:
            - v1_role
      provides:
        - name: '*'
          settings:
            - name: dns_alt_names
              from:
                - user
              schema:
                type: array
                description: Subject Alternative Names for host certificates
                items:
                  type: string
                  format: hostname
          flags:
            - key: v1_dns_alt_names
              from: setting
              setting: dns_alt_names
              to: annotation
          servicespec:
            selector:
              ops.aep.asag.io/maintenance: 'false'
  podspec:
    containers:
      - name: '*'
        imagePullPolicy: Always
        readinessProbe:
          exec:
            command:
              - /usr/local/bin/pod_ready.sh
          failureThreshold: 3
          initialDelaySeconds: 30
          periodSeconds: 10
          successThreshold: 2
          timeoutSeconds: 10
        securityContext:
          privileged: false
        volumeMounts:
          - mountPath: /var/lib/aep/podinfo
            name: aep-podinfo
        env:
          - name: user-data
            value: "#cloud-config\nwrite_files:\n- path: '/usr/local/sbin/run_puppet.sh'\n\
              \  permissions: '0750'\n  owner: 'root:root'\n  content: |\n    #!/bin/bash\n\
              \    export LANG='C.UTF-8'\n    export PATH='/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/puppetlabs/bin'\n\
              \    DELAY_FINAL_CONFIG=\"$(grep -oP '^serviceflag\\.aep\\.asag\\.io/delay_final_config=\"\
              \\K[^\"]*' /var/lib/aep/podinfo/annotations)\"\n    if [ \"${DELAY_FINAL_CONFIG,,}\"\
              \ = 'true' ]; then\n      export AEP_HIDE_ROLE='y'\n    fi\n    HOST=\"\
              $(grep -oP '^dns\\.service\\.aep\\.asag.io/name=\"\\K[^\"]*' /var/lib/aep/podinfo/labels)\"\
              \n    FQDN=\"$(grep -oP '^dns\\.service\\.aep\\.asag.io/fqdn=\"\\K[^\"\
              ]*' /var/lib/aep/podinfo/labels)\"\n    ADDR=\"$(ip -br a s eth0 | awk\
              \ '{sub(/\\/.*/,\"\",$3); print $3}')\"\n    if [ -z \"$ADDR\" ]; then\n\
              \      echo 'Cannot find eth0 IP address.' 1>&2\n      exit 1\n    fi\n\
              \    cat <<EOF >/etc/hosts\n    # /etc/hosts\n    127.0.0.1 localhost\n\
              \    ${ADDR} ${FQDN} ${HOST}\n    178.250.132.131 apt-1.asag.io apt-1\n\
              \    # The following lines are desirable for IPv6 capable hosts\n  \
              \  ::1 ip6-localhost ip6-loopback\n    fe00::0 ip6-localnet\n    ff00::0\
              \ ip6-mcastprefix\n    ff02::1 ip6-allnodes\n    ff02::2 ip6-allrouters\n\
              \    ff02::3 ip6-allhosts\n    EOF\n    PUPPETMASTER=\"$(grep -oP '^serviceflag\\\
              .aep\\.asag\\.io/v1_puppetmaster=\"\\K[^\"]*' /var/lib/aep/podinfo/annotations)\"\
              \n    ENVIRONMENT=\"$(grep -oP '^serviceflag\\.aep\\.asag\\.io/v1_environment=\"\
              \\K[^\"]*' /var/lib/aep/podinfo/annotations)\"\n    #TODO: service can\
              \ have zero or more endpoints\n    DNS_ALT_NAMES=\"$(grep -oP '^(dns\\\
              .service\\.aep\\.asag\\.io/endpoint\\.fqdn\\..*|serviceflag\\.aep\\\
              .asag\\.io/.*/v1_dns_alt_names)=\"\\K[^\"]*' /var/lib/aep/podinfo/annotations\
              \ | tr ' \\n' ',')\"\n    if [ -z \"${DNS_ALT_NAMES:-}\" ]; then\n \
              \     DNS_ALT_NAMES=\"$FQDN\"\n    else\n      DNS_ALT_NAMES=\"${FQDN},${DNS_ALT_NAMES}\"\
              \n    fi\n    cat <<EOF >/etc/puppetlabs/puppet/puppet.conf\n    [main]\n\
              \      environment = ${ENVIRONMENT}\n    [agent]\n      server = ${PUPPETMASTER}\n\
              \      ca_server = ${PUPPETMASTER}\n      dns_alt_names = ${DNS_ALT_NAMES}\n\
              \      preferred_serialization_format = pson\n    EOF\n    chmod 0750\
              \ /etc/puppetlabs/puppet/puppet.conf\n    agent_timeout=\"$(facter v1_aep.puppet_timeout)\"\
              \n    if grep -Eq '^serviceflag\\.aep\\.asag\\.io/operator_sshkey='\
              \ /var/lib/aep/podinfo/annotations; then\n      grep -oP '^serviceflag\\\
              .aep.asag\\.io/operator_sshkey=\"\\K[^\"]*' /var/lib/aep/podinfo/annotations\
              \ >/root/.ssh/auth.operator\n    fi\n    for i in {1..10}; do\n    \
              \  rc='0'\n      echo \"iteration: ${i}\"\n      apt-get -q -y update\
              \ >/dev/null\n      puppet agent --enable\n      timeout \"$agent_timeout\"\
              \ puppet agent --test && break\n      rc=\"$?\"\n      puppet agent\
              \ --disable 'initial run'\n      echo \"puppet run finished with status\
              \ $rc\"\n      [ \"$rc\" -eq 1 ] && sleep 30   # another puppet run\
              \ already in progress\n      rc='1'\n    done\n    if [ \"${DELAY_FINAL_CONFIG,,}\"\
              \ = 'true' ]; then\n      rm -f /etc/cron.d/puppet\n      unset AEP_HIDE_ROLE\n\
              \      timeout \"$agent_timeout\" puppet agent --test --noop || true\n\
              \    fi\n    exit \"$rc\"\nruncmd:\n- /usr/local/bin/write_resolvconf.rb\n\
              - /usr/local/sbin/run_puppet.sh\n"
    volumes:
      - name: aep-podinfo
        downwardAPI:
          items:
            - path: labels
              fieldRef:
                fieldPath: metadata.labels
            - path: annotations
              fieldRef:
                fieldPath: metadata.annotations
            - path: name
              fieldRef:
                fieldPath: metadata.name
            - path: namespace
              fieldRef:
                fieldPath: metadata.namespace
