# _helpers.tpl
{{/* vim: set filetype=mustache: */}}
{{/*
Expand the name of the chart.
*/}}
{{- define "msoms.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
*/}}
{{- define "msoms.fullname" -}}
{{- $name := default .Chart.Name .Values.nameOverride -}}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}}
{{- end -}}
 # omsagent-daemonset.yaml
{{- if and (ne .Values.omsagent.secret.key "<your_workspace_key>") (ne .Values.omsagent.secret.wsid "<your_workspace_id>")}}
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: {{ template "msoms.fullname" . }}
  labels:
    app: {{ template "msoms.name" . }}
    chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
spec:
  updateStrategy:
    type: RollingUpdate
  selector:
    matchLabels:
      app: {{ template "msoms.name" . }}
      release: {{ .Release.Name }}
  template:
    metadata:
    labels:
      release: {{ .Release.Name }}
      app: {{ template "msoms.name" .   }}
    annotations:
      agentVersion: {{ .Values.omsagent.image.tag }}
      dockerProviderVersion: {{ .Values.omsagent.image.dockerProviderVersion }}
    spec:
    containers:
    - name: {{ template "msoms.name" . }}
      image: "microsoft/oms:{{ .Values.omsagent.image.tag }}"
      imagePullPolicy: {{ .Values.omsagent.image.pullPolicy }}
      resources:
{{ toYaml .Values.resources | indent 8 }}
      securityContext:
        privileged: true
      ports:
      - containerPort: 25225
        protocol: TCP
      - containerPort: 25224
        protocol: UDP
      volumeMounts:
        - mountPath: /var/run/docker.sock
          name: docker-sock
        - mountPath: /var/log
          name: host-log
        - mountPath: /var/lib/docker/containers
          name: containerlog-path
        - mountPath: /etc/omsagent-secret
          name: omsagent-secret
          readOnly: true
      livenessProbe:
        exec:
        command:
        - /bin/bash
        - -c
        - ps -ef | pgrep omsagent
        initialDelaySeconds: 60
        periodSeconds: 60
    volumes:
    - name: docker-sock
      hostPath:
      path: /var/run/docker.sock
    - name: host-log
      hostPath:
      path: /var/log
    - name: containerlog-path
      hostPath:
      path: /var/lib/docker/containers
    - name: omsagent-secret
      secret:
      secretName: {{ template "msoms.fullname" .}}
  {{- if .Values.nodeSelector }}
    nodeSelector:
  {{ toYaml .Values.nodeSelector | indent 4 }}
  {{- end }}
    # Tolerate a NoSchedule taint on master that ACS Engine sets.
    tolerations:
    - key: "node-role.kubernetes.io/master"
      operator: "Equal"
      value: "true"
      effect: "NoSchedule"
  {{- if .Values.tolerations }}
  {{ toYaml .Values.tolerations | indent 4 }}
  {{- end }}
  {{- end }}
 # omsagent-secret.yaml
apiVersion: v1
kind: Secret
metadata:
  name: {{ template "msoms.fullname" . }}
  labels:
    app: {{ template "msoms.name" . }}
    chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
type: Opaque
data:
  WSID: {{ required "A valid workspace id is required!" .Values.omsagent.secret.wsid | b64enc | quote }}
  KEY: {{ required "A valid workspace key is required!" .Values.omsagent.secret.key | b64enc | quote }}
  DOMAIN: {{ .Values.omsagent.domain | b64enc | quote }}

## Default values for omsagent.

## Microsoft OMS Agent image
## ref: https://github.com/Microsoft/OMS-docker/blob/master/ReleaseNote.md
omsagent:
  image:
    tag: 1.4.3-174
    pullPolicy: IfNotPresent
    dockerProviderVersion: 1.0.0-30
## To get your workspace id and key do the following
## - In the Azure Log Analytics portal, on the Overview page, click the Settings tile. Click the Connected Sources tab at the left and select "Linux Server" on the right hand side.
## - On the right of Workspace ID, click the copy icon and paste the ID into Notepad.
## - On the right of Primary Key, click the copy icon and paste the key into Notepad.
## For more information, please go [here.](https://docs.microsoft.com/en-us/azure/log-analytics/log-analytics-windows-agents#download-the-agent-setup-file-from-oms)

  secret:
    wsid: <your_workspace_id>
    key: <your_workspace_key>
  domain: opinsights.azure.com

## Configure resource requests and limits
## ref: http://kubernetes.io/docs/user-guide/compute-resources/
##
resources:
  requests:
    cpu: 100m
    memory: 512Mi
  limits:
    cpu: 500m
    memory: 768Mi

# Node selector - default to Linux as cannot deploy OMS agent container to Windows nodes
nodeSelector:
  beta.kubernetes.io/os: linux

# optional tolerations to handle custom taints
tolerations: []
