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

[Devfile] Remove namespace flag from odo commands #2909

Merged
Merged
Show file tree
Hide file tree
Changes from 5 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
4 changes: 0 additions & 4 deletions pkg/odo/cli/component/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -801,10 +801,6 @@ func NewCmdCreate(name, fullName string) *cobra.Command {
componentCreateCmd.Flags().StringSliceVarP(&co.componentPorts, "port", "p", []string{}, "Ports to be used when the component is created (ex. 8080,8100/tcp,9100/udp)")
componentCreateCmd.Flags().StringSliceVar(&co.componentEnvVars, "env", []string{}, "Environmental variables for the component. For example --env VariableName=Value")

if experimental.IsExperimentalModeEnabled() {
componentCreateCmd.Flags().StringVarP(&co.devfileMetadata.componentNamespace, "namespace", "n", "", "Create devfile component under specific namespace")
}

componentCreateCmd.SetUsageTemplate(odoutil.CmdUsageTemplate)

// Adding `--now` flag
Expand Down
9 changes: 8 additions & 1 deletion pkg/odo/cli/component/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ func (do *DeleteOptions) Complete(name string, cmd *cobra.Command, args []string
if err != nil {
return err
}

// Retrieve the project/namespace used to push the changes to
if cmd.Flags().Changed("project") {
do.namespace, err = cmd.Flags().GetString("project")
if err != nil {
return err
}
}
return nil
}

Expand Down Expand Up @@ -232,7 +240,6 @@ func NewCmdDelete(name, fullName string) *cobra.Command {
// enable devfile flag if experimental mode is enabled
if experimental.IsExperimentalModeEnabled() {
componentDeleteCmd.Flags().StringVar(&do.devfilePath, "devfile", "./devfile.yaml", "Path to a devfile.yaml")
componentDeleteCmd.Flags().StringVar(&do.namespace, "namespace", "", "Namespace to push the component to")
}

componentDeleteCmd.Flags().BoolVarP(&do.componentForceDeleteFlag, "force", "f", false, "Delete component without prompting")
Expand Down
9 changes: 7 additions & 2 deletions pkg/odo/cli/component/devfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (po *PushOptions) DevfilePush() (err error) {
if pushtarget.IsPushTargetDocker() {
platformContext = nil
} else {
if len(po.namespace) <= 0 {
if po.namespace == "" {
po.namespace, err = getNamespace()
if err != nil {
return err
Expand Down Expand Up @@ -152,7 +152,12 @@ func (do *DeleteOptions) DevfileComponentDelete() error {
if err != nil {
return err
}

if len(do.namespace) <= 0 {
do.namespace, err = getNamespace()
if err != nil {
return err
}
}
kc := kubernetes.KubernetesContext{
Namespace: do.namespace,
}
Expand Down
11 changes: 11 additions & 0 deletions pkg/odo/cli/component/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/openshift/odo/pkg/component"
"github.com/openshift/odo/pkg/log"
projectCmd "github.com/openshift/odo/pkg/odo/cli/project"
"github.com/openshift/odo/pkg/odo/genericclioptions"
"github.com/openshift/odo/pkg/odo/util/completion"
"github.com/openshift/odo/pkg/odo/util/experimental"
Expand Down Expand Up @@ -65,6 +66,13 @@ func (po *PushOptions) Complete(name string, cmd *cobra.Command, args []string)
}
po.EnvSpecificInfo = envinfo
po.Context = genericclioptions.NewDevfileContext(cmd)

if cmd.Flags().Changed("project") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this needs to be moved here https://github.com/openshift/odo/blob/6740e5177ae11db734f7d3d303c4f689015e3b91/pkg/odo/genericclioptions/context.go#L343. We are repeating the same steps in all the above files.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mik-dass You're right, good catch!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, we don't need to move any code to resolveNamespace, just update it to retrieve the namespace from the --project flag.

So long as we initialize the devfile context in each command that needs it, we can retrieve the namespace via kclient.Namespace

po.namespace, err = cmd.Flags().GetString("project")
if err != nil {
return err
}
}
return nil
}

Expand Down Expand Up @@ -167,6 +175,9 @@ func NewCmdPush(name, fullName string) *cobra.Command {
pushCmd.Flags().StringVar(&po.devfileRunCommand, "run-command", "", "Devfile Run Command to execute")
}

//Adding `--project` flag
projectCmd.AddProjectFlag(pushCmd)

pushCmd.SetUsageTemplate(odoutil.CmdUsageTemplate)
completion.RegisterCommandHandler(pushCmd, completion.ComponentNameCompletionHandler)
completion.RegisterCommandFlagHandler(pushCmd, "context", completion.FileCompletionHandler)
Expand Down
13 changes: 13 additions & 0 deletions pkg/odo/cli/component/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,19 @@ func (wo *WatchOptions) Complete(name string, cmd *cobra.Command, args []string)
return err
}

// Retrieve the project/namespace used to push the changes to
if cmd.Flags().Changed("project") {
wo.namespace, err = cmd.Flags().GetString("project")
if err != nil {
return err
}
}
if wo.namespace == "" {
wo.namespace, err = getNamespace()
if err != nil {
return err
}
}
kc := kubernetes.KubernetesContext{
Namespace: wo.namespace,
}
Expand Down
7 changes: 0 additions & 7 deletions tests/integration/devfile/cmd_devfile_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,6 @@ var _ = Describe("odo devfile create command tests", func() {
})
})

Context("When executing odo create with devfile component type argument and --namespace flag", func() {
It("should successfully create the devfile component", func() {
componentNamespace := helper.RandString(6)
helper.CmdShouldPass("odo", "create", "openLiberty", "--namespace", componentNamespace)
})
})

Context("When executing odo create with devfile component name that contains unsupported character", func() {
It("should failed with devfile component name is not valid and prompt supported character", func() {
componentName := "BAD@123"
Expand Down
8 changes: 4 additions & 4 deletions tests/integration/devfile/cmd_devfile_delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ var _ = Describe("odo devfile delete command tests", func() {

helper.CmdShouldPass("odo", "url", "create", "example", "--host", "1.2.3.4.nip.io")

helper.CmdShouldPass("odo", "push", "--devfile", "devfile.yaml", "--namespace", namespace)
helper.CmdShouldPass("odo", "push", "--devfile", "devfile.yaml", "--project", namespace)

helper.CmdShouldPass("odo", "delete", "--devfile", "devfile.yaml", "--namespace", namespace, "-f")
helper.CmdShouldPass("odo", "delete", "--devfile", "devfile.yaml", "--project", namespace, "-f")

oc.WaitAndCheckForExistence("deployments", namespace, 1)
oc.WaitAndCheckForExistence("pods", namespace, 1)
Expand All @@ -78,11 +78,11 @@ var _ = Describe("odo devfile delete command tests", func() {

helper.CopyExample(filepath.Join("source", "devfiles", "nodejs"), projectDirPath)

helper.CmdShouldPass("odo", "push", "--devfile", "devfile.yaml", "--namespace", namespace)
helper.CmdShouldPass("odo", "push", "--devfile", "devfile.yaml", "--project", namespace)

helper.CmdShouldPass("odo", "url", "create", "example", "--host", "1.2.3.4.nip.io", "--context", projectDirPath)

helper.CmdShouldPass("odo", "delete", "--devfile", "devfile.yaml", "--namespace", namespace, "-f", "--all")
helper.CmdShouldPass("odo", "delete", "--devfile", "devfile.yaml", "--project", namespace, "-f", "--all")

oc.WaitAndCheckForExistence("deployments", namespace, 1)

Expand Down
38 changes: 19 additions & 19 deletions tests/integration/devfile/cmd_devfile_push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ var _ = Describe("odo devfile push command tests", func() {

helper.RenameFile("devfile.yaml", "devfile-old.yaml")
helper.RenameFile("devfile-no-endpoints.yaml", "devfile.yaml")
helper.CmdShouldPass("odo", "push", "--devfile", "devfile.yaml", "--namespace", namespace)
helper.CmdShouldPass("odo", "push", "--devfile", "devfile.yaml", "--project", namespace)
output := oc.GetServices(namespace)
Expect(output).NotTo(ContainSubstring(cmpName))

helper.RenameFile("devfile-old.yaml", "devfile.yaml")
output = helper.CmdShouldPass("odo", "push", "--devfile", "devfile.yaml", "--namespace", namespace)
output = helper.CmdShouldPass("odo", "push", "--devfile", "devfile.yaml", "--project", namespace)

Expect(output).To(ContainSubstring("Changes successfully pushed to component"))
output = oc.GetServices(namespace)
Expand All @@ -80,12 +80,12 @@ var _ = Describe("odo devfile push command tests", func() {

helper.CopyExample(filepath.Join("source", "devfiles", "nodejs"), projectDirPath)

output := helper.CmdShouldPass("odo", "push", "--devfile", "devfile.yaml", "--namespace", namespace)
output := helper.CmdShouldPass("odo", "push", "--devfile", "devfile.yaml", "--project", namespace)
Expect(output).To(ContainSubstring("Changes successfully pushed to component"))

// update devfile and push again
helper.ReplaceString("devfile.yaml", "name: FOO", "name: BAR")
helper.CmdShouldPass("odo", "push", "--devfile", "devfile.yaml", "--namespace", namespace)
helper.CmdShouldPass("odo", "push", "--devfile", "devfile.yaml", "--project", namespace)
})

})
Expand All @@ -100,12 +100,12 @@ var _ = Describe("odo devfile push command tests", func() {

helper.CopyExample(filepath.Join("source", "devfiles", "nodejs"), projectDirPath)

helper.CmdShouldPass("odo", "push", "--devfile", "devfile.yaml", "--namespace", namespace)
output := helper.CmdShouldPass("odo", "push", "--devfile", "devfile.yaml", "--namespace", namespace)
helper.CmdShouldPass("odo", "push", "--devfile", "devfile.yaml", "--project", namespace)
output := helper.CmdShouldPass("odo", "push", "--devfile", "devfile.yaml", "--project", namespace)
Expect(output).To(ContainSubstring("No file changes detected, skipping build"))

helper.ReplaceString(filepath.Join(projectDirPath, "app", "app.js"), "Hello World!", "UPDATED!")
helper.CmdShouldPass("odo", "push", "--devfile", "devfile.yaml", "--namespace", namespace)
helper.CmdShouldPass("odo", "push", "--devfile", "devfile.yaml", "--project", namespace)
})

It("should be able to create a file, push, delete, then push again propagating the deletions", func() {
Expand All @@ -127,7 +127,7 @@ var _ = Describe("odo devfile push command tests", func() {
helper.MakeDir(newDirPath)

// Push
helper.CmdShouldPass("odo", "push", "--devfile", "devfile.yaml", "--namespace", namespace)
helper.CmdShouldPass("odo", "push", "--devfile", "devfile.yaml", "--project", namespace)

// Check to see if it's been pushed (foobar.txt abd directory testdir)
podName := oc.GetRunningPodNameByComponent(cmpName, namespace)
Expand All @@ -139,7 +139,7 @@ var _ = Describe("odo devfile push command tests", func() {
// Now we delete the file and dir and push
helper.DeleteDir(newFilePath)
helper.DeleteDir(newDirPath)
helper.CmdShouldPass("odo", "push", "--devfile", "devfile.yaml", "--namespace", namespace, "-v4")
helper.CmdShouldPass("odo", "push", "--devfile", "devfile.yaml", "--project", namespace, "-v4")

// Then check to see if it's truly been deleted
stdOut = oc.ExecListDir(podName, namespace, sourcePath)
Expand All @@ -155,7 +155,7 @@ var _ = Describe("odo devfile push command tests", func() {

helper.CopyExample(filepath.Join("source", "devfiles", "nodejs"), projectDirPath)

helper.CmdShouldPass("odo", "push", "--devfile", "devfile.yaml", "--namespace", namespace)
helper.CmdShouldPass("odo", "push", "--devfile", "devfile.yaml", "--project", namespace)

// Check to see if it's been pushed (foobar.txt abd directory testdir)
podName := oc.GetRunningPodNameByComponent(cmpName, namespace)
Expand All @@ -173,7 +173,7 @@ var _ = Describe("odo devfile push command tests", func() {
)
Expect(statErr).ToNot(HaveOccurred())
Expect(os.Remove(filepath.Join(projectDirPath, "app", "app.js"))).NotTo(HaveOccurred())
helper.CmdShouldPass("odo", "push", "--devfile", "devfile.yaml", "--namespace", namespace)
helper.CmdShouldPass("odo", "push", "--devfile", "devfile.yaml", "--project", namespace)

oc.CheckCmdOpInRemoteDevfilePod(
podName,
Expand All @@ -197,10 +197,10 @@ var _ = Describe("odo devfile push command tests", func() {

helper.CopyExample(filepath.Join("source", "devfiles", "nodejs"), projectDirPath)

helper.CmdShouldPass("odo", "push", "--devfile", "devfile.yaml", "--namespace", namespace)
helper.CmdShouldPass("odo", "push", "--devfile", "devfile.yaml", "--project", namespace)

// use the force build flag and push
output := helper.CmdShouldPass("odo", "push", "--devfile", "devfile.yaml", "--namespace", namespace, "-f")
output := helper.CmdShouldPass("odo", "push", "--devfile", "devfile.yaml", "--project", namespace, "-f")
Expect(output).To(Not(ContainSubstring("No file changes detected, skipping build")))
})

Expand All @@ -212,7 +212,7 @@ var _ = Describe("odo devfile push command tests", func() {

helper.CopyExample(filepath.Join("source", "devfiles", "springboot"), projectDirPath)

output := helper.CmdShouldPass("odo", "push", "--devfile", "devfile.yaml", "--namespace", namespace)
output := helper.CmdShouldPass("odo", "push", "--devfile", "devfile.yaml", "--project", namespace)
Expect(output).To(ContainSubstring("Executing devbuild command \"/artifacts/bin/build-container-full.sh\""))
Expect(output).To(ContainSubstring("Executing devrun command \"/artifacts/bin/start-server.sh\""))
})
Expand All @@ -227,7 +227,7 @@ var _ = Describe("odo devfile push command tests", func() {
helper.RenameFile("devfile.yaml", "devfile-old.yaml")
helper.RenameFile("devfile-without-devbuild.yaml", "devfile.yaml")

output := helper.CmdShouldPass("odo", "push", "--devfile", "devfile.yaml", "--namespace", namespace)
output := helper.CmdShouldPass("odo", "push", "--devfile", "devfile.yaml", "--project", namespace)
Expect(output).NotTo(ContainSubstring("Executing devbuild command"))
Expect(output).To(ContainSubstring("Executing devrun command \"npm install && nodemon app.js\""))
})
Expand All @@ -243,7 +243,7 @@ var _ = Describe("odo devfile push command tests", func() {
// Rename the devrun command
helper.ReplaceString(filepath.Join(projectDirPath, "devfile.yaml"), "devrun", "randomcommand")

output := helper.CmdShouldFail("odo", "push", "--devfile", "devfile.yaml", "--namespace", namespace)
output := helper.CmdShouldFail("odo", "push", "--devfile", "devfile.yaml", "--project", namespace)
Expect(output).NotTo(ContainSubstring("Executing devrun command"))
Expect(output).To(ContainSubstring("The command \"devrun\" was not found in the devfile"))
})
Expand All @@ -256,7 +256,7 @@ var _ = Describe("odo devfile push command tests", func() {

helper.CopyExample(filepath.Join("source", "devfiles", "nodejs"), projectDirPath)

output := helper.CmdShouldPass("odo", "push", "--devfile", "devfile.yaml", "--namespace", namespace, "--build-command", "build", "--run-command", "run")
output := helper.CmdShouldPass("odo", "push", "--devfile", "devfile.yaml", "--project", namespace, "--build-command", "build", "--run-command", "run")
Expect(output).To(ContainSubstring("Executing build command \"npm install\""))
Expect(output).To(ContainSubstring("Executing run command \"nodemon app.js\""))
})
Expand All @@ -271,7 +271,7 @@ var _ = Describe("odo devfile push command tests", func() {

helper.CopyExample(filepath.Join("source", "devfiles", "nodejs"), projectDirPath)

output := helper.CmdShouldFail("odo", "push", "--devfile", "devfile.yaml", "--namespace", namespace, "--build-command", garbageCommand)
output := helper.CmdShouldFail("odo", "push", "--devfile", "devfile.yaml", "--project", namespace, "--build-command", garbageCommand)
Expect(output).NotTo(ContainSubstring("Executing buildgarbage command"))
Expect(output).To(ContainSubstring("The command \"%v\" was not found in the devfile", garbageCommand))
})
Expand All @@ -286,7 +286,7 @@ var _ = Describe("odo devfile push command tests", func() {
helper.RenameFile("devfile.yaml", "devfile-old.yaml")
helper.RenameFile("devfile-with-volumes.yaml", "devfile.yaml")

output := helper.CmdShouldPass("odo", "push", "--devfile", "devfile.yaml", "--namespace", namespace)
output := helper.CmdShouldPass("odo", "push", "--devfile", "devfile.yaml", "--project", namespace)
Expect(output).To(ContainSubstring("Executing devbuild command"))
Expect(output).To(ContainSubstring("Executing devrun command"))

Expand Down
10 changes: 5 additions & 5 deletions tests/integration/devfile/cmd_devfile_url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ var _ = Describe("odo devfile url command tests", func() {
return false
})
helper.CmdShouldPass("odo", "url", "delete", url1, "-f")
helper.CmdShouldPass("odo", "push", "--devfile", "devfile.yaml", "--namespace", namespace)
helper.CmdShouldPass("odo", "push", "--devfile", "devfile.yaml", "--project", namespace)

stdout = helper.CmdShouldFail("odo", "url", "list")
Expect(stdout).To(ContainSubstring("no URLs found"))
Expand All @@ -94,7 +94,7 @@ var _ = Describe("odo devfile url command tests", func() {
helper.CopyExample(filepath.Join("source", "devfiles", "nodejs"), projectDirPath)

helper.CmdShouldPass("odo", "url", "create", url1, "--port", "3000", "--host", host)
helper.CmdShouldPass("odo", "push", "--devfile", "devfile.yaml", "--namespace", namespace)
helper.CmdShouldPass("odo", "push", "--devfile", "devfile.yaml", "--project", namespace)
// odo url list -o json
helper.WaitForCmdOut("odo", []string{"url", "list", "-o", "json"}, 1, true, func(output string) bool {
desiredURLListJSON := fmt.Sprintf(`{"kind":"List","apiVersion":"udo.udo.io/v1alpha1","metadata":{},"items":[{"kind":"Ingress","apiVersion":"extensions/v1beta1","metadata":{"name":"%s","creationTimestamp":null},"spec":{"rules":[{"host":"%s","http":{"paths":[{"path":"/","backend":{"serviceName":"%s","servicePort":3000}}]}}]},"status":{"loadBalancer":{}}}]}`, url1, url1+"."+host, componentName)
Expand Down Expand Up @@ -122,12 +122,12 @@ var _ = Describe("odo devfile url command tests", func() {

helper.CmdShouldPass("odo", "url", "create", url1, "--port", "3000", "--host", host, "--secure")

stdout = helper.CmdShouldPass("odo", "push", "--devfile", "devfile.yaml", "--namespace", namespace)
stdout = helper.CmdShouldPass("odo", "push", "--devfile", "devfile.yaml", "--project", namespace)
helper.MatchAllInOutput(stdout, []string{"https:", url1 + "." + host})
stdout = helper.CmdShouldPass("odo", "url", "list")
helper.MatchAllInOutput(stdout, []string{"https:", url1 + "." + host, "true"})
helper.CmdShouldPass("odo", "url", "delete", url1, "-f")
helper.CmdShouldPass("odo", "push", "--devfile", "devfile.yaml", "--namespace", namespace)
helper.CmdShouldPass("odo", "push", "--devfile", "devfile.yaml", "--project", namespace)

stdout = helper.CmdShouldFail("odo", "url", "list")
Expect(stdout).To(ContainSubstring("no URLs found"))
Expand Down Expand Up @@ -168,7 +168,7 @@ var _ = Describe("odo devfile url command tests", func() {
stdout = helper.CmdShouldFail("odo", "url", "describe", url1)
helper.MatchAllInOutput(stdout, []string{url1, "exists in local", "odo push"})

helper.CmdShouldPass("odo", "push", "--devfile", "devfile.yaml", "--namespace", namespace)
helper.CmdShouldPass("odo", "push", "--devfile", "devfile.yaml", "--project", namespace)
helper.WaitForCmdOut("odo", []string{"url", "describe", url1}, 1, false, func(output string) bool {
if strings.Contains(output, url1) {
Expect(output).Should(ContainSubstring(url1 + "." + host))
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/devfile/cmd_devfile_watch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ var _ = Describe("odo devfile watch command tests", func() {
// Devfile push requires experimental mode to be set
helper.CmdShouldPass("odo", "preference", "set", "Experimental", "true")
cmpName := helper.RandString(6)
helper.CmdShouldPass("odo", "create", "nodejs", "--namespace", namespace, cmpName)
helper.CmdShouldPass("odo", "create", "nodejs", "--project", namespace, cmpName)
helper.CopyExample(filepath.Join("source", "devfiles", "nodejs"), context)

output := helper.CmdShouldFail("odo", "watch", "--devfile", filepath.Join(context, "devfile.yaml"))
Expand Down