Skip to content

Commit

Permalink
Merge pull request #944 from jjbustamante/builder-info-in-prepare-log
Browse files Browse the repository at this point in the history
Builder info in prepare log
  • Loading branch information
Tyler Phelan authored Jun 10, 2022
2 parents bc0e200 + 00ff1d1 commit 0a30a5a
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 2 deletions.
9 changes: 9 additions & 0 deletions cmd/build-init/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ var (
buildChanges = flag.String("build-changes", os.Getenv("BUILD_CHANGES"), "JSON string of build changes and their reason")
descriptorPath = flag.String("project-descriptor-path", os.Getenv("PROJECT_DESCRIPTOR_PATH"), "path to project descriptor file")

builderImage= flag.String("builder-image", os.Getenv("BUILDER_IMAGE"), "The builder image used to build the application")
builderName = flag.String("builder-name", os.Getenv("BUILDER_NAME"), "The builder name provided during creation")
builderKind = flag.String("builder-kind", os.Getenv("BUILDER_KIND"), "The builder kind")

basicGitCredentials flaghelpers.CredentialsFlags
sshGitCredentials flaghelpers.CredentialsFlags
basicDockerCredentials flaghelpers.CredentialsFlags
Expand Down Expand Up @@ -147,6 +151,11 @@ func main() {
logger.Fatalf("error while processing the project descriptor: %s", err)
}

if *builderImage != "" && *builderName != "" && *builderKind != "" {
logger.Printf("Builder:\n Image: %s \n Name: %s \n Kind: %s ", *builderImage,
*builderName, *builderKind)
}

err = cnb.SetupPlatformEnvVars(platformDir, *platformEnvVars)
if err != nil {
logger.Fatalf("error setting up platform env vars %s", err)
Expand Down
2 changes: 1 addition & 1 deletion hack/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,4 @@ function compile() {
-v rebase_image=${rebase_image} \
-v completion_image=${completion_image} \
-v lifecycle_image=${lifecycle_image} > $output
}
}
15 changes: 15 additions & 0 deletions pkg/apis/build/v1alpha2/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,18 @@ func (b *Build) additionalBuildNeeded() bool {
_, ok := b.Annotations[BuildNeededAnnotation]
return ok
}


func (b *Build) builderName() string {
if b == nil {
return ""
}
return b.GetAnnotations()[BuilderNameAnnotation]
}

func (b *Build) builderKind() string {
if b == nil {
return ""
}
return b.GetAnnotations()[BuilderKindAnnotation]
}
12 changes: 12 additions & 0 deletions pkg/apis/build/v1alpha2/build_pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,18 @@ func (b *Build) BuildPod(images BuildPodImages, buildContext BuildContext) (*cor
Name: "RUN_IMAGE",
Value: buildContext.BuildPodBuilderConfig.RunImage,
},
corev1.EnvVar{
Name: "BUILDER_IMAGE",
Value: b.BuilderSpec().Image,
},
corev1.EnvVar{
Name: "BUILDER_NAME",
Value: b.builderName(),
},
corev1.EnvVar{
Name: "BUILDER_KIND",
Value: b.builderKind(),
},
corev1.EnvVar{
Name: "DNS_PROBE_HOSTNAME",
Value: dnsProbeHost,
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/build/v1alpha2/builder_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ type BuilderResource interface {
Ready() bool
BuildpackMetadata() corev1alpha1.BuildpackMetadataList
RunImage() string
GetKind() string
}
5 changes: 5 additions & 0 deletions pkg/apis/build/v1alpha2/image_builds.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ const (
BuildChangesAnnotation = "image.kpack.io/buildChanges"
BuildNeededAnnotation = "image.kpack.io/additionalBuildNeeded"

BuilderNameAnnotation = "image.kpack.io/builderName"
BuilderKindAnnotation = "image.kpack.io/builderKind"

BuildReasonConfig = "CONFIG"
BuildReasonCommit = "COMMIT"
BuildReasonBuildpack = "BUILDPACK"
Expand All @@ -48,6 +51,8 @@ func (im *Image) Build(sourceResolver *SourceResolver, builder BuilderResource,
Annotations: combine(im.Annotations, map[string]string{
BuildReasonAnnotation: reasons,
BuildChangesAnnotation: changes,
BuilderNameAnnotation: builder.GetName(),
BuilderKindAnnotation: builder.GetKind(),
}),
},
Spec: BuildSpec{
Expand Down
8 changes: 7 additions & 1 deletion pkg/apis/build/v1alpha2/image_builds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func testImageBuilds(t *testing.T, when spec.G, it spec.S) {
builder := &TestBuilderResource{
Name: "builder-Name",
LatestImage: "some/builder@sha256:builder-digest",
Kind: BuilderKind,
BuilderReady: true,
BuilderMetadata: []corev1alpha1.BuildpackMetadata{
{Id: "buildpack.matches", Version: "1"},
Expand Down Expand Up @@ -127,7 +128,7 @@ func testImageBuilds(t *testing.T, when spec.G, it spec.S) {

it("propagates image's annotations onto the build", func() {
build := image.Build(sourceResolver, builder, latestBuild, "some-reasons", "some-changes", 27, "")
assert.Equal(t, map[string]string{"annotation-key": "annotation-value", "image.kpack.io/buildChanges": "some-changes", "image.kpack.io/reason": "some-reasons"}, build.Annotations)
assert.Equal(t, map[string]string{"annotation-key": "annotation-value", "image.kpack.io/buildChanges": "some-changes", "image.kpack.io/reason": "some-reasons", "image.kpack.io/builderKind": "Builder", "image.kpack.io/builderName": "builder-Name"}, build.Annotations)
})

it("sets labels from image metadata and propagates image labels", func() {
Expand Down Expand Up @@ -349,6 +350,7 @@ type TestBuilderResource struct {
BuilderReady bool
BuilderMetadata []corev1alpha1.BuildpackMetadata
ImagePullSecrets []corev1.LocalObjectReference
Kind string
LatestImage string
LatestRunImage string
Name string
Expand Down Expand Up @@ -376,3 +378,7 @@ func (t TestBuilderResource) RunImage() string {
func (t TestBuilderResource) GetName() string {
return t.Name
}

func (t TestBuilderResource) GetKind() string {
return t.Kind
}
9 changes: 9 additions & 0 deletions pkg/duckbuilder/duck_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ type DuckBuilder struct {
Spec DuckBuilderSpec `json:"spec"`
Status buildapi.BuilderStatus `json:"status"`
}

func (b *DuckBuilder) GetName() string {
return b.Name
}

func (b *DuckBuilder) GetKind() string {
return b.Kind
}

type DuckBuilderSpec struct {
ImagePullSecrets []v1.LocalObjectReference
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/reconciler/image/build_required_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func testImageBuilds(t *testing.T, when spec.G, it spec.S) {
{Id: "buildpack.matches", Version: "1"},
},
LatestRunImage: "some.registry.io/run-image@sha256:67e3de2af270bf09c02e9a644aeb7e87e6b3c049abe6766bf6b6c3728a83e7fb",
Kind: buildapi.BuilderKind,
}

latestBuild := &buildapi.Build{
Expand Down Expand Up @@ -803,6 +804,7 @@ type TestBuilderResource struct {
LatestImage string
LatestRunImage string
Name string
Kind string
}

func (t TestBuilderResource) BuildBuilderSpec() corev1alpha1.BuildBuilderSpec {
Expand All @@ -827,3 +829,7 @@ func (t TestBuilderResource) RunImage() string {
func (t TestBuilderResource) GetName() string {
return t.Name
}

func (t TestBuilderResource) GetKind() string {
return t.Kind
}
32 changes: 32 additions & 0 deletions pkg/reconciler/image/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ func testImageReconciler(t *testing.T, when spec.G, it spec.S) {
Name: builderName,
Namespace: namespace,
},
TypeMeta: metav1.TypeMeta{
Kind: buildapi.BuilderKind,
},
Status: buildapi.BuilderStatus{
LatestImage: "some/builder@sha256:acf123",
BuilderMetadata: corev1alpha1.BuildpackMetadataList{
Expand Down Expand Up @@ -143,6 +146,9 @@ func testImageReconciler(t *testing.T, when spec.G, it spec.S) {
ObjectMeta: metav1.ObjectMeta{
Name: clusterBuilderName,
},
TypeMeta: metav1.TypeMeta{
Kind: buildapi.ClusterBuilderKind,
},
Status: buildapi.BuilderStatus{
LatestImage: "some/clusterbuilder@sha256:acf123",
BuilderMetadata: corev1alpha1.BuildpackMetadataList{
Expand Down Expand Up @@ -689,6 +695,8 @@ func testImageReconciler(t *testing.T, when spec.G, it spec.S) {
someLabelKey: someValueToPassThrough,
},
Annotations: map[string]string{
buildapi.BuilderNameAnnotation: builderName,
buildapi.BuilderKindAnnotation: buildapi.BuilderKind,
buildapi.BuildReasonAnnotation: buildapi.BuildReasonConfig,
buildapi.BuildChangesAnnotation: testhelpers.CompactJSON(`
[
Expand Down Expand Up @@ -779,6 +787,8 @@ func testImageReconciler(t *testing.T, when spec.G, it spec.S) {
someLabelKey: someValueToPassThrough,
},
Annotations: map[string]string{
buildapi.BuilderNameAnnotation: clusterBuilderName,
buildapi.BuilderKindAnnotation: buildapi.ClusterBuilderKind,
buildapi.BuildReasonAnnotation: buildapi.BuildReasonConfig,
buildapi.BuildChangesAnnotation: testhelpers.CompactJSON(`
[
Expand Down Expand Up @@ -869,6 +879,8 @@ func testImageReconciler(t *testing.T, when spec.G, it spec.S) {
someLabelKey: someValueToPassThrough,
},
Annotations: map[string]string{
buildapi.BuilderNameAnnotation: builderName,
buildapi.BuilderKindAnnotation: buildapi.BuilderKind,
buildapi.BuildReasonAnnotation: buildapi.BuildReasonConfig,
buildapi.BuildChangesAnnotation: testhelpers.CompactJSON(`
[
Expand Down Expand Up @@ -960,6 +972,8 @@ func testImageReconciler(t *testing.T, when spec.G, it spec.S) {
someLabelKey: someValueToPassThrough,
},
Annotations: map[string]string{
buildapi.BuilderNameAnnotation: clusterBuilderName,
buildapi.BuilderKindAnnotation: buildapi.ClusterBuilderKind,
buildapi.BuildReasonAnnotation: buildapi.BuildReasonConfig,
buildapi.BuildChangesAnnotation: testhelpers.CompactJSON(`
[
Expand Down Expand Up @@ -1053,6 +1067,8 @@ func testImageReconciler(t *testing.T, when spec.G, it spec.S) {
someLabelKey: someValueToPassThrough,
},
Annotations: map[string]string{
buildapi.BuilderNameAnnotation: builderName,
buildapi.BuilderKindAnnotation: buildapi.BuilderKind,
buildapi.BuildReasonAnnotation: buildapi.BuildReasonConfig,
buildapi.BuildChangesAnnotation: testhelpers.CompactJSON(`
[
Expand Down Expand Up @@ -1186,6 +1202,8 @@ func testImageReconciler(t *testing.T, when spec.G, it spec.S) {
buildapi.ImageGenerationLabel: generation(image),
},
Annotations: map[string]string{
buildapi.BuilderNameAnnotation: builderName,
buildapi.BuilderKindAnnotation: buildapi.BuilderKind,
buildapi.BuildReasonAnnotation: strings.Join([]string{
buildapi.BuildReasonCommit,
buildapi.BuildReasonConfig,
Expand Down Expand Up @@ -1343,6 +1361,8 @@ func testImageReconciler(t *testing.T, when spec.G, it spec.S) {
someLabelKey: someValueToPassThrough,
},
Annotations: map[string]string{
buildapi.BuilderNameAnnotation: builderName,
buildapi.BuilderKindAnnotation: buildapi.BuilderKind,
buildapi.BuildReasonAnnotation: buildapi.BuildReasonCommit,
buildapi.BuildChangesAnnotation: `[{"reason":"COMMIT","old":"1234567","new":"new-commit"}]`,
},
Expand Down Expand Up @@ -1404,6 +1424,9 @@ func testImageReconciler(t *testing.T, when spec.G, it spec.S) {
Name: builderName,
Namespace: namespace,
},
TypeMeta: metav1.TypeMeta{
Kind: buildapi.BuilderKind,
},
Status: buildapi.BuilderStatus{
Status: corev1alpha1.Status{
Conditions: corev1alpha1.Conditions{
Expand Down Expand Up @@ -1491,6 +1514,8 @@ func testImageReconciler(t *testing.T, when spec.G, it spec.S) {
someLabelKey: someValueToPassThrough,
},
Annotations: map[string]string{
buildapi.BuilderNameAnnotation: builderName,
buildapi.BuilderKindAnnotation: buildapi.BuilderKind,
buildapi.BuildReasonAnnotation: buildapi.BuildReasonBuildpack,
buildapi.BuildChangesAnnotation: testhelpers.CompactJSON(`
[
Expand Down Expand Up @@ -1564,6 +1589,9 @@ func testImageReconciler(t *testing.T, when spec.G, it spec.S) {
Name: builderName,
Namespace: namespace,
},
TypeMeta: metav1.TypeMeta{
Kind: buildapi.BuilderKind,
},
Status: buildapi.BuilderStatus{
Status: corev1alpha1.Status{
Conditions: corev1alpha1.Conditions{
Expand Down Expand Up @@ -1651,6 +1679,8 @@ func testImageReconciler(t *testing.T, when spec.G, it spec.S) {
someLabelKey: someValueToPassThrough,
},
Annotations: map[string]string{
buildapi.BuilderNameAnnotation: builderName,
buildapi.BuilderKindAnnotation: buildapi.BuilderKind,
buildapi.BuildReasonAnnotation: buildapi.BuildReasonStack,
buildapi.BuildChangesAnnotation: testhelpers.CompactJSON(`
[
Expand Down Expand Up @@ -1772,6 +1802,8 @@ func testImageReconciler(t *testing.T, when spec.G, it spec.S) {
someLabelKey: someValueToPassThrough,
},
Annotations: map[string]string{
buildapi.BuilderNameAnnotation: builderName,
buildapi.BuilderKindAnnotation: buildapi.BuilderKind,
buildapi.BuildReasonAnnotation: strings.Join([]string{
buildapi.BuildReasonCommit,
buildapi.BuildReasonConfig,
Expand Down

0 comments on commit 0a30a5a

Please sign in to comment.