Skip to content

Commit

Permalink
Passing cluster-name as a string in the fluentbit daemon-set (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
mitali-salvi committed Feb 28, 2024
1 parent 2f0b66d commit 10179a5
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 35 deletions.
47 changes: 23 additions & 24 deletions helm/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,37 @@ Expand the name of the chart.
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Function to generate a random name and store it in .Release
*/}}
{{- define "generate_static_name_randomiser" -}}
{{- if not (index .Release "randomiser") -}}
{{- $_ := set .Release "randomiser" dict -}}
{{- end -}}
{{- $key := printf "%s_%s" .Release.Name "randomiser" -}}
{{- if not (index .Release.randomiser $key) -}}
{{- $_ := set .Release.randomiser $key (randAlphaNum 3) -}}
{{- end -}}
{{- index .Release.randomiser $key -}}
{{- end -}}

{{/*
Name of k8s cluster
*/}}
{{- define "kubernetes-cluster.name" -}}
{{- if empty .Values.clusterName }}
{{- default "" (printf "k8s-cluster-%s" (sha256sum .Release.Name | trunc 7)) }}
{{- else }}
{{- default "" .Values.clusterName }}
{{- end }}
{{- default (printf "k8s-cluster-%s-%s" (sha256sum .Release.Name | trunc 7) (include "generate_static_name_randomiser" .)) .Values.clusterName }}
{{- end }}

{{/*
Helper function to modify cloudwatch-agent config
*/}}
{{- define "cloudwatch-agent.config-modifier" -}}
{{- $configCopy := deepCopy .Values.agent.config }}
{{- $configCopy := deepCopy .Config }}

{{- $agent := pluck "agent" $configCopy | first }}
{{- if and (empty $agent) (empty $agent.region) }}
{{- $agent := set $agent "region" .Values.region }}
{{- $agentRegion := dict "region" .Values.region }}
{{- $agent := set $configCopy "agent" $agentRegion }}
{{- end }}

{{- $appSignals := pluck "app_signals" $configCopy.logs.metrics_collected | first }}
Expand All @@ -43,26 +54,12 @@ Helper function to modify cloudwatch-agent config
{{/*
Helper function to modify customer supplied agent config if ContainerInsights or ApplicationSignals is enabled
*/}}
{{- define "cloudwatch-agent.supplied-config" -}}
{{- if or (hasKey .Values.agent.config.logs "app_signals") (and (hasKey .Values.agent.config.logs "metrics_collected") (hasKey .Values.agent.config.logs.metrics_collected "kubernetes")) }}
{{- define "cloudwatch-agent.modify-config" -}}
{{- if or (hasKey .Config.logs "app_signals") (and (hasKey .Config.logs "metrics_collected") (hasKey .Config.logs.metrics_collected "kubernetes")) }}
{{- include "cloudwatch-agent.config-modifier" . }}
{{- else }}
{{- default "" .Values.agent.config | toJson | quote }}
{{- end }}
{{- default "" .Config | toJson | quote }}
{{- end }}

{{/*
Helper function to modify default agent config
*/}}
{{- define "cloudwatch-agent.modify-default-config" -}}
{{- $configCopy := deepCopy .Values.agent.defaultConfig }}
{{- $agentRegion := dict "region" .Values.region }}
{{- $agent := set $configCopy "agent" $agentRegion }}
{{- $appSignals := pluck "app_signals" $configCopy.logs.metrics_collected | first }}
{{- $appSignals := set $appSignals "hosted_in" (include "kubernetes-cluster.name" .) }}
{{- $containerInsights := pluck "kubernetes" $configCopy.logs.metrics_collected | first }}
{{- $containerInsights := set $containerInsights "cluster_name" (include "kubernetes-cluster.name" .) }}
{{- default "" $configCopy | toJson | quote }}
{{- end }}

{{/*
Expand All @@ -88,6 +85,7 @@ Get the current recommended cloudwatch agent image for a region
Get the current recommended cloudwatch agent operator image for a region
*/}}
{{- define "cloudwatch-agent-operator.image" -}}
{{- $region := .Values.region | required ".Values.region is required." -}}
{{- $imageDomain := "" -}}
{{- $imageDomain = index .Values.manager.image.repositoryDomainMap .Values.region -}}
{{- if not $imageDomain -}}
Expand All @@ -100,6 +98,7 @@ Get the current recommended cloudwatch agent operator image for a region
Get the current recommended fluent-bit image for a region
*/}}
{{- define "fluent-bit.image" -}}
{{- $region := .Values.region | required ".Values.region is required." -}}
{{- $imageDomain := "" -}}
{{- $imageDomain = index .Values.containerLogs.fluentBit.image.repositoryDomainMap .Values.region -}}
{{- if not $imageDomain -}}
Expand Down
8 changes: 3 additions & 5 deletions helm/templates/cloudwatch-agent-daemonset.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{{- if .Values.agent.enabled }}
{{- if empty .Values.region }}
{{- fail "region is a required field" }}
{{- end }}
{{- $region := .Values.region | required ".Values.region is required." -}}
apiVersion: cloudwatch.aws.amazon.com/v1alpha1
kind: AmazonCloudWatchAgent
metadata:
Expand All @@ -12,9 +10,9 @@ spec:
mode: daemonset
serviceAccount: {{ template "cloudwatch-agent.serviceAccountName" . }}
{{- if .Values.agent.config }}
config: {{ template "cloudwatch-agent.supplied-config" . }}
config: {{ include "cloudwatch-agent.modify-config" (merge (dict "Config" .Values.agent.config) . ) }}
{{- else }}
config: {{ template "cloudwatch-agent.modify-default-config" . }}
config: {{ include "cloudwatch-agent.modify-config" (merge (dict "Config" .Values.agent.defaultConfig) . ) }}
{{- end }}
resources:
requests:
Expand Down
6 changes: 2 additions & 4 deletions helm/templates/fluent-bit-daemonset.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
{{- if empty .Values.region }}
{{- fail "region is a required field" }}
{{- end }}
{{- $region := .Values.region | required ".Values.region is required." -}}
apiVersion: apps/v1
kind: DaemonSet
metadata:
Expand Down Expand Up @@ -31,7 +29,7 @@ spec:
- name: AWS_REGION
value: {{ .Values.region }}
- name: CLUSTER_NAME
value: {{ include "kubernetes-cluster.name" . }}
value: {{ (include "kubernetes-cluster.name" .) | quote }}
- name: READ_FROM_HEAD
value: "Off"
- name: READ_FROM_TAIL
Expand Down
4 changes: 2 additions & 2 deletions helm/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ nameOverride: ""
## Reference one or more secrets to be used when pulling images from authenticated repositories.
imagePullSecrets: [ ]

## Provide the ClusterName
## Provide the ClusterName (optional parameter, if not provided, a generated name will be used)
clusterName:

## Provide the Region
## Provide the Region (this is a required parameter)
region:

containerLogs:
Expand Down

0 comments on commit 10179a5

Please sign in to comment.