-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from paketo-buildpacks/support-config-via-binding
Add config support via binding
- Loading branch information
Showing
14 changed files
with
279 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright 2022 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/buildpacks/libcnb" | ||
"github.com/paketo-buildpacks/libpak/bard" | ||
"github.com/paketo-buildpacks/libpak/sherpa" | ||
|
||
"github.com/paketo-buildpacks/opentelemetry/helper" | ||
) | ||
|
||
func main() { | ||
sherpa.Execute(func() error { | ||
var ( | ||
err error | ||
p = helper.Properties{Logger: bard.NewLogger(os.Stdout)} | ||
) | ||
|
||
p.Bindings, err = libcnb.NewBindingsFromEnvironment() | ||
if err != nil { | ||
return fmt.Errorf("unable to read bindings from environment\n%w", err) | ||
} | ||
|
||
return sherpa.Helpers(map[string]sherpa.ExecD{ | ||
"properties": p, | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright 2022 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package helper_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/sclevine/spec" | ||
"github.com/sclevine/spec/report" | ||
) | ||
|
||
func TestUnit(t *testing.T) { | ||
suite := spec.New("helper", spec.Report(report.Terminal{})) | ||
suite("Properties", testProperties) | ||
suite.Run(t) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright 2022 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package helper | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/buildpacks/libcnb" | ||
"github.com/paketo-buildpacks/libpak/bard" | ||
"github.com/paketo-buildpacks/libpak/bindings" | ||
) | ||
|
||
type Properties struct { | ||
Bindings libcnb.Bindings | ||
Logger bard.Logger | ||
} | ||
|
||
func (p Properties) Execute() (map[string]string, error) { | ||
b, ok, err := bindings.ResolveOne(p.Bindings, bindings.OfType("opentelemetry")) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to resolve binding opentelemetry\n%w", err) | ||
} else if !ok { | ||
return nil, nil | ||
} | ||
|
||
p.Logger.Info("Configuring OpenTelemetry Agent properties") | ||
|
||
/* | ||
* Properties are expected to be passed via a configtree format, | ||
* where each key is the name of a file and the value its content. | ||
*/ | ||
e := make(map[string]string, len(b.Secret)) | ||
for k, v := range b.Secret { | ||
s := strings.ToUpper(k) | ||
s = strings.ReplaceAll(s, "-", "_") | ||
s = strings.ReplaceAll(s, ".", "_") | ||
|
||
e[s] = v | ||
} | ||
|
||
return e, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright 2022 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package helper_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/buildpacks/libcnb" | ||
. "github.com/onsi/gomega" | ||
"github.com/sclevine/spec" | ||
|
||
"github.com/paketo-buildpacks/opentelemetry/helper" | ||
) | ||
|
||
func testProperties(t *testing.T, context spec.G, it spec.S) { | ||
var ( | ||
Expect = NewWithT(t).Expect | ||
|
||
p helper.Properties | ||
) | ||
|
||
it("does not contribute properties if no binding exists", func() { | ||
Expect(p.Execute()).To(BeNil()) | ||
}) | ||
|
||
it("contributes properties if binding exists as a config tree", func() { | ||
p.Bindings = libcnb.Bindings{ | ||
{ | ||
Name: "test-binding", | ||
Type: "opentelemetry", | ||
Secret: map[string]string{ | ||
"otel.test.my-key": "test-value", | ||
"otel.testagain.my-other-key": "other test value", | ||
}, | ||
}, | ||
} | ||
|
||
Expect(p.Execute()).To(Equal(map[string]string{ | ||
"OTEL_TEST_MY_KEY": "test-value", | ||
"OTEL_TESTAGAIN_MY_OTHER_KEY": "other test value", | ||
})) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.