Skip to content

Commit

Permalink
Fix bug where upstream env vars not set (hashicorp#550)
Browse files Browse the repository at this point in the history
* Fix bug where upstream env vars not set

Fixes issue where we weren't setting the upstream environment variables
when the upstream annotation was set:
* <NAME>_CONNECT_SERVICE_HOST
* <NAME>_CONNECT_SERVICE_PORT

Since we're modifying a slice's values during iteration we must access
the element via reference using the index.
  • Loading branch information
lkysow authored Jul 6, 2021
1 parent 7d7ce5d commit 5297df3
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## UNRELEASED

BUG FIXES:
* Connect: Fix bug where environment variables `<NAME>_CONNECT_SERVICE_HOST` and
`<NAME>_CONNECT_SERVICE_PORT` weren't being set when the upstream annotation was used. [[GH-549](https://github.com/hashicorp/consul-k8s/issues/549)]

## 0.26.0 (June 22, 2021)

FEATURES:
Expand Down
8 changes: 4 additions & 4 deletions connect-inject/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,12 @@ func (h *Handler) Handle(ctx context.Context, req admission.Request) admission.R
// Add the upstream services as environment variables for easy
// service discovery.
containerEnvVars := h.containerEnvVars(pod)
for _, container := range pod.Spec.InitContainers {
container.Env = append(container.Env, containerEnvVars...)
for i := range pod.Spec.InitContainers {
pod.Spec.InitContainers[i].Env = append(pod.Spec.InitContainers[i].Env, containerEnvVars...)
}

for _, container := range pod.Spec.Containers {
container.Env = append(container.Env, containerEnvVars...)
for i := range pod.Spec.Containers {
pod.Spec.Containers[i].Env = append(pod.Spec.Containers[i].Env, containerEnvVars...)
}

// Add the init container which copies the Consul binary to /consul/connect-inject/.
Expand Down
4 changes: 4 additions & 0 deletions connect-inject/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ func TestHandlerHandle(t *testing.T) {
Operation: "add",
Path: "/spec/containers/1",
},
{
Operation: "add",
Path: "/spec/containers/0/env",
},
},
},

Expand Down

0 comments on commit 5297df3

Please sign in to comment.