Skip to content

Commit

Permalink
fix(pkg/resource/composite) SetClaimReference pass to underlying obje…
Browse files Browse the repository at this point in the history
…ct just claimRef allowed fields

`spec.claimRef` schema is just subset of corev1.ObjectReference, and hence
`SetClaimReference` might get a reference that have more fields set, e.g. UID.
The fields that do not exist in claimRef schema must be not set on the underlying object,
otherwise K8s API server complains about non-existing field when client sends a patch request.

Signed-off-by: Predrag Knezevic <[email protected]>
  • Loading branch information
pedjak committed Oct 4, 2023
1 parent b537456 commit 0b31516
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
12 changes: 11 additions & 1 deletion pkg/resource/unstructured/composite/composite.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,17 @@ func (c *Unstructured) GetClaimReference() *corev1.ObjectReference {

// SetClaimReference of this Composite resource.
func (c *Unstructured) SetClaimReference(ref *corev1.ObjectReference) {
_ = fieldpath.Pave(c.Object).SetValue("spec.claimRef", ref)
// spec.claimRef schema is just subset of ObjectReference
// the passed ref might have more fields set, e.g. UID
// and they need to be removed, otherwise K8s API server complains about non-existing field
// when client sends patch request
claimRef := &corev1.ObjectReference{
APIVersion: ref.APIVersion,
Kind: ref.Kind,
Namespace: ref.Namespace,
Name: ref.Name,
}
_ = fieldpath.Pave(c.Object).SetValue("spec.claimRef", claimRef)
}

// GetResourceReferences of this Composite resource.
Expand Down
5 changes: 3 additions & 2 deletions pkg/resource/unstructured/composite/composite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,8 @@ func TestCompositionUpdatePolicy(t *testing.T) {
}

func TestClaimReference(t *testing.T) {
ref := &corev1.ObjectReference{Namespace: "ns", Name: "cool"}
ref := &corev1.ObjectReference{Namespace: "ns", Name: "cool", APIVersion: "acme.com/v1", Kind: "Foo", UID: "12345"}
claimRef := &corev1.ObjectReference{Namespace: "ns", Name: "cool", APIVersion: "acme.com/v1", Kind: "Foo"}
cases := map[string]struct {
u *Unstructured
set *corev1.ObjectReference
Expand All @@ -250,7 +251,7 @@ func TestClaimReference(t *testing.T) {
"NewRef": {
u: New(),
set: ref,
want: ref,
want: claimRef,
},
}

Expand Down

0 comments on commit 0b31516

Please sign in to comment.