From a74bdee313ba171961ba4fc54953924e6e37923f Mon Sep 17 00:00:00 2001 From: Tom Kennedy Date: Wed, 27 Sep 2023 10:24:44 -0400 Subject: [PATCH] Move build pod commands to vars and configure them during ko builds - This allows us to modify the command for images built with ko because they have a different entrypoint Signed-off-by: Tom Kennedy --- hack/build.sh | 23 +++++++++++++++++----- pkg/apis/build/v1alpha2/build_pod.go | 29 +++++++++++++++++++--------- 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/hack/build.sh b/hack/build.sh index 855e0d54e..696f4516a 100644 --- a/hack/build.sh +++ b/hack/build.sh @@ -99,14 +99,15 @@ EOT } function generate_kbld_config_ko() { - path=$1 - registry=$2 + kbld_config_path=$1 + ko_config_path=$2 + registry=$3 args=("--disable-optimizations") args+=($buildArgs) args="${args[@]}"; - cat < $path + cat < $kbld_config_path apiVersion: kbld.k14s.io/v1alpha1 kind: Config sources: @@ -162,9 +163,20 @@ function generate_kbld_config_ko() { 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() { @@ -189,8 +201,9 @@ function compile() { 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 $registry + generate_kbld_config_ko $kbld_config_path $ko_config_path $registry elif [ $type = "pack" ]; then generate_kbld_config_pack $kbld_config_path $registry else @@ -199,5 +212,5 @@ function compile() { fi echo "Building Images" - ytt -f config | kbld -f $kbld_config_path -f- > $output + 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/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(