Skip to content

Commit 00e7f8d

Browse files
authored
Merge pull request #2001 from Jemgoss/installation.namespace
Add installation.namespace template var
2 parents dbea000 + 2ec8f55 commit 00e7f8d

File tree

7 files changed

+44
-6
lines changed

7 files changed

+44
-6
lines changed

CONTRIBUTORS.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,5 @@ and we will add you. **All** contributors belong here. 💯
6161
* [Avinash Upadhyaya](https://github.com/avinashupadhya99)
6262
* [Mahendra Bishnoi](https://github.com/mahendrabishnoi2)
6363
* [Yingrong Zhao](https://github.com/VinozzZ)
64-
* [Saksham Sharma](https://github.com/sakkshm26)
64+
* [Saksham Sharma](https://github.com/sakkshm26)
65+
* [Jeremy Goss](https://github.com/Jemgoss)

docs/content/authors/templates.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ The installation variable contains data related to the execution of the bundle.
4747
| Variable | Description |
4848
|----------|--------------|
4949
| installation.name | The name of the installation. |
50+
| installation.namespace | The namespace of the installation. |
5051
5152
In the example below, we install a helm chart and set the release name to the installation name of the bundle:
5253
@@ -58,6 +59,17 @@ install:
5859
chart: charts/myapp
5960
```
6061
62+
In the next example, we install some kubernetes resources using the namespace of the bundle:
63+
64+
```yaml
65+
install:
66+
kubernetes:
67+
description: Install myapp
68+
namespace: "{{ installation.namespace }}"
69+
manifests:
70+
- manifests
71+
```
72+
6173
### bundle
6274
6375
The bundle variable contains data that was declared in the bundle definition (porter.yaml).

pkg/cnab/provider/action.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ func (r *Runtime) ApplyConfig(args ActionArguments) action.OperationConfigs {
5252
return action.OperationConfigs{
5353
r.SetOutput(),
5454
r.AddFiles(args),
55+
r.AddEnvironment(args),
5556
r.AddRelocation(args),
5657
}
5758
}
@@ -84,6 +85,14 @@ func (r *Runtime) AddFiles(args ActionArguments) action.OperationConfigFunc {
8485
}
8586
}
8687

88+
func (r *Runtime) AddEnvironment(args ActionArguments) action.OperationConfigFunc {
89+
return func(op *driver.Operation) error {
90+
op.Environment[config.EnvPorterInstallationNamespace] = args.Installation.Namespace
91+
op.Environment[config.EnvPorterInstallationName] = args.Installation.Name
92+
return nil
93+
}
94+
}
95+
8796
// AddRelocation operates on an ActionArguments and adds any provided relocation mapping
8897
// to the operation's files.
8998
func (r *Runtime) AddRelocation(args ActionArguments) action.OperationConfigFunc {

pkg/config/config.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const (
3535
// EnvDEBUG is a custom porter parameter that signals that --debug flag has been passed through from the client to the runtime.
3636
EnvDEBUG = "PORTER_DEBUG"
3737

38-
// EnvCORRELATION_ID is the name of the environment variable containing the
38+
// EnvCorrelationID is the name of the environment variable containing the
3939
// id to correlate logs with a workflow.
4040
EnvCorrelationID = "PORTER_CORRELATION_ID"
4141

@@ -49,6 +49,14 @@ const (
4949

5050
// ClaimFilepath is the filepath to the claim.json inside of an invocation image
5151
ClaimFilepath = "/cnab/claim.json"
52+
53+
// EnvPorterInstallationNamespace is the name of the environment variable which is injected into the
54+
// invocation image, containing the namespace of the installation.
55+
EnvPorterInstallationNamespace = "PORTER_INSTALLATION_NAMESPACE"
56+
57+
// EnvPorterInstallationName is the name of the environment variable which is injected into the
58+
// invocation image, containing the name of the installation.
59+
EnvPorterInstallationName = "PORTER_INSTALLATION_NAME"
5260
)
5361

5462
// These are functions that afero doesn't support, so this lets us stub them out for tests to set the

pkg/exec/builder/action.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func LoadAction(cxt *portercontext.Context, commandFile string, unmarshal func([
8484
if cxt.Debug {
8585
fmt.Fprintf(cxt.Err, "DEBUG Parsed Input:\n%#v\n", result)
8686
}
87-
return errors.Wrapf(err, "could unmarshal input:\n %s", string(contents))
87+
return errors.Wrapf(err, "could not unmarshal input:\n %s", string(contents))
8888
}
8989

9090
func readInputFromStdinOrFile(cxt *portercontext.Context, commandFile string) ([]byte, error) {

pkg/runtime/runtime-manifest.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,12 @@ func (m *RuntimeManifest) loadBundle() error {
8888
return nil
8989
}
9090

91+
func (m *RuntimeManifest) GetInstallationNamespace() string {
92+
return m.Getenv(config.EnvPorterInstallationNamespace)
93+
}
94+
9195
func (m *RuntimeManifest) GetInstallationName() string {
92-
return m.Getenv(config.EnvInstallationName)
96+
return m.Getenv(config.EnvPorterInstallationName)
9397
}
9498

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

226230
inst := make(map[string]interface{})
227231
data["installation"] = inst
232+
inst["namespace"] = m.GetInstallationNamespace()
228233
inst["name"] = m.GetInstallationName()
229234

230235
bun := make(map[string]interface{})

pkg/runtime/runtime-manifest_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -993,23 +993,26 @@ func TestResolveStepEncoding(t *testing.T) {
993993
assert.Equal(t, flags["c"], wantValue)
994994
}
995995

996-
func TestResolveInstallationName(t *testing.T) {
996+
func TestResolveInstallation(t *testing.T) {
997997
cxt := portercontext.NewTestContext(t)
998-
cxt.Setenv(config.EnvInstallationName, "mybun")
998+
cxt.Setenv(config.EnvPorterInstallationNamespace, "mynamespace")
999+
cxt.Setenv(config.EnvPorterInstallationName, "mybun")
9991000

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

10031004
s := &manifest.Step{
10041005
Data: map[string]interface{}{
10051006
"description": "Do a helm release",
1007+
"ns": "{{ installation.namespace }}",
10061008
"release": "{{ installation.name }}",
10071009
},
10081010
}
10091011

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

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

0 commit comments

Comments
 (0)