Skip to content

Commit

Permalink
Determine which dependency extension is used by a bundle
Browse files Browse the repository at this point in the history
Signed-off-by: Carolyn Van Slyck <me@carolynvanslyck.com>
  • Loading branch information
carolynvs committed Sep 1, 2022
1 parent c906335 commit dcbc21d
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 0 deletions.
28 changes: 28 additions & 0 deletions pkg/manifest/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,34 @@ func (m *Manifest) GetTemplatedDependencyOutputs() DependencyOutputReferences {
return outputs
}

// DetermineDependenciesExtensionUsed looks for how dependencies are used
// by the bundle and which version of the dependency extension can be used.
func (m *Manifest) DetermineDependenciesExtensionUsed() string {
if len(m.Dependencies.Requires) == 0 {
// dependencies are not used at all
return ""
}

// Check if v2 deps are explicitly specified
for _, ext := range m.Required {
if ext.Name == cnab.DependenciesV2ExtensionShortHand ||
ext.Name == cnab.DependenciesV2ExtensionKey {
return cnab.DependenciesV2ExtensionKey
}
}

// Check each dependency for use of v2 only features
for _, dep := range m.Dependencies.Requires {
if dep.Installation != nil ||
len(dep.Credentials) > 0 ||
dep.Bundle.Interface != nil {
return cnab.DependenciesV2ExtensionKey
}
}

return cnab.DependenciesV1ExtensionKey
}

type CustomDefinitions map[string]interface{}

func (cd *CustomDefinitions) UnmarshalYAML(unmarshal func(interface{}) error) error {
Expand Down
102 changes: 102 additions & 0 deletions pkg/manifest/manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package manifest

import (
"context"
"get.porter.sh/porter/pkg/cnab"
"io/ioutil"
"testing"

Expand Down Expand Up @@ -849,3 +850,104 @@ func TestManifest_getTemplatePrefix(t *testing.T) {
})
}
}

func TestManifest_DetermineDependenciesExtensionUsed(t *testing.T) {
t.Run("no dependencies used", func(t *testing.T) {
m := Manifest{}
depsExt := m.DetermineDependenciesExtensionUsed()
assert.Empty(t, depsExt)
})

t.Run("v1 features only", func(t *testing.T) {
m := Manifest{
Dependencies: Dependencies{Requires: []*Dependency{
{
Name: "mysql",
Bundle: BundleCriteria{Reference: "mysql:5.7", Version: "5.7 - 6"},
Parameters: map[string]string{"loglevel": "4"},
},
}},
}
depsExt := m.DetermineDependenciesExtensionUsed()
assert.Equal(t, cnab.DependenciesV1ExtensionKey, depsExt)
})

t.Run("v2 declared but no deps defined", func(t *testing.T) {
m := Manifest{
Required: []RequiredExtension{
{Name: cnab.DependenciesV2ExtensionShortHand},
},
}
depsExt := m.DetermineDependenciesExtensionUsed()
assert.Empty(t, depsExt)
})

t.Run("v2 shorthand declared", func(t *testing.T) {
m := Manifest{
Required: []RequiredExtension{
{Name: cnab.DependenciesV2ExtensionShortHand},
},
Dependencies: Dependencies{Requires: []*Dependency{
{Name: "mysql", Bundle: BundleCriteria{Reference: "mysql:5.7", Version: "5.7 - 6"}},
}},
}
depsExt := m.DetermineDependenciesExtensionUsed()
assert.Equal(t, cnab.DependenciesV2ExtensionKey, depsExt)
})

t.Run("v2 full key declared", func(t *testing.T) {
m := Manifest{
Required: []RequiredExtension{
{Name: cnab.DependenciesV2ExtensionKey},
},
Dependencies: Dependencies{Requires: []*Dependency{
{Name: "mysql", Bundle: BundleCriteria{Reference: "mysql:5.7", Version: "5.7 - 6"}},
}},
}
depsExt := m.DetermineDependenciesExtensionUsed()
assert.Equal(t, cnab.DependenciesV2ExtensionKey, depsExt)
})

t.Run("bundle interface criteria used", func(t *testing.T) {
m := Manifest{
Dependencies: Dependencies{Requires: []*Dependency{
{
Name: "mysql",
Bundle: BundleCriteria{
Reference: "mysql:5.7",
Version: "5.7 - 6",
Interface: &BundleInterface{}}},
}},
}
depsExt := m.DetermineDependenciesExtensionUsed()
assert.Equal(t, cnab.DependenciesV2ExtensionKey, depsExt)
})

t.Run("installation criteria used", func(t *testing.T) {
m := Manifest{
Dependencies: Dependencies{Requires: []*Dependency{
{
Name: "mysql",
Bundle: BundleCriteria{Reference: "mysql:5.7", Version: "5.7 - 6"},
Installation: &DependencyInstallationConfig{},
},
}},
}
depsExt := m.DetermineDependenciesExtensionUsed()
assert.Equal(t, cnab.DependenciesV2ExtensionKey, depsExt)
})

t.Run("credential wiring used", func(t *testing.T) {
m := Manifest{
Dependencies: Dependencies{Requires: []*Dependency{
{
Name: "mysql",
Bundle: BundleCriteria{Reference: "mysql:5.7", Version: "5.7 - 6"},
Credentials: map[string]string{"kubeconfig": "bundle.credentials.kubeconfig"},
},
}},
}
depsExt := m.DetermineDependenciesExtensionUsed()
assert.Equal(t, cnab.DependenciesV2ExtensionKey, depsExt)
})
}

0 comments on commit dcbc21d

Please sign in to comment.