Skip to content

Commit

Permalink
Select and pull a devfile using Alizer (redhat-developer#5464)
Browse files Browse the repository at this point in the history
* Add alizer library and test functionality

<!--
Thank you for opening a PR! Here are some things you need to know before submitting:

1. Please read our developer guideline: https://github.com/redhat-developer/odo/wiki/Developer-Guidelines
2. Label this PR accordingly with the '/kind' line
3. Ensure you have written and ran the appropriate tests: https://github.com/redhat-developer/odo/wiki/Writing-and-running-tests
4. Read how we approve and LGTM each PR: https://github.com/redhat-developer/odo/wiki/PR-Review

Documentation:

If you are pushing a change to documentation, please read: https://github.com/redhat-developer/odo/wiki/Contributing-to-Docs
-->

**What type of PR is this:**

<!--
Add one of the following kinds:
/kind bug
/kind cleanup
/kind tests
/kind documentation

Feel free to use other [labels](https://github.com/redhat-developer/odo/labels) as needed. However one of the above labels must be present or the PR will not be reviewed. This instruction is for reviewers as well.
-->

/kind feature

**What does this PR do / why we need it:**

Adds the alizer library from
https://github.com/redhat-developer/alizer/tree/main/go as part of our
implementaion of `odo dev` and `odo init`.

This builds upon @feloy 's PR located here: redhat-developer#5434

**Which issue(s) this PR fixes:**
<!--
Specifying the issue will automatically close it when this PR is merged
-->

Fixes #

**PR acceptance criteria:**

- [X] Unit test

- [X] Integration test

- [X] Documentation

**How to test changes / Special notes to the reviewer:**

N/A. Only function implementation

* New alizer version

* Use alizer for odo init

* Add integration tests

* Add Alizer to odo deploy

* review

* Ask component name for odo deploy

* Fix unit test

Co-authored-by: Charlie Drage <[email protected]>
  • Loading branch information
feloy and cdrage committed Aug 31, 2022
1 parent 28d1a74 commit b215e75
Show file tree
Hide file tree
Showing 74 changed files with 13,223 additions and 117 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ require (
github.com/pborman/uuid v1.2.0
github.com/pkg/errors v0.9.1
github.com/posener/complete v1.1.1
github.com/redhat-developer/alizer/go v0.0.0-20220215154256-33df7feef4ae
github.com/redhat-developer/service-binding-operator v0.9.0
github.com/securego/gosec/v2 v2.8.0
github.com/segmentio/backo-go v0.0.0-20200129164019-23eae7c10bd3 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1007,6 +1007,10 @@ github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40T
github.com/pseudomuto/protoc-gen-doc v1.3.2/go.mod h1:y5+P6n3iGrbKG+9O04V5ld71in3v/bX88wUwgt+U8EA=
github.com/pseudomuto/protokit v0.2.0/go.mod h1:2PdH30hxVHsup8KpBTOXTBeMVhJZVio3Q8ViKSAXT0Q=
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
github.com/redhat-developer/alizer/go v0.0.0-20220204121940-dec463ed3af3 h1:PFDkd6xGpa1SwWMj4bKrHc2xPAwA0x2PKZOlo1LfWYQ=
github.com/redhat-developer/alizer/go v0.0.0-20220204121940-dec463ed3af3/go.mod h1:18H8Trq+vkpxqek82e7SimdNTdFfOGGjN7gAUGDO3jU=
github.com/redhat-developer/alizer/go v0.0.0-20220215154256-33df7feef4ae h1:N2wsIYtziHQ51GNcJY5YcB0YldpR5BwPoTvby+l0vy8=
github.com/redhat-developer/alizer/go v0.0.0-20220215154256-33df7feef4ae/go.mod h1:EKkrP0Am7Xt/yg3dF8uH1SSoOcaZmBom8Iy6CJPPDok=
github.com/redhat-developer/service-binding-operator v0.9.0 h1:CS+eEtzu/PtWuyvYQFQpZXd6ukSuFtN+U0EKKtTsvlA=
github.com/redhat-developer/service-binding-operator v0.9.0/go.mod h1:D415gZQiz5Q8zyRbmrNrlieb6Xp73oFtCb+nCuTL6GA=
github.com/remyoudompheng/bigfft v0.0.0-20170806203942-52369c62f446/go.mod h1:uYEyJGbgTkfkS4+E/PavXkNJcbFIpEtjt2B0KDQ5+9M=
Expand Down Expand Up @@ -1299,6 +1303,8 @@ golang.org/x/mod v0.3.1-0.20200828183125-ce943fd02449/go.mod h1:s0Qsj1ACt9ePp/hM
golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.2 h1:Gz96sIWK3OalVv/I/qNygP42zyoKp3xptRVCWRFEBvo=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.5.1 h1:OJxoQ/rynoF0dcCdI7cLPktw/hR2cueqYfjm43oqK38=
golang.org/x/mod v0.5.1/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro=
golang.org/x/net v0.0.0-20170114055629-f2499483f923/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down
1 change: 1 addition & 0 deletions pkg/catalog/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ func createRegistryDevfiles(registry Registry, devfileIndex []indexSchema.Schema
Registry: registry,
Language: devfileIndexEntry.Language,
Tags: devfileIndexEntry.Tags,
ProjectType: devfileIndexEntry.ProjectType,
}
registryDevfiles = append(registryDevfiles, stackDevfile)
}
Expand Down
1 change: 1 addition & 0 deletions pkg/catalog/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type DevfileComponentType struct {
Registry Registry
Language string
Tags []string
ProjectType string
}

// DevfileComponentTypeList lists all the DevfileComponentType's
Expand Down
46 changes: 46 additions & 0 deletions pkg/devfile/location/location.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package location

import (
"os"
"path/filepath"

"github.com/redhat-developer/odo/pkg/testingutil/filesystem"
"github.com/redhat-developer/odo/pkg/util"
)

Expand Down Expand Up @@ -30,3 +32,47 @@ func DevfileLocation(contexDir string) string {
devFile := DevfileFilenamesProvider(contexDir)
return filepath.Join(contexDir, devFile)
}

// IsDevfileName returns true if name is a supported name for a devfile
func IsDevfileName(name string) bool {
for _, devFile := range possibleDevfileNames {
if devFile == name {
return true
}
}
return false
}

// DirectoryContainsDevfile returns true if the given directory contains a devfile with a supported name
func DirectoryContainsDevfile(fsys filesystem.Filesystem, dir string) (bool, error) {
for _, devFile := range possibleDevfileNames {
_, err := fsys.Stat(filepath.Join(dir, devFile))
if os.IsNotExist(err) {
continue
} else if err != nil {
return false, err
}
// path to file does exist
return true, nil
}
return false, nil
}

// DirIsEmpty returns true if the given directory contains no file
func DirIsEmpty(fsys filesystem.Filesystem, path string) (bool, error) {
files, err := fsys.ReadDir(path)
if err != nil {
return false, err
}
return len(files) == 0, nil
}

// DirContainsOnlyDevfile returns true if the directory contains only one file which is a devfile
// with a supported name
func DirContainsOnlyDevfile(fsys filesystem.Filesystem, path string) (bool, error) {
files, err := fsys.ReadDir(path)
if err != nil {
return false, err
}
return len(files) == 1 && IsDevfileName(files[0].Name()), nil
}
13 changes: 13 additions & 0 deletions pkg/init/asker/asker.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,16 @@ func (o *Survey) AskName(defaultName string) (string, error) {
}
return answer, nil
}

func (o *Survey) AskCorrect() (bool, error) {
question := &survey.Confirm{
Message: "Is this correct?",
Default: true,
}
var answer bool
err := survey.AskOne(question, &answer)
if err != nil {
return false, err
}
return answer, nil
}
3 changes: 3 additions & 0 deletions pkg/init/asker/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ type Asker interface {

// AskName asks for a devfile component name
AskName(defaultName string) (string, error)

// AskCorrect asks for confirmation
AskCorrect() (bool, error)
}
15 changes: 15 additions & 0 deletions pkg/init/asker/mock.go

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

92 changes: 92 additions & 0 deletions pkg/init/backend/alizer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package backend

import (
"fmt"
"reflect"

"github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
"github.com/devfile/library/pkg/devfile/parser"
"github.com/redhat-developer/alizer/go/pkg/apis/recognizer"
"github.com/redhat-developer/odo/pkg/catalog"
"github.com/redhat-developer/odo/pkg/init/asker"
"github.com/redhat-developer/odo/pkg/testingutil/filesystem"
)

type AlizerBackend struct {
askerClient asker.Asker
catalogClient catalog.Client
}

func NewAlizerBackend(askerClient asker.Asker, catalogClient catalog.Client) *AlizerBackend {
return &AlizerBackend{
askerClient: askerClient,
catalogClient: catalogClient,
}
}

func (o *AlizerBackend) Validate(flags map[string]string, fs filesystem.Filesystem, dir string) error {
return nil
}

// detectFramework uses the alizer library in order to detect the devfile
// to use depending on the files in the path
func (o *AlizerBackend) detectFramework(path string) (recognizer.DevFileType, catalog.Registry, error) {
types := []recognizer.DevFileType{}
components, err := o.catalogClient.ListDevfileComponents("")
if err != nil {
return recognizer.DevFileType{}, catalog.Registry{}, err
}
for _, component := range components.Items {
types = append(types, recognizer.DevFileType{
Name: component.Name,
Language: component.Language,
ProjectType: component.ProjectType,
Tags: component.Tags,
})
}
typ, err := recognizer.SelectDevFileFromTypes(path, types)
if err != nil {
return recognizer.DevFileType{}, catalog.Registry{}, err
}

// TODO(feloy): This part won't be necessary when SelectDevFileFromTypes returns the index
var indexOfDetected int
for i, typeFromList := range types {
if reflect.DeepEqual(typeFromList, typ) {
indexOfDetected = i
break
}
}
registry := components.Items[indexOfDetected].Registry
return typ, registry, nil
}

// SelectDevfile calls thz Alizer to detect the devfile and asks for confirmation to the user
func (o *AlizerBackend) SelectDevfile(flags map[string]string, fs filesystem.Filesystem, dir string) (location *DevfileLocation, err error) {
selected, registry, err := o.detectFramework(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)
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 {
return nil, err
}
if !confirm {
return nil, nil
}
return &DevfileLocation{
Devfile: selected.Name,
DevfileRegistry: registry.Name,
}, nil
}

func (o *AlizerBackend) SelectStarterProject(devfile parser.DevfileObj, flags map[string]string) (starter *v1alpha2.StarterProject, err error) {
return nil, nil
}

func (o *AlizerBackend) PersonalizeName(devfile parser.DevfileObj, flags map[string]string) error {
return nil
}
Loading

0 comments on commit b215e75

Please sign in to comment.