Skip to content

Commit

Permalink
Logic to generate default cluster_name if not supplied (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
mitali-salvi authored Feb 8, 2024
1 parent a6aee9b commit 0bb89bd
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/operator-integration-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ jobs:
- name: Test case for AmazonCloudWatchAgent pod creation
run: |
helm template --namespace amazon-cloudwatch -s templates/cloudwatch-agent-daemonset.yaml ./helm | kubectl apply --namespace amazon-cloudwatch -f -
helm template --namespace amazon-cloudwatch -s templates/cloudwatch-agent-daemonset.yaml ./helm --set region=us-west-2 | kubectl apply --namespace amazon-cloudwatch -f -
sleep 60
kubectl describe pods -n amazon-cloudwatch
pod_name="$(kubectl get pods -n amazon-cloudwatch -l app.kubernetes.io/component=amazon-cloudwatch-agent,app.kubernetes.io/instance=amazon-cloudwatch.cloudwatch-agent -o=jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}')"
Expand Down
64 changes: 62 additions & 2 deletions helm/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,66 @@ Expand the name of the chart.
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
{{- 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 }}
{{- end }}

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

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

{{- $appSignals := pluck "app_signals" $configCopy.logs.metrics_collected | first }}
{{- if empty $appSignals.hosted_in }}
{{- $appSignals := set $appSignals "hosted_in" (include "kubernetes-cluster.name" .) }}
{{- end }}

{{- $containerInsights := pluck "kubernetes" $configCopy.logs.metrics_collected | first }}
{{- if empty $containerInsights.cluster_name }}
{{- $containerInsights := set $containerInsights "cluster_name" (include "kubernetes-cluster.name" .) }}
{{- end }}

{{- default "" $configCopy | toJson | quote }}
{{- end }}

{{/*
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")) }}
{{- include "cloudwatch-agent.config-modifier" . }}
{{- else }}
{{- default "" .Values.agent.config | toJson | quote }}
{{- end }}
{{- 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 }}

{{/*
Name for cloudwatch-agent
*/}}
Expand Down Expand Up @@ -56,7 +116,7 @@ Common labels
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: EKS
app.kubernetes.io/managed-by: "amazon-cloudwatch-agent-operator"
{{- end }}

{{/*
Expand Down Expand Up @@ -113,4 +173,4 @@ Define the default service name
*/}}
{{- define "amazon-cloudwatch-observability.webhookServiceName" -}}
{{- default (printf "%s-webhook-service" (include "amazon-cloudwatch-observability.name" .)) .Values.manager.service.name }}
{{- end -}}
{{- end -}}
7 changes: 5 additions & 2 deletions helm/templates/cloudwatch-agent-daemonset.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{{- if .Values.agent.enabled }}
{{- if empty .Values.region }}
{{- fail "region is a required field" }}
{{- end }}
apiVersion: cloudwatch.aws.amazon.com/v1alpha1
kind: AmazonCloudWatchAgent
metadata:
Expand All @@ -9,9 +12,9 @@ spec:
mode: daemonset
serviceAccount: {{ template "cloudwatch-agent.serviceAccountName" . }}
{{- if .Values.agent.config }}
config: {{ .Values.agent.config | toJson | quote }}
config: {{ template "cloudwatch-agent.supplied-config" . }}
{{- else }}
config: {{ .Values.agent.defaultConfig | toJson | quote }}
config: {{ template "cloudwatch-agent.modify-default-config" . }}
{{- end }}
resources:
requests:
Expand Down
5 changes: 4 additions & 1 deletion helm/templates/fluent-bit-daemonset.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{{- if empty .Values.region }}
{{- fail "region is a required field" }}
{{- end }}
apiVersion: apps/v1
kind: DaemonSet
metadata:
Expand Down Expand Up @@ -28,7 +31,7 @@ spec:
- name: AWS_REGION
value: {{ .Values.region }}
- name: CLUSTER_NAME
value: {{ .Values.clusterName | quote }}
value: {{ include "kubernetes-cluster.name" . }}
- name: READ_FROM_HEAD
value: "Off"
- name: READ_FROM_TAIL
Expand Down
8 changes: 4 additions & 4 deletions helm/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ nameOverride: ""
## Reference one or more secrets to be used when pulling images from authenticated repositories.
imagePullSecrets: [ ]

## Provide the ClusterName (optional when installing via EKS add-on)
clusterName: EKS_CLUSTER_NAME
## Provide the ClusterName
clusterName:

## Provide the Region (optional when installing via EKS add-on)
region: AWS_REGION_NAME
## Provide the Region
region:

containerLogs:
enabled: true
Expand Down
6 changes: 5 additions & 1 deletion integration-tests/terraform/helm/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,11 @@ resource "helm_release" "this" {
name = "amazon-cloudwatch-observability"
namespace = "amazon-cloudwatch"
create_namespace = true
chart = "${var.helm_dir}"
chart = "${var.helm_dir}"
set {
name = "region"
value = "${var.region}"
}
}

resource "null_resource" "validator" {
Expand Down

0 comments on commit 0bb89bd

Please sign in to comment.