From 5c121d7a485036f4ab27575eed4f6e44bab39cea Mon Sep 17 00:00:00 2001 From: freddygv Date: Mon, 8 Nov 2021 09:26:14 -0700 Subject: [PATCH] handle error scenario of empty local DC --- agent/consul/config_endpoint.go | 4 ++++ agent/consul/config_endpoint_test.go | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/agent/consul/config_endpoint.go b/agent/consul/config_endpoint.go index af736ea091..add7aaeb53 100644 --- a/agent/consul/config_endpoint.go +++ b/agent/consul/config_endpoint.go @@ -598,6 +598,10 @@ func gateWriteToSecondary(targetDC, localDC, primaryDC, kind string) error { if kind != structs.PartitionExports { return nil } + if localDC == "" { + // This should not happen because the datacenter is defaulted in DefaultConfig. + return fmt.Errorf("unknown local datacenter") + } if primaryDC == "" { primaryDC = localDC diff --git a/agent/consul/config_endpoint_test.go b/agent/consul/config_endpoint_test.go index 6c4d187333..a0672ec3bb 100644 --- a/agent/consul/config_endpoint_test.go +++ b/agent/consul/config_endpoint_test.go @@ -2151,6 +2151,14 @@ func Test_gateWriteToSecondary(t *testing.T) { }, wantErr: "must target the primary datacenter explicitly", }, + { + name: "empty local DC", + args: args{ + localDC: "", + kind: structs.PartitionExports, + }, + wantErr: "unknown local datacenter", + }, } for _, tc := range tt { t.Run(tc.name, func(t *testing.T) {