Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logic to generate default cluster_name if not supplied #80

Merged
merged 6 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)) }}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the release name unique enough for our purposes here? Is a hash good enough or do we need randomness?
If the same release is installed on multiple clusters with the same name, wont all the clusters have the same hash?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good point, and we should add some randomness to it.

{{- else }}
{{- default "" .Values.clusterName }}
Comment on lines +12 to +15
Copy link
Contributor

@sky333999 sky333999 Feb 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isnt lines 12-16 the same as

{{- default (printf "k8s-cluster-%s" (sha256sum .Release.Name | trunc 7)) .Values.clusterName }}

?

{{- end }}
{{- end }}

{{/*
Helper function to modify cloudwatch-agent config
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whats diff between the two funcs below?
I would have expected a generic helper function that takes a config (either user provided or default) as an input and then just merges region & cluster name on top of that.

.Values.agent.config | injectRegionAndClusterName | toJson | quote
.Values.agent.defaultConfig | injectRegionAndClusterName | toJson | quote

*/}}
{{- 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 }}
Comment on lines +1 to +3
Copy link
Contributor

@sky333999 sky333999 Feb 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the built in required func, Lines 1-3 can instead be:

{{- $region := .Values.region | required ".Values.region is required." -}}

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" . }}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@movence doesnt this bring back the bug you fixed for numeric only cluster names not being quoted?

- 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we keep the comments on these saying this is required and the cluster name is optional and we generate one if not provdied

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
Loading