From 2b18648b96d30b6c31e7e1f9725322bf697c7f78 Mon Sep 17 00:00:00 2001 From: Sambhav Kothari Date: Thu, 13 May 2021 17:36:37 +0100 Subject: [PATCH] Add support for k8s service bindings during build time Signed-off-by: Sambhav Kothari --- build.go | 2 +- platform.go | 15 ++++++++ platform_test.go | 90 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+), 1 deletion(-) diff --git a/build.go b/build.go index b85708b..8890864 100644 --- a/build.go +++ b/build.go @@ -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 } diff --git a/platform.go b/platform.go index dd5f157..7acf494 100644 --- a/platform.go +++ b/platform.go @@ -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 { diff --git a/platform_test.go b/platform_test.go index 6960f86..cbbcb36 100644 --- a/platform_test.go +++ b/platform_test.go @@ -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{})) + }) + }) + + }) }) })