diff --git a/pkg/controller/build/build_controller.go b/pkg/controller/build/build_controller.go index cafa5d1b02..2a71660eae 100644 --- a/pkg/controller/build/build_controller.go +++ b/pkg/controller/build/build_controller.go @@ -339,10 +339,10 @@ func (r *ReconcileBuild) validateBuildRunOwnerReferences(ctx context.Context, b if err = r.client.Update(ctx, &buildRun); err != nil { return err } - ctxlog.Info(ctx, fmt.Sprintf("succesfully updated BuildRun %s", buildRun.Name), namespace, buildRun.Namespace, name, buildRun.Name) + ctxlog.Info(ctx, fmt.Sprintf("successfully updated BuildRun %s", buildRun.Name), namespace, buildRun.Namespace, name, buildRun.Name) } } - case "false": + case "", "false": // if the buildRun have an ownerreference to the Build, lets remove it for _, buildRun := range buildRunList.Items { if index := r.validateBuildOwnerReference(buildRun.OwnerReferences, b); index != -1 { @@ -350,7 +350,7 @@ func (r *ReconcileBuild) validateBuildRunOwnerReferences(ctx context.Context, b if err := r.client.Update(ctx, &buildRun); err != nil { return err } - ctxlog.Info(ctx, fmt.Sprintf("succesfully updated BuildRun %s", buildRun.Name), namespace, buildRun.Namespace, name, buildRun.Name) + ctxlog.Info(ctx, fmt.Sprintf("successfully updated BuildRun %s", buildRun.Name), namespace, buildRun.Namespace, name, buildRun.Name) } } diff --git a/test/integration/build_to_buildruns_test.go b/test/integration/build_to_buildruns_test.go index e7d9d99aee..bbeb743945 100644 --- a/test/integration/build_to_buildruns_test.go +++ b/test/integration/build_to_buildruns_test.go @@ -6,11 +6,13 @@ package integration_test import ( "fmt" + "strings" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/types" "github.com/shipwright-io/build/pkg/apis/build/v1alpha1" "github.com/shipwright-io/build/test" @@ -369,6 +371,35 @@ var _ = Describe("Integration tests Build and BuildRuns", func() { Expect(err).To(BeNil()) Expect(ownerReferenceNames(br.OwnerReferences)).ShouldNot(ContainElement(buildObject.Name)) + }) + It("does not deletes the buildrun if the annotation is removed", func() { + + Expect(tb.CreateBuild(buildObject)).To(BeNil()) + + autoDeleteBuildRun, err := tb.Catalog.LoadBRWithNameAndRef( + BUILDRUN+tb.Namespace, + BUILD+tb.Namespace, + []byte(test.MinimalBuildRun), + ) + Expect(err).To(BeNil()) + + Expect(tb.CreateBR(autoDeleteBuildRun)).To(BeNil()) + + _, err = tb.GetBRTillStartTime(autoDeleteBuildRun.Name) + Expect(err).To(BeNil()) + + // we remove the annotation so automatic delete does not take place, "/" is escaped by "~1" in a JSON pointer + data := []byte(fmt.Sprintf(`[{"op":"remove","path":"/metadata/annotations/%s"}]`, strings.ReplaceAll(v1alpha1.AnnotationBuildRunDeletion, "/", "~1"))) + _, err = tb.PatchBuildWithPatchType(BUILD+tb.Namespace, data, types.JSONPatchType) + Expect(err).To(BeNil()) + + err = tb.DeleteBuild(BUILD + tb.Namespace) + Expect(err).To(BeNil()) + + br, err := tb.GetBR(BUILDRUN + tb.Namespace) + Expect(err).To(BeNil()) + Expect(ownerReferenceNames(br.OwnerReferences)).ShouldNot(ContainElement(buildObject.Name)) + }) It("does delete the buildrun after several modifications of the annotation", func() { diff --git a/test/integration/utils/builds.go b/test/integration/utils/builds.go index 1eb72f6eb0..4c14ea9e26 100644 --- a/test/integration/utils/builds.go +++ b/test/integration/utils/builds.go @@ -38,10 +38,15 @@ func (t *TestBuild) GetBuild(name string) (*v1alpha1.Build, error) { Get(name, metav1.GetOptions{}) } -// PatchBuild patches an existing Build +// PatchBuild patches an existing Build using the merge patch type func (t *TestBuild) PatchBuild(buildName string, data []byte) (*v1alpha1.Build, error) { + return t.PatchBuildWithPatchType(buildName, data, types.MergePatchType) +} + +// PatchBuildWithPatchType patches an existing Build and allows specifying the patch type +func (t *TestBuild) PatchBuildWithPatchType(buildName string, data []byte, pt types.PatchType) (*v1alpha1.Build, error) { bInterface := t.BuildClientSet.BuildV1alpha1().Builds(t.Namespace) - b, err := bInterface.Patch(buildName, types.MergePatchType, data) + b, err := bInterface.Patch(buildName, pt, data) if err != nil { return nil, err }