Skip to content

Commit 0039876

Browse files
Merge pull request #1925 from adambkaplan/set-fieldowner
feat: Set Field Owners for Build/BuildRun Controllers
2 parents 4f0ce77 + 8a1b58d commit 0039876

File tree

5 files changed

+69
-4
lines changed

5 files changed

+69
-4
lines changed

pkg/reconciler/build/build.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ type ReconcileBuild struct {
5252
func NewReconciler(c *config.Config, mgr manager.Manager, ownerRef setOwnerReferenceFunc) reconcile.Reconciler {
5353
return &ReconcileBuild{
5454
config: c,
55-
client: mgr.GetClient(),
55+
client: client.WithFieldOwner(mgr.GetClient(), "shipwright-build-controller"),
5656
scheme: mgr.GetScheme(),
5757
setOwnerReferenceFunc: ownerRef,
5858
}

pkg/reconciler/buildrun/buildrun.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ type ReconcileBuildRun struct {
5757
func NewReconciler(c *config.Config, mgr manager.Manager, ownerRef setOwnerReferenceFunc) reconcile.Reconciler {
5858
return &ReconcileBuildRun{
5959
config: c,
60-
client: mgr.GetClient(),
60+
client: client.WithFieldOwner(mgr.GetClient(), "shipwright-buildrun-controller"),
6161
scheme: mgr.GetScheme(),
6262
setOwnerReferenceFunc: ownerRef,
6363
}

pkg/reconciler/buildrunttlcleanup/buildrun_ttl_cleanup.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type ReconcileBuildRun struct {
3131
func NewReconciler(c *config.Config, mgr manager.Manager) reconcile.Reconciler {
3232
return &ReconcileBuildRun{
3333
config: c,
34-
client: mgr.GetClient(),
34+
client: client.WithFieldOwner(mgr.GetClient(), "shipwright-buildrun-ttl-cleanup-controller"),
3535
}
3636
}
3737

test/integration/build_to_taskruns_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
. "github.com/onsi/ginkgo/v2"
1111
. "github.com/onsi/gomega"
1212
corev1 "k8s.io/api/core/v1"
13+
"k8s.io/apimachinery/pkg/util/sets"
1314
"k8s.io/utils/ptr"
1415

1516
"github.com/shipwright-io/build/pkg/apis/build/v1beta1"
@@ -146,6 +147,19 @@ var _ = Describe("Integration tests Build and TaskRun", func() {
146147
Expect(*buildObject.Status.Reason).To(Equal(v1beta1.SucceedStatus))
147148
Expect(*buildObject.Status.Message).To(Equal(v1beta1.AllValidationsSucceeded))
148149
})
150+
151+
It("should set the buildrun controller as a field manager", func() {
152+
Expect(tb.CreateBuild(buildObject)).To(Succeed())
153+
154+
buildObject, err = tb.GetBuildTillValidation(buildObject.Name)
155+
Expect(err).NotTo(HaveOccurred())
156+
157+
fieldManagers := sets.NewString()
158+
for _, field := range buildObject.GetObjectMeta().GetManagedFields() {
159+
fieldManagers.Insert(field.Manager)
160+
}
161+
Expect(fieldManagers).To(HaveKey("shipwright-build-controller"), "build controller should be registered as a field manager")
162+
})
149163
})
150164

151165
Context("when creating the taskrun", func() {

test/integration/buildruns_to_taskruns_test.go

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package integration_test
66

77
import (
88
"context"
9+
"encoding/json"
910
"fmt"
1011
"strings"
1112
"time"
@@ -21,6 +22,7 @@ import (
2122
corev1 "k8s.io/api/core/v1"
2223
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2324
"k8s.io/apimachinery/pkg/types"
25+
"k8s.io/apimachinery/pkg/util/sets"
2426
"k8s.io/apimachinery/pkg/util/validation"
2527
"k8s.io/apimachinery/pkg/util/wait"
2628
"k8s.io/apimachinery/pkg/watch"
@@ -212,10 +214,32 @@ var _ = Describe("Integration tests BuildRuns and TaskRuns", func() {
212214
})
213215

214216
Context("when a buildrun is created", func() {
217+
218+
var (
219+
buildRunObject *v1beta1.BuildRun
220+
taskRunObject *pipelineapi.TaskRun
221+
)
222+
223+
AfterEach(func() {
224+
if !CurrentSpecReport().Failed() {
225+
return
226+
}
227+
if buildRunObject != nil {
228+
jsonData, err := json.MarshalIndent(buildRunObject, "", " ")
229+
Expect(err).NotTo(HaveOccurred(), "failed to marshal buildRunObject to JSON")
230+
GinkgoWriter.Printf("buildRunObject JSON:\n%s\n", string(jsonData))
231+
}
232+
if taskRunObject != nil {
233+
jsonData, err := json.MarshalIndent(taskRunObject, "", " ")
234+
Expect(err).NotTo(HaveOccurred(), "failed to marshal taskRunObject to JSON")
235+
GinkgoWriter.Printf("taskRunObject JSON:\n%s\n", string(jsonData))
236+
}
237+
})
238+
215239
It("should reflect a Pending and Running reason", func() {
216240
// use a custom strategy here that just sleeps 30 seconds to prevent it from completing too fast so that we do not get the Running state
217241
WithCustomClusterBuildStrategy([]byte(test.ClusterBuildStrategySleep30s), func() {
218-
_, _, buildRunObject := setupBuildAndBuildRun([]byte(test.BuildCBSMinimal), []byte(test.MinimalBuildRun), STRATEGY+tb.Namespace+"custom")
242+
_, _, buildRunObject = setupBuildAndBuildRun([]byte(test.BuildCBSMinimal), []byte(test.MinimalBuildRun), STRATEGY+tb.Namespace+"custom")
219243

220244
_, err = tb.GetBRTillStartTime(buildRunObject.Name)
221245
Expect(err).To(BeNil())
@@ -242,6 +266,33 @@ var _ = Describe("Integration tests BuildRuns and TaskRuns", func() {
242266
Expect(err).To(BeNil(), fmt.Sprintf("failed to get desired reason; expected %s, got %s", expectedReason, actualReason))
243267
})
244268
})
269+
270+
It("should create a taskrun that is owned by the buildrun", func() {
271+
WithCustomClusterBuildStrategy([]byte(test.ClusterBuildStrategyNoOp), func() {
272+
_, _, buildRunObject = setupBuildAndBuildRun([]byte(test.BuildCBSMinimal), []byte(test.MinimalBuildRun), STRATEGY+tb.Namespace+"custom")
273+
274+
_, err = tb.GetBRTillStartTime(buildRunObject.Name)
275+
Expect(err).To(BeNil())
276+
277+
taskRunObject, err = tb.GetTaskRunFromBuildRun(buildRunObject.Name)
278+
Expect(err).To(BeNil())
279+
280+
// Check that the taskrun is owned by the buildrun
281+
Expect(taskRunObject.OwnerReferences).To(HaveLen(1), "taskrun should have exactly one owner reference")
282+
ownerRef := taskRunObject.OwnerReferences[0]
283+
Expect(ownerRef.Kind).To(Equal("BuildRun"), "taskrun should have a buildrun owner reference")
284+
Expect(ownerRef.Name).To(Equal(buildRunObject.Name), "taskrun should have a buildrun owner reference")
285+
286+
// Check that the buildrun controller is registered as a field manager
287+
fieldManagers := sets.NewString()
288+
for _, field := range taskRunObject.GetObjectMeta().GetManagedFields() {
289+
fieldManagers.Insert(field.Manager)
290+
}
291+
// Sets uses a map[string]struct{} internally, so we need to check for the key.
292+
// We can't use ContainElement because it would check for the value, not the key.
293+
Expect(fieldManagers).To(HaveKey("shipwright-buildrun-controller"), "buildrun controller should be registered as a field manager")
294+
})
295+
})
245296
})
246297

247298
Context("when a buildrun is created but fails because of a timeout", func() {

0 commit comments

Comments
 (0)