Skip to content

Commit fcbb2cd

Browse files
authored
Merge pull request #518 from turkenh/extend-composed-type
Extend unstructured composed package with additional methods
2 parents 6aee6b7 + 04ceabf commit fcbb2cd

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

pkg/resource/unstructured/composed/composed.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package composed
2020
import (
2121
corev1 "k8s.io/api/core/v1"
2222
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
23+
"k8s.io/apimachinery/pkg/types"
2324

2425
xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
2526
"github.com/crossplane/crossplane-runtime/pkg/fieldpath"
@@ -112,3 +113,55 @@ func (cr *Unstructured) GetPublishConnectionDetailsTo() *xpv1.PublishConnectionD
112113
func (cr *Unstructured) SetPublishConnectionDetailsTo(ref *xpv1.PublishConnectionDetailsTo) {
113114
_ = fieldpath.Pave(cr.Object).SetValue("spec.publishConnectionDetailsTo", ref)
114115
}
116+
117+
// OwnedBy returns true if the supplied UID is an owner of the composed
118+
func (cr *Unstructured) OwnedBy(u types.UID) bool {
119+
for _, owner := range cr.GetOwnerReferences() {
120+
if owner.UID == u {
121+
return true
122+
}
123+
}
124+
return false
125+
}
126+
127+
// RemoveOwnerRef removes the supplied UID from the composed resource's owner
128+
func (cr *Unstructured) RemoveOwnerRef(u types.UID) {
129+
refs := cr.GetOwnerReferences()
130+
for i := range refs {
131+
if refs[i].UID == u {
132+
cr.SetOwnerReferences(append(refs[:i], refs[i+1:]...))
133+
return
134+
}
135+
}
136+
}
137+
138+
// An ListOption modifies an unstructured list of composed resource.
139+
type ListOption func(*UnstructuredList)
140+
141+
// FromReferenceToList returns a ListOption that propagates the metadata in the
142+
// supplied reference to an unstructured list composed resource.
143+
func FromReferenceToList(ref corev1.ObjectReference) ListOption {
144+
return func(list *UnstructuredList) {
145+
list.SetAPIVersion(ref.APIVersion)
146+
list.SetKind(ref.Kind + "List")
147+
}
148+
}
149+
150+
// NewList returns a new unstructured list of composed resources.
151+
func NewList(opts ...ListOption) *UnstructuredList {
152+
cr := &UnstructuredList{unstructured.UnstructuredList{Object: make(map[string]any)}}
153+
for _, f := range opts {
154+
f(cr)
155+
}
156+
return cr
157+
}
158+
159+
// An UnstructuredList of composed resources.
160+
type UnstructuredList struct {
161+
unstructured.UnstructuredList
162+
}
163+
164+
// GetUnstructuredList returns the underlying *unstructured.Unstructured.
165+
func (cr *UnstructuredList) GetUnstructuredList() *unstructured.UnstructuredList {
166+
return &cr.UnstructuredList
167+
}

0 commit comments

Comments
 (0)