Mohammed Naser | 8a2c8fb | 2023-02-19 17:23:55 +0000 | [diff] [blame] | 1 | {{/* |
| 2 | Expand the name of the chart. |
| 3 | */}} |
| 4 | {{- define "vector.name" -}} |
| 5 | {{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }} |
| 6 | {{- end }} |
| 7 | |
| 8 | {{/* |
| 9 | Create a default fully qualified app name. |
| 10 | We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec). |
| 11 | If release name contains chart name it will be used as a full name. |
| 12 | */}} |
| 13 | {{- define "vector.fullname" -}} |
| 14 | {{- if .Values.fullnameOverride }} |
| 15 | {{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }} |
| 16 | {{- else }} |
| 17 | {{- $name := default .Chart.Name .Values.nameOverride }} |
| 18 | {{- if contains $name .Release.Name }} |
| 19 | {{- .Release.Name | trunc 63 | trimSuffix "-" }} |
| 20 | {{- else }} |
| 21 | {{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }} |
| 22 | {{- end }} |
| 23 | {{- end }} |
| 24 | {{- end }} |
| 25 | |
| 26 | {{/* |
| 27 | Create chart name and version as used by the chart label. |
| 28 | */}} |
| 29 | {{- define "vector.chart" -}} |
| 30 | {{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }} |
| 31 | {{- end }} |
| 32 | |
| 33 | {{/* |
| 34 | Common labels. |
| 35 | */}} |
| 36 | {{- define "vector.labels" -}} |
| 37 | helm.sh/chart: {{ include "vector.chart" . }} |
| 38 | {{ include "vector.selectorLabels" . }} |
| 39 | {{- if .Chart.AppVersion }} |
| 40 | app.kubernetes.io/version: {{ .Values.image.tag | default .Chart.AppVersion | quote }} |
| 41 | {{- end }} |
| 42 | app.kubernetes.io/managed-by: {{ .Release.Service }} |
| 43 | {{ with .Values.commonLabels }} |
| 44 | {{- toYaml . -}} |
| 45 | {{- end }} |
| 46 | {{- end }} |
| 47 | |
| 48 | {{/* |
| 49 | Selector labels. |
| 50 | */}} |
| 51 | {{- define "vector.selectorLabels" -}} |
| 52 | app.kubernetes.io/name: {{ include "vector.name" . }} |
| 53 | app.kubernetes.io/instance: {{ .Release.Name }} |
| 54 | {{- if or (ne .Values.role "Agent") (ne .Values.role "Aggregator") (ne .Values.role "Stateless-Aggregator") }} |
| 55 | app.kubernetes.io/component: {{ .Values.role }} |
| 56 | {{- end }} |
| 57 | {{- end }} |
| 58 | |
| 59 | {{/* |
| 60 | Create the name of the service account to use. |
| 61 | */}} |
| 62 | {{- define "vector.serviceAccountName" -}} |
| 63 | {{- if .Values.serviceAccount.create }} |
| 64 | {{- default (include "vector.fullname" .) .Values.serviceAccount.name }} |
| 65 | {{- else }} |
| 66 | {{- default "default" .Values.serviceAccount.name }} |
| 67 | {{- end }} |
| 68 | {{- end }} |
| 69 | |
| 70 | {{/* |
| 71 | Return the appropriate apiVersion for PodDisruptionBudget policy APIs. |
| 72 | */}} |
| 73 | {{- define "policy.poddisruptionbudget.apiVersion" -}} |
| 74 | {{- if or (.Capabilities.APIVersions.Has "policy/v1/PodDisruptionBudget") (semverCompare ">=1.21" .Capabilities.KubeVersion.Version) -}} |
| 75 | "policy/v1" |
| 76 | {{- else -}} |
| 77 | "policy/v1beta1" |
| 78 | {{- end -}} |
| 79 | {{- end -}} |
| 80 | |
| 81 | {{/* |
| 82 | Return the appropriate apiVersion for HPA autoscaling APIs. |
| 83 | */}} |
| 84 | {{- define "autoscaling.apiVersion" -}} |
| 85 | {{- if or (.Capabilities.APIVersions.Has "autoscaling/v2/HorizontalPodAutoscaler") (semverCompare ">=1.23" .Capabilities.KubeVersion.Version) -}} |
| 86 | "autoscaling/v2" |
| 87 | {{- else -}} |
| 88 | "autoscaling/v2beta2" |
| 89 | {{- end -}} |
| 90 | {{- end -}} |
| 91 | |
| 92 | {{/* |
| 93 | Generate an array of ServicePorts based on `.Values.customConfig`. |
| 94 | */}} |
| 95 | {{- define "vector.ports" -}} |
| 96 | {{- range $componentKind, $components := .Values.customConfig }} |
| 97 | {{- if eq $componentKind "sources" }} |
| 98 | {{- tuple $components "_helper.generatePort" | include "_helper.componentIter" }} |
| 99 | {{- else if eq $componentKind "sinks" }} |
| 100 | {{- tuple $components "_helper.generatePort" | include "_helper.componentIter" }} |
| 101 | {{- else if eq $componentKind "api" }} |
| 102 | {{- if $components.enabled }} |
| 103 | - name: api |
| 104 | port: {{ mustRegexFind "[0-9]+$" (get $components "address") }} |
| 105 | protocol: TCP |
| 106 | targetPort: {{ mustRegexFind "[0-9]+$" (get $components "address") }} |
| 107 | {{- end }} |
| 108 | {{- end }} |
| 109 | {{- end }} |
| 110 | {{- end }} |
| 111 | |
| 112 | {{/* |
| 113 | Iterate over the components defined in `.Values.customConfig`. |
| 114 | */}} |
| 115 | {{- define "_helper.componentIter" -}} |
| 116 | {{- $components := index . 0 }} |
| 117 | {{- $helper := index . 1 }} |
| 118 | {{- range $id, $options := $components }} |
| 119 | {{- if (hasKey $options "address") }} |
| 120 | {{- tuple $id $options | include $helper -}} |
| 121 | {{- end }} |
| 122 | {{- end }} |
| 123 | {{- end }} |
| 124 | |
| 125 | {{/* |
| 126 | Generate a single ServicePort based on a component configuration. |
| 127 | */}} |
| 128 | {{- define "_helper.generatePort" -}} |
| 129 | {{- $name := index . 0 | kebabcase -}} |
| 130 | {{- $config := index . 1 -}} |
| 131 | {{- $port := mustRegexFind "[0-9]+$" (get $config "address") -}} |
| 132 | {{- $protocol := default "TCP" (get $config "mode" | upper) }} |
| 133 | - name: {{ $name }} |
| 134 | port: {{ $port }} |
| 135 | protocol: {{ $protocol }} |
| 136 | targetPort: {{ $port }} |
| 137 | {{- if not (mustHas $protocol (list "TCP" "UDP")) }} |
| 138 | {{ fail "Component's `mode` is not a supported protocol, please raise a issue at https://github.com/vectordotdev/vector" }} |
| 139 | {{- end }} |
| 140 | {{- end }} |
| 141 | |
| 142 | {{/* |
| 143 | Generate an array of ContainerPorts based on `.Values.customConfig`. |
| 144 | */}} |
| 145 | {{- define "vector.containerPorts" -}} |
| 146 | {{- range $componentKind, $components := .Values.customConfig }} |
| 147 | {{- if eq $componentKind "sources" }} |
| 148 | {{- tuple $components "_helper.generateContainerPort" | include "_helper.componentIter" }} |
| 149 | {{- else if eq $componentKind "sinks" }} |
| 150 | {{- tuple $components "_helper.generateContainerPort" | include "_helper.componentIter" }} |
| 151 | {{- else if eq $componentKind "api" }} |
| 152 | {{- if $components.enabled }} |
| 153 | - name: api |
| 154 | containerPort: {{ mustRegexFind "[0-9]+$" (get $components "address") }} |
| 155 | protocol: TCP |
| 156 | {{- end }} |
| 157 | {{- end }} |
| 158 | {{- end }} |
| 159 | {{- end }} |
| 160 | |
| 161 | {{/* |
| 162 | Generate a single ContainerPort based on a component configuration. |
| 163 | */}} |
| 164 | {{- define "_helper.generateContainerPort" -}} |
| 165 | {{- $name := index . 0 | kebabcase -}} |
| 166 | {{- $config := index . 1 -}} |
| 167 | {{- $port := mustRegexFind "[0-9]+$" (get $config "address") -}} |
| 168 | {{- $protocol := default "TCP" (get $config "mode" | upper) }} |
| 169 | - name: {{ $name | trunc 15 | trimSuffix "-" }} |
| 170 | containerPort: {{ $port }} |
| 171 | protocol: {{ $protocol }} |
| 172 | {{- if not (mustHas $protocol (list "TCP" "UDP")) }} |
| 173 | {{ fail "Component's `mode` is not a supported protocol, please raise a issue at https://github.com/vectordotdev/vector" }} |
| 174 | {{- end }} |
| 175 | {{- end }} |
| 176 | |
| 177 | {{/* |
| 178 | Print Vector's logo. |
| 179 | */}} |
| 180 | {{- define "_logo" -}} |
| 181 | {{ print "__ __ __" }} |
| 182 | {{ print "\\ \\ / / / /" }} |
| 183 | {{ print " \\ V / / / " }} |
| 184 | {{ print " \\_/ \\/ " }} |
| 185 | {{ print "V E C T O R" }} |
| 186 | {{- end }} |
| 187 | |
| 188 | {{/* |
| 189 | Print line divider. |
| 190 | */}} |
| 191 | {{- define "_divider" -}} |
| 192 | {{ print "--------------------------------------------------------------------------------" }} |
| 193 | {{- end }} |
| 194 | |
| 195 | {{/* |
| 196 | Print `vector top` instructions. |
| 197 | */}} |
| 198 | {{- define "_vector.top" -}} |
| 199 | {{- if eq "true" (include "_vector.apiEnabled" $) -}} |
| 200 | {{ print "Vector is starting in your cluster. After a few minutes, you can use Vector's" }} |
| 201 | {{ println "API to view internal metrics by running:" }} |
| 202 | {{- $resource := include "_vector.role" $ -}} |
| 203 | {{- $url := include "_vector.url" $ }} |
| 204 | $ kubectl -n {{ $.Release.Namespace }} exec -it {{ $resource }}/{{ include "vector.fullname" $ }} -- vector top {{ $url }} |
| 205 | {{- else -}} |
| 206 | {{- $resource := include "_vector.role" $ -}} |
| 207 | {{ print "Vector is starting in your cluster. After a few minutes, you can view Vector's" }} |
| 208 | {{ println "internal logs by running:" }} |
| 209 | $ kubectl -n {{ $.Release.Namespace }} logs -f {{ $resource }}/{{ include "vector.fullname" $ }} |
| 210 | {{- end }} |
| 211 | {{- end }} |
| 212 | |
| 213 | {{/* |
| 214 | Return `true` if we can determine if Vector's API is enabled. |
| 215 | */}} |
| 216 | {{- define "_vector.apiEnabled" -}} |
| 217 | {{- if $.Values.existingConfigMaps -}} |
| 218 | false |
| 219 | {{- else if $.Values.customConfig -}} |
| 220 | {{- if $.Values.customConfig.api -}} |
| 221 | {{- if $.Values.customConfig.api.enabled -}} |
| 222 | true |
| 223 | {{- end }} |
| 224 | {{- end }} |
| 225 | {{- else -}} |
| 226 | true |
| 227 | {{- end }} |
| 228 | {{- end }} |
| 229 | |
| 230 | {{/* |
| 231 | Return Vector's Resource type based on its `.Values.role`. |
| 232 | */}} |
| 233 | {{- define "_vector.role" -}} |
| 234 | {{- if eq $.Values.role "Stateless-Aggregator" -}} |
| 235 | deployment |
| 236 | {{- else if eq $.Values.role "Agent" -}} |
| 237 | daemonset |
| 238 | {{- else -}} |
| 239 | statefulset |
| 240 | {{- end -}} |
| 241 | {{- end }} |
| 242 | |
| 243 | {{/* |
| 244 | Print the `url` option for the Vector command. |
| 245 | */}} |
| 246 | {{- define "_vector.url" -}} |
| 247 | {{- if $.Values.customConfig -}} |
| 248 | {{- if $.Values.customConfig.api -}} |
| 249 | {{- if $.Values.customConfig.api.address -}} |
| 250 | {{ print "\\" }} |
| 251 | --url {{ printf "http://%s/graphql" $.Values.customConfig.api.address }} |
| 252 | {{- end }} |
| 253 | {{- end }} |
| 254 | {{- end }} |
| 255 | {{- end }} |
| 256 | |
| 257 | {{/* |
| 258 | Configuring Datadog Agents to forward to Vector. |
| 259 | This is really alpha level, and should be refactored |
| 260 | to be more generally usable for other components. |
| 261 | */}} |
| 262 | {{- define "_configure.datadog" -}} |
| 263 | {{- $hasSourceDatadogAgent := false }} |
| 264 | {{- $sourceDatadogAgentPort := "" }} |
| 265 | {{- $hasTls := "" }} |
| 266 | {{- $protocol := "http" }} |
| 267 | {{- range $componentKind, $configs := .Values.customConfig }} |
| 268 | {{- if eq $componentKind "sources" }} |
| 269 | {{- range $componentId, $componentConfig := $configs }} |
| 270 | {{- if eq (get $componentConfig "type") "datadog_agent" }} |
| 271 | {{- $hasSourceDatadogAgent = true }} |
| 272 | {{- $sourceDatadogAgentPort = mustRegexFind "[0-9]+$" (get $componentConfig "address") }} |
| 273 | {{- if (hasKey $componentConfig "tls") }} |
| 274 | {{- $tlsOpts := get $componentConfig "tls" }} |
| 275 | {{- $hasTls = get $tlsOpts "enabled" }} |
| 276 | {{- if $hasTls }}{{ $protocol = "https" }}{{ end }} |
| 277 | {{- end }} |
| 278 | {{- end }} |
| 279 | {{- end }} |
| 280 | {{- end }} |
| 281 | {{- end }} |
| 282 | {{- if or (not .Values.customConfig) (and .Values.customConfig $hasSourceDatadogAgent) }} |
| 283 | {{- template "_divider" }} |
| 284 | |
| 285 | {{ print "datadog_agent:" }} |
| 286 | |
| 287 | To forward logs from Datadog Agents deployed with the "datadog" Helm chart, |
| 288 | include the following in the "values.yaml" for your "datadog" chart: |
| 289 | |
| 290 | For Datadog Agents version "7.34"/"6.34" or lower: |
| 291 | |
| 292 | datadog: |
| 293 | containerExclude: "name:vector" |
| 294 | logs: |
| 295 | enabled: true |
| 296 | containerCollectAll: true |
| 297 | agents: |
| 298 | useConfigMap: true |
| 299 | customAgentConfig: |
| 300 | kubelet_tls_verify: false |
| 301 | logs_config: |
| 302 | {{- if .Values.haproxy.enabled }} |
| 303 | logs_dd_url: "{{ include "haproxy.fullname" $ }}.{{ $.Release.Namespace }}:{{ $sourceDatadogAgentPort | default "8282" }}" |
| 304 | {{- else }} |
| 305 | logs_dd_url: "{{ include "vector.fullname" $ }}.{{ $.Release.Namespace }}:{{ $sourceDatadogAgentPort | default "8282" }}" |
| 306 | {{- end }} |
| 307 | {{- if $hasTls }} |
| 308 | logs_no_ssl: false |
| 309 | {{- else }} |
| 310 | logs_no_ssl: true |
| 311 | {{- end }} |
| 312 | use_http: true |
| 313 | {{- end }} |
| 314 | |
| 315 | For Datadog Agents version "7.35"/"6.35" or greater: |
| 316 | |
| 317 | datadog: |
| 318 | containerExclude: "name:vector" |
| 319 | logs: |
| 320 | enabled: true |
| 321 | containerCollectAll: true |
| 322 | agents: |
| 323 | useConfigMap: true |
| 324 | customAgentConfig: |
| 325 | kubelet_tls_verify: false |
| 326 | vector: |
| 327 | logs: |
| 328 | enabled: true |
| 329 | {{- if .Values.haproxy.enabled }} |
| 330 | url: "{{ $protocol }}://{{ include "haproxy.fullname" $ }}.{{ $.Release.Namespace }}:{{ $sourceDatadogAgentPort | default "8282" }}" |
| 331 | {{- else }} |
| 332 | url: "{{ $protocol }}://{{ include "vector.fullname" $ }}.{{ $.Release.Namespace }}:{{ $sourceDatadogAgentPort | default "8282" }}" |
| 333 | {{- end }} |
| 334 | metrics: |
| 335 | enabled: true |
| 336 | {{- if .Values.haproxy.enabled }} |
| 337 | url: "{{ $protocol }}://{{ include "haproxy.fullname" $ }}.{{ $.Release.Namespace }}:{{ $sourceDatadogAgentPort | default "8282" }}" |
| 338 | {{- else }} |
| 339 | url: "{{ $protocol }}://{{ include "vector.fullname" $ }}.{{ $.Release.Namespace }}:{{ $sourceDatadogAgentPort | default "8282" }}" |
| 340 | {{- end }} |
| 341 | {{- end }} |