Skip to content

Commit

Permalink
odo dev/deploy should display a warning about default namespace on cl…
Browse files Browse the repository at this point in the history
…uster

Signed-off-by: Parthvi Vala <pvala@redhat.com>
  • Loading branch information
valaparthvi committed Mar 27, 2023
1 parent 2ef5316 commit dd32ed8
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 42 deletions.
2 changes: 2 additions & 0 deletions pkg/odo/cli/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ func (o *DeployOptions) Validate(ctx context.Context) error {
if devfileObj == nil {
return genericclioptions.NewNoDevfileError(odocontext.GetWorkingDirectory(ctx))
}

genericclioptions.WarnIfDefaultNamespace(odocontext.GetNamespace(ctx), o.clientset.KubernetesClient)
componentName := odocontext.GetComponentName(ctx)
err := dfutil.ValidateK8sResourceName("component name", componentName)
return err
Expand Down
1 change: 1 addition & 0 deletions pkg/odo/cli/dev/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ func (o *DevOptions) Validate(ctx context.Context) error {
return kclient.NewNoConnectionError()
}
scontext.SetPlatform(ctx, o.clientset.KubernetesClient)
genericclioptions.WarnIfDefaultNamespace(odocontext.GetNamespace(ctx), o.clientset.KubernetesClient)
case commonflags.PlatformPodman:
if o.ignoreLocalhostFlag && o.forwardLocalhostFlag {
return errors.New("--ignore-localhost and --forward-localhost cannot be used together")
Expand Down
15 changes: 15 additions & 0 deletions pkg/odo/genericclioptions/util.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package genericclioptions

import (
"github.com/redhat-developer/odo/pkg/kclient"
"github.com/redhat-developer/odo/pkg/log"
pkgUtil "github.com/redhat-developer/odo/pkg/util"

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

return nil
}

// WarnIfDefaultNamespace warns when user tries to run `odo dev` or `odo deploy` in the default namespace
func WarnIfDefaultNamespace(namespace string, kubeClient kclient.ClientInterface) {
if namespace == "default" {
noun := "namespace"
if isOC, _ := kubeClient.IsProjectSupported(); isOC {
noun = "project"
}

log.Warningf("You are using \"default\" %[1]s, odo may not work as expected in the default %[1]s.", noun)
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)
}
}
10 changes: 7 additions & 3 deletions tests/helper/helper_dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,18 +255,22 @@ func RunDevMode(options DevSessionOpts, inside func(session *gexec.Session, outC
return nil
}

// DevModeShouldFail runs `odo dev` with an intention to fail, and checks for a given substring
// WaitForDevModeToContain runs `odo dev` until it contains a given substring in output or errOut(depending on checkErrOut arg).
// `odo dev` runs in an infinite reconciliation loop, and hence running it with Cmd will not work for a lot of failing cases,
// this function is helpful in such cases.
// TODO(pvala): Modify StartDevMode to take substring arg into account, and replace this method with it.
func DevModeShouldFail(options DevSessionOpts, substring string) (DevSession, []byte, []byte, error) {
func WaitForDevModeToContain(options DevSessionOpts, substring string, checkErrOut bool) (DevSession, []byte, []byte, error) {
args := []string{"dev", "--random-ports"}
args = append(args, options.CmdlineArgs...)
if options.RunOnPodman {
args = append(args, "--platform", "podman")
}
session := Cmd("odo", args...).AddEnv(options.EnvVars...).Runner().session
WaitForOutputToContain(substring, 360, 10, session)
if checkErrOut {
WaitForErroutToContain(substring, 360, 10, session)
} else {
WaitForOutputToContain(substring, 360, 10, session)
}
result := DevSession{
session: session,
}
Expand Down
6 changes: 5 additions & 1 deletion tests/helper/helper_generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,11 @@ func RunTestSpecs(t *testing.T, description string) {
}

func IsKubernetesCluster() bool {
return os.Getenv("KUBERNETES") == "true"
k8s, err := strconv.ParseBool(os.Getenv("KUBERNETES"))
if err != nil {
return false
}
return k8s
}

type ResourceInfo struct {
Expand Down
58 changes: 20 additions & 38 deletions tests/integration/cmd_dev_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1807,11 +1807,12 @@ CMD ["npm", "start"]
})

It("should not build images when odo dev is run", func() {
_, sessionOut, _, err := helper.DevModeShouldFail(
_, sessionOut, _, err := helper.WaitForDevModeToContain(
helper.DevSessionOpts{
EnvVars: env,
},
"failed to retrieve "+url)
"failed to retrieve "+url,
false)
Expect(err).To(BeNil())
Expect(sessionOut).NotTo(ContainSubstring("build -t quay.io/unknown-account/myimage -f "))
Expect(sessionOut).NotTo(ContainSubstring("push quay.io/unknown-account/myimage"))
Expand Down Expand Up @@ -2658,47 +2659,28 @@ CMD ["npm", "start"]
}))
}

Context("using Kubernetes cluster", func() {
Context("using a default namespace", func() {
BeforeEach(func() {
if os.Getenv("KUBERNETES") != "true" {
Skip("This is a Kubernetes specific scenario, skipping")
}
})

It("should run odo dev successfully on default namespace", func() {
helper.CopyExample(filepath.Join("source", "nodejs"), commonVar.Context)
helper.Cmd("odo", "init", "--name", cmpName, "--devfile-path", helper.GetExamplePath("source", "devfiles", "nodejs", "devfile.yaml")).ShouldPass()
helper.CopyExample(filepath.Join("source", "devfiles", "nodejs", "project"), commonVar.Context)

session, _, errContents, _, err := helper.StartDevMode(helper.DevSessionOpts{})
Expect(err).ToNot(HaveOccurred())
defer func() {
session.Stop()
session.WaitEnd()
}()
helper.DontMatchAllInOutput(string(errContents), []string{"odo may not work as expected in the default project"})
commonVar.CliRunner.SetProject("default")
})
})

/* TODO(feloy) Issue #5591
Context("using OpenShift cluster", func() {
BeforeEach(func() {
if os.Getenv("KUBERNETES") == "true" {
Skip("This is a OpenShift specific scenario, skipping")
}
AfterEach(func() {
commonVar.CliRunner.SetProject(commonVar.Project)
})
It("should run odo dev successfully on default namespace", func() {
helper.CopyExample(filepath.Join("source", "nodejs"), commonVar.Context)
helper.Cmd("odo", "init", "--name", cmpName, "--devfile-path", helper.GetExamplePath("source", "devfiles", "nodejs", "devfile.yaml")).ShouldPass()
helper.CopyExample(filepath.Join("source", "devfiles", "nodejs", "project"), commonVar.Context)
session, _, errContents, err := helper.StartDevMode(helper.DevSessionOpts{})
Expect(err).ToNot(HaveOccurred())
defer session.Stop()
helper.MatchAllInOutput(string(errContents), []string{"odo may not work as expected in the default project"})
When("a component is created", func() {
BeforeEach(func() {
helper.CopyExample(filepath.Join("source", "nodejs"), commonVar.Context)
helper.Cmd("odo", "init", "--name", cmpName, "--devfile-path", helper.GetExamplePath("source", "devfiles", "nodejs", "devfile.yaml")).ShouldPass()
helper.CopyExample(filepath.Join("source", "devfiles", "nodejs", "project"), commonVar.Context)
})
It("should print warning about default namespace when running odo dev", func() {
namespace := "project"
if helper.IsKubernetesCluster() {
namespace = "namespace"
}
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)
})
})
})
*/

// Test reused and adapted from the now-removed `cmd_devfile_delete_test.go`.
// cf. https://github.com/redhat-developer/odo/blob/24fd02673d25eb4c7bb166ec3369554a8e64b59c/tests/integration/devfile/cmd_devfile_delete_test.go#L172-L238
Expand Down
26 changes: 26 additions & 0 deletions tests/integration/cmd_devfile_deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,33 @@ var _ = Describe("odo devfile deploy command tests", func() {

})
})
When("using a default namespace", func() {
BeforeEach(func() {
commonVar.CliRunner.SetProject("default")
})
AfterEach(func() {
helper.Cmd("odo", "delete", "component", "-f").ShouldPass()
commonVar.CliRunner.SetProject(commonVar.Project)
})
When("deploying a devfile with deploy command", func() {
BeforeEach(func() {
helper.CopyExample(filepath.Join("source", "nodejs"), commonVar.Context)
helper.CopyExampleDevFile(
filepath.Join("source", "devfiles", "nodejs", "devfile-deploy.yaml"),
path.Join(commonVar.Context, "devfile.yaml"),
helper.DevfileMetadataNameSetter(cmpName))
})
It("should display warning when running the deploy command", func() {
errOut := helper.Cmd("odo", "deploy").AddEnv("PODMAN_CMD=echo").ShouldRun().Err()
namespace := "project"
if helper.IsKubernetesCluster() {
namespace = "namespace"
}
Expect(errOut).To(ContainSubstring(fmt.Sprintf("You are using \"default\" %[1]s, odo may not work as expected in the default %[1]s.", namespace)))
})
})

})
for _, ctx := range []struct {
title string
devfileName string
Expand Down

0 comments on commit dd32ed8

Please sign in to comment.