Skip to content

Commit

Permalink
Add support for k8s service bindings during build time
Browse files Browse the repository at this point in the history
Signed-off-by: Sambhav Kothari <[email protected]>
  • Loading branch information
sambhav committed Jun 8, 2021
1 parent 767778e commit 2b18648
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 1 deletion.
2 changes: 1 addition & 1 deletion build.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func Build(builder Builder, options ...Option) {
}

file = filepath.Join(ctx.Platform.Path, "bindings")
if ctx.Platform.Bindings, err = NewBindingsFromPath(file); err != nil {
if ctx.Platform.Bindings, err = NewBindingsFromEnvOrPath(file); err != nil {
config.exitHandler.Error(fmt.Errorf("unable to read platform bindings %s\n%w", file, err))
return
}
Expand Down
15 changes: 15 additions & 0 deletions platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,21 @@ func NewBindingsFromPath(path string) (Bindings, error) {
return bindings, nil
}

// NewBindingsFromEnvOrPath creates a new bindings from all the bindings at the path defined by $SERVICE_BINDING_ROOT
// or $CNB_BINDINGS if it does not exist. If neither is defined, it defaults to using the given path.
func NewBindingsFromEnvOrPath(path string) (Bindings, error) {
if path, ok := os.LookupEnv("SERVICE_BINDING_ROOT"); ok {
return NewBindingsFromPath(path)
}

// TODO: Remove as CNB_BINDINGS ages out
if path, ok := os.LookupEnv("CNB_BINDINGS"); ok {
return NewBindingsFromPath(path)
}

return NewBindingsFromPath(path)
}

// Platform is the contents of the platform directory.
type Platform struct {

Expand Down
90 changes: 90 additions & 0 deletions platform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,96 @@ func testPlatform(t *testing.T, context spec.G, it spec.S) {
}))
})
})

context("from environment or path", func() {
context("when SERVICE_BINDING_ROOT is defined but CNB_BINDINGS or the passed path does not exist", func() {
it.Before(func() {
Expect(os.Setenv("SERVICE_BINDING_ROOT", path))
Expect(os.Setenv("CNB_BINDINGS", "does not exist"))
})

it.After(func() {
Expect(os.Unsetenv("SERVICE_BINDING_ROOT"))
Expect(os.Unsetenv("CNB_BINDINGS"))
})

it("creates bindings from path in SERVICE_BINDING_ROOT", func() {
Expect(libcnb.NewBindingsFromEnvOrPath("random-path-that-does-not-exist")).To(Equal(libcnb.Bindings{
libcnb.Binding{
Name: "alpha",
Path: filepath.Join(path, "alpha"),
Type: "test-type",
Provider: "test-provider",
Secret: map[string]string{"test-secret-key": "test-secret-value"},
},
libcnb.Binding{
Name: "bravo",
Path: filepath.Join(path, "bravo"),
Type: "test-type",
Provider: "test-provider",
Secret: map[string]string{"test-secret-key": "test-secret-value"},
},
}))
})
})

context("when CNB_BINDINGS is defined but the path does not exist", func() {
it.Before(func() {
Expect(os.Setenv("CNB_BINDINGS", path))
})

it.After(func() {
Expect(os.Unsetenv("CNB_BINDINGS"))
})

it("creates bindings from path in CNB_BINDINGS", func() {
Expect(libcnb.NewBindingsFromEnvOrPath("random-path-that-does-not-exist")).To(Equal(libcnb.Bindings{
libcnb.Binding{
Name: "alpha",
Path: filepath.Join(path, "alpha"),
Type: "test-type",
Provider: "test-provider",
Secret: map[string]string{"test-secret-key": "test-secret-value"},
},
libcnb.Binding{
Name: "bravo",
Path: filepath.Join(path, "bravo"),
Type: "test-type",
Provider: "test-provider",
Secret: map[string]string{"test-secret-key": "test-secret-value"},
},
}))
})
})

context("when SERVICE_BINDING_ROOT and CNB_BINDINGS is not defined but the path exists", func() {
it("creates bindings from the given path", func() {
Expect(libcnb.NewBindingsFromEnvOrPath(path)).To(Equal(libcnb.Bindings{
libcnb.Binding{
Name: "alpha",
Path: filepath.Join(path, "alpha"),
Type: "test-type",
Provider: "test-provider",
Secret: map[string]string{"test-secret-key": "test-secret-value"},
},
libcnb.Binding{
Name: "bravo",
Path: filepath.Join(path, "bravo"),
Type: "test-type",
Provider: "test-provider",
Secret: map[string]string{"test-secret-key": "test-secret-value"},
},
}))
})
})

context("when no valid binding variable is set", func() {
it("returns an an empty binding", func() {
Expect(libcnb.NewBindingsFromEnvOrPath("does-not-exist")).To(Equal(libcnb.Bindings{}))
})
})

})
})
})

Expand Down

0 comments on commit 2b18648

Please sign in to comment.