Skip to content

Commit

Permalink
add hostIP as env to server-statefulset and substitute env variables …
Browse files Browse the repository at this point in the history
…in extraConfig (#1042)

* add host_ip as an env variable, evaluate HOST_IP/POD_IP/HOSTNAME at runtime and add new config file for server and client

Co-authored-by: Ashwin Venkatesh <ashwin@hashicorp.com>
  • Loading branch information
kschoche and thisisnotashwin authored Jul 21, 2021
1 parent 3d235a8 commit b1b21dd
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Unreleased

IMPROVEMENTS:
* Substitute `HOST_IP/POD_IP/HOSTNAME` variables in `server.extraConfig` and `client.extraConfig` so they are passed in to server/client config already evaluated at runtime. [[GH-1042](https://github.com/hashicorp/consul-helm/pull/1042)]
* Set failurePolicy to Fail for connectInject mutating webhook so that pods fail to schedule when the webhook is offline. This can be controlled via `connectInject.failurePolicy`. [[GH-1024](https://github.com/hashicorp/consul-helm/pull/1024)]
* Allow setting global.logLevel and global.logJSON and propogate this to all consul-k8s commands. [[GH-980](https://github.com/hashicorp/consul-helm/pull/980)]
* Allow setting `connectInject.replicas` to control number of replicas of webhook injector. [[GH-1029](https://github.com/hashicorp/consul-helm/pull/1029)]
Expand Down
14 changes: 14 additions & 0 deletions templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ as well as the global.name setting.
{{- end -}}
{{- end -}}

{{/*
Sets up the extra-from-values config file passed to consul and then uses sed to do any necessary
substitution for HOST_IP/POD_IP/HOSTNAME. Useful for dogstats telemetry. The output file
is passed to consul as a -config-file param on command line.
*/}}
{{- define "consul.extraconfig" -}}
mkdir -p /consul/extra-config
cp /consul/config/extra-from-values.json /consul/extra-config/extra-from-values.json
[ -n "${HOST_IP}" ] && sed -Ei "s|HOST_IP|${HOST_IP?}|g" /consul/extra-config/extra-from-values.json
[ -n "${POD_IP}" ] && sed -Ei "s|POD_IP|${POD_IP?}|g" /consul/extra-config/extra-from-values.json
[ -n "${HOSTNAME}" ] && sed -Ei "s|HOSTNAME|${HOSTNAME?}|g" /consul/extra-config/extra-from-values.json
{{- end -}}


{{/*
Create chart name and version as used by the chart label.
*/}}
Expand Down
3 changes: 3 additions & 0 deletions templates/client-daemonset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ spec:
- |
CONSUL_FULLNAME="{{template "consul.fullname" . }}"
{{ template "consul.extraconfig" }}
exec /bin/consul agent \
-node="${NODE}" \
-advertise="${ADVERTISE_IP}" \
Expand Down Expand Up @@ -261,6 +263,7 @@ spec:
{{- range $value := .Values.global.recursors }}
-recursor={{ quote $value }} \
{{- end }}
-config-file=/consul/extra-config/extra-from-values.json \
-domain={{ .Values.global.domain }}
volumeMounts:
- name: data
Expand Down
7 changes: 7 additions & 0 deletions templates/server-statefulset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ spec:
{{- else }}
fieldPath: status.podIP
{{- end }}
- name: HOST_IP
valueFrom:
fieldRef:
fieldPath: status.hostIP
- name: POD_IP
valueFrom:
fieldRef:
Expand Down Expand Up @@ -176,6 +180,8 @@ spec:
- |
CONSUL_FULLNAME="{{template "consul.fullname" . }}"
{{ template "consul.extraconfig" }}
exec /bin/consul agent \
-advertise="${ADVERTISE_IP}" \
-bind=0.0.0.0 \
Expand Down Expand Up @@ -236,6 +242,7 @@ spec:
{{- range $value := .Values.global.recursors }}
-recursor={{ quote $value }} \
{{- end }}
-config-file=/consul/extra-config/extra-from-values.json \
-server
volumeMounts:
- name: data-{{ .Release.Namespace }}
Expand Down
8 changes: 4 additions & 4 deletions test/unit/server-statefulset.bats
Original file line number Diff line number Diff line change
Expand Up @@ -837,19 +837,19 @@ load _helpers
yq -r '.spec.template.spec.containers[0].env' | tee /dev/stderr)

local actual=$(echo $object |
yq -r '.[3].name' | tee /dev/stderr)
yq -r '.[4].name' | tee /dev/stderr)
[ "${actual}" = "custom_proxy" ]

local actual=$(echo $object |
yq -r '.[3].value' | tee /dev/stderr)
yq -r '.[4].value' | tee /dev/stderr)
[ "${actual}" = "fakeproxy" ]

local actual=$(echo $object |
yq -r '.[4].name' | tee /dev/stderr)
yq -r '.[5].name' | tee /dev/stderr)
[ "${actual}" = "no_proxy" ]

local actual=$(echo $object |
yq -r '.[4].value' | tee /dev/stderr)
yq -r '.[5].value' | tee /dev/stderr)
[ "${actual}" = "custom_no_proxy" ]
}

Expand Down

3 comments on commit b1b21dd

@eb4x
Copy link
Contributor

@eb4x eb4x commented on b1b21dd Jun 24, 2022

Choose a reason for hiding this comment

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

I'm getting mkdir: can't create directory '/consul/extra-config': Permission denied in my openshift cluster.

@david-yu
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you file an issue on this @eb4x

@eb4x
Copy link
Contributor

@eb4x eb4x commented on b1b21dd Jun 25, 2022

Choose a reason for hiding this comment

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

Could you file an issue on this @eb4x

Issue #1306 with proposed fix #1307.

Please sign in to comment.