Skip to content

Commit

Permalink
support for HCP_SCADA_ADDRESS (#13)
Browse files Browse the repository at this point in the history
* support for HCP_SCADA_ADDRESS

* set scada address via env

* add server-statefuleset bats tests for HCP_SCADA_ADDRESS

* add scada address tests to cloud preset tests

Co-authored-by: John Murret <john.murret@hashicorp.com>
  • Loading branch information
hanshasselberg and jmurret committed Sep 27, 2022
1 parent 04ee440 commit 5e9b7bb
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 2 deletions.
5 changes: 5 additions & 0 deletions charts/consul/templates/server-statefulset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,11 @@ spec:
secretKeyRef:
name: {{ .Values.global.cloud.secretName }}
key: api-hostname
- name: HCP_SCADA_ADDRESS
valueFrom:
secretKeyRef:
name: {{ .Values.global.cloud.secretName }}
key: scada-address
{{- end }}
{{- include "consul.extraEnvironmentVars" .Values.server | nindent 12 }}
command:
Expand Down
20 changes: 18 additions & 2 deletions charts/consul/test/unit/server-statefulset.bats
Original file line number Diff line number Diff line change
Expand Up @@ -1923,7 +1923,7 @@ load _helpers
[ "${envvar}" = "" ]
}

@test "server/StatefulSet: does not create HCP_RESOURCE_ID, HCP_CLIENT_ID, HCP_CLIENT_SECRET, HCP_AUTH_URL, and HCP_API_HOSTNAME envvars in consul container when global.cloud.enabled is not set" {
@test "server/StatefulSet: does not create HCP_RESOURCE_ID, HCP_CLIENT_ID, HCP_CLIENT_SECRET, HCP_AUTH_URL, HCP_SCADA_ADDRESS, and HCP_API_HOSTNAME envvars in consul container when global.cloud.enabled is not set" {
cd `chart_dir`
local object=$(helm template \
-s templates/server-statefulset.yaml \
Expand Down Expand Up @@ -1953,6 +1953,10 @@ load _helpers
yq -r '.env[] | select(.name == "HCP_API_HOSTNAME")' | tee /dev/stderr)
[ "${envvar}" = "" ]

envvar=$(echo "$container" |
yq -r '.env[] | select(.name == "HCP_SCADA_ADDRESS")' | tee /dev/stderr)
[ "${envvar}" = "" ]

}

@test "server/StatefulSet: cloud config is set in command when global.cloud.enabled is set" {
Expand All @@ -1969,7 +1973,7 @@ load _helpers
}


@test "server/StatefulSet: creates HCP_RESOURCE_ID, HCP_CLIENT_ID, HCP_CLIENT_SECRET, HCP_AUTH_URL, and HCP_API_HOSTNAME envvars in consul container when global.cloud.enabled is set" {
@test "server/StatefulSet: creates HCP_RESOURCE_ID, HCP_CLIENT_ID, HCP_CLIENT_SECRET, HCP_AUTH_URL, HCP_SCADA_ADDRESS, and HCP_API_HOSTNAME envvars in consul container when global.cloud.enabled is set" {
cd `chart_dir`
local object=$(helm template \
-s templates/server-statefulset.yaml \
Expand Down Expand Up @@ -2039,6 +2043,18 @@ load _helpers
actual=$(echo "$envvar" |
yq -r '.valueFrom.secretKeyRef.key' | tee /dev/stderr)
[ "${actual}" = "api-hostname" ]

# HCP_SCADA_ADDRESS
envvar=$(echo "$container" |
yq -r '.env[] | select(.name == "HCP_SCADA_ADDRESS")' | tee /dev/stderr)

local actual=$(echo "$envvar" |
yq -r '.valueFrom.secretKeyRef.name' | tee /dev/stderr)
[ "${actual}" = "foo" ]

actual=$(echo "$envvar" |
yq -r '.valueFrom.secretKeyRef.key' | tee /dev/stderr)
[ "${actual}" = "scada-address" ]
}

@test "server/StatefulSet: cloud config is set in command global.cloud.enabled is not set" {
Expand Down
2 changes: 2 additions & 0 deletions cli/cmd/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ const (
envHCPClientSecret = "HCP_CLIENT_SECRET"
envHCPAuthURL = "HCP_AUTH_URL"
envHCPAPIHost = "HCP_API_HOST"
envHCPScadaAddress = "HCP_SCADA_ADDRESS"

flagNameDemo = "demo"
defaultDemo = false
Expand Down Expand Up @@ -655,6 +656,7 @@ func (c *Command) getPreset(name string) (preset.Preset, error) {
ClientSecret: os.Getenv(envHCPClientSecret),
AuthURL: os.Getenv(envHCPAuthURL),
APIHostname: os.Getenv(envHCPAPIHost),
ScadaAddress: os.Getenv(envHCPScadaAddress),
}
getPresetConfig := &preset.GetPresetConfig{
Name: name,
Expand Down
3 changes: 3 additions & 0 deletions cli/preset/cloud_preset.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const (
secretKeyHCPResourceID = "resource-id"
secretKeyHCPAuthURL = "auth-url"
secretKeyHCPAPIHostname = "api-hostname"
secretKeyHCPScadaAddress = "scada-address"
secretKeyGossipKey = "key"
secretKeyBootstrapToken = "token"
)
Expand All @@ -52,6 +53,7 @@ type HCPConfig struct {
ClientSecret string
AuthURL string
APIHostname string
ScadaAddress string
}

// ConsulConfig represents 'cluster.consul_config' in the response
Expand Down Expand Up @@ -302,6 +304,7 @@ func (i *CloudPreset) saveServerHCPConfigSecret(config *CloudBootstrapConfig) er
secretKeyHCPResourceID: []byte(config.HCPConfig.ResourceID),
secretKeyHCPAuthURL: []byte(config.HCPConfig.AuthURL),
secretKeyHCPAPIHostname: []byte(config.HCPConfig.APIHostname),
secretKeyHCPScadaAddress: []byte(config.HCPConfig.ScadaAddress),
}
if err := i.saveSecret(secretNameHCPConfig, data, corev1.SecretTypeOpaque); err != nil {
return err
Expand Down
2 changes: 2 additions & 0 deletions cli/preset/cloud_preset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ var hcpConfig *HCPConfig = &HCPConfig{
ClientSecret: hcpClientSecret,
AuthURL: "https://foobar",
APIHostname: "https://foo.bar",
ScadaAddress: "10.10.10.10",
}

var validBootstrapConfig *CloudBootstrapConfig = &CloudBootstrapConfig{
Expand Down Expand Up @@ -398,6 +399,7 @@ func checkSecretsWereSaved(t require.TestingT, k8s kubernetes.Interface, expecte
require.Equal(t, expectedConfig.HCPConfig.ClientSecret, string(hcpConfigSecret.Data[secretKeyHCPClientSecret]))
require.Equal(t, expectedConfig.HCPConfig.ResourceID, string(hcpConfigSecret.Data[secretKeyHCPResourceID]))
require.Equal(t, expectedConfig.HCPConfig.AuthURL, string(hcpConfigSecret.Data[secretKeyHCPAuthURL]))
require.Equal(t, expectedConfig.HCPConfig.ScadaAddress, string(hcpConfigSecret.Data[secretKeyHCPScadaAddress]))
require.Equal(t, expectedConfig.HCPConfig.APIHostname, string(hcpConfigSecret.Data[secretKeyHCPAPIHostname]))
require.Equal(t, corev1.SecretTypeOpaque, hcpConfigSecret.Type)
require.Equal(t, common.CLILabelValue, hcpConfigSecret.Labels[common.CLILabelKey])
Expand Down

0 comments on commit 5e9b7bb

Please sign in to comment.