Skip to content

Commit

Permalink
Write Succeeded condition only after adding droplet info into `Buil…
Browse files Browse the repository at this point in the history
…dWorkload` status (#3497)
  • Loading branch information
pbusko authored Oct 11, 2024
1 parent e9d686e commit 1a39a25
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 8 deletions.
16 changes: 8 additions & 8 deletions kpack-image-builder/controllers/buildworkload_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,14 +257,6 @@ func (r *BuildWorkloadReconciler) ReconcileResource(ctx context.Context, buildWo
ObservedGeneration: buildWorkload.Generation,
})
} else if latestBuildSuccessful.IsTrue() {
meta.SetStatusCondition(&buildWorkload.Status.Conditions, metav1.Condition{
Type: korifiv1alpha1.SucceededConditionType,
Status: metav1.ConditionTrue,
Reason: "BuildSucceeded",
Message: "Image built successfully",
ObservedGeneration: buildWorkload.Generation,
})

foundServiceAccount := corev1.ServiceAccount{}
err = r.k8sClient.Get(ctx, types.NamespacedName{
Namespace: buildWorkload.Namespace,
Expand All @@ -280,6 +272,14 @@ func (r *BuildWorkloadReconciler) ReconcileResource(ctx context.Context, buildWo
log.Info("error when compiling the DropletStatus", "reason", err)
return ctrl.Result{}, err
}

meta.SetStatusCondition(&buildWorkload.Status.Conditions, metav1.Condition{
Type: korifiv1alpha1.SucceededConditionType,
Status: metav1.ConditionTrue,
Reason: "BuildSucceeded",
Message: "Image built successfully",
ObservedGeneration: buildWorkload.Generation,
})
}

return ctrl.Result{}, nil
Expand Down
60 changes: 60 additions & 0 deletions kpack-image-builder/controllers/buildworkload_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package controllers_test

import (
"encoding/base64"
"errors"
"fmt"
"strconv"

korifiv1alpha1 "code.cloudfoundry.org/korifi/controllers/api/v1alpha1"
"code.cloudfoundry.org/korifi/kpack-image-builder/controllers"
"code.cloudfoundry.org/korifi/tests/helpers"
"code.cloudfoundry.org/korifi/tests/matchers"
"code.cloudfoundry.org/korifi/tools/image"
"code.cloudfoundry.org/korifi/tools/k8s"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -1149,6 +1151,64 @@ var _ = Describe("BuildWorkloadReconciler", func() {
})
})
})

When("failure during generateDropletStatus call", func() {
var (
build *buildv1alpha2.Build
)
BeforeEach(func() {
fakeImageConfigGetter.ConfigReturns(image.Config{}, errors.New("fake error"))
buildWorkload = buildWorkloadObject(buildWorkloadGUID, namespaceGUID, source, env, services, reconcilerName, buildpacks)
Expect(adminClient.Create(ctx, buildWorkload)).To(Succeed())

createdKpackImage := new(buildv1alpha2.Image)
Eventually(func() error {
return adminClient.Get(ctx, types.NamespacedName{Name: appGUID, Namespace: namespaceGUID}, createdKpackImage)
}).Should(Succeed())

build = &buildv1alpha2.Build{
ObjectMeta: metav1.ObjectMeta{
Name: "build",
Namespace: namespaceGUID,
Labels: map[string]string{
buildv1alpha2.ImageLabel: appGUID,
buildv1alpha2.ImageGenerationLabel: "1",
buildv1alpha2.BuildNumberLabel: "1",
},
},
}

Expect(adminClient.Create(ctx, build)).To(Succeed())

Expect(k8s.Patch(ctx, adminClient, build, func() {
build.Status.Conditions = append(build.Status.Conditions, corev1alpha1.Condition{
Type: corev1alpha1.ConditionType("Succeeded"),
Status: corev1.ConditionStatus("True"),
Reason: "",
})

build.Status.Stack.ID = "cflinux3"
build.Status.LatestImage = "foo/bar:asd"
})).To(Succeed())
})

It("does not set the Succeeded condition", func() {
Eventually(func(g Gomega) {
g.Expect(adminClient.Get(ctx, client.ObjectKeyFromObject(buildWorkload), buildWorkload)).To(Succeed())
g.Expect(buildWorkload.Status.Conditions).To(ContainElements(
SatisfyAll(
matchers.HasType(Equal(korifiv1alpha1.SucceededConditionType)),
matchers.HasStatus(Not(Equal(metav1.ConditionTrue))),
),
SatisfyAll(
matchers.HasType(Equal(korifiv1alpha1.StatusConditionReady)),
matchers.HasStatus(Equal(metav1.ConditionFalse)),
matchers.HasMessage(ContainSubstring("fake error")),
),
))
}).Should(Succeed())
})
})
})

func setKpackImageStatus(kpackImage *buildv1alpha2.Image, conditionType string, conditionStatus metav1.ConditionStatus) {
Expand Down

0 comments on commit 1a39a25

Please sign in to comment.