Skip to content

Commit

Permalink
Rework code to pass object back on status update
Browse files Browse the repository at this point in the history
  • Loading branch information
alvaroaleman committed Sep 10, 2023
1 parent c527239 commit aa2bc6a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
13 changes: 3 additions & 10 deletions pkg/client/fake/client.go
Expand Up @@ -403,8 +403,9 @@ func (t versionedTracker) update(gvr schema.GroupVersionResource, obj runtime.Ob
if err := copyStatusFrom(obj, oldObject); err != nil {
return fmt.Errorf("failed to copy non-status field for object with status subresouce: %w", err)
}

obj = oldObject.DeepCopyObject().(client.Object)
passedRV := accessor.GetResourceVersion()
reflect.ValueOf(obj).Elem().Set(reflect.ValueOf(oldObject.DeepCopyObject()).Elem())
accessor.SetResourceVersion(passedRV)
} else { // copy status from original object
if err := copyStatusFrom(oldObject, obj); err != nil {
return fmt.Errorf("failed to copy the status for object with status subresource: %w", err)
Expand Down Expand Up @@ -437,14 +438,6 @@ func (t versionedTracker) update(gvr schema.GroupVersionResource, obj runtime.Ob
intResourceVersion++
accessor.SetResourceVersion(strconv.FormatUint(intResourceVersion, 10))

// obtain the current obj accessor's pointer,
// so we can make an update on the current obj
currentAccessor, err := meta.Accessor(obj)
if err != nil {
return fmt.Errorf("failed to get accessor for object: %w", err)
}
currentAccessor.SetResourceVersion(strconv.FormatUint(intResourceVersion, 10))

if !deleting && !deletionTimestampEqual(accessor, oldAccessor) {
return fmt.Errorf("error: Unable to edit %s: metadata.deletionTimestamp field is immutable", accessor.GetName())
}
Expand Down
23 changes: 14 additions & 9 deletions pkg/client/fake/client_test.go
Expand Up @@ -45,6 +45,11 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client/interceptor"
)

const (
machineIDFromStatusUpdate = "machine-id-from-status-update"
cidrFromStatusUpdate = "cidr-from-status-update"
)

var _ = Describe("Fake client", func() {
var dep *appsv1.Deployment
var dep2 *appsv1.Deployment
Expand Down Expand Up @@ -1456,15 +1461,15 @@ var _ = Describe("Fake client", func() {
cl := NewClientBuilder().WithStatusSubresource(obj).WithObjects(obj).Build()
objOriginal := obj.DeepCopy()

obj.Spec.PodCIDR = "cidr-from-status-update"
obj.Spec.PodCIDR = cidrFromStatusUpdate
obj.Annotations = map[string]string{
"some-annotation-key": "some-annotation-value",
}
obj.Labels = map[string]string{
"some-label-key": "some-label-value",
}

obj.Status.NodeInfo.MachineID = "machine-id-from-status-update"
obj.Status.NodeInfo.MachineID = machineIDFromStatusUpdate
Expect(cl.Status().Update(context.Background(), obj)).NotTo(HaveOccurred())

actual := &corev1.Node{ObjectMeta: metav1.ObjectMeta{Name: obj.Name}}
Expand All @@ -1473,7 +1478,7 @@ var _ = Describe("Fake client", func() {
objOriginal.APIVersion = actual.APIVersion
objOriginal.Kind = actual.Kind
objOriginal.ResourceVersion = actual.ResourceVersion
objOriginal.Status.NodeInfo.MachineID = "machine-id-from-status-update"
objOriginal.Status.NodeInfo.MachineID = machineIDFromStatusUpdate
Expect(cmp.Diff(objOriginal, actual)).To(BeEmpty())
})

Expand All @@ -1494,7 +1499,7 @@ var _ = Describe("Fake client", func() {
cl := NewClientBuilder().WithStatusSubresource(obj).WithObjects(obj).Build()
expectedObj := obj.DeepCopy()

obj.Status.NodeInfo.MachineID = "machine-id-from-status-update"
obj.Status.NodeInfo.MachineID = machineIDFromStatusUpdate
Expect(cl.Status().Update(context.Background(), obj)).NotTo(HaveOccurred())

obj.Annotations = map[string]string{
Expand All @@ -1511,7 +1516,7 @@ var _ = Describe("Fake client", func() {
expectedObj.APIVersion = actual.APIVersion
expectedObj.Kind = actual.Kind
expectedObj.ResourceVersion = actual.ResourceVersion
expectedObj.Status.NodeInfo.MachineID = "machine-id-from-status-update"
expectedObj.Status.NodeInfo.MachineID = machineIDFromStatusUpdate
Expect(cmp.Diff(expectedObj, actual)).To(BeEmpty())
})

Expand Down Expand Up @@ -1540,8 +1545,8 @@ var _ = Describe("Fake client", func() {
}
Expect(cl.Update(context.Background(), obj)).NotTo(HaveOccurred())

obj.Spec.PodCIDR = "cidr-from-status-update"
obj.Status.NodeInfo.MachineID = "machine-id-from-status-update"
obj.Spec.PodCIDR = cidrFromStatusUpdate
obj.Status.NodeInfo.MachineID = machineIDFromStatusUpdate
Expect(cl.Status().Update(context.Background(), obj)).NotTo(HaveOccurred())

actual := &corev1.Node{ObjectMeta: metav1.ObjectMeta{Name: obj.Name}}
Expand All @@ -1550,7 +1555,7 @@ var _ = Describe("Fake client", func() {
expectedObj.APIVersion = actual.APIVersion
expectedObj.Kind = actual.Kind
expectedObj.ResourceVersion = actual.ResourceVersion
expectedObj.Status.NodeInfo.MachineID = "machine-id-from-status-update"
expectedObj.Status.NodeInfo.MachineID = machineIDFromStatusUpdate
Expect(cmp.Diff(expectedObj, actual)).To(BeEmpty())
})

Expand Down Expand Up @@ -1614,7 +1619,7 @@ var _ = Describe("Fake client", func() {
cl := NewClientBuilder().WithStatusSubresource(obj).WithObjects(obj).Build()
objOriginal := obj.DeepCopy()

obj.Spec.PodCIDR = "cidr-from-status-update"
obj.Spec.PodCIDR = cidrFromStatusUpdate
obj.Status.NodeInfo.MachineID = "machine-id"
Expect(cl.Status().Patch(context.Background(), obj, client.MergeFrom(objOriginal))).NotTo(HaveOccurred())

Expand Down

0 comments on commit aa2bc6a

Please sign in to comment.