Add new --run-port flag to odo init to set ports non-interactively (#6953)

* Add new `--run-port` flag to `odo init` to set ports non-interactively

As depicted in [1], this leverages the default (or single non-default) run command to find the linked container component.
As such, it assumes that the command found is an exec command,
and that the linked component is a container component.

[1] https://github.com/redhat-developer/odo/issues/6925

* Add unit and integration tests highlighting the expectations

* Document the new `--run-port` flag

* Fix some typos and language correctness issues in the `odo init` doc

* Add doc automation test for the output of `odo init --run-port`

This ensures the output and sample in the doc are kept in sync with the code base.
This commit is contained in:
Armel Soro
2023-07-06 16:35:24 +02:00
committed by GitHub
parent f6bb4e2689
commit c4b103d9c4
11 changed files with 926 additions and 14 deletions

View File

@@ -212,6 +212,16 @@ var _ = Describe("doc command reference odo init", Label(helper.LabelNoCluster),
diff := cmp.Diff(want, got)
Expect(diff).To(BeEmpty(), file)
})
It("set application ports after fetching Devfile", func() {
args := []string{"init", "--devfile", "go", "--name", "my-go-app", "--run-port", "3456", "--run-port", "9876"}
out := helper.Cmd("odo", args...).ShouldPass().Out()
got := fmt.Sprintf(outputStringFormat, strings.Join(args, " "), helper.StripSpinner(out))
file := "devfile_with_run-port_output.mdx"
want := helper.GetMDXContent(filepath.Join(commonPath, file))
diff := cmp.Diff(want, got)
Expect(diff).To(BeEmpty(), file)
})
})
})

View File

@@ -10,6 +10,8 @@ import (
. "github.com/onsi/gomega"
apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/utils/pointer"
"github.com/redhat-developer/odo/pkg/devfile"
)
// DevfileUpdater is a helper type that can mutate a Devfile object.
@@ -105,3 +107,10 @@ func SetFsGroup(containerName string, fsGroup int) DevfileUpdater {
return nil
}
}
// ReadRawDevfile parses and validates the Devfile specified and returns its raw content.
func ReadRawDevfile(devfilePath string) parser.DevfileObj {
d, err := devfile.ParseAndValidateFromFile(devfilePath, "", false)
Expect(err).ToNot(HaveOccurred())
return d
}

View File

@@ -6,6 +6,8 @@ import (
"os"
"path/filepath"
"github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
"github.com/devfile/library/v2/pkg/devfile/parser/data/v2/common"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/tidwall/gjson"
@@ -616,4 +618,57 @@ spec:
})
})
Context("setting application ports", func() {
When("running odo init --run-port with a Devfile with no commands", func() {
BeforeEach(func() {
helper.Cmd("odo", "init", "--name", "aname", "--devfile-path",
filepath.Join(helper.GetExamplePath(), "source", "devfiles", "nodejs", "devfile-without-commands.yaml"),
"--run-port", "1234", "--run-port", "2345", "--run-port", "3456").ShouldPass().Out()
})
It("should ignore the run ports", func() {
d := helper.ReadRawDevfile(filepath.Join(commonVar.Context, "devfile.yaml"))
components, err := d.Data.GetComponents(common.DevfileOptions{
ComponentOptions: common.ComponentOptions{
ComponentType: v1alpha2.ContainerComponentType,
},
})
Expect(err).ShouldNot(HaveOccurred())
Expect(components).To(HaveLen(1))
Expect(components[0].Name).Should(Equal("runtime"))
Expect(components[0].Container).ShouldNot(BeNil())
Expect(components[0].Container.Endpoints).Should(HaveLen(2))
Expect(components[0].Container.Endpoints[0].Name).Should(Equal("http-node"))
Expect(components[0].Container.Endpoints[0].TargetPort).Should(Equal(3000))
Expect(components[0].Container.Endpoints[1].Name).Should(Equal("debug"))
Expect(components[0].Container.Endpoints[1].TargetPort).Should(Equal(5858))
Expect(components[0].Container.Endpoints[1].Exposure).Should(Equal(v1alpha2.NoneEndpointExposure))
})
})
When("running odo init --run-port with a Devfile with no commands", func() {
BeforeEach(func() {
helper.Cmd("odo", "init", "--name", "aname", "--devfile-path",
filepath.Join(helper.GetExamplePath(), "source", "devfiles", "nodejs", "devfile-with-debugrun.yaml"),
"--run-port", "1234", "--run-port", "2345", "--run-port", "3456").ShouldPass().Out()
})
It("should overwrite the ports into the container component referenced by the default run command", func() {
d := helper.ReadRawDevfile(filepath.Join(commonVar.Context, "devfile.yaml"))
components, err := d.Data.GetComponents(common.DevfileOptions{
ComponentOptions: common.ComponentOptions{
ComponentType: v1alpha2.ContainerComponentType,
},
})
Expect(err).ShouldNot(HaveOccurred())
Expect(components).To(HaveLen(1))
Expect(components[0].Name).Should(Equal("runtime"))
Expect(components[0].Container).ShouldNot(BeNil())
Expect(components[0].Container.Endpoints).Should(HaveLen(3))
for i, p := range []int{1234, 2345, 3456} {
Expect(components[0].Container.Endpoints[i].Name).Should(Equal(fmt.Sprintf("port-%d-tcp", p)))
Expect(components[0].Container.Endpoints[i].TargetPort).Should(Equal(p))
Expect(components[0].Container.Endpoints[i].Protocol).Should(Equal(v1alpha2.TCPEndpointProtocol))
}
})
})
})
})