Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ $ go build alizer.go
```sh
--log {debug|info|warning} sets the logging level of the CLI. The arg accepts only 3 values [`debug`, `info`, `warning`]. The default value is `warning` and the logging level is `ErrorLevel`.
--registry strings registry where to download the devfiles. Default value: https://registry.devfile.io
--min-version strings the minimum SchemaVersion of the matched devfile(s). The minimum accepted value is `2.0.0`, otherwise an error is returned.
--max-version strings the maximum SchemaVersion of the matched devfile(s). The minimum accepted value is `2.0.0`, otherwise an error is returned.
--min-schema-version strings the minimum SchemaVersion of the matched devfile(s). The minimum accepted value is `2.0.0`, otherwise an error is returned.
--max-schema-version strings the maximum SchemaVersion of the matched devfile(s). The minimum accepted value is `2.0.0`, otherwise an error is returned.
```

### Library Package
Expand Down
50 changes: 29 additions & 21 deletions docs/proposals/support_22x_devfiles.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ As mentioned above, there is common ground on the two issues. This means that we
## CLI Arguments
The `alizer devfile` command should have arguments in order to specify which devfiles are acceptable or not.

### max-version
### max-schema-version
If this argument is passed in a alizer devfile command then, alizer should match a devfile with **equal or older version** than the one given.

### min-version
### min-schema-version
If this argument is passed in a alizer devfile command then, alizer should match a devfile with **equal or newer version** than the one given.

_The results of devfiles have to be sorted from newest to oldest._
Expand Down Expand Up @@ -104,10 +104,10 @@ metadata:
...
```

### Case A - Define only a max-version.
If we choose `max-version = 2.1.0` we should get a list of 2 devfiles from our devfile command:
### Case A - Define only a max-schema-version.
If we choose `max-schema-version = 2.1.0` we should get a list of 2 devfiles from our devfile command:
```bash
$ ./alizer devfile --max-version 2.1.0
$ ./alizer devfile --max-schema-version 2.1.0
[
{
"Name": "devfileB",
Expand All @@ -128,10 +128,10 @@ $ ./alizer devfile --max-version 2.1.0
]
```

### Case B - Define only a min-version.
If we choose `min-version = 2.1.0` we should get a list of 2 devfiles from our devfile command:
### Case B - Define only a min-schema-version.
If we choose `min-schema-version = 2.1.0` we should get a list of 2 devfiles from our devfile command:
```bash
$ ./alizer devfile --min-version 2.1.0
$ ./alizer devfile --min-schema-version 2.1.0
[
{
"Name": "devfileC",
Expand All @@ -152,10 +152,10 @@ $ ./alizer devfile --min-version 2.1.0
]
```

### Case C - Define both min-version and max-version.
If we choose `min-version = 2.1.0` and `max-version = 2.1.2` we should get a list of 1 devfile from our devfile command:
### Case C - Define both min-schema-version and max-schema-version.
If we choose `min-schema-version = 2.1.0` and `max-schema-version = 2.1.2` we should get a list of 1 devfile from our devfile command:
```bash
$ ./alizer devfile --min-version 2.1.0 --max-version 2.1.2
$ ./alizer devfile --min-schema-version 2.1.0 --max-schema-version 2.1.2
[
{
"Name": "devfileB",
Expand All @@ -168,7 +168,7 @@ $ ./alizer devfile --min-version 2.1.0 --max-version 2.1.2
]
```

_An error will be raised if the min-version is greater than the max-version_
_An error will be raised if the min-schema-version is greater than the max-schema-version_

## Library
In order to facilitate these updates we will have to update our library. The main work will focus inside `devfile_recognizer.go`. There, we will have to implement a new function (`MatchDevfiles`) which will handle cases that we want to filter the devfiles list fetched from a registry. Another addition would be to add a new attribute `SchemaVersion` string in the `model.DevFileType` (`SchemaVersion string`).
Expand All @@ -181,21 +181,29 @@ _We have to keep the old way of fetching devfiles from library in order to ensur
This function will take 3 parameters `(path string, devFileTypes []model.DevFileType, filter map[string]interface{})`. If someone wants to filter the selected devfiles for specific version range the have to write:
```golang
import "github.com/devfile/alizer/pkg/apis/recognizer"
filter := map[string]interface{} {
"max-version": "2.1.0",
"min-version": "2.0.0",
import "github.com/devfile/alizer/pkg/apis/model"

devifileFilter := model.DevfileFilter {
MinSchemaVersion: "2.0.0",
MaxSchemaVersion: "2.2.0",
}
devfiles, err := recognizer.MatchDevfiles("myproject", devfiles, filter)
devfiles, err := recognizer.MatchDevfiles("myproject", devfiles, devifileFilter)
```

### model.DevfileType
In order to be able to check and compare the version of each devfile with the given attribute we have to create a dedicated attribute inside this model:
```golang
type Version struct {
SchemaVersion string
Default bool
Version string
}

type DevFileType struct {
Name string
Language string
ProjectType string
Tags []string
SchemaVersion string
Name string
Language string
ProjectType string
Tags []string
Versions []Version
}
```
5 changes: 3 additions & 2 deletions pkg/apis/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,12 @@ type DevFileType struct {
Language string
ProjectType string
Tags []string
Versions []Version
}

type DevfileFilter struct {
MinVersion string
MaxVersion string
MinSchemaVersion string
MaxSchemaVersion string
}

type ApplicationFileInfo struct {
Expand Down
30 changes: 15 additions & 15 deletions pkg/apis/recognizer/devfile_recognizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func SelectDevFilesFromRegistry(path string, url string) ([]model.DevFileType, e
alizerLogger := utils.GetOrCreateLogger()
alizerLogger.V(0).Info("Starting devfile matching")
alizerLogger.V(1).Info(fmt.Sprintf("Downloading devfiles from registry %s", url))
devFileTypesFromRegistry, err := downloadDevFileTypesFromRegistry(url, model.DevfileFilter{MinVersion: "", MaxVersion: ""})
devFileTypesFromRegistry, err := downloadDevFileTypesFromRegistry(url, model.DevfileFilter{MinSchemaVersion: "", MaxSchemaVersion: ""})
if err != nil {
return []model.DevFileType{}, err
}
Expand All @@ -145,7 +145,7 @@ func selectDevfiles(path string, devFileTypesFromRegistry []model.DevFileType) (
}

func SelectDevFileFromRegistry(path string, url string) (model.DevFileType, error) {
devFileTypes, err := downloadDevFileTypesFromRegistry(url, model.DevfileFilter{MinVersion: "", MaxVersion: ""})
devFileTypes, err := downloadDevFileTypesFromRegistry(url, model.DevfileFilter{MinSchemaVersion: "", MaxSchemaVersion: ""})
if err != nil {
return model.DevFileType{}, err
}
Expand All @@ -157,47 +157,47 @@ func SelectDevFileFromRegistry(path string, url string) (model.DevFileType, erro
return devFileTypes[index], nil
}

func GetUrlWithVersions(url, minVersion, maxVersion string) (string, error) {
func GetUrlWithVersions(url, minSchemaVersion, maxSchemaVersion string) (string, error) {
minAllowedVersion, err := version.NewVersion(MinimumAllowedVersion)
if err != nil {
return "", nil
}

if minVersion != "" && maxVersion != "" {
minV, err := version.NewVersion(minVersion)
if minSchemaVersion != "" && maxSchemaVersion != "" {
minV, err := version.NewVersion(minSchemaVersion)
if err != nil {
return url, nil
}
maxV, err := version.NewVersion(maxVersion)
maxV, err := version.NewVersion(maxSchemaVersion)
if err != nil {
return url, nil
}
if maxV.LessThan(minV) {
return "", fmt.Errorf("max-version cannot be lower than min-version")
return "", fmt.Errorf("max-schema-version cannot be lower than min-schema-version")
}
if maxV.LessThan(minAllowedVersion) || minV.LessThan(minAllowedVersion) {
return "", fmt.Errorf("min and/or max version are lower than the minimum allowed version (2.0.0)")
}

return fmt.Sprintf("%s?minSchemaVersion=%s&maxSchemaVersion=%s", url, minVersion, maxVersion), nil
} else if minVersion != "" {
minV, err := version.NewVersion(minVersion)
return fmt.Sprintf("%s?minSchemaVersion=%s&maxSchemaVersion=%s", url, minSchemaVersion, maxSchemaVersion), nil
} else if minSchemaVersion != "" {
minV, err := version.NewVersion(minSchemaVersion)
if err != nil {
return "", nil
}
if minV.LessThan(minAllowedVersion) {
return "", fmt.Errorf("min version is lower than the minimum allowed version (2.0.0)")
}
return fmt.Sprintf("%s?minSchemaVersion=%s", url, minVersion), nil
} else if maxVersion != "" {
maxV, err := version.NewVersion(maxVersion)
return fmt.Sprintf("%s?minSchemaVersion=%s", url, minSchemaVersion), nil
} else if maxSchemaVersion != "" {
maxV, err := version.NewVersion(maxSchemaVersion)
if err != nil {
return "", nil
}
if maxV.LessThan(minAllowedVersion) {
return "", fmt.Errorf("max version is lower than the minimum allowed version (2.0.0)")
}
return fmt.Sprintf("%s?maxSchemaVersion=%s", url, maxVersion), nil
return fmt.Sprintf("%s?maxSchemaVersion=%s", url, maxSchemaVersion), nil
} else {
return url, nil
}
Expand All @@ -206,7 +206,7 @@ func GetUrlWithVersions(url, minVersion, maxVersion string) (string, error) {
func downloadDevFileTypesFromRegistry(url string, filter model.DevfileFilter) ([]model.DevFileType, error) {
url = adaptUrl(url)
tmpUrl := appendIndexPath(url)
url, err := GetUrlWithVersions(tmpUrl, filter.MinVersion, filter.MaxVersion)
url, err := GetUrlWithVersions(tmpUrl, filter.MinSchemaVersion, filter.MaxSchemaVersion)
if err != nil {
return nil, err
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/cli/devfile/devfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/spf13/cobra"
)

var logLevel, registry, minVersion, maxVersion string
var logLevel, registry, minSchemaVersion, maxSchemaVersion string

func NewCmdDevfile() *cobra.Command {
devfileCmd := &cobra.Command{
Expand All @@ -18,8 +18,8 @@ func NewCmdDevfile() *cobra.Command {
Run: doSelectDevfile,
}
devfileCmd.Flags().StringVar(&logLevel, "log", "", "log level for alizer. Default value: error. Accepted values: [debug, info, warning]")
devfileCmd.Flags().StringVar(&minVersion, "min-version", "", "minimum version of devfile schemaVersion. Minimum allowed version: 2.0.0")
devfileCmd.Flags().StringVar(&maxVersion, "max-version", "", "maximum version of devfile schemaVersion. Minimum allowed version: 2.0.0")
devfileCmd.Flags().StringVar(&minSchemaVersion, "min-schema-version", "", "minimum version of devfile schemaVersion. Minimum allowed version: 2.0.0")
devfileCmd.Flags().StringVar(&maxSchemaVersion, "max-schema-version", "", "maximum version of devfile schemaVersion. Minimum allowed version: 2.0.0")
devfileCmd.Flags().StringVarP(&registry, "registry", "r", "", "registry where to download the devfiles. Default value: https://registry.devfile.io")
return devfileCmd
}
Expand All @@ -38,8 +38,8 @@ func doSelectDevfile(cmd *cobra.Command, args []string) {
return
}
filter := model.DevfileFilter{
MinVersion: minVersion,
MaxVersion: maxVersion,
MinSchemaVersion: minSchemaVersion,
MaxSchemaVersion: maxSchemaVersion,
}
utils.PrintPrettifyOutput(recognizer.MatchDevfiles(args[0], registry, filter))
}
110 changes: 55 additions & 55 deletions test/apis/devfile_recognizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,93 +137,93 @@ func TestDetectLaravelDevfile(t *testing.T) {

func TestGetUrlWithVersions(t *testing.T) {
tests := []struct {
name string
minVersion string
maxVersion string
testUrl string
expectedError error
expectedUrl string
name string
minSchemaVersion string
maxSchemaVersion string
testUrl string
expectedError error
expectedUrl string
}{
{

name: "Case 1: Url with valid min and max versions",
minVersion: "2.0.0",
maxVersion: "2.2.0",
testUrl: "http://localhost:5000/",
expectedError: nil,
name: "Case 1: Url with valid min and max versions",
minSchemaVersion: "2.0.0",
maxSchemaVersion: "2.2.0",
testUrl: "http://localhost:5000/",
expectedError: nil,
},
{
name: "Case 2: Url with valid min version",
minVersion: "2.0.0",
maxVersion: "",
testUrl: "http://localhost:5000",
expectedError: nil,
name: "Case 2: Url with valid min version",
minSchemaVersion: "2.0.0",
maxSchemaVersion: "",
testUrl: "http://localhost:5000",
expectedError: nil,
},
{
name: "Case 3: Url with valid max version",
minVersion: "",
maxVersion: "2.2.0",
testUrl: "http://localhost:5000/",
expectedError: nil,
name: "Case 3: Url with valid max version",
minSchemaVersion: "",
maxSchemaVersion: "2.2.0",
testUrl: "http://localhost:5000/",
expectedError: nil,
},
{
name: "Case 4: Url with max version lower than min version",
minVersion: "2.2.0",
maxVersion: "2.1.0",
testUrl: "http://localhost:5000/v2index",
expectedError: fmt.Errorf("max-version cannot be lower than min-version"),
name: "Case 4: Url with max version lower than min version",
minSchemaVersion: "2.2.0",
maxSchemaVersion: "2.1.0",
testUrl: "http://localhost:5000/v2index",
expectedError: fmt.Errorf("max-schema-version cannot be lower than min-schema-version"),
},
{
name: "Case 5: Url with max version lower than minimum allowed version",
minVersion: "0.1.0",
maxVersion: "1.1.0",
testUrl: "http://localhost:5000/v2index",
expectedError: fmt.Errorf("min and/or max version are lower than the minimum allowed version (2.0.0)"),
name: "Case 5: Url with max version lower than minimum allowed version",
minSchemaVersion: "0.1.0",
maxSchemaVersion: "1.1.0",
testUrl: "http://localhost:5000/v2index",
expectedError: fmt.Errorf("min and/or max version are lower than the minimum allowed version (2.0.0)"),
},
{
name: "Case 6: Url with max version lower than minimum allowed version & minVersion",
minVersion: "2.1.0",
maxVersion: "1.1.0",
testUrl: "http://localhost:5000/v2index",
expectedError: fmt.Errorf("max-version cannot be lower than min-version"),
name: "Case 6: Url with max-schema-version lower than minimum allowed version & min-schema-version",
minSchemaVersion: "2.1.0",
maxSchemaVersion: "1.1.0",
testUrl: "http://localhost:5000/v2index",
expectedError: fmt.Errorf("max-schema-version cannot be lower than min-schema-version"),
},
{
name: "Case 7: Url with min version lower than minimum allowed version",
minVersion: "1.1.0",
maxVersion: "",
testUrl: "http://localhost:5000/v2index",
expectedError: fmt.Errorf("min version is lower than the minimum allowed version (2.0.0)"),
name: "Case 7: Url with min version lower than minimum allowed version",
minSchemaVersion: "1.1.0",
maxSchemaVersion: "",
testUrl: "http://localhost:5000/v2index",
expectedError: fmt.Errorf("min version is lower than the minimum allowed version (2.0.0)"),
},
{
name: "Case 8: Url with max version lower than minimum allowed version",
minVersion: "",
maxVersion: "1.1.0",
testUrl: "http://localhost:5000/v2index",
expectedError: fmt.Errorf("max version is lower than the minimum allowed version (2.0.0)"),
name: "Case 8: Url with max version lower than minimum allowed version",
minSchemaVersion: "",
maxSchemaVersion: "1.1.0",
testUrl: "http://localhost:5000/v2index",
expectedError: fmt.Errorf("max version is lower than the minimum allowed version (2.0.0)"),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := recognizer.GetUrlWithVersions(tt.testUrl, tt.minVersion, tt.maxVersion)
result, err := recognizer.GetUrlWithVersions(tt.testUrl, tt.minSchemaVersion, tt.maxSchemaVersion)
if err != nil {
assert.EqualValues(t, tt.expectedError, err)
} else {
assert.EqualValues(t, getExceptedVersionsUrl(tt.testUrl, tt.minVersion, tt.maxVersion, tt.expectedError), result)
assert.EqualValues(t, getExceptedVersionsUrl(tt.testUrl, tt.minSchemaVersion, tt.maxSchemaVersion, tt.expectedError), result)
}
})
}
}

func getExceptedVersionsUrl(url, minVersion, maxVersion string, err error) string {
func getExceptedVersionsUrl(url, minSchemaVersion, maxSchemaVersion string, err error) string {
if err != nil {
return ""
} else if minVersion != "" && maxVersion != "" {
return fmt.Sprintf("%s?minSchemaVersion=%s&maxSchemaVersion=%s", url, minVersion, maxVersion)
} else if minVersion != "" {
return fmt.Sprintf("%s?minSchemaVersion=%s", url, minVersion)
} else if maxVersion != "" {
return fmt.Sprintf("%s?maxSchemaVersion=%s", url, maxVersion)
} else if minSchemaVersion != "" && maxSchemaVersion != "" {
return fmt.Sprintf("%s?minSchemaVersion=%s&maxSchemaVersion=%s", url, minSchemaVersion, maxSchemaVersion)
} else if minSchemaVersion != "" {
return fmt.Sprintf("%s?minSchemaVersion=%s", url, minSchemaVersion)
} else if maxSchemaVersion != "" {
return fmt.Sprintf("%s?maxSchemaVersion=%s", url, maxSchemaVersion)
} else {
return url
}
Expand Down