Skip to content

Commit

Permalink
Add script to test extender and run it for extending the build image
Browse files Browse the repository at this point in the history
Signed-off-by: Natalie Arellano <[email protected]>
  • Loading branch information
natalieparellano committed Jan 20, 2022
1 parent 115e7f3 commit d5fb974
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 33 deletions.
57 changes: 24 additions & 33 deletions extender/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ type BuildMetadata struct {
}

type DockerfileArg struct {
Key string `toml:"name"` // TODO: which do we want?
Key string `toml:"name"`
Value string `toml:"value"`
}

type Dockerfile struct {
ExtensionID string `toml:"extension_id"` // TODO: nest [[dockerfiles]] under [[extensions]]?
ExtensionID string `toml:"extension_id"`
Path string `toml:"path"`
Type string `toml:"type"`
Args []DockerfileArg `toml:"args"`
Expand Down Expand Up @@ -53,6 +53,7 @@ func main() {
}
}

// TODO: this only knows how to extend the build image (in container). Extension for run image will be added later.
func doKaniko(kind, baseimage string) {
fmt.Println("Starting the Kaniko application to process a Dockerfile ...")

Expand Down Expand Up @@ -117,6 +118,7 @@ func doKaniko(kind, baseimage string) {
srcPath := path.Join("/", b.LayerTarFileName)
dstPath := path.Join(b.CacheDir, b.LayerTarFileName)

// Ensure cache directory exists
fmt.Printf("Creating %s dir ...\n", b.CacheDir)
err = os.MkdirAll(b.CacheDir, os.ModePerm)
if err != nil {
Expand All @@ -129,19 +131,6 @@ func doKaniko(kind, baseimage string) {
panic(err)
}

// ANTHONY: everything below just isn't working...
// b.ExtractImageTarFile(dstPath)

// fmt.Println("Extract the layer file(s)")
// descriptor, err := b.LoadDescriptorAndConfig()
// if err != nil {
// panic(err)
// }
//
// fmt.Printf("%+v\n", descriptor)
// layers := descriptor[0].Layers
// b.ExtractTarGZFilesWithoutBaseImage(layers[0])

fmt.Printf("Extract the content of the tarball file %s under the cache %s\n", b.Opts.TarPath, b.Opts.CacheDir)
err = untar(dstPath, b.CacheDir)
if err != nil {
Expand All @@ -153,29 +142,31 @@ func doKaniko(kind, baseimage string) {
panic(err)
}

// We're in "build" mode, untar layers to root filesystem: /
for _, layerFile := range layerFiles {
workingDirectory := "/"
tarPath := "/layers/kaniko/" + layerFile
if kind == "build" {
// We're in "build" mode, untar layers to root filesystem: /
for _, layerFile := range layerFiles {
workingDirectory := "/"
tarPath := "/layers/kaniko/" + layerFile

err = untar(tarPath, workingDirectory)
if err != nil {
panic(err)
}
}

err = untar(tarPath, workingDirectory)
// Run the build for buildpacks with lowered privileges.
// We must assume that this extender is run as root.
cmd := exec.Command("/cnb/lifecycle/builder", "-app", "/workspace", "-log-level", "debug")
cmd.Env = append(cmd.Env, "CNB_PLATFORM_API=0.8")
cmd.SysProcAttr = &syscall.SysProcAttr{}
cmd.SysProcAttr.Credential = &syscall.Credential{Uid: 1000, Gid: 1000}
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
panic(err)
}
}

// Run the build with lowered privileges.
// We must assume that this extender is run as root
cmd := exec.Command("/cnb/lifecycle/builder", "-app", "/workspace", "-log-level", "debug")
cmd.Env = append(cmd.Env, "CNB_PLATFORM_API=0.8")
cmd.SysProcAttr = &syscall.SysProcAttr{}
cmd.SysProcAttr.Credential = &syscall.Credential{Uid: 1000, Gid: 1000}
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
panic(err)
}
}

func untar(tarPath, workingDirectory string) error {
Expand Down
59 changes: 59 additions & 0 deletions test-extender.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
cd $LIFECYCLE_REPO_PATH

echo ">>>>>>>>>> Building lifecycle..."

docker image rm test-builder --force

make clean build-linux-amd64

cd out/linux-amd64

cat << EOF > Dockerfile
FROM cnbs/sample-builder:bionic
COPY ./lifecycle /cnb/lifecycle
EOF

docker build -t test-builder .

cd $SAMPLES_REPO_PATH

rm -rf $SAMPLES_REPO_PATH/kaniko
mkdir -p $SAMPLES_REPO_PATH/kaniko
rm -rf $SAMPLES_REPO_PATH/layers/kaniko
mkdir -p $SAMPLES_REPO_PATH/layers/kaniko

echo ">>>>>>>>>> Running detect..."

docker run \
-v $PWD/workspace/:/workspace \
-v $PWD/layers/:/layers \
-v $PWD/platform/:/platform \
-v $PWD/cnb/ext/:/cnb/ext \
-v $PWD/cnb/buildpacks/:/cnb/buildpacks \
test-builder \
/cnb/lifecycle/detector -order /layers/order.toml -log-level debug

echo ">>>>>>>>>> Running build for extensions..."

docker run \
-v $PWD/workspace/:/workspace \
-v $PWD/layers/:/layers \
-v $PWD/platform/:/platform \
-v $PWD/cnb/ext/:/cnb/ext \
-v $PWD/cnb/buildpacks/:/cnb/buildpacks \
test-builder \
/cnb/lifecycle/builder -use-extensions -log-level debug

echo ">>>>>>>>>> Running extend on build image followed by build for buildpacks..."

docker run \
-v $PWD/workspace/:/workspace \
-v $PWD/kaniko/:/kaniko \
-v $PWD/layers/:/layers \
-v $PWD/platform/:/platform \
-v $PWD/cnb/ext/:/cnb/ext \
-v $PWD/cnb/buildpacks/:/cnb/buildpacks \
-u root \
test-builder \
/cnb/lifecycle/extender kaniko build ubuntu:bionic
# args: <kaniko|buildah> <build|run> <base-image>

0 comments on commit d5fb974

Please sign in to comment.