Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[proposal] Use golang based cp tool rather than busybox in autoinstrumentation images. #1727

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .chloggen/add-autoinstrumentation-utils-image.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: 'enhancement'

# The name of the component, or a single word describing the area of concern, (e.g. operator, target allocator, github action)
component: autoinstrumentation

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Create a utils image to replace busybox as the base image in autoinstrumentation images. Update the Java image autoinstrumentation as an example.

# One or more tracking issues related to the change
issues: [1600]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:
65 changes: 65 additions & 0 deletions .github/workflows/publish-autoinstrumentation-utils.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
name: "Publish Utils for Auto-Instrumentation"

on:
push:
paths:
- 'autoinstrumentation/utils/**'
- '.github/workflows/publish-autoinstrumentation-utils.yaml'
branches:
- main
pull_request:
paths:
- 'autoinstrumentation/utils/**'
- '.github/workflows/publish-autoinstrumentation-utils.yaml'
workflow_dispatch:

jobs:
publish:
runs-on: ubuntu-20.04

steps:
- uses: actions/checkout@v3

- name: Read version
run: echo "VERSION=$(cat autoinstrumentation/utils/version.txt)" >> $GITHUB_ENV

- name: Docker meta
id: meta
uses: docker/metadata-action@v4
with:
images: ghcr.io/open-telemetry/opentelemetry-operator/autoinstrumentation-utils
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to be more explicit here e.g. ghcr.io/open-telemetry/opentelemetry-operator/autoinstrumentation-cp

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Depending on where the final code for this lives, this can vary. If we keep the cp code here in this repo, we will use the proposed name.

tags: |
type=match,pattern=v(.*),group=1,value=v${{ env.VERSION }}

- name: Set up QEMU
uses: docker/setup-qemu-action@v2

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Cache Docker layers
uses: actions/cache@v3
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-

- name: Login to GitHub Package Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build and push
uses: docker/build-push-action@v4
with:
context: autoinstrumentation/utils
platforms: linux/amd64,linux/arm64
push: ${{ github.event_name == 'push' }}
build-args: version=${{ env.VERSION }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache
2 changes: 2 additions & 0 deletions .github/workflows/publish-images.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ jobs:
grep -v '\#' versions.txt | grep opentelemetry-collector | awk -F= '{print "OTELCOL_VERSION="$2}' >> $GITHUB_ENV
grep -v '\#' versions.txt | grep targetallocator | awk -F= '{print "TARGETALLOCATOR_VERSION="$2}' >> $GITHUB_ENV
grep -v '\#' versions.txt | grep operator-opamp-bridge | awk -F= '{print "OPERATOR_OPAMP_BRIDGE_VERSION="$2}' >> $GITHUB_ENV
grep -v '\#' versions.txt | grep autoinstrumentation-utils | awk -F= '{print "AUTO_INSTRUMENTATION_UTILS_VERSION="$2}' >> $GITHUB_ENV
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure why this is needed the utils version is not need to be listed in the root versions.txt

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably don't need this, I was doing my best trying to understand the publish logic. WiIl fix later.

grep -v '\#' versions.txt | grep autoinstrumentation-java | awk -F= '{print "AUTO_INSTRUMENTATION_JAVA_VERSION="$2}' >> $GITHUB_ENV
grep -v '\#' versions.txt | grep autoinstrumentation-nodejs | awk -F= '{print "AUTO_INSTRUMENTATION_NODEJS_VERSION="$2}' >> $GITHUB_ENV
grep -v '\#' versions.txt | grep autoinstrumentation-python | awk -F= '{print "AUTO_INSTRUMENTATION_PYTHON_VERSION="$2}' >> $GITHUB_ENV
Expand Down Expand Up @@ -89,6 +90,7 @@ jobs:
OTELCOL_VERSION=${{ env.OTELCOL_VERSION }}
TARGETALLOCATOR_VERSION=${{ env.TARGETALLOCATOR_VERSION }}
OPERATOR_OPAMP_BRIDGE_VERSION=${{ env.OPERATOR_OPAMP_BRIDGE_VERSION }}
AUTO_INSTRUMENTATION_UTILS_VERSION=${{ env.AUTO_INSTRUMENTATION_UTILS_VERSION }}
AUTO_INSTRUMENTATION_JAVA_VERSION=${{ env.AUTO_INSTRUMENTATION_JAVA_VERSION }}
AUTO_INSTRUMENTATION_NODEJS_VERSION=${{ env.AUTO_INSTRUMENTATION_NODEJS_VERSION }}
AUTO_INSTRUMENTATION_PYTHON_VERSION=${{ env.AUTO_INSTRUMENTATION_PYTHON_VERSION }}
Expand Down
13 changes: 9 additions & 4 deletions autoinstrumentation/java/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@
# - Grant the necessary access to the jar. `chmod -R go+r /javaagent.jar`
# - For auto-instrumentation by container injection, the Linux command cp is
# used and must be availabe in the image.
FROM busybox

# TODO: Remove dev image reference
FROM jvsplk/opentelemetry-operator-utils:v0.0.1 AS utils
ARG version
ARG release_url=https://github.com/open-telemetry/opentelemetry-java-instrumentation/releases/download/v${version}/opentelemetry-javaagent.jar
WORKDIR /utils/
ADD $release_url javaagent.jar
RUN chmod 644 -R javaagent.jar

ADD https://github.com/open-telemetry/opentelemetry-java-instrumentation/releases/download/v$version/opentelemetry-javaagent.jar /javaagent.jar

RUN chmod -R go+r /javaagent.jar
FROM scratch AS final
COPY --from=utils /utils/ /
ENV PATH="${PATH}:/bin"
11 changes: 11 additions & 0 deletions autoinstrumentation/utils/Dockerfile
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we rename the directory to cp?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Started the changes for this, still need to add more soon.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# This utils image is intended to be used as a base image for OpenTelemetry Operator
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not a base image. The docker image is used just to get the tool into the base image.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. After some more thought, I think we could rework this image and use it as a base image and cut down on lines of code in several areas. Checking...

Copy link
Contributor Author

@jvoravong jvoravong May 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was able to get change the utils image name to cp. I was able to make it a base image which I believe makes understandability and adoptability much easier. See: autoinstrumentation/cp/Dockerfil

Related Code Changes

# autoinstrumentation images. The utils will allow autoinstrumentation packages to be
# copied (via a go based cp command) from the init container to the final destination volume.
FROM golang as utils
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This Dockerfile still results in an image that is distributed with the golang base image right? What is golang built on top of? I think this would need to be a multistage build to avoid this where the last stage is FROM scratch.

Copy link
Contributor Author

@jvoravong jvoravong May 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are correct. I refactored the code and it now lives in autoinstrumentation/cp/Dockerfile.
I would really like us to follow a similar pattern shown in this file whether or not the code is based off of rust or golang. This way downstream auto-instrumentation vendors don't have to worry about the implementation because it is abstracted enough for them not to care about it.
Let me know your thoughts if you have them.

Related Code Changes

WORKDIR /utils/
COPY cp.go .

# add a slim helper executable
# -s omits the symbol table and debug information
# -w omits the DWARF symbol table
RUN go build -ldflags "-s -w" -o /utils/bin/cp cp.go
122 changes: 122 additions & 0 deletions autoinstrumentation/utils/cp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// Copyright The OpenTelemetry 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
//
// http://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 (
"errors"
"fmt"
"io"
"os"
"path/filepath"
)

func copyFile(src, dst string) error {
sourceFileStat, err := os.Stat(src)
if err != nil {
return err
}

if !sourceFileStat.Mode().IsRegular() {
return fmt.Errorf("%s is not a regular file", src)
}

source, err := os.Open(src)
if err != nil {
return err
}
defer source.Close()

destination, err := os.Create(dst)
if err != nil {
return err
}
defer destination.Close()

_, err = io.Copy(destination, source)
return err
}

func copyDir(src, dst string) error {
sourceFileStat, err := os.Stat(src)
if err != nil {
return err
}

if !sourceFileStat.Mode().IsDir() {
return fmt.Errorf("%s is not a directory", src)
}

err = filepath.Walk(src, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
relativePath, err := filepath.Rel(src, path)
if err != nil {
return err
}

if info.IsDir() {
return os.Mkdir(filepath.Join(dst, relativePath), info.Mode().Perm())
} else {
return copyFile(path, filepath.Join(dst, relativePath))
}
})

return err
}

func runCopy() error {
args := os.Args[1:]

if len(args) < 2 {
return errors.New("Not enough arguments given.")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we make this more user-friendly? How many arguments are expected?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still working on this and nearly done. I did upload another commit that is unrelated to this.

}

src := args[len(args)-2]
dest := args[len(args)-1]

stat, err := os.Stat(src)
if err != nil {
return err
}
print(args)
if stat.Mode().IsDir() {
if len(args) == 3 && args[0] == "-a" {
err = copyDir(src, dest)
} else {
err = fmt.Errorf("Invalid arguments given.")
}
} else {
if len(args) > 2 {
return errors.New("Too many arguments given.")
}
err = copyFile(src, dest)
}

if err != nil {
return err
}

fmt.Println("File copied successfully!")
return nil
}

func main() {
err := runCopy()
if err != nil {
fmt.Println("Error:", err)
os.Exit(1)
}
}
137 changes: 137 additions & 0 deletions autoinstrumentation/utils/cp_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
// Copyright The OpenTelemetry 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
//
// http://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 (
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
)

type testCase struct {
name string
args []string
expectedError bool
}

func TestCopy(t *testing.T) {
testCases := []testCase{
{
name: "copy single file successfully",
args: []string{"src_dir/file1.txt", "dest_dir/file1.txt"},
expectedError: false,
},
{
name: "copy single file with incorrect source path",
args: []string{"src_dir/missing-file1.txt", "dest_dir/file1_copy.txt"},
expectedError: true,
},
{
name: "copy single file with incorrect destination path",
args: []string{"src_dir/file1.txt", "missing_dest_dir/file1_copy.txt"},
expectedError: true,
},
{
name: "copy directory successfully",
args: []string{"-a", "src_dir", "dest_dir/src_dir_copy"},
expectedError: false,
},
{
name: "copy directory with incorrect source path",
args: []string{"-a", "missing_src_dir", "dest_dir/src_dir_copy"},
expectedError: true,
},
{
name: "copy directory with incorrect destination path",
args: []string{"-a", "src_dir", "missing-dest_dir/src_dir_copy"},
expectedError: true,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
tempDir, err := os.MkdirTemp("", "example")
if err != nil {
t.Fatal(err)
}
srcDir := filepath.Join(tempDir, "src_dir")
err = os.Mkdir(srcDir, 0700)
if err != nil {
t.Fatal(err)
}
err = os.WriteFile(filepath.Join(srcDir, "file1.txt"), []byte("hello"), 0600)
if err != nil {
t.Fatal(err)
}
if tc.args[0] == "-a" {
srcDir2 := filepath.Join(tempDir, "src_dir/src_dir2")
err = os.Mkdir(srcDir2, 0700)
if err != nil {
t.Fatal(err)
}
err = os.WriteFile(filepath.Join(srcDir2, "file2.txt"), []byte("world"), 0600)
if err != nil {
t.Fatal(err)
}
}
destDir := filepath.Join(tempDir, "dest_dir")
err = os.Mkdir(destDir, 0700)
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tempDir)

tc.args[len(tc.args)-2] = filepath.Join(tempDir, tc.args[len(tc.args)-2])
tc.args[len(tc.args)-1] = filepath.Join(tempDir, tc.args[len(tc.args)-1])
tc.args = append([]string{"./cp"}, tc.args...) // ["cp", "-a", "/src", "/dest"]
os.Args = tc.args
runCopy()

if len(tc.args) == 3 && !tc.expectedError {
source, err := os.ReadFile(tc.args[1])
assert.NoError(t, err)

destination, err := os.ReadFile(tc.args[2])
assert.NoError(t, err)

assert.Equal(t, source, destination)
} else if len(tc.args) == 4 && tc.args[1] == "-a" && !tc.expectedError {
err = filepath.Walk(tc.args[2], func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
relativePath, err := filepath.Rel(tc.args[2], path)
if err != nil {
return err
}
expectedPath := filepath.Join(tc.args[3], relativePath)

if !info.IsDir() {
source, err := os.ReadFile(path)
assert.NoError(t, err)
destination, err := os.ReadFile(expectedPath)
assert.NoError(t, err)
assert.Equal(t, source, destination)
}

return nil
})
assert.NoError(t, err)
}
})
}
}
Loading
Loading