Skip to content

Commit

Permalink
Merge pull request #2001 from Jemgoss/installation.namespace
Browse files Browse the repository at this point in the history
Add installation.namespace template var
  • Loading branch information
carolynvs authored Apr 5, 2022
2 parents dbea000 + 2ec8f55 commit 00e7f8d
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 6 deletions.
3 changes: 2 additions & 1 deletion CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,5 @@ and we will add you. **All** contributors belong here. 💯
* [Avinash Upadhyaya](https://github.com/avinashupadhya99)
* [Mahendra Bishnoi](https://github.com/mahendrabishnoi2)
* [Yingrong Zhao](https://github.com/VinozzZ)
* [Saksham Sharma](https://github.com/sakkshm26)
* [Saksham Sharma](https://github.com/sakkshm26)
* [Jeremy Goss](https://github.com/Jemgoss)
12 changes: 12 additions & 0 deletions docs/content/authors/templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ The installation variable contains data related to the execution of the bundle.
| Variable | Description |
|----------|--------------|
| installation.name | The name of the installation. |
| installation.namespace | The namespace of the installation. |
In the example below, we install a helm chart and set the release name to the installation name of the bundle:
Expand All @@ -58,6 +59,17 @@ install:
chart: charts/myapp
```
In the next example, we install some kubernetes resources using the namespace of the bundle:
```yaml
install:
kubernetes:
description: Install myapp
namespace: "{{ installation.namespace }}"
manifests:
- manifests
```
### bundle
The bundle variable contains data that was declared in the bundle definition (porter.yaml).
Expand Down
9 changes: 9 additions & 0 deletions pkg/cnab/provider/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func (r *Runtime) ApplyConfig(args ActionArguments) action.OperationConfigs {
return action.OperationConfigs{
r.SetOutput(),
r.AddFiles(args),
r.AddEnvironment(args),
r.AddRelocation(args),
}
}
Expand Down Expand Up @@ -84,6 +85,14 @@ func (r *Runtime) AddFiles(args ActionArguments) action.OperationConfigFunc {
}
}

func (r *Runtime) AddEnvironment(args ActionArguments) action.OperationConfigFunc {
return func(op *driver.Operation) error {
op.Environment[config.EnvPorterInstallationNamespace] = args.Installation.Namespace
op.Environment[config.EnvPorterInstallationName] = args.Installation.Name
return nil
}
}

// AddRelocation operates on an ActionArguments and adds any provided relocation mapping
// to the operation's files.
func (r *Runtime) AddRelocation(args ActionArguments) action.OperationConfigFunc {
Expand Down
10 changes: 9 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const (
// EnvDEBUG is a custom porter parameter that signals that --debug flag has been passed through from the client to the runtime.
EnvDEBUG = "PORTER_DEBUG"

// EnvCORRELATION_ID is the name of the environment variable containing the
// EnvCorrelationID is the name of the environment variable containing the
// id to correlate logs with a workflow.
EnvCorrelationID = "PORTER_CORRELATION_ID"

Expand All @@ -49,6 +49,14 @@ const (

// ClaimFilepath is the filepath to the claim.json inside of an invocation image
ClaimFilepath = "/cnab/claim.json"

// EnvPorterInstallationNamespace is the name of the environment variable which is injected into the
// invocation image, containing the namespace of the installation.
EnvPorterInstallationNamespace = "PORTER_INSTALLATION_NAMESPACE"

// EnvPorterInstallationName is the name of the environment variable which is injected into the
// invocation image, containing the name of the installation.
EnvPorterInstallationName = "PORTER_INSTALLATION_NAME"
)

// These are functions that afero doesn't support, so this lets us stub them out for tests to set the
Expand Down
2 changes: 1 addition & 1 deletion pkg/exec/builder/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func LoadAction(cxt *portercontext.Context, commandFile string, unmarshal func([
if cxt.Debug {
fmt.Fprintf(cxt.Err, "DEBUG Parsed Input:\n%#v\n", result)
}
return errors.Wrapf(err, "could unmarshal input:\n %s", string(contents))
return errors.Wrapf(err, "could not unmarshal input:\n %s", string(contents))
}

func readInputFromStdinOrFile(cxt *portercontext.Context, commandFile string) ([]byte, error) {
Expand Down
7 changes: 6 additions & 1 deletion pkg/runtime/runtime-manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,12 @@ func (m *RuntimeManifest) loadBundle() error {
return nil
}

func (m *RuntimeManifest) GetInstallationNamespace() string {
return m.Getenv(config.EnvPorterInstallationNamespace)
}

func (m *RuntimeManifest) GetInstallationName() string {
return m.Getenv(config.EnvInstallationName)
return m.Getenv(config.EnvPorterInstallationName)
}

func (m *RuntimeManifest) loadDependencyDefinitions() error {
Expand Down Expand Up @@ -225,6 +229,7 @@ func (m *RuntimeManifest) buildSourceData() (map[string]interface{}, error) {

inst := make(map[string]interface{})
data["installation"] = inst
inst["namespace"] = m.GetInstallationNamespace()
inst["name"] = m.GetInstallationName()

bun := make(map[string]interface{})
Expand Down
7 changes: 5 additions & 2 deletions pkg/runtime/runtime-manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -993,23 +993,26 @@ func TestResolveStepEncoding(t *testing.T) {
assert.Equal(t, flags["c"], wantValue)
}

func TestResolveInstallationName(t *testing.T) {
func TestResolveInstallation(t *testing.T) {
cxt := portercontext.NewTestContext(t)
cxt.Setenv(config.EnvInstallationName, "mybun")
cxt.Setenv(config.EnvPorterInstallationNamespace, "mynamespace")
cxt.Setenv(config.EnvPorterInstallationName, "mybun")

m := &manifest.Manifest{}
rm := NewRuntimeManifest(cxt.Context, cnab.ActionInstall, m)

s := &manifest.Step{
Data: map[string]interface{}{
"description": "Do a helm release",
"ns": "{{ installation.namespace }}",
"release": "{{ installation.name }}",
},
}

err := rm.ResolveStep(s)
require.NoError(t, err, "ResolveStep failed")

assert.Equal(t, "mynamespace", s.Data["ns"], "installation.namespace was not rendered")
assert.Equal(t, "mybun", s.Data["release"], "installation.name was not rendered")
}

Expand Down

0 comments on commit 00e7f8d

Please sign in to comment.