Skip to content

Commit

Permalink
odo describe component: display remote source code location for each …
Browse files Browse the repository at this point in the history
…container component (#6497)

* Describe remote source code location for container component

Signed-off-by: Parthvi Vala <pvala@redhat.com>

* Add integration test

Signed-off-by: Parthvi Vala <pvala@redhat.com>

* Fix cI failures

* Add extra check for .mountSources and add more integration tests

Signed-off-by: Parthvi Vala <pvala@redhat.com>

* Use GetMountSources() instead of property

Signed-off-by: Parthvi Vala <pvala@redhat.com>

* Use random name in integration test

Signed-off-by: Parthvi Vala <pvala@redhat.com>

Signed-off-by: Parthvi Vala <pvala@redhat.com>
  • Loading branch information
valaparthvi authored Jan 19, 2023
1 parent b86afbc commit 992c48a
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 1 deletion.
30 changes: 29 additions & 1 deletion pkg/odo/cli/describe/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import (
"strings"

"github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
"github.com/devfile/library/pkg/devfile/generator"
"github.com/devfile/library/pkg/devfile/parser"
"github.com/devfile/library/pkg/devfile/parser/data/v2/common"
"github.com/spf13/cobra"
"k8s.io/klog"
ktemplates "k8s.io/kubectl/pkg/util/templates"

"github.com/redhat-developer/odo/pkg/api"
Expand Down Expand Up @@ -201,6 +203,7 @@ func (o *ComponentOptions) describeNamedComponent(ctx context.Context, name stri
// Display RunningOn field only if the feature is enabled
cmp.RunningOn = nil
}

return cmp, &devfile, nil
}

Expand Down Expand Up @@ -306,9 +309,30 @@ func (o *ComponentOptions) describeDevfileComponent(ctx context.Context) (result
// Display RunningOn field only if the feature is enabled
cmp.RunningOn = nil
}
updateWithRemoteSourceLocation(&cmp)
return cmp, devfileObj, err
}

func updateWithRemoteSourceLocation(cmp *api.Component) {
components, err := cmp.DevfileData.Devfile.GetComponents(common.DevfileOptions{
ComponentOptions: common.ComponentOptions{ComponentType: v1alpha2.ContainerComponentType},
})
if err != nil {
return
}
for _, comp := range components {
if comp.Container.GetMountSources() {
if comp.Container.SourceMapping == "" {
comp.Container.SourceMapping = generator.DevfileSourceVolumeMount
err = cmp.DevfileData.Devfile.UpdateComponent(comp)
if err != nil {
klog.V(2).Infof("error occurred while updating the component %s; cause: %s", comp.Name, err)
}
}
}
}
}

func getRunningOn(ctx context.Context, n string, kubeClient kclient.ClientInterface, podmanClient podman.Client) (map[string]api.RunningModes, error) {
var runningOn map[string]api.RunningModes
runningModesMap, err := component.GetRunningModes(ctx, kubeClient, podmanClient, n)
Expand Down Expand Up @@ -443,7 +467,11 @@ func listComponentsNames(title string, devfileObj *parser.DevfileObj, typ v1alph
}
log.Info(title)
for _, container := range containers {
log.Printf("%s", container.Name)
printmsg := container.Name
if container.Container != nil && container.Container.GetMountSources() {
printmsg += fmt.Sprintf("\n Source Mapping: %s", container.Container.SourceMapping)
}
log.Printf(printmsg)
}
fmt.Println()
return nil
Expand Down
89 changes: 89 additions & 0 deletions tests/integration/cmd_describe_component_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -599,4 +599,93 @@ var _ = Describe("odo describe component command tests", func() {
})
}
})
Context("checking for remote source code location", func() {
for _, podman := range []bool{true, false} {
podman := podman
for _, ctx := range []struct {
devfile, title string
checker func(output string, isJSON bool)
beforeEach func()
}{
{
title: "devfile with sourceMapping",
devfile: "devfileSourceMapping.yaml",
checker: func(output string, isJSON bool) {
const location = "/test"
if isJSON {
helper.JsonPathContentIs(output, "devfileData.devfile.components.#(name==runtime).container.sourceMapping", location)
return
}
Expect(output).To(ContainSubstring(location))
},
},
{
devfile: "devfile.yaml",
title: "devfile with no sourceMapping, defaults to /projects",
checker: func(output string, isJSON bool) {
const location = "/projects"
if isJSON {
helper.JsonPathContentIs(output, "devfileData.devfile.components.#(name==runtime).container.sourceMapping", location)
return
}
Expect(output).To(ContainSubstring(location))
},
},
{
devfile: "devfileCompositeBuildRunDebugInMultiContainersAndSharedVolume.yaml",
title: "devfile with containers that has mountSource set to false",
checker: func(output string, isJSON bool) {
if isJSON {
helper.JsonPathContentIs(output, "devfileData.devfile.components.#(name==runtime).container.sourceMapping", "/projects")
helper.JsonPathDoesNotExist(output, "devfileData.devfile.components.#(name==sleeper-run).container.sourceMapping")
helper.JsonPathDoesNotExist(output, "devfileData.devfile.components.#(name==sleeper-build).container.sourceMapping")
helper.JsonPathDoesNotExist(output, "devfileData.devfile.components.#(name==echo-er).container.sourceMapping")
helper.JsonPathDoesNotExist(output, "devfileData.devfile.components.#(name==build-checker).container.sourceMapping")
return
}
Expect(output).To(ContainSubstring("runtime\n Source Mapping: /projects"))
helper.DontMatchAllInOutput(output, []string{"sleeper-run\n Source Mapping: /projects", "sleeper-build\n Source Mapping:", "echo-er\n Source Mapping:", "build-checker\n Source Mapping:"})
},
},
} {
ctx := ctx
When(fmt.Sprintf("using %s and starting an odo dev session", ctx.title), helper.LabelPodmanIf(podman, func() {
var devSession helper.DevSession
BeforeEach(func() {
helper.CopyExample(filepath.Join("source", "nodejs"), commonVar.Context)
helper.Cmd("odo", "init", "--name", cmpName, "--devfile-path", helper.GetExamplePath("source", "devfiles", "nodejs", ctx.devfile)).ShouldPass()
var err error
devSession, _, _, _, err = helper.StartDevMode(helper.DevSessionOpts{RunOnPodman: podman})
Expect(err).ToNot(HaveOccurred())
})
AfterEach(func() {
devSession.Stop()
devSession.WaitEnd()
})
It("should show remote source code location in odo describe component output", func() {
By("checking human readable output", func() {
args := []string{"describe", "component"}
cmd := helper.Cmd("odo", args...)
if podman {
args = append(args, "--platform=podman")
cmd = helper.Cmd("odo", args...).AddEnv("ODO_EXPERIMENTAL_MODE=true")
}
output := cmd.ShouldPass().Out()
ctx.checker(output, false)
})
By("checking JSON output", func() {
args := []string{"describe", "component", "-ojson"}
cmd := helper.Cmd("odo", args...)
if podman {
args = append(args, "--platform=podman")
cmd = helper.Cmd("odo", args...).AddEnv("ODO_EXPERIMENTAL_MODE=true")
}
output := cmd.ShouldPass().Out()
ctx.checker(output, true)
})
})
}))
}
}
})
})

0 comments on commit 992c48a

Please sign in to comment.