Skip to content

Commit

Permalink
Support outputs variable in dependency outputs section (#3156)
Browse files Browse the repository at this point in the history
* Pass in config to ReadManifest

This is needed to allow us to check if the DepV2 flag is enabled

Signed-off-by: Kim Christensen <[email protected]>

* Support outputs variable in dependencies

Signed-off-by: Kim Christensen <[email protected]>

* Handle duplicates

Signed-off-by: Kim Christensen <[email protected]>

* Extract to functions

Signed-off-by: Kim Christensen <[email protected]>

* Update golden file

Signed-off-by: Kim Christensen <[email protected]>

* Update template documentation

Signed-off-by: Kim Christensen <[email protected]>

---------

Signed-off-by: Kim Christensen <[email protected]>
  • Loading branch information
kichristensen authored Jul 27, 2024
1 parent 16b5011 commit 7c29ccc
Show file tree
Hide file tree
Showing 11 changed files with 239 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,41 @@ install:
database-password: ${ bundle.dependencies.mysql.outputs.mysql-password }
```

##### Dependecies v2 (experimental)

The second version of dependencies, called DependenciesV2, is available under the
[**experimental** flag](https://porter.sh/docs/configuration/configuration/#experimental-feature-flags).

You can enable the experimental flag by setting an environment variable as follows:

```
PORTER_EXPERIMENTAL=dependencies-v2
```

When DependenciesV2 is enabled, it is possible to use templating inside the dependency output section, like this:

```yaml
dependencies:
requires:
- name: mysql
bundle:
reference: getporter/mysql:v0.1.3
outputs:
connection-string: "Server=mysql;Database=myDb;Uid=${bundle.parameters.uid};Pwd=${bundles.dependencies.mysql.mysql-password};"
```

If referencing an output from the same dependency, the format `outputs.OUTPUT_NAME` can also be used.

```yaml
dependencies:
requires:
- name: mysql
bundle:
reference: getporter/mysql:v0.1.3
outputs:
connection-string: "Server=mysql;Database=myDb;Uid=${bundle.parameters.uid};Pwd=${outputs.mysql-password};"
```

#### images

The bundle.images variable contains metadata about [referenced images] in the bundle.
Expand Down
2 changes: 1 addition & 1 deletion pkg/cnab/config-adapter/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

func LoadTestBundle(t *testing.T, config *config.Config, path string) cnab.ExtendedBundle {
ctx := context.Background()
m, err := manifest.ReadManifest(config.Context, path)
m, err := manifest.ReadManifest(config.Context, path, config)
require.NoError(t, err)
b, err := ConvertToTestBundle(ctx, config, m)
require.NoError(t, err)
Expand Down
22 changes: 22 additions & 0 deletions pkg/cnab/config-adapter/testdata/myenv-depsv2.bundle.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@
"env": "LOGLEVEL"
}
},
"porter-app-port-dep-output": {
"definition": "porter-app-port-dep-output",
"description": "Wires up the app dependency port output for use as a parameter. Porter internal parameter that should not be set manually.",
"destination": {
"env": "PORTER_APP_PORT_DEP_OUTPUT"
}
},
"porter-debug": {
"definition": "porter-debug-parameter",
"description": "Print debug information from Porter when executing the bundle",
Expand Down Expand Up @@ -69,6 +76,10 @@
"default": "info",
"type": "string"
},
"porter-app-port-dep-output": {
"$comment": "porter-internal",
"$id": "https://porter.sh/generated-bundle/#porter-parameter-source-definition"
},
"porter-debug-parameter": {
"$comment": "porter-internal",
"$id": "https://porter.sh/generated-bundle/#porter-debug",
Expand Down Expand Up @@ -99,6 +110,17 @@
],
"custom": {
"io.cnab.parameter-sources": {
"porter-app-port-dep-output": {
"priority": [
"dependencies.output"
],
"sources": {
"dependencies.output": {
"dependency": "app",
"name": "port"
}
}
},
"porter-infra-ip-dep-output": {
"priority": [
"dependencies.output"
Expand Down
9 changes: 5 additions & 4 deletions pkg/linter/linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"strings"

"get.porter.sh/porter/pkg/config"
"get.porter.sh/porter/pkg/manifest"
"get.porter.sh/porter/pkg/mixin/query"
"get.porter.sh/porter/pkg/pkgmgmt"
Expand Down Expand Up @@ -160,7 +161,7 @@ type action struct {
steps manifest.Steps
}

func (l *Linter) Lint(ctx context.Context, m *manifest.Manifest) (Results, error) {
func (l *Linter) Lint(ctx context.Context, m *manifest.Manifest, config *config.Config) (Results, error) {
// Check for reserved porter prefix on parameter names
reservedPrefixes := []string{"porter-", "porter_"}
params := m.Parameters
Expand Down Expand Up @@ -205,7 +206,7 @@ func (l *Linter) Lint(ctx context.Context, m *manifest.Manifest) (Results, error
actions = append(actions, action{actionName, steps})
}
for _, action := range actions {
res, err := validateParamsAppliesToAction(m, action.steps, tmplParams, action.name)
res, err := validateParamsAppliesToAction(m, action.steps, tmplParams, action.name, config)
if err != nil {
return nil, span.Error(fmt.Errorf("error validating action: %s", action.name))
}
Expand Down Expand Up @@ -266,15 +267,15 @@ func (l *Linter) Lint(ctx context.Context, m *manifest.Manifest) (Results, error
return results, nil
}

func validateParamsAppliesToAction(m *manifest.Manifest, steps manifest.Steps, tmplParams manifest.ParameterDefinitions, actionName string) (Results, error) {
func validateParamsAppliesToAction(m *manifest.Manifest, steps manifest.Steps, tmplParams manifest.ParameterDefinitions, actionName string, config *config.Config) (Results, error) {
var results Results
for stepNumber, step := range steps {
data, err := yaml.Marshal(step.Data)
if err != nil {
return nil, fmt.Errorf("error during marshalling: %w", err)
}

tmplResult, err := m.ScanManifestTemplating(data)
tmplResult, err := m.ScanManifestTemplating(data, config)
if err != nil {
return nil, fmt.Errorf("error parsing templating: %w", err)
}
Expand Down
29 changes: 18 additions & 11 deletions pkg/linter/linter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"testing"

"get.porter.sh/porter/pkg/config"
"get.porter.sh/porter/pkg/manifest"
"get.porter.sh/porter/pkg/mixin"
"get.porter.sh/porter/pkg/portercontext"
Expand All @@ -13,6 +14,8 @@ import (

func TestLinter_Lint(t *testing.T) {
ctx := context.Background()
testConfig := config.NewTestConfig(t).Config

t.Run("no results", func(t *testing.T) {
cxt := portercontext.NewTestContext(t)
mixins := mixin.NewTestMixinProvider()
Expand All @@ -26,7 +29,7 @@ func TestLinter_Lint(t *testing.T) {
}
mixins.LintResults = nil

results, err := l.Lint(ctx, m)
results, err := l.Lint(ctx, m, testConfig)
require.NoError(t, err, "Lint failed")
require.Len(t, results, 0, "linter should have returned 0 results")
})
Expand All @@ -50,7 +53,7 @@ func TestLinter_Lint(t *testing.T) {
},
}

results, err := l.Lint(ctx, m)
results, err := l.Lint(ctx, m, testConfig)
require.NoError(t, err, "Lint failed")
require.Len(t, results, 1, "linter should have returned 1 result")
require.Equal(t, mixins.LintResults, results, "unexpected lint results")
Expand All @@ -68,7 +71,7 @@ func TestLinter_Lint(t *testing.T) {
},
}

results, err := l.Lint(ctx, m)
results, err := l.Lint(ctx, m, testConfig)
require.NoError(t, err, "Lint failed")
require.Len(t, results, 0, "linter should ignore mixins that doesn't support the lint command")
})
Expand Down Expand Up @@ -121,7 +124,7 @@ func TestLinter_Lint(t *testing.T) {
},
}

results, err := l.Lint(ctx, m)
results, err := l.Lint(ctx, m, testConfig)
require.NoError(t, err, "Lint failed")
require.Len(t, results, 1, "linter should have returned 1 result")
require.Equal(t, mixins.LintResults, results, "unexpected lint results")
Expand Down Expand Up @@ -149,7 +152,7 @@ func TestLinter_Lint(t *testing.T) {
},
}

results, err := l.Lint(ctx, m)
results, err := l.Lint(ctx, m, testConfig)
require.NoError(t, err, "Lint failed")
require.Len(t, results, 0, "linter should have returned 1 result")
})
Expand All @@ -168,7 +171,7 @@ func TestLinter_Lint(t *testing.T) {
Parameters: param,
}

results, err := l.Lint(ctx, m)
results, err := l.Lint(ctx, m, testConfig)
require.NoError(t, err, "Lint failed")
require.Len(t, results, 1, "linter should have returned 1 result")
require.NotContains(t, results[0].String(), ": 0th step in the mixin ()")
Expand All @@ -189,6 +192,7 @@ func TestLinter_Lint_ParameterDoesNotApplyTo(t *testing.T) {
m.CustomActions["customAction"] = steps
}},
}
testConfig := config.NewTestConfig(t).Config

for _, tc := range testCases {
t.Run(tc.action, func(t *testing.T) {
Expand Down Expand Up @@ -236,7 +240,7 @@ func TestLinter_Lint_ParameterDoesNotApplyTo(t *testing.T) {
URL: "https://porter.sh/docs/references/linter/#porter-101",
},
}
results, err := l.Lint(ctx, m)
results, err := l.Lint(ctx, m, testConfig)
require.NoError(t, err, "Lint failed")
require.Len(t, results, 1, "linter should have returned 1 result")
require.Equal(t, lintResults, results, "unexpected lint results")
Expand All @@ -258,6 +262,7 @@ func TestLinter_Lint_ParameterAppliesTo(t *testing.T) {
m.CustomActions["customAction"] = steps
}},
}
testConfig := config.NewTestConfig(t).Config

for _, tc := range testCases {
t.Run(tc.action, func(t *testing.T) {
Expand Down Expand Up @@ -290,14 +295,16 @@ func TestLinter_Lint_ParameterAppliesTo(t *testing.T) {
}
tc.setSteps(m, steps)

results, err := l.Lint(ctx, m)
results, err := l.Lint(ctx, m, testConfig)
require.NoError(t, err, "Lint failed")
require.Len(t, results, 0, "linter should have returned 1 result")
})
}
}

func TestLinter_DependencyMultipleTimes(t *testing.T) {
testConfig := config.NewTestConfig(t).Config

t.Run("dependency defined multiple times", func(t *testing.T) {
cxt := portercontext.NewTestContext(t)
mixins := mixin.NewTestMixinProvider()
Expand All @@ -321,7 +328,7 @@ func TestLinter_DependencyMultipleTimes(t *testing.T) {
},
}

results, err := l.Lint(context.Background(), m)
results, err := l.Lint(context.Background(), m, testConfig)
require.NoError(t, err, "Lint failed")
require.Len(t, results, 1, "linter should have returned 1 result")
require.Equal(t, expectedResult, results, "unexpected lint results")
Expand All @@ -340,7 +347,7 @@ func TestLinter_DependencyMultipleTimes(t *testing.T) {
},
}

results, err := l.Lint(context.Background(), m)
results, err := l.Lint(context.Background(), m, testConfig)
require.NoError(t, err, "Lint failed")
require.Len(t, results, 0, "linter should have returned 0 result")
})
Expand All @@ -351,7 +358,7 @@ func TestLinter_DependencyMultipleTimes(t *testing.T) {

m := &manifest.Manifest{}

results, err := l.Lint(context.Background(), m)
results, err := l.Lint(context.Background(), m, testConfig)
require.NoError(t, err, "Lint failed")
require.Len(t, results, 0, "linter should have returned 0 result")
})
Expand Down
Loading

0 comments on commit 7c29ccc

Please sign in to comment.