Skip to content

Commit

Permalink
Move build pod commands to vars and configure them during ko builds
Browse files Browse the repository at this point in the history
- This allows us to modify the command for images built with ko because they have a different entrypoint

Signed-off-by: Tom Kennedy <[email protected]>
  • Loading branch information
tomkennedy513 committed Sep 27, 2023
1 parent e39eab1 commit 998061d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 16 deletions.
23 changes: 18 additions & 5 deletions hack/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 <<EOT > $path
cat <<EOT > $kbld_config_path
apiVersion: kbld.k14s.io/v1alpha1
kind: Config
sources:
Expand Down Expand Up @@ -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 <<EOT > $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() {
Expand All @@ -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
Expand All @@ -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
}
4 changes: 2 additions & 2 deletions hack/local.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Prerequisites:
OPTIONS
--help -h prints the command usage
--buildType <buildType> build system to use. valid options are pack or ko.
--build-type <buildType> build system to use. valid options are pack or ko.
--registry <registry> registry to publish built images to (e.g. gcr.io/myproject/my-repo or my-dockerhub-username)
--output <output> filepath for generated deployment yaml. defaults to a temp file
--apply (boolean) apply deployment yaml to current kubectl context
Expand All @@ -39,7 +39,7 @@ function main() {
exit 0
;;

--buildType)
--build-type)
buildType=("${2}")
shift 2
;;
Expand Down
29 changes: 20 additions & 9 deletions pkg/apis/build/v1alpha2/build_pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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()},
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand All @@ -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(
Expand Down Expand Up @@ -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},
Expand All @@ -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(
Expand Down

0 comments on commit 998061d

Please sign in to comment.