Skip to content

Commit dd32ed8

Browse files
committed
odo dev/deploy should display a warning about default namespace on cluster
Signed-off-by: Parthvi Vala <[email protected]>
1 parent 2ef5316 commit dd32ed8

File tree

7 files changed

+76
-42
lines changed

7 files changed

+76
-42
lines changed

pkg/odo/cli/deploy/deploy.go

+2
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ func (o *DeployOptions) Validate(ctx context.Context) error {
6363
if devfileObj == nil {
6464
return genericclioptions.NewNoDevfileError(odocontext.GetWorkingDirectory(ctx))
6565
}
66+
67+
genericclioptions.WarnIfDefaultNamespace(odocontext.GetNamespace(ctx), o.clientset.KubernetesClient)
6668
componentName := odocontext.GetComponentName(ctx)
6769
err := dfutil.ValidateK8sResourceName("component name", componentName)
6870
return err

pkg/odo/cli/dev/dev.go

+1
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ func (o *DevOptions) Validate(ctx context.Context) error {
118118
return kclient.NewNoConnectionError()
119119
}
120120
scontext.SetPlatform(ctx, o.clientset.KubernetesClient)
121+
genericclioptions.WarnIfDefaultNamespace(odocontext.GetNamespace(ctx), o.clientset.KubernetesClient)
121122
case commonflags.PlatformPodman:
122123
if o.ignoreLocalhostFlag && o.forwardLocalhostFlag {
123124
return errors.New("--ignore-localhost and --forward-localhost cannot be used together")

pkg/odo/genericclioptions/util.go

+15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package genericclioptions
22

33
import (
4+
"github.com/redhat-developer/odo/pkg/kclient"
5+
"github.com/redhat-developer/odo/pkg/log"
46
pkgUtil "github.com/redhat-developer/odo/pkg/util"
57

68
dfutil "github.com/devfile/library/v2/pkg/util"
@@ -34,3 +36,16 @@ func ApplyIgnore(ignores *[]string, sourcePath string) (err error) {
3436

3537
return nil
3638
}
39+
40+
// WarnIfDefaultNamespace warns when user tries to run `odo dev` or `odo deploy` in the default namespace
41+
func WarnIfDefaultNamespace(namespace string, kubeClient kclient.ClientInterface) {
42+
if namespace == "default" {
43+
noun := "namespace"
44+
if isOC, _ := kubeClient.IsProjectSupported(); isOC {
45+
noun = "project"
46+
}
47+
48+
log.Warningf("You are using \"default\" %[1]s, odo may not work as expected in the default %[1]s.", noun)
49+
log.Warningf("You may set a new %[1]s by running `odo create %[1]s <name>`, or set an existing one by running `odo set %[1]s <name>`", noun)
50+
}
51+
}

tests/helper/helper_dev.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -255,18 +255,22 @@ func RunDevMode(options DevSessionOpts, inside func(session *gexec.Session, outC
255255
return nil
256256
}
257257

258-
// DevModeShouldFail runs `odo dev` with an intention to fail, and checks for a given substring
258+
// WaitForDevModeToContain runs `odo dev` until it contains a given substring in output or errOut(depending on checkErrOut arg).
259259
// `odo dev` runs in an infinite reconciliation loop, and hence running it with Cmd will not work for a lot of failing cases,
260260
// this function is helpful in such cases.
261261
// TODO(pvala): Modify StartDevMode to take substring arg into account, and replace this method with it.
262-
func DevModeShouldFail(options DevSessionOpts, substring string) (DevSession, []byte, []byte, error) {
262+
func WaitForDevModeToContain(options DevSessionOpts, substring string, checkErrOut bool) (DevSession, []byte, []byte, error) {
263263
args := []string{"dev", "--random-ports"}
264264
args = append(args, options.CmdlineArgs...)
265265
if options.RunOnPodman {
266266
args = append(args, "--platform", "podman")
267267
}
268268
session := Cmd("odo", args...).AddEnv(options.EnvVars...).Runner().session
269-
WaitForOutputToContain(substring, 360, 10, session)
269+
if checkErrOut {
270+
WaitForErroutToContain(substring, 360, 10, session)
271+
} else {
272+
WaitForOutputToContain(substring, 360, 10, session)
273+
}
270274
result := DevSession{
271275
session: session,
272276
}

tests/helper/helper_generic.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,11 @@ func RunTestSpecs(t *testing.T, description string) {
347347
}
348348

349349
func IsKubernetesCluster() bool {
350-
return os.Getenv("KUBERNETES") == "true"
350+
k8s, err := strconv.ParseBool(os.Getenv("KUBERNETES"))
351+
if err != nil {
352+
return false
353+
}
354+
return k8s
351355
}
352356

353357
type ResourceInfo struct {

tests/integration/cmd_dev_test.go

+20-38
Original file line numberDiff line numberDiff line change
@@ -1807,11 +1807,12 @@ CMD ["npm", "start"]
18071807
})
18081808

18091809
It("should not build images when odo dev is run", func() {
1810-
_, sessionOut, _, err := helper.DevModeShouldFail(
1810+
_, sessionOut, _, err := helper.WaitForDevModeToContain(
18111811
helper.DevSessionOpts{
18121812
EnvVars: env,
18131813
},
1814-
"failed to retrieve "+url)
1814+
"failed to retrieve "+url,
1815+
false)
18151816
Expect(err).To(BeNil())
18161817
Expect(sessionOut).NotTo(ContainSubstring("build -t quay.io/unknown-account/myimage -f "))
18171818
Expect(sessionOut).NotTo(ContainSubstring("push quay.io/unknown-account/myimage"))
@@ -2658,47 +2659,28 @@ CMD ["npm", "start"]
26582659
}))
26592660
}
26602661

2661-
Context("using Kubernetes cluster", func() {
2662+
Context("using a default namespace", func() {
26622663
BeforeEach(func() {
2663-
if os.Getenv("KUBERNETES") != "true" {
2664-
Skip("This is a Kubernetes specific scenario, skipping")
2665-
}
2666-
})
2667-
2668-
It("should run odo dev successfully on default namespace", func() {
2669-
helper.CopyExample(filepath.Join("source", "nodejs"), commonVar.Context)
2670-
helper.Cmd("odo", "init", "--name", cmpName, "--devfile-path", helper.GetExamplePath("source", "devfiles", "nodejs", "devfile.yaml")).ShouldPass()
2671-
helper.CopyExample(filepath.Join("source", "devfiles", "nodejs", "project"), commonVar.Context)
2672-
2673-
session, _, errContents, _, err := helper.StartDevMode(helper.DevSessionOpts{})
2674-
Expect(err).ToNot(HaveOccurred())
2675-
defer func() {
2676-
session.Stop()
2677-
session.WaitEnd()
2678-
}()
2679-
helper.DontMatchAllInOutput(string(errContents), []string{"odo may not work as expected in the default project"})
2664+
commonVar.CliRunner.SetProject("default")
26802665
})
2681-
})
2682-
2683-
/* TODO(feloy) Issue #5591
2684-
Context("using OpenShift cluster", func() {
2685-
BeforeEach(func() {
2686-
if os.Getenv("KUBERNETES") == "true" {
2687-
Skip("This is a OpenShift specific scenario, skipping")
2688-
}
2666+
AfterEach(func() {
2667+
commonVar.CliRunner.SetProject(commonVar.Project)
26892668
})
2690-
It("should run odo dev successfully on default namespace", func() {
2691-
helper.CopyExample(filepath.Join("source", "nodejs"), commonVar.Context)
2692-
helper.Cmd("odo", "init", "--name", cmpName, "--devfile-path", helper.GetExamplePath("source", "devfiles", "nodejs", "devfile.yaml")).ShouldPass()
2693-
helper.CopyExample(filepath.Join("source", "devfiles", "nodejs", "project"), commonVar.Context)
2694-
2695-
session, _, errContents, err := helper.StartDevMode(helper.DevSessionOpts{})
2696-
Expect(err).ToNot(HaveOccurred())
2697-
defer session.Stop()
2698-
helper.MatchAllInOutput(string(errContents), []string{"odo may not work as expected in the default project"})
2669+
When("a component is created", func() {
2670+
BeforeEach(func() {
2671+
helper.CopyExample(filepath.Join("source", "nodejs"), commonVar.Context)
2672+
helper.Cmd("odo", "init", "--name", cmpName, "--devfile-path", helper.GetExamplePath("source", "devfiles", "nodejs", "devfile.yaml")).ShouldPass()
2673+
helper.CopyExample(filepath.Join("source", "devfiles", "nodejs", "project"), commonVar.Context)
2674+
})
2675+
It("should print warning about default namespace when running odo dev", func() {
2676+
namespace := "project"
2677+
if helper.IsKubernetesCluster() {
2678+
namespace = "namespace"
2679+
}
2680+
helper.WaitForDevModeToContain(helper.DevSessionOpts{}, fmt.Sprintf("You are using \"default\" %[1]s, odo may not work as expected in the default %[1]s.", namespace), true)
2681+
})
26992682
})
27002683
})
2701-
*/
27022684

27032685
// Test reused and adapted from the now-removed `cmd_devfile_delete_test.go`.
27042686
// cf. https://github.com/redhat-developer/odo/blob/24fd02673d25eb4c7bb166ec3369554a8e64b59c/tests/integration/devfile/cmd_devfile_delete_test.go#L172-L238

tests/integration/cmd_devfile_deploy_test.go

+26
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,33 @@ var _ = Describe("odo devfile deploy command tests", func() {
4343

4444
})
4545
})
46+
When("using a default namespace", func() {
47+
BeforeEach(func() {
48+
commonVar.CliRunner.SetProject("default")
49+
})
50+
AfterEach(func() {
51+
helper.Cmd("odo", "delete", "component", "-f").ShouldPass()
52+
commonVar.CliRunner.SetProject(commonVar.Project)
53+
})
54+
When("deploying a devfile with deploy command", func() {
55+
BeforeEach(func() {
56+
helper.CopyExample(filepath.Join("source", "nodejs"), commonVar.Context)
57+
helper.CopyExampleDevFile(
58+
filepath.Join("source", "devfiles", "nodejs", "devfile-deploy.yaml"),
59+
path.Join(commonVar.Context, "devfile.yaml"),
60+
helper.DevfileMetadataNameSetter(cmpName))
61+
})
62+
It("should display warning when running the deploy command", func() {
63+
errOut := helper.Cmd("odo", "deploy").AddEnv("PODMAN_CMD=echo").ShouldRun().Err()
64+
namespace := "project"
65+
if helper.IsKubernetesCluster() {
66+
namespace = "namespace"
67+
}
68+
Expect(errOut).To(ContainSubstring(fmt.Sprintf("You are using \"default\" %[1]s, odo may not work as expected in the default %[1]s.", namespace)))
69+
})
70+
})
4671

72+
})
4773
for _, ctx := range []struct {
4874
title string
4975
devfileName string

0 commit comments

Comments
 (0)