Skip to content

Commit

Permalink
Support Alizer-based automatic port detection with odo init interacti…
Browse files Browse the repository at this point in the history
…ve mode (redhat-developer#6365)

* Do not display "Port configuration using flag is currently not supported"

This is confusing to users.

* Display ports detected as part of the Alizer component detection logic

This is because we specifically need to display the ports
at the same moment when the language, project type and Devfile
are displayed to the end user (when source code is present)

* Update the loaded Devfile object with the application ports detected prior to asking for its customization

The case of multi-container components will be handled in [1].
Otherwise, the container component ports (all but the Debug ports) are replaced with the right application ports.

[1] redhat-developer#6264

* Add integration test case

* Add application ports detected to "odo analyze -o json" output

* Update documentation

* Add utility functions for helping handle Debug endpoints

* Make application ports detected appear first in the endpoint list

This allows such ports to be port-forwarded first, before the Debug ones.
  • Loading branch information
rm3l authored Dec 6, 2022
1 parent 6883fb9 commit 3c87ecd
Show file tree
Hide file tree
Showing 27 changed files with 807 additions and 65 deletions.
67 changes: 64 additions & 3 deletions docs/website/docs/command-reference/init.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@ title: odo init
The `odo init` command is the first command to be executed when you want to bootstrap a new component, using `odo`. If sources already exist,
the command `odo dev` should be considered instead.

This command must be executed from an empty directory, and as a result, the command will download a `devfile.yaml` file and, optionally, a starter project.
This command must be executed from a directory with no `devfile.yaml` file.

The command can be executed in two flavors, either interactive or non-interactive.

## Running the command
### Interactive mode

In interactive mode, you will be guided to:
In interactive mode, the behavior of `odo init` depends on whether the current directory already contains source code or not.

#### Empty directory

If the directory is empty, you will be guided to:
- choose a devfile from the list of devfiles present in the registry or registries referenced (using the `odo registry` command),
- configure the devfile if there is an existing project
- configure the devfile
- choose a starter project referenced by the selected devfile,
- choose a name for the component present in the devfile; this name must follow the [Kubernetes naming convention](https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#dns-label-names) and not be all-numeric.

Expand All @@ -41,6 +45,63 @@ To deploy your component to a cluster use "odo deploy".
```
</details>

#### Directory with sources

If the current directory is not empty, `odo init` will make its best to autodetect the type of application and propose you a Devfile that should suit your project.
It will try to detect the following, based on the files in the current directory:
- Language
- Project Type
- Ports used in your application
- A Devfile that should help you start with `odo`

If the information detected does not seem correct to you, you are able to select a different Devfile.

In all cases, you will be guided to:
- configure the devfile
- choose a name for the component present in the devfile; this name must follow the [Kubernetes naming convention](https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#dns-label-names) and not be all-numeric.

```console
odo init
```

<details>
<summary>Example</summary>

```console
$ odo init
__
/ \__ Initializing a new component
\__/ \ Files: Source code detected, a Devfile will be determined based upon source code autodetection
/ \__/ odo version: v3.3.0
\__/
Interactive mode enabled, please answer the following questions:
Based on the files in the current directory odo detected
Language: JavaScript
Project type: Node.js
Application ports: 3000
The devfile "nodejs" from the registry "DefaultDevfileRegistry" will be downloaded.
? Is this correct? Yes
✓ Downloading devfile "nodejs" from registry "DefaultDevfileRegistry" [1s]
↪ Container Configuration "runtime":
OPEN PORTS:
- 5858
- 3000
ENVIRONMENT VARIABLES:
- DEBUG_PORT = 5858
? Select container for which you want to change configuration? NONE - configuration is correct
? Enter component name: nodejs
You can automate this command by executing:
odo init --name nodejs --devfile nodejs --devfile-registry DefaultDevfileRegistry

Your new component 'nodejs' is ready in the current directory.
To start editing your component, use 'odo dev' and open this folder in your favorite IDE.
Changes will be directly reflected on the cluster.
```
</details>

### Non-interactive mode

Expand Down
28 changes: 17 additions & 11 deletions docs/website/docs/command-reference/json-output.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ The structures used to return information using JSON output are defined in [the

## odo analyze -o json

The `analyze` command analyzes the files in the current directory to select the best devfiles to use,
from the devfiles in the registries defined in the list of preferred registries with the command `odo preference view`.
The `analyze` command analyzes the files in the current directory and returns the following information:
- the best devfiles to use, from the devfiles in the registries defined in the list of preferred registries with the command `odo preference view`
- the ports used in the application, if that was possible to determine.

The output of this command contains a list of devfile name and registry name:

Expand All @@ -31,14 +32,18 @@ odo analyze -o json
[
{
"devfile": "nodejs",
"devfileRegistry": "DefaultDevfileRegistry"
"devfileRegistry": "DefaultDevfileRegistry",
"ports": [
3000
]
}
]
```
```console
echo $?
```
```console

The exit code should be zero in this case:

```bash
$ echo $?
0
```

Expand All @@ -52,10 +57,11 @@ odo analyze -o json
"message": "No valid devfile found for project in /home/user/my/empty/directory"
}
```
```console
echo $?
```
```console

The command should terminate with a non-zero exit code:

```bash
$ echo $?
1
```

Expand Down
3 changes: 2 additions & 1 deletion docs/website/docs/user-guides/quickstart/_creating_app.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ Interactive mode enabled, please answer the following questions:
Based on the files in the current directory odo detected
Language: `}{props.language}{`
Project type: `}{props.name}{`
The devfile "nodejs" from the registry "DefaultDevfileRegistry" will be downloaded.
Application ports: `}{props.port}{`
The devfile "`}{props.name}{`" from the registry "DefaultDevfileRegistry" will be downloaded.
? Is this correct? Yes
✓ Downloading devfile "`}{props.name}{`" from registry "DefaultDevfileRegistry" [501ms]
Current component configuration:
Expand Down
30 changes: 23 additions & 7 deletions pkg/alizer/alizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (o *Alizer) DetectFramework(ctx context.Context, path string) (model.DevFil
return types[typ], components.Items[typ].Registry, nil
}

// DetectName retrieves the name of the project (if available)
// DetectName retrieves the name of the project (if available).
// If source code is detected:
// 1. Detect the name (pom.xml for java, package.json for nodejs, etc.)
// 2. If unable to detect the name, use the directory name
Expand All @@ -58,11 +58,11 @@ func (o *Alizer) DetectFramework(ctx context.Context, path string) (model.DevFil
// 1. Use the directory name
//
// Last step. Sanitize the name so it's valid for a component name

//
// Use:
// import "github.com/redhat-developer/alizer/pkg/apis/recognizer"
// components, err := recognizer.DetectComponents("./")

//
// In order to detect the name, the name will first try to find out the name based on the program (pom.xml, etc.) but then if not, it will use the dir name.
func (o *Alizer) DetectName(path string) (string, error) {
if path == "" {
Expand Down Expand Up @@ -116,9 +116,25 @@ func (o *Alizer) DetectName(path string) (string, error) {
return name, nil
}

func GetDevfileLocationFromDetection(typ model.DevFileType, registry api.Registry) *api.DevfileLocation {
return &api.DevfileLocation{
Devfile: typ.Name,
DevfileRegistry: registry.Name,
func (o *Alizer) DetectPorts(path string) ([]int, error) {
//TODO(rm3l): Find a better way not to call recognizer.DetectComponents multiple times (in DetectFramework, DetectName and DetectPorts)
components, err := recognizer.DetectComponents(path)
if err != nil {
return nil, err
}

if len(components) == 0 {
klog.V(4).Infof("no components found at path %q", path)
return nil, nil
}

return components[0].Ports, nil
}

func NewDetectionResult(typ model.DevFileType, registry api.Registry, appPorts []int) *api.DetectionResult {
return &api.DetectionResult{
Devfile: typ.Name,
DevfileRegistry: registry.Name,
ApplicationPorts: appPorts,
}
}
1 change: 1 addition & 0 deletions pkg/alizer/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ import (
type Client interface {
DetectFramework(ctx context.Context, path string) (model.DevFileType, api.Registry, error)
DetectName(path string) (string, error)
DetectPorts(path string) ([]int, error)
}
15 changes: 15 additions & 0 deletions pkg/alizer/mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions pkg/api/devfile-location.go → pkg/api/analyze.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package api

// DevfileLocation indicates the location of a devfile, either in a devfile registry or using a path or an URI
type DevfileLocation struct {
// DetectionResult indicates the result of an analysis against a given project.
// Analysis might be performed via the Alizer backend or non-interactively via the Flags backend.
// It contains detection analysis information such as the location of a devfile,
// either in a devfile registry or using a path or a URI or the application ports if any.
type DetectionResult struct {
// name of the Devfile in Devfile registry (required if DevfilePath is not defined)
Devfile string `json:"devfile,omitempty"`

Expand All @@ -10,4 +13,7 @@ type DevfileLocation struct {

// path to a devfile. This is alternative to using devfile from Devfile registry. It can be local filesystem path or http(s) URL (required if Devfile is not defined)
DevfilePath string `json:"devfilePath,omitempty"`

// ApplicationPorts represents the list of ports detected
ApplicationPorts []int `json:"ports,omitempty"`
}
27 changes: 24 additions & 3 deletions pkg/init/backend/alizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package backend
import (
"context"
"fmt"
"strconv"
"strings"

"github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
"github.com/devfile/library/pkg/devfile/parser"

"github.com/redhat-developer/odo/pkg/alizer"
"github.com/redhat-developer/odo/pkg/api"
"github.com/redhat-developer/odo/pkg/init/asker"
Expand All @@ -31,13 +34,27 @@ func (o *AlizerBackend) Validate(flags map[string]string, fs filesystem.Filesyst
}

// SelectDevfile calls thz Alizer to detect the devfile and asks for confirmation to the user
func (o *AlizerBackend) SelectDevfile(ctx context.Context, flags map[string]string, fs filesystem.Filesystem, dir string) (location *api.DevfileLocation, err error) {
func (o *AlizerBackend) SelectDevfile(ctx context.Context, flags map[string]string, fs filesystem.Filesystem, dir string) (location *api.DetectionResult, err error) {
selected, registry, err := o.alizerClient.DetectFramework(ctx, dir)
if err != nil {
return nil, err
}

fmt.Printf("Based on the files in the current directory odo detected\nLanguage: %s\nProject type: %s\n", selected.Language, selected.ProjectType)
msg := fmt.Sprintf("Based on the files in the current directory odo detected\nLanguage: %s\nProject type: %s", selected.Language, selected.ProjectType)

appPorts, err := o.alizerClient.DetectPorts(dir)
if err != nil {
return nil, err
}
appPortsAsString := make([]string, 0, len(appPorts))
for _, p := range appPorts {
appPortsAsString = append(appPortsAsString, strconv.Itoa(p))
}
if len(appPorts) > 0 {
msg += fmt.Sprintf("\nApplication ports: %s", strings.Join(appPortsAsString, ", "))
}

fmt.Println(msg)
fmt.Printf("The devfile %q from the registry %q will be downloaded.\n", selected.Name, registry.Name)
confirm, err := o.askerClient.AskCorrect()
if err != nil {
Expand All @@ -46,7 +63,7 @@ func (o *AlizerBackend) SelectDevfile(ctx context.Context, flags map[string]stri
if !confirm {
return nil, nil
}
return alizer.GetDevfileLocationFromDetection(selected, registry), nil
return alizer.NewDetectionResult(selected, registry, appPorts), nil
}

func (o *AlizerBackend) SelectStarterProject(devfile parser.DevfileObj, flags map[string]string) (starter *v1alpha2.StarterProject, err error) {
Expand All @@ -65,3 +82,7 @@ func (o *AlizerBackend) PersonalizeName(devfile parser.DevfileObj, flags map[str
func (o *AlizerBackend) PersonalizeDevfileConfig(devfile parser.DevfileObj) (parser.DevfileObj, error) {
return devfile, nil
}

func (o *AlizerBackend) HandleApplicationPorts(devfileobj parser.DevfileObj, ports []int, flags map[string]string) (parser.DevfileObj, error) {
return devfileobj, nil
}
Loading

0 comments on commit 3c87ecd

Please sign in to comment.