diff --git a/DEVELOPMENT.md b/DEVELOPMENT.md index c7f5d0680..b2cf9cf84 100644 --- a/DEVELOPMENT.md +++ b/DEVELOPMENT.md @@ -16,8 +16,7 @@ You'll also need: ```bash kubectl cluster-info # ensure you have access to a cluster -docker login # must be writable and publicly accessible; e.g., your Docker Hub username or gcr.io/ -./hack/apply.sh +./hack/local.sh --help #this will provide all options for building/deploying kpack ``` #### When using private registries @@ -85,7 +84,7 @@ make unit * 🍿 These tests can take anywhere from 20-30 minutes depending on your setup ```bash -IMAGE_REGISTRY=gcr.io/ \ + IMAGE_REGISTRY=gcr.io/ \ IMAGE_REGISTRY_USERNAME=_json_key \ IMAGE_REGISTRY_PASSWORD=$(cat gcp.json) \ make e2e diff --git a/hack/apply.sh b/hack/apply.sh deleted file mode 100755 index 969b75026..000000000 --- a/hack/apply.sh +++ /dev/null @@ -1,18 +0,0 @@ -#!/usr/bin/env bash - -cd $(dirname "${BASH_SOURCE[0]}")/.. - -if [ -z "$1" ]; then - echo "Usage: ./hack/apply.sh " - exit 0 -fi - -source hack/common.sh - -set -e - -TMP_DIR="$(mktemp -d)" - -compile $1 ${TMP_DIR}/out.yaml - -kubectl apply -f ${TMP_DIR}/out.yaml \ No newline at end of file diff --git a/hack/build.sh b/hack/build.sh new file mode 100644 index 000000000..696f4516a --- /dev/null +++ b/hack/build.sh @@ -0,0 +1,216 @@ +#!/bin/bash + +function lifecycle_image_build() { + image=$1 + go run hack/lifecycle/main.go --tag=${image} +} + +function generate_kbld_config_pack() { + path=$1 + registry=$2 + + controller_args=("--env" "BP_GO_TARGETS=./cmd/controller") + controller_args+=($buildArgs) + controller_args="${controller_args[@]}"; + + webhook_args=("--env" "BP_GO_TARGETS=./cmd/webhook") + webhook_args+=($buildArgs) + webhook_args="${webhook_args[@]}"; + + build_init_args=("--env" "BP_GO_TARGETS=./cmd/build-init") + build_init_args+=($buildArgs) + build_init_args="${build_init_args[@]}"; + + build_waiter_args=("--env" "BP_GO_TARGETS=./cmd/build-waiter") + build_waiter_args+=($buildArgs) + build_waiter_args="${build_waiter_args[@]}"; + + rebase_args=("--env" "BP_GO_TARGETS=./cmd/rebase") + rebase_args+=($buildArgs) + rebase_args="${rebase_args[@]}"; + + completion_args=("--env" "BP_GO_TARGETS=./cmd/completion") + completion_args+=($buildArgs) + completion_args="${completion_args[@]}"; + + cat < $path + apiVersion: kbld.k14s.io/v1alpha1 + kind: Config + sources: + - image: controller + path: . + pack: + build: + builder: paketobuildpacks/builder-jammy-tiny + rawOptions: [${controller_args// /,}] + - image: webhook + path: . + pack: + build: + builder: paketobuildpacks/builder-jammy-tiny + rawOptions: [${webhook_args// /,}] + - image: build-init + path: . + pack: + build: + builder: paketobuildpacks/builder-jammy-tiny + rawOptions: [${build_init_args// /,}] + - image: build-waiter + path: . + pack: + build: + builder: paketobuildpacks/builder-jammy-tiny + rawOptions: [${build_waiter_args// /,}] + - image: rebase + path: . + pack: + build: + builder: paketobuildpacks/builder-jammy-tiny + rawOptions: [${rebase_args// /,}] + - image: completion + path: . + pack: + build: + builder: paketobuildpacks/builder-jammy-tiny + rawOptions: [${completion_args// /,}] + overrides: + - image: build-init-windows + newImage: build-init-windows + preresolved: true + - image: completion-windows + newImage: completion-windows + preresolved: true + - image: lifecycle + newImage: $lifecycle_image + destinations: + - image: controller + newImage: $controller_image + - image: webhook + newImage: $webhook_image + - image: build-init + newImage: $build_init_image + - image: build-waiter + newImage: $build_waiter_image + - image: rebase + newImage: $rebase_image + - image: completion + newImage: $completion_image +EOT +} + +function generate_kbld_config_ko() { + kbld_config_path=$1 + ko_config_path=$2 + registry=$3 + + args=("--disable-optimizations") + args+=($buildArgs) + args="${args[@]}"; + + cat < $kbld_config_path + apiVersion: kbld.k14s.io/v1alpha1 + kind: Config + sources: + - image: controller + path: cmd/controller + ko: + build: + rawOptions: [${args// /,}] + - image: webhook + path: cmd/webhook + ko: + build: + rawOptions: [${args// /,}] + - image: build-init + path: cmd/build-init + ko: + build: + rawOptions: [${args// /,}] + - image: build-waiter + path: cmd/build-waiter + ko: + build: + rawOptions: [${args// /,}] + - image: rebase + path: cmd/rebase + ko: + build: + rawOptions: [${args// /,}] + - image: completion + path: cmd/completion + ko: + build: + rawOptions: [${args// /,}] + overrides: + - image: build-init-windows + newImage: build-init-windows + preresolved: true + - image: completion-windows + newImage: completion-windows + preresolved: true + - image: lifecycle + newImage: $lifecycle_image + destinations: + - image: controller + newImage: $controller_image + - image: webhook + newImage: $webhook_image + - image: build-init + newImage: $build_init_image + - image: build-waiter + newImage: $build_waiter_image + - image: rebase + newImage: $rebase_image + - image: completion + newImage: $completion_image +EOT + + prefix="github.com/pivotal/kpack/pkg/apis/build/v1alpha2" + cat < $ko_config_path + defaultBaseImage: paketobuildpacks/run-jammy-tiny + + builds: + - id: controller + ldflags: + - -X ${prefix}.CompletionCommand=/ko-app/completion + - -X ${prefix}.PrepareCommand=/ko-app/build-init + - -X ${prefix}.RebaseCommand=/ko-app/rebase +EOT + +} + +function compile() { + type=$1 + registry=$2 + output=$3 + + # Overrides registry=$1 for Docker Hub images + # e.g. IMAGE_PREFIX=username/kpack- + IMAGE_PREFIX=${IMAGE_PREFIX:-"${registry}/"} + controller_image=${IMAGE_PREFIX}controller + webhook_image=${IMAGE_PREFIX}webhook + build_init_image=${IMAGE_PREFIX}build-init + build_waiter_image=${IMAGE_PREFIX}build-waiter + rebase_image=${IMAGE_PREFIX}rebase + completion_image=${IMAGE_PREFIX}completion + lifecycle_image=${IMAGE_PREFIX}lifecycle + + echo "Building Lifecycle" + lifecycle_image_build ${lifecycle_image} + + echo "Generating kbld config" + temp_dir=$(mktemp -d) + kbld_config_path="${temp_dir}/kbld-config" + ko_config_path="${temp_dir}/.ko.yaml" + if [ $type = "ko" ]; then + generate_kbld_config_ko $kbld_config_path $ko_config_path $registry + elif [ $type = "pack" ]; then + generate_kbld_config_pack $kbld_config_path $registry + else + echo "invalid build type, either 'pack' or 'ko' is allowed" + exit 1 + fi + + echo "Building Images" + ytt -f config | KO_CONFIG_PATH="$ko_config_path" kbld -f $kbld_config_path -f- > $output +} \ No newline at end of file diff --git a/hack/common.sh b/hack/common.sh deleted file mode 100644 index 34176d9fc..000000000 --- a/hack/common.sh +++ /dev/null @@ -1,67 +0,0 @@ -#!/bin/bash - -function pack_build() { - image=$1 - target=$2 - pack_args=${@:3} - builder="gcr.io/cf-build-service-public/ci/kpack-builder" # builder used ci - - pack build ${image} --builder ${builder} -e BP_GO_TARGETS=${target} ${pack_args} --publish --trust-builder - - docker pull ${image} - resolved_image_name=$(docker inspect ${image} --format '{{index .RepoDigests 0}}' ) -} - -function lifecycle_image_build() { - image=$1 - go run hack/lifecycle/main.go --tag=${image} - - docker pull ${image} - resolved_image_name=$(docker inspect ${image} --format '{{index .RepoDigests 0}}' ) -} - -function compile() { - registry=$1 - output=$2 - # Overrides registry=$1 for Docker Hub images - # e.g. IMAGE_PREFIX=username/kpack- - IMAGE_PREFIX=${IMAGE_PREFIX:-"${registry}/"} - - controller_image=${IMAGE_PREFIX}controller - webhook_image=${IMAGE_PREFIX}webhook - build_init_image=${IMAGE_PREFIX}build-init - build_waiter_image=${IMAGE_PREFIX}build-waiter - rebase_image=${IMAGE_PREFIX}rebase - completion_image=${IMAGE_PREFIX}completion - lifecycle_image=${IMAGE_PREFIX}lifecycle - - pack_build ${controller_image} "./cmd/controller" - controller_image=${resolved_image_name} - - pack_build ${build_waiter_image} "./cmd/build-waiter" - build_waiter_image=${resolved_image_name} - - pack_build ${webhook_image} "./cmd/webhook" - webhook_image=${resolved_image_name} - - pack_build ${build_init_image} "./cmd/build-init" - build_init_image=${resolved_image_name} - - pack_build ${rebase_image} "./cmd/rebase" - rebase_image=${resolved_image_name} - - pack_build ${completion_image} "./cmd/completion" - completion_image=${resolved_image_name} - - lifecycle_image_build ${lifecycle_image} - lifecycle_image=${resolved_image_name} - - ytt -f config/. \ - -v controller.image=${controller_image} \ - -v webhook.image=${webhook_image} \ - -v build_init.image=${build_init_image} \ - -v build_waiter.image=${build_waiter_image} \ - -v rebase.image=${rebase_image} \ - -v completion.image=${completion_image} \ - -v lifecycle.image=${lifecycle_image} > $output -} diff --git a/hack/local.sh b/hack/local.sh new file mode 100755 index 000000000..33bfb5e73 --- /dev/null +++ b/hack/local.sh @@ -0,0 +1,110 @@ +#!/usr/bin/env bash + +set -eu +set -o pipefail + +cd $(dirname "${BASH_SOURCE[0]}")/.. + +function usage() { + cat <<-USAGE +local.sh [OPTIONS] + +Builds and generates a deployment yaml for kpack. This only builds linux images. + +Prerequisites: +- pack or ko installed +- kubectl installed +- docker login to your registry + +OPTIONS + --help -h prints the command usage + --build-type build system to use. valid options are pack or ko. + --registry registry to publish built images to (e.g. gcr.io/myproject/my-repo or my-dockerhub-username) + --output filepath for generated deployment yaml. defaults to a temp file + --apply (boolean) apply deployment yaml to current kubectl context + --build-args argument to pass to build system (i.e --clear-cache for pack) + +USAGE +} + +function main() { + local buildType registry output apply buildArgs + apply="false" + + while [[ "${#}" != 0 ]]; do + case "${1}" in + --help|-h) + shift 1 + usage + exit 0 + ;; + + --build-type) + buildType=("${2}") + shift 2 + ;; + + --registry) + registry=("${2}") + shift 2 + ;; + + --output) + output=("${2}") + shift 2 + ;; + + --build-args) + buildArgs=("${2}") + shift 2 + ;; + + --apply) + apply="true" + shift 1 + ;; + + "") + # skip if the argument is empty + shift 1 + ;; + + *) + echo -e "unknown argument \"${1}\"" >&2 + exit 1 + esac + done + + if [ -z "${registry:-}" ]; then + echo "--registry is required" + usage + exit 1 + fi + + if [ -z "${buildType:-}" ]; then + echo "--buildType is required" + usage + exit 1 + fi + + if [ -z "${output:-}" ]; then + tmp_dir="$(mktemp -d)" + output=${tmp_dir}/out.yaml + echo "will write to $output" + fi + + if [ -z "${buildArgs:-}" ]; then + buildArgs="" + fi + + source hack/build.sh + compile $buildType $registry $output + + if "${apply}"; then + echo "Applying $output to cluster" + kubectl apply -f $output + fi + +} + +main "${@:-}" \ No newline at end of file diff --git a/hack/release.sh b/hack/release.sh deleted file mode 100755 index 82e232a63..000000000 --- a/hack/release.sh +++ /dev/null @@ -1,19 +0,0 @@ -#!/usr/bin/env bash - -cd $(dirname "${BASH_SOURCE[0]}")/.. - -set -o errexit -set -o nounset -set -o pipefail - -if [ -z "$2" ]; then - echo "Usage: ./hack/release.sh " - exit 0 -fi - -source hack/common.sh - -# e.g. path/to/release.yml -release_yaml=$2 - -compile $1 ${release_yaml} \ No newline at end of file diff --git a/pkg/apis/build/v1alpha2/build_pod.go b/pkg/apis/build/v1alpha2/build_pod.go index bf114cf19..ff12f99f2 100644 --- a/pkg/apis/build/v1alpha2/build_pod.go +++ b/pkg/apis/build/v1alpha2/build_pod.go @@ -69,6 +69,17 @@ const ( PlatformEnvVarPrefix = "PLATFORM_ENV_" ) +var ( + PrepareCommand = "/cnb/process/build-init" + AnalyzeCommand = "/cnb/lifecycle/analyzer" + DetectCommand = "/cnb/lifecycle/detector" + RestoreCommand = "/cnb/lifecycle/restorer" + BuildCommand = "/cnb/lifecycle/builder" + ExportCommand = "/cnb/lifecycle/exporter" + CompletionCommand = "/cnb/process/completion" + RebaseCommand = "/cnb/process/rebase" +) + type ServiceBinding interface { ServiceName() string } @@ -265,7 +276,7 @@ func (b *Build) BuildPod(images BuildPodImages, buildContext BuildContext) (*cor analyzeContainer := corev1.Container{ Name: AnalyzeContainerName, Image: b.Spec.Builder.Image, - Command: []string{"/cnb/lifecycle/analyzer"}, + Command: []string{AnalyzeCommand}, Resources: b.Spec.Resources, Args: args( []string{ @@ -312,7 +323,7 @@ func (b *Build) BuildPod(images BuildPodImages, buildContext BuildContext) (*cor detectContainer := corev1.Container{ Name: DetectContainerName, Image: b.Spec.Builder.Image, - Command: []string{"/cnb/lifecycle/detector"}, + Command: []string{DetectCommand}, Resources: b.Spec.Resources, Args: []string{ "-app=/workspace", @@ -361,7 +372,7 @@ func (b *Build) BuildPod(images BuildPodImages, buildContext BuildContext) (*cor corev1.Container{ Name: CompletionContainerName, Image: images.completion(buildContext.os()), - Command: []string{"/cnb/process/completion"}, + Command: []string{CompletionCommand}, Env: []corev1.EnvVar{ homeEnv, {Name: CacheTagEnvVar, Value: b.Spec.RegistryCacheTag()}, @@ -396,7 +407,7 @@ func (b *Build) BuildPod(images BuildPodImages, buildContext BuildContext) (*cor corev1.Container{ Name: PrepareContainerName, Image: images.buildInit(buildContext.os()), - Command: []string{"/cnb/process/build-init"}, + Command: []string{PrepareCommand}, Args: append(secretArgs, imagePullArgs...), Resources: b.Spec.Resources, SecurityContext: containerSecurityContext(buildContext.BuildPodBuilderConfig), @@ -465,7 +476,7 @@ func (b *Build) BuildPod(images BuildPodImages, buildContext BuildContext) (*cor corev1.Container{ Name: RestoreContainerName, Image: b.Spec.Builder.Image, - Command: []string{"/cnb/lifecycle/restorer"}, + Command: []string{RestoreCommand}, Resources: b.Spec.Resources, SecurityContext: containerSecurityContext(buildContext.BuildPodBuilderConfig), Args: args([]string{ @@ -494,7 +505,7 @@ func (b *Build) BuildPod(images BuildPodImages, buildContext BuildContext) (*cor corev1.Container{ Name: BuildContainerName, Image: b.Spec.Builder.Image, - Command: []string{"/cnb/lifecycle/builder"}, + Command: []string{BuildCommand}, Resources: b.Spec.Resources, SecurityContext: containerSecurityContext(buildContext.BuildPodBuilderConfig), Args: []string{ @@ -520,7 +531,7 @@ func (b *Build) BuildPod(images BuildPodImages, buildContext BuildContext) (*cor corev1.Container{ Name: ExportContainerName, Image: b.Spec.Builder.Image, - Command: []string{"/cnb/lifecycle/exporter"}, + Command: []string{ExportCommand}, Resources: b.Spec.Resources, SecurityContext: containerSecurityContext(buildContext.BuildPodBuilderConfig), Args: args( @@ -883,7 +894,7 @@ func (b *Build) rebasePod(buildContext BuildContext, images BuildPodImages) (*co { Name: CompletionContainerName, Image: images.completion(buildContext.os()), - Command: []string{"/cnb/process/completion"}, + Command: []string{CompletionCommand}, Env: []corev1.EnvVar{ {Name: CacheTagEnvVar, Value: b.Spec.RegistryCacheTag()}, {Name: TerminationMessagePathEnvVar, Value: completionTerminationMessagePath}, @@ -910,7 +921,7 @@ func (b *Build) rebasePod(buildContext BuildContext, images BuildPodImages) (*co { Name: RebaseContainerName, Image: images.RebaseImage, - Command: []string{"/cnb/process/rebase"}, + Command: []string{RebaseCommand}, Resources: b.Spec.Resources, SecurityContext: containerSecurityContext(buildContext.BuildPodBuilderConfig), Args: args(a(