-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Previously, a binding was required at build time in order to contribute the layers for the Application Insights components. We've started to think that requiring a binding during build is too onerous a restriction and instead environment variables should be used to indicate that a dependency should be contributed. The knock-on effect of this change is that we have to move a lot more of the conditional logic out to launch time including ensuring that if the binding doesn't exist _nothing_ is contributed. This change makes those updates to the buildpack's behavior. In additional, this includes a fix where the provided build plan wasn't exhaustive for combinations that a user need when using the buildpack. Signed-off-by: Ben Hale <[email protected]>
- Loading branch information
Showing
35 changed files
with
1,008 additions
and
533 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
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 |
---|---|---|
@@ -1,31 +1,39 @@ | ||
# `gcr.io/paketo-buildpacks/azure-application-insights` | ||
The Paketo Azure Application Insights Buildpack is a Cloud Native Buildpack that contributes the Application Insights Agent and configures it to connect to the service. | ||
# `paketo-buildpacks/microsoft-azure` | ||
The Paketo Microsoft Azure Buildpack is a Cloud Native Buildpack that contributes Microsoft Azure agents and configures them to connec to their services. | ||
|
||
## Behavior | ||
This buildpack will participate if all the following conditions are met | ||
|
||
* A binding exists with `type` of `ApplicationInsights` | ||
* If `$BP_APPLICATION_INSIGHTS_ENABLED` is set to `true` and the application is Java | ||
* At build time, contributes an agent to a layer | ||
* At launch time, if credentials are available, configures the application to use the agent | ||
* If `$BP_APPLICATION_INSIGHTS_ENABLED` is set to `true` and the application is NodeJS | ||
* At build time, contributes an agent to a layer | ||
* At launch time, if credentials are available, configures `$NODE_MODULES` with the agent path. If the main module does not already require `applicationinsights`, prepends the main module with `require('applicationinsights').start();`. | ||
|
||
The buildpack will do the following for Java applications: | ||
### Credential Availability | ||
If the applications runs within Microsoft Azure and the [Azure Metadata Service][m] is accessible, those credentials will be used. If the application runs within any other environment, credentials must be provided with a service binding as described below. | ||
|
||
* Contributes a Java agent to a layer and configures `JAVA_TOOL_OPTIONS` to use it | ||
* Transforms the contents of the binding secret to environment variables with the pattern `APPINSIGHTS_<KEY>=<VALUE>` | ||
[m]: https://docs.microsoft.com/en-us/azure/virtual-machines/windows/instance-metadata-service | ||
|
||
The buildpack will do the following NodeJS applications: | ||
|
||
* Contributes a NodeJS agent to a layer and configures `$NODE_MODULES` to use it | ||
* If main module does not already require `appinsights` module, prepends the main module with `require('applicationinsights').start();` | ||
* Transforms the contents of the binding secret to environment variables with the pattern `APPINSIGHTS_<KEY>=<VALUE>` | ||
|
||
## License | ||
This buildpack is released under version 2.0 of the [Apache License][a]. | ||
## Configuration | ||
| Environment Variable | Description | ||
| -------------------- | ----------- | ||
| `$BP_AZURE_APPLICATION_INSIGHTS_ENABLED` | Whether to add Microsoft Azure Application Insights during build | ||
|
||
## Bindings | ||
The buildpack optionally accepts the following bindings: | ||
|
||
### Type: `MicrosoftAzure` | ||
|Key | Value | Description | ||
|---------------------|---------|------------ | ||
|`InstrumentationKey` | `<key>` | Azure Application Insights instrumentation key | ||
|
||
### Type: `dependency-mapping` | ||
|Key | Value | Description | ||
|----------------------|---------|------------ | ||
|`<dependency-digest>` | `<uri>` | If needed, the buildpack will fetch the dependency with digest `<dependency-digest>` from `<uri>` | ||
|
||
## License | ||
This buildpack is released under version 2.0 of the [Apache License][a]. | ||
|
||
[a]: http://www.apache.org/licenses/LICENSE-2.0 |
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,31 @@ | ||
/* | ||
* Copyright 2018-2020 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 appinsights_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/sclevine/spec" | ||
"github.com/sclevine/spec/report" | ||
) | ||
|
||
func TestUnit(t *testing.T) { | ||
suite := spec.New("appinsights", spec.Report(report.Terminal{})) | ||
suite("Java", testJava) | ||
suite("NodeJS", testNodeJS) | ||
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
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,135 @@ | ||
/* | ||
* Copyright 2018-2020 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 appinsights_test | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/buildpacks/libcnb" | ||
. "github.com/onsi/gomega" | ||
"github.com/sclevine/spec" | ||
|
||
"github.com/paketo-buildpacks/libpak" | ||
"github.com/paketo-buildpacks/microsoft-azure/appinsights" | ||
"github.com/paketo-buildpacks/microsoft-azure/internal/common" | ||
) | ||
|
||
func testJava(t *testing.T, context spec.G, it spec.S) { | ||
var ( | ||
Expect = NewWithT(t).Expect | ||
) | ||
|
||
context("Build", func() { | ||
var ( | ||
ctx libcnb.BuildContext | ||
) | ||
|
||
it.Before(func() { | ||
var err error | ||
|
||
ctx.Buildpack.Path, err = ioutil.TempDir("", "appinsights-java-build-buildpack") | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
Expect(os.MkdirAll(filepath.Join(ctx.Buildpack.Path, "resources"), 0755)).To(Succeed()) | ||
Expect(ioutil.WriteFile(filepath.Join(ctx.Buildpack.Path, "resources", "AI-Agent.xml"), []byte{}, 0644)). | ||
To(Succeed()) | ||
|
||
ctx.Layers.Path, err = ioutil.TempDir("", "appinsights-java-build-layers") | ||
Expect(err).NotTo(HaveOccurred()) | ||
}) | ||
|
||
it.After(func() { | ||
Expect(os.RemoveAll(ctx.Buildpack.Path)).To(Succeed()) | ||
Expect(os.RemoveAll(ctx.Layers.Path)).To(Succeed()) | ||
}) | ||
|
||
it("contributes Java agent", func() { | ||
dep := libpak.BuildpackDependency{ | ||
URI: "https://localhost/stub-azure-application-insights-agent.jar", | ||
SHA256: "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", | ||
} | ||
dc := libpak.DependencyCache{CachePath: "testdata"} | ||
|
||
layer, err := ctx.Layers.Layer("test-layer") | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
layer, err = appinsights.NewJavaBuild(ctx.Buildpack.Path, dep, dc, &libcnb.BuildpackPlan{}).Contribute(layer) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
Expect(layer.Launch).To(BeTrue()) | ||
Expect(filepath.Join(layer.Path, "stub-azure-application-insights-agent.jar")).To(BeARegularFile()) | ||
Expect(filepath.Join(layer.Path, "AI-Agent.xml")).To(BeARegularFile()) | ||
Expect(layer.LaunchEnvironment[fmt.Sprintf("%s.default", appinsights.AgentPath)]). | ||
To(Equal(filepath.Join(layer.Path, "stub-azure-application-insights-agent.jar"))) | ||
}) | ||
|
||
}) | ||
|
||
context("Launch", func() { | ||
var ( | ||
l = appinsights.JavaLaunch{ | ||
CredentialSource: common.MetadataServer, | ||
} | ||
) | ||
|
||
it.Before(func() { | ||
Expect(os.Setenv(appinsights.AgentPath, "test-path")).To(Succeed()) | ||
}) | ||
|
||
it.After(func() { | ||
Expect(os.Unsetenv(appinsights.AgentPath)).To(Succeed()) | ||
}) | ||
|
||
it("does not contribute if source is None", func() { | ||
l.CredentialSource = common.None | ||
|
||
Expect(l.Execute()).To(BeNil()) | ||
}) | ||
|
||
it("returns error if BPI_AZURE_APPLICATION_INSIGHTS_AGENT_PATH is not set", func() { | ||
Expect(os.Unsetenv(appinsights.AgentPath)).To(Succeed()) | ||
|
||
_, err := l.Execute() | ||
Expect(err).To(MatchError("$BPI_AZURE_APPLICATION_INSIGHTS_AGENT_PATH must be set")) | ||
}) | ||
|
||
it("contributes JAVA_TOOL_OPTIONS", func() { | ||
Expect(l.Execute()).To(Equal(map[string]string{"JAVA_TOOL_OPTIONS": "-javaagent:test-path"})) | ||
}) | ||
|
||
context("existing $JAVA_TOOL_OPTIONS", func() { | ||
|
||
it.Before(func() { | ||
Expect(os.Setenv("JAVA_TOOL_OPTIONS", "test-java-tool-options")).To(Succeed()) | ||
}) | ||
|
||
it.After(func() { | ||
Expect(os.Unsetenv("JAVA_TOOL_OPTIONS")).To(Succeed()) | ||
}) | ||
|
||
it("contributes JAVA_TOOL_OPTIONS", func() { | ||
Expect(l.Execute()).To(Equal(map[string]string{ | ||
"JAVA_TOOL_OPTIONS": "test-java-tool-options -javaagent:test-path", | ||
})) | ||
}) | ||
}) | ||
}) | ||
} |
Oops, something went wrong.