Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix dependencies v1 definition and conversion #2232

Merged
merged 2 commits into from
Jul 11, 2022
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
29 changes: 21 additions & 8 deletions pkg/cnab/config-adapter/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"get.porter.sh/porter/pkg/manifest"
"get.porter.sh/porter/pkg/mixin"
"get.porter.sh/porter/pkg/tracing"
"github.com/Masterminds/semver/v3"
"github.com/cnabio/cnab-go/bundle"
"github.com/cnabio/cnab-go/bundle/definition"
)
Expand Down Expand Up @@ -71,7 +72,11 @@ func (c *ManifestConverter) ToBundle(ctx context.Context) (cnab.ExtendedBundle,
b.Outputs = c.generateBundleOutputs(ctx, &b.Definitions)
b.Credentials = c.generateBundleCredentials()
b.Images = c.generateBundleImages()
b.Custom = c.generateCustomExtensions(&b)
custom, err := c.generateCustomExtensions(&b)
if err != nil {
return cnab.ExtendedBundle{}, err
}
b.Custom = custom
b.RequiredExtensions = c.generateRequiredExtensions(b)

b.Custom[config.CustomPorterKey] = stamp
Expand Down Expand Up @@ -401,10 +406,9 @@ func (c *ManifestConverter) generateBundleImages() map[string]bundle.Image {
return images
}

func (c *ManifestConverter) generateDependencies() *cnab.Dependencies {

func (c *ManifestConverter) generateDependencies() (*cnab.Dependencies, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I'm missing something, I can't find where this function is returning an error?

Copy link
Member Author

@carolynvs carolynvs Jul 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry the I was chopping up #2099 into refactorings and while that larger PR needs to return an error this exact change here doesn't use it yet. Are you okay with me keeping that in place here so that I don't need to redo both PRs to shuffle when we introduce that change? I promise we will be returning errors by the time I finish splitting up that big PR. 😀

if len(c.Manifest.Dependencies.RequiredDependencies) == 0 {
return nil
return nil, nil
}

deps := &cnab.Dependencies{
Expand All @@ -421,12 +425,18 @@ func (c *ManifestConverter) generateDependencies() *cnab.Dependencies {
dependencyRef.Version = &cnab.DependencyVersion{
Ranges: []string{dep.Bundle.Version},
}

// If we can detect that prereleases are used in the version, then set AllowPrereleases to true
v, err := semver.NewVersion(dep.Bundle.Version)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we return the error here if it's not nil?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, because we are just parsing the value to see if we can detect the use of a prerelease value. If they are using a semver constrain, the value isn't parsable as a singular version.

If we want to validate values from the porter.yaml, it should be in the Manifest.Validate function. In general though we aren't going to do that for deps since not all tags are semver values.

if err == nil {
dependencyRef.Version.AllowPrereleases = v.Prerelease() != ""
}
}
deps.Sequence = append(deps.Sequence, dep.Name)
deps.Requires[dep.Name] = dependencyRef
}

return deps
return deps, nil
}

func (c *ManifestConverter) generateParameterSources(b *cnab.ExtendedBundle) cnab.ParameterSources {
Expand Down Expand Up @@ -576,7 +586,7 @@ func toFloat(v float64) *float64 {
return &v
}

func (c *ManifestConverter) generateCustomExtensions(b *cnab.ExtendedBundle) map[string]interface{} {
func (c *ManifestConverter) generateCustomExtensions(b *cnab.ExtendedBundle) (map[string]interface{}, error) {
customExtensions := map[string]interface{}{
cnab.FileParameterExtensionKey: struct{}{},
}
Expand All @@ -587,7 +597,10 @@ func (c *ManifestConverter) generateCustomExtensions(b *cnab.ExtendedBundle) map
}

// Add the dependency extension
deps := c.generateDependencies()
deps, err := c.generateDependencies()
if err != nil {
return nil, err
}
if deps != nil && len(deps.Requires) > 0 {
customExtensions[cnab.DependenciesExtensionKey] = deps
}
Expand All @@ -603,7 +616,7 @@ func (c *ManifestConverter) generateCustomExtensions(b *cnab.ExtendedBundle) map
customExtensions[lookupExtensionKey(ext.Name)] = ext.Config
}

return customExtensions
return customExtensions, nil
}

func (c *ManifestConverter) generateRequiredExtensions(b cnab.ExtendedBundle) []string {
Expand Down
20 changes: 9 additions & 11 deletions pkg/cnab/config-adapter/adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -544,33 +544,30 @@ func TestManifestConverter_generateDependencies(t *testing.T) {
Name: "mysql",
Bundle: "getporter/azure-mysql:5.7",
}},
{"no-ranges", cnab.Dependency{
{"no-ranges, uses prerelease", cnab.Dependency{
Name: "ad",
Bundle: "getporter/azure-active-directory",
Version: &cnab.DependencyVersion{
AllowPrereleases: true,
Ranges: []string{"1.0.0-0"},
},
}},
{"with-ranges", cnab.Dependency{
Name: "storage",
Bundle: "getporter/azure-blob-storage",
Version: &cnab.DependencyVersion{
Ranges: []string{
"1.x - 2",
"2.1 - 3.x",
"1.x - 2,2.1 - 3.x",
},
},
}},
{"with-tag", cnab.Dependency{
Name: "dep-with-tag",
Bundle: "getporter/dep-bun:v0.1.0",
}},
}

for _, tc := range testcases {
tc := tc

t.Run(tc.name, func(t *testing.T) {
t.Parallel()
tc := tc

c := config.NewTestConfig(t)
c.TestContext.AddTestFile("testdata/porter-with-deps.yaml", config.Name)
Expand All @@ -581,9 +578,10 @@ func TestManifestConverter_generateDependencies(t *testing.T) {

a := NewManifestConverter(c.Config, m, nil, nil)

deps := a.generateDependencies()
require.Len(t, deps.Requires, 4, "incorrect number of dependencies were generated")
require.Equal(t, []string{"mysql", "ad", "storage", "dep-with-tag"}, deps.Sequence, "incorrect sequence was generated")
deps, err := a.generateDependencies()
require.NoError(t, err, "generateDependencies failed")
require.Len(t, deps.Requires, 3, "incorrect number of dependencies were generated")
require.Equal(t, []string{"mysql", "ad", "storage"}, deps.Sequence, "incorrect sequence was generated")

var dep *cnab.Dependency
for _, d := range deps.Requires {
Expand Down
4 changes: 1 addition & 3 deletions pkg/cnab/config-adapter/testdata/porter-with-deps.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@ dependencies:
- name: ad
bundle:
reference: "getporter/azure-active-directory"
version: 1.0.0-0
- name: storage
bundle:
reference: "getporter/azure-blob-storage"
version: 1.x - 2,2.1 - 3.x
- name: dep-with-tag
bundle:
reference: "getporter/dep-bun:v0.1.0"

mixins:
- exec
Expand Down
2 changes: 1 addition & 1 deletion pkg/manifest/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ type BundleCriteria struct {
// This includes considering prereleases to be invalid if the ranges does not include one.
// If you want to have it include pre-releases a simple solution is to include -0 in your range."
// https://github.com/Masterminds/semver/blob/master/README.md#checking-version-constraints
Version string `yaml:"versions,omitempty"`
Version string `yaml:"version,omitempty"`
}

func (d *RequiredDependency) Validate(cxt *portercontext.Context) error {
Expand Down
2 changes: 1 addition & 1 deletion pkg/porter/testdata/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"type": "string"
},
"version": {
"description": "Bundle version contstraint for version matching, see https://github.com/Masterminds/semver/blob/master/README.md#checking-version-constraints",
"description": "Bundle version constraint for version matching, see https://github.com/Masterminds/semver/blob/master/README.md#checking-version-constraints",
"type": "string"
}
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/schema/manifest.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@
"type": "string"
},
"version": {
"description": "Bundle version contstraint for version matching, see https://github.com/Masterminds/semver/blob/master/README.md#checking-version-constraints",
"description": "Bundle version constraint for version matching, see https://github.com/Masterminds/semver/blob/master/README.md#checking-version-constraints",
"type": "string"
}
},
Expand Down