Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: expand namespace with env variables #8222

Merged
merged 2 commits into from
Dec 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions integration/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -582,3 +582,32 @@ func TestRunNoOptFlags(t *testing.T) {
WaitForLogs(t, out, test.targetLog)
})
}

func TestRunKubectlDefaultNamespace(t *testing.T) {
tests := []struct {
description string
namespaceToCreate string
projectDir string
podName string
envVariable string
}{
{
description: "run with defaultNamespace when namespace exists in cluster",
namespaceToCreate: "namespace-test",
projectDir: "testdata/kubectl-with-default-namespace",
podName: "getting-started",
envVariable: "ENV1",
},
}

for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
MarkIntegrationTest(t.T, CanRunWithoutGcp)
ns, client := SetupNamespace(t.T)
t.SetEnvs(map[string]string{test.envVariable: ns.Name})
skaffold.Run().InDir(test.projectDir).RunOrFail(t.T)
pod := client.GetPod(test.podName)
t.CheckNotNil(pod)
})
}
}
14 changes: 14 additions & 0 deletions integration/testdata/kubectl-with-default-namespace/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM golang:1.18 as builder
WORKDIR /code
COPY main.go .
COPY go.mod .
# `skaffold debug` sets SKAFFOLD_GO_GCFLAGS to disable compiler optimizations
ARG SKAFFOLD_GO_GCFLAGS
RUN go build -gcflags="${SKAFFOLD_GO_GCFLAGS}" -trimpath -o /app main.go

FROM alpine:3
# Define GOTRACEBACK to mark this container as using the Go language runtime
# for `skaffold debug` (https://skaffold.dev/docs/workflows/debug/).
ENV GOTRACEBACK=single
CMD ["./app"]
COPY --from=builder /app .
3 changes: 3 additions & 0 deletions integration/testdata/kubectl-with-default-namespace/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/GoogleContainerTools/skaffold/examples/cross-platform-builds

go 1.18
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apiVersion: v1
kind: Pod
metadata:
name: getting-started
spec:
containers:
- name: getting-started
image: skaffold-example
15 changes: 15 additions & 0 deletions integration/testdata/kubectl-with-default-namespace/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

import (
"fmt"
"runtime"
"time"
)

func main() {
for {
fmt.Printf("Hello world! Running on %s/%s\n", runtime.GOOS, runtime.GOARCH)

time.Sleep(time.Second * 1)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
apiVersion: skaffold/v4beta1
kind: Config

build:
artifacts:
- image: skaffold-example
context: .
docker:
dockerfile: Dockerfile

manifests:
rawYaml:
- k8s-*

deploy:
kubectl:
defaultNamespace: "{{.ENV1}}"
5 changes: 5 additions & 0 deletions pkg/skaffold/runner/runcontext/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,11 @@ func (rc *RunContext) GetNamespace() string {
}
}
if defaultNamespace != "" {
defaultNamespace, err := util.ExpandEnvTemplate(defaultNamespace, nil)
if err != nil {
return ""
}

return defaultNamespace
}
b, err := (&util.Commander{}).RunCmdOut(context.Background(), exec.Command("kubectl", "config", "view", "--minify", "-o", "jsonpath='{..namespace}'"))
Expand Down