Skip to content

Commit f91a57d

Browse files
authored
Merge pull request #852 from dgageot/namespace
Support setting namespace for every deployer
2 parents 5382fed + bf6ce07 commit f91a57d

File tree

9 files changed

+165
-153
lines changed

9 files changed

+165
-153
lines changed

cmd/skaffold/app/cmd/cmd.go

+26
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"context"
2121
"fmt"
2222
"io"
23+
"os"
24+
"strings"
2325

2426
cmdutil "github.com/GoogleContainerTools/skaffold/cmd/skaffold/app/cmd/util"
2527
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
@@ -29,6 +31,7 @@ import (
2931
"github.com/pkg/errors"
3032
"github.com/sirupsen/logrus"
3133
"github.com/spf13/cobra"
34+
"github.com/spf13/pflag"
3235
)
3336

3437
var (
@@ -78,6 +81,9 @@ func NewSkaffoldCommand(out, err io.Writer) *cobra.Command {
7881
rootCmd.AddCommand(NewCmdFix(out))
7982

8083
rootCmd.PersistentFlags().StringVarP(&v, "verbosity", "v", constants.DefaultLogLevel.String(), "Log level (debug, info, warn, error, fatal, panic")
84+
85+
setFlagsFromEnvVariables(rootCmd.Commands())
86+
8187
return rootCmd
8288
}
8389

@@ -100,6 +106,26 @@ func updateCheck(ch chan string) error {
100106
return nil
101107
}
102108

109+
// Each flag can also be set with an env variable whose name starts with `SKAFFOLD_`.
110+
func setFlagsFromEnvVariables(commands []*cobra.Command) {
111+
for _, cmd := range commands {
112+
cmd.Flags().VisitAll(func(f *pflag.Flag) {
113+
// special case for backward compatibility.
114+
if f.Name == "namespace" {
115+
if val, present := os.LookupEnv("SKAFFOLD_DEPLOY_NAMESPACE"); present {
116+
logrus.Warnln("Using SKAFFOLD_DEPLOY_NAMESPACE env variable is deprecated. Please use SKAFFOLD_NAMESPACE instead.")
117+
cmd.Flags().Set(f.Name, val)
118+
}
119+
}
120+
121+
envVar := fmt.Sprintf("SKAFFOLD_%s", strings.Replace(strings.ToUpper(f.Name), "-", "_", -1))
122+
if val, present := os.LookupEnv(envVar); present {
123+
cmd.Flags().Set(f.Name, val)
124+
}
125+
})
126+
}
127+
}
128+
103129
func AddDevFlags(cmd *cobra.Command) {
104130
cmd.Flags().BoolVar(&opts.Cleanup, "cleanup", true, "Delete deployments after dev mode is interrupted")
105131
}

integration/run_test.go

+55-112
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ package integration
2121
import (
2222
"bytes"
2323
"flag"
24-
"fmt"
2524
"os"
2625
"os/exec"
2726
"testing"
@@ -34,18 +33,16 @@ import (
3433
"k8s.io/api/core/v1"
3534
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3635
"k8s.io/client-go/kubernetes"
37-
"k8s.io/client-go/tools/clientcmd"
38-
"k8s.io/client-go/tools/clientcmd/api"
3936
)
4037

41-
var gkeZone = flag.String("gke-zone", "us-central1-a", "gke zone")
42-
var gkeClusterName = flag.String("gke-cluster-name", "integration-tests", "name of the integration test cluster")
43-
var gcpProject = flag.String("gcp-project", "k8s-skaffold", "the gcp project where the integration test cluster lives")
44-
var remote = flag.Bool("remote", false, "if true, run tests on a remote GKE cluster")
38+
var (
39+
gkeZone = flag.String("gke-zone", "us-central1-a", "gke zone")
40+
gkeClusterName = flag.String("gke-cluster-name", "integration-tests", "name of the integration test cluster")
41+
gcpProject = flag.String("gcp-project", "k8s-skaffold", "the gcp project where the integration test cluster lives")
42+
remote = flag.Bool("remote", false, "if true, run tests on a remote GKE cluster")
4543

46-
var client kubernetes.Interface
47-
48-
var context *api.Context
44+
client kubernetes.Interface
45+
)
4946

5047
func TestMain(m *testing.M) {
5148
flag.Parse()
@@ -62,95 +59,57 @@ func TestMain(m *testing.M) {
6259
logrus.Fatalf("Test setup error: getting kubernetes client: %s", err)
6360
}
6461

65-
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
66-
kubeConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, &clientcmd.ConfigOverrides{})
67-
68-
cfg, err := kubeConfig.RawConfig()
69-
if err != nil {
70-
logrus.Fatalf("loading kubeconfig: %s", err)
71-
}
72-
73-
context = cfg.Contexts[cfg.CurrentContext]
74-
7562
exitCode := m.Run()
7663

77-
// Reset default context and namespace
78-
if err := exec.Command("kubectl", "config", "set-context", context.Cluster, "--namespace", context.Namespace).Run(); err != nil {
79-
logrus.Warn(err)
80-
}
81-
8264
os.Exit(exitCode)
8365
}
8466

8567
func TestRun(t *testing.T) {
86-
type testObject struct {
87-
name string
88-
}
89-
9068
type testRunCase struct {
9169
description string
9270
dir string
71+
filename string
9372
args []string
94-
deployments []testObject
95-
pods []testObject
73+
deployments []string
74+
pods []string
9675
deploymentValidation func(t *testing.T, d *appsv1.Deployment)
97-
env map[string]string
76+
env []string
9877

9978
remoteOnly bool
100-
cleanup func(t *testing.T)
10179
}
10280

10381
var testCases = []testRunCase{
10482
{
10583
description: "getting-started example",
10684
args: []string{"run"},
107-
pods: []testObject{
108-
{
109-
name: "getting-started",
110-
},
111-
},
112-
dir: "../examples/getting-started",
85+
pods: []string{"getting-started"},
86+
dir: "../examples/getting-started",
11387
},
11488
{
11589
description: "annotated getting-started example",
116-
args: []string{"run", "-f", "annotated-skaffold.yaml"},
117-
pods: []testObject{
118-
{
119-
name: "getting-started",
120-
},
121-
},
122-
dir: "../examples",
90+
args: []string{"run"},
91+
filename: "annotated-skaffold.yaml",
92+
pods: []string{"getting-started"},
93+
dir: "../examples",
12394
},
12495
{
12596
description: "getting-started envTagger",
12697
args: []string{"run"},
127-
pods: []testObject{
128-
{
129-
name: "getting-started",
130-
},
131-
},
132-
dir: "../examples/tagging-with-environment-variables",
133-
env: map[string]string{"FOO": "foo"},
98+
pods: []string{"getting-started"},
99+
dir: "../examples/tagging-with-environment-variables",
100+
env: []string{"FOO=foo"},
134101
},
135102
{
136103
description: "gcb builder example",
137104
args: []string{"run", "-p", "gcb"},
138-
pods: []testObject{
139-
{
140-
name: "getting-started",
141-
},
142-
},
143-
dir: "../examples/getting-started",
144-
remoteOnly: true,
105+
pods: []string{"getting-started"},
106+
dir: "../examples/getting-started",
107+
remoteOnly: true,
145108
},
146109
{
147110
description: "deploy kustomize",
148111
args: []string{"deploy", "--images", "index.docker.io/library/busybox:1"},
149-
deployments: []testObject{
150-
{
151-
name: "kustomize-test",
152-
},
153-
},
112+
deployments: []string{"kustomize-test"},
154113
deploymentValidation: func(t *testing.T, d *appsv1.Deployment) {
155114
if d == nil {
156115
t.Fatalf("Could not find deployment")
@@ -174,31 +133,16 @@ func TestRun(t *testing.T) {
174133
{
175134
description: "kaniko example",
176135
args: []string{"run"},
177-
pods: []testObject{
178-
{
179-
name: "getting-started-kaniko",
180-
},
181-
},
182-
dir: "../examples/kaniko",
183-
remoteOnly: true,
136+
pods: []string{"getting-started-kaniko"},
137+
dir: "../examples/kaniko",
138+
remoteOnly: true,
184139
},
185140
{
186141
description: "helm example",
187142
args: []string{"run"},
188-
deployments: []testObject{
189-
{
190-
name: "skaffold-helm",
191-
},
192-
},
193-
dir: "../examples/helm-deployment",
194-
remoteOnly: true,
195-
cleanup: func(t *testing.T) {
196-
cmd := exec.Command("helm", "delete", "--purge", "skaffold-helm")
197-
output, err := util.RunCmdOut(cmd)
198-
if err != nil {
199-
t.Fatalf("skaffold: %s %v", output, err)
200-
}
201-
},
143+
deployments: []string{"skaffold-helm"},
144+
dir: "../examples/helm-deployment",
145+
remoteOnly: true,
202146
},
203147
}
204148

@@ -211,70 +155,69 @@ func TestRun(t *testing.T) {
211155
ns, deleteNs := setupNamespace(t)
212156
defer deleteNs()
213157

214-
cmd := exec.Command("skaffold", testCase.args...)
215-
env := os.Environ()
216-
for k, v := range testCase.env {
217-
env = append(env, fmt.Sprintf("%s=%s", k, v))
158+
args := []string{}
159+
args = append(args, testCase.args...)
160+
args = append(args, "--namespace", ns.Name)
161+
if testCase.filename != "" {
162+
args = append(args, "-f", testCase.filename)
218163
}
219-
cmd.Env = env
164+
165+
cmd := exec.Command("skaffold", args...)
166+
cmd.Env = append(os.Environ(), testCase.env...)
220167
cmd.Dir = testCase.dir
221-
output, err := util.RunCmdOut(cmd)
222-
if err != nil {
168+
if output, err := util.RunCmdOut(cmd); err != nil {
223169
t.Fatalf("skaffold: %s %v", output, err)
224170
}
225171

226172
for _, p := range testCase.pods {
227-
if err := kubernetesutil.WaitForPodReady(client.CoreV1().Pods(ns.Name), p.name); err != nil {
173+
if err := kubernetesutil.WaitForPodReady(client.CoreV1().Pods(ns.Name), p); err != nil {
228174
t.Fatalf("Timed out waiting for pod ready")
229175
}
230176
}
231177

232178
for _, d := range testCase.deployments {
233-
if err := kubernetesutil.WaitForDeploymentToStabilize(client, ns.Name, d.name, 10*time.Minute); err != nil {
179+
if err := kubernetesutil.WaitForDeploymentToStabilize(client, ns.Name, d, 10*time.Minute); err != nil {
234180
t.Fatalf("Timed out waiting for deployment to stabilize")
235181
}
236182
if testCase.deploymentValidation != nil {
237-
deployment, err := client.AppsV1().Deployments(ns.Name).Get(d.name, meta_v1.GetOptions{})
183+
deployment, err := client.AppsV1().Deployments(ns.Name).Get(d, meta_v1.GetOptions{})
238184
if err != nil {
239185
t.Fatalf("Could not find deployment: %s %s", ns.Name, d)
240186
}
241187
testCase.deploymentValidation(t, deployment)
242188
}
189+
}
243190

244-
if testCase.cleanup != nil {
245-
testCase.cleanup(t)
246-
}
191+
// Cleanup
192+
args = []string{"delete", "--namespace", ns.Name}
193+
if testCase.filename != "" {
194+
args = append(args, "-f", testCase.filename)
195+
}
196+
cmd = exec.Command("skaffold", args...)
197+
cmd.Dir = testCase.dir
198+
if output, err := util.RunCmdOut(cmd); err != nil {
199+
t.Fatalf("skaffold delete: %s %v", output, err)
247200
}
248201
})
249202
}
250203
}
251204

252205
func setupNamespace(t *testing.T) (*v1.Namespace, func()) {
253-
namespaceName := util.RandomID()
254206
ns, err := client.CoreV1().Namespaces().Create(&v1.Namespace{
255207
ObjectMeta: meta_v1.ObjectMeta{
256-
Name: namespaceName,
257-
Namespace: namespaceName,
208+
GenerateName: "skaffold",
258209
},
259210
})
260211
if err != nil {
261212
t.Fatalf("creating namespace: %s", err)
262213
}
263214

264-
kubectlCmd := exec.Command("kubectl", "config", "set-context", context.Cluster, "--namespace", ns.Name)
265-
if err := util.RunCmd(kubectlCmd); err != nil {
266-
t.Fatalf("kubectl config set-context --namespace: %v", err)
267-
}
268-
269-
os.Setenv("SKAFFOLD_DEPLOY_NAMESPACE", namespaceName)
270-
271215
return ns, func() {
272216
client.CoreV1().Namespaces().Delete(ns.Name, &meta_v1.DeleteOptions{})
273-
os.Setenv("SKAFFOLD_DEPLOY_NAMESPACE", "")
274217
}
275218
}
276219
func TestFix(t *testing.T) {
277-
_, deleteNs := setupNamespace(t)
220+
ns, deleteNs := setupNamespace(t)
278221
defer deleteNs()
279222

280223
fixCmd := exec.Command("skaffold", "fix", "-f", "skaffold.yaml")
@@ -284,7 +227,7 @@ func TestFix(t *testing.T) {
284227
t.Fatalf("testing error: %v", err)
285228
}
286229

287-
runCmd := exec.Command("skaffold", "run", "-f", "-")
230+
runCmd := exec.Command("skaffold", "run", "--namespace", ns.Name, "-f", "-")
288231
runCmd.Dir = "testdata/old-config"
289232
runCmd.Stdin = bytes.NewReader(out)
290233
err = util.RunCmd(runCmd)

pkg/skaffold/deploy/helm.go

-2
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,6 @@ func (h *HelmDeployer) deployRelease(out io.Writer, r v1alpha2.HelmRelease, buil
176176
ns = h.namespace
177177
} else if r.Namespace != "" {
178178
ns = r.Namespace
179-
} else {
180-
ns = os.Getenv("SKAFFOLD_DEPLOY_NAMESPACE")
181179
}
182180
if ns != "" {
183181
args = append(args, "--namespace", ns)

0 commit comments

Comments
 (0)