Skip to content

Commit

Permalink
Set addresses when using external servers/agentless
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Eckert committed Nov 10, 2022
1 parent 706fb91 commit b81790c
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 5 deletions.
12 changes: 10 additions & 2 deletions charts/consul/templates/api-gateway-controller-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,18 @@ spec:
value: "/consul/login/acl-token"
{{- end }}
- name: CONSUL_HTTP_ADDR
{{- if .Values.externalServers.enabled }}
{{- if .Values.global.tls.enabled }}
value: https://$(HOST_IP):8501
value: {{ first .Values.externalServers.hosts }}:{{ .Values.externalServers.httpsPort }}
{{- else }}
value: http://$(HOST_IP):8500
value: {{ first .Values.externalServers.hosts }}:{{ .Values.externalServers.httpPort }}
{{- end }}
{{- else }}
{{- if .Values.global.tls.enabled }}
value: {{ template "consul.fullname" . }}-server:8501
{{- else }}
value: {{ template "consul.fullname" . }}-server:8500
{{- end }}
{{- end }}
command:
- "/bin/sh"
Expand Down
20 changes: 17 additions & 3 deletions charts/consul/templates/api-gateway-gatewayclassconfig.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ metadata:
component: api-gateway
spec:
consul:
{{- if .Values.externalServers.enabled }}
address: {{ first .Values.externalServers.hosts }}
{{- else }}
address: {{ template "consul.fullname" . }}-server
{{- end }}
authentication:
{{- if .Values.global.acls.manageSystemACLs }}
managed: true
Expand All @@ -25,12 +30,21 @@ spec:
scheme: http
{{- end }}
ports:
{{- if .Values.externalServers.enabled }}
grpc: {{ .Values.externalServers.grpcPort }}
{{- if .Values.global.tls.enabled }}
http: {{ .Values.externalServers.httpsPort }}
{{- else }}
http: {{ .Values.externalServers.httpPort }}
{{- end }}
{{- else }}
grpc: 8502
{{- if .Values.global.tls.enabled }}
{{- if .Values.global.tls.enabled }}
http: 8501
{{- else }}
{{- else }}
http: 8500
{{- end }}
{{- end }}
{{- end }}
{{- with .Values.apiGateway.managedGatewayClass.deployment }}
deployment:
{{- toYaml . | nindent 4 }}
Expand Down
60 changes: 60 additions & 0 deletions charts/consul/test/unit/api-gateway-controller-deployment.bats
Original file line number Diff line number Diff line change
Expand Up @@ -1131,3 +1131,63 @@ load _helpers

[[ "$output" =~ "When either global.cloud.scadaAddress.secretName or global.cloud.scadaAddress.secretKey is defined, both must be set." ]]
}

#--------------------------------------------------------------------
# CONSUL_HTTP_ADDR

@test "apiGateway/Deployment: CONSUL_HTTP_ADDR set correctly when using external servers and TLS." {
cd `chart_dir`
local actual=$(helm template \
-s templates/api-gateway-controller-deployment.yaml \
--set 'apiGateway.enabled=true' \
--set 'apiGateway.image=bar' \
--set 'global.tls.enabled=true' \
--set 'externalServers.enabled=true' \
--set 'externalServers.hosts[0]=external-consul.host' \
--set 'externalServers.httpsPort=8501' \
--set 'server.enabled=false' \
. | tee /dev/stderr |
yq '[.spec.template.spec.containers[0].env[2].value] | any(contains("external-consul.host:8501"))' | tee /dev/stderr)
[ "${actual}" = "true" ]
}

@test "apiGateway/Deployment: CONSUL_HTTP_ADDR set correctly when using external servers and no-TLS." {
cd `chart_dir`
local actual=$(helm template \
-s templates/api-gateway-controller-deployment.yaml \
--set 'apiGateway.enabled=true' \
--set 'apiGateway.image=bar' \
--set 'global.tls.enabled=false' \
--set 'externalServers.enabled=true' \
--set 'externalServers.hosts[0]=external-consul.host' \
--set 'externalServers.httpPort=8500' \
--set 'server.enabled=false' \
. | tee /dev/stderr |
yq '[.spec.template.spec.containers[0].env[1].value] | any(contains("external-consul.host:8500"))' | tee /dev/stderr)
[ "${actual}" = "true" ]
}

@test "apiGateway/Deployment: CONSUL_HTTP_ADDR set correctly when not using external servers and using TLS." {
cd `chart_dir`
local actual=$(helm template \
-s templates/api-gateway-controller-deployment.yaml \
--set 'apiGateway.enabled=true' \
--set 'apiGateway.image=bar' \
--set 'global.tls.enabled=true' \
. | tee /dev/stderr |
yq '[.spec.template.spec.containers[0].env[2].value] | any(contains("release-name-consul-server:8501"))' | tee /dev/stderr)
[ "${actual}" = "true" ]
}

@test "apiGateway/Deployment: CONSUL_HTTP_ADDR set correctly when not using external servers or TLS." {
cd `chart_dir`
local actual=$(helm template \
-s templates/api-gateway-controller-deployment.yaml \
--set 'apiGateway.enabled=true' \
--set 'apiGateway.image=bar' \
--set 'global.tls.enabled=false' \
. | tee /dev/stderr |
yq '[.spec.template.spec.containers[0].env[1].value] | any(contains("release-name-consul-server:8500"))' | tee /dev/stderr)
[ "${actual}" = "true" ]
}

77 changes: 77 additions & 0 deletions charts/consul/test/unit/api-gateway-gatewayclassconfig.bats
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,80 @@ load _helpers
yq '.spec.deployment.minInstances == 3' | tee /dev/stderr)
[ "${actual}" = "true" ]
}

#--------------------------------------------------------------------
# Consul server address

@test "apiGateway/GatewayClassConfig: Consul server address set when using external servers." {
cd `chart_dir`
local actual=$(helm template \
-s templates/api-gateway-gatewayclassconfig.yaml \
--set 'apiGateway.enabled=true' \
--set 'apiGateway.image=foo' \
--set 'externalServers.enabled=true' \
--set 'externalServers.hosts[0]=external-consul.host' \
--set 'server.enabled=false' \
. | tee /dev/stderr |
yq '.spec.consul.address == "external-consul.host"' | tee /dev/stderr)
[ "${actual}" = "true" ]
}

@test "apiGateway/GatewayClassConfig: Consul server address set when not using external servers." {
cd `chart_dir`
local actual=$(helm template \
-s templates/api-gateway-gatewayclassconfig.yaml \
--set 'apiGateway.enabled=true' \
--set 'apiGateway.image=foo' \
. | tee /dev/stderr |
yq '.spec.consul.address == "release-name-consul-server"' | tee /dev/stderr)
[ "${actual}" = "true" ]
}

#--------------------------------------------------------------------
# externalServers ports

@test "apiGateway/GatewayClassConfig: ports for externalServers when not using TLS." {
cd `chart_dir`
local ports=$(helm template \
-s templates/api-gateway-gatewayclassconfig.yaml \
--set 'apiGateway.enabled=true' \
--set 'apiGateway.image=foo' \
--set 'global.tls.enabled=false' \
--set 'externalServers.enabled=true' \
--set 'externalServers.hosts[0]=external-consul.host' \
--set 'externalServers.grpcPort=1234' \
--set 'externalServers.httpPort=5678' \
--set 'server.enabled=false' \
. | tee /dev/stderr |
yq '.spec.consul.ports' | tee /dev/stderr)

local actual
actual=$(echo $ports | jq -r '.grpc' | tee /dev/stderr)
[ "${actual}" = "1234" ]

actual=$(echo $ports | jq -r '.http' | tee /dev/stderr)
[ "${actual}" = "5678" ]
}

@test "apiGateway/GatewayClassConfig: ports for externalServers when using TLS." {
cd `chart_dir`
local ports=$(helm template \
-s templates/api-gateway-gatewayclassconfig.yaml \
--set 'apiGateway.enabled=true' \
--set 'apiGateway.image=foo' \
--set 'global.tls.enabled=true' \
--set 'externalServers.enabled=true' \
--set 'externalServers.hosts[0]=external-consul.host' \
--set 'externalServers.grpcPort=1234' \
--set 'externalServers.httpsPort=5678' \
--set 'server.enabled=false' \
. | tee /dev/stderr |
yq '.spec.consul.ports' | tee /dev/stderr)

local actual
actual=$(echo $ports | jq -r '.grpc' | tee /dev/stderr)
[ "${actual}" = "1234" ]

actual=$(echo $ports | jq -r '.http' | tee /dev/stderr)
[ "${actual}" = "5678" ]
}
3 changes: 3 additions & 0 deletions charts/consul/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1149,6 +1149,9 @@ externalServers:
# @type: array<string>
hosts: []

# The HTTP port of the Consul servers.
httpPort: 8500

# The HTTPS port of the Consul servers.
httpsPort: 8501

Expand Down

0 comments on commit b81790c

Please sign in to comment.