Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend unstructured composed package with additional methods #518

Merged
merged 1 commit into from
Aug 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions pkg/resource/unstructured/composed/composed.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package composed
import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/types"

xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
"github.com/crossplane/crossplane-runtime/pkg/fieldpath"
Expand Down Expand Up @@ -112,3 +113,55 @@ func (cr *Unstructured) GetPublishConnectionDetailsTo() *xpv1.PublishConnectionD
func (cr *Unstructured) SetPublishConnectionDetailsTo(ref *xpv1.PublishConnectionDetailsTo) {
_ = fieldpath.Pave(cr.Object).SetValue("spec.publishConnectionDetailsTo", ref)
}

// OwnedBy returns true if the supplied UID is an owner of the composed
func (cr *Unstructured) OwnedBy(u types.UID) bool {
for _, owner := range cr.GetOwnerReferences() {
if owner.UID == u {
return true
}
}
return false
}

// RemoveOwnerRef removes the supplied UID from the composed resource's owner
func (cr *Unstructured) RemoveOwnerRef(u types.UID) {
refs := cr.GetOwnerReferences()
for i := range refs {
if refs[i].UID == u {
cr.SetOwnerReferences(append(refs[:i], refs[i+1:]...))
return
}
}
}

// An ListOption modifies an unstructured list of composed resource.
type ListOption func(*UnstructuredList)

// FromReferenceToList returns a ListOption that propagates the metadata in the
// supplied reference to an unstructured list composed resource.
func FromReferenceToList(ref corev1.ObjectReference) ListOption {
return func(list *UnstructuredList) {
list.SetAPIVersion(ref.APIVersion)
list.SetKind(ref.Kind + "List")
}
}

// NewList returns a new unstructured list of composed resources.
func NewList(opts ...ListOption) *UnstructuredList {
cr := &UnstructuredList{unstructured.UnstructuredList{Object: make(map[string]any)}}
for _, f := range opts {
f(cr)
}
return cr
}

// An UnstructuredList of composed resources.
type UnstructuredList struct {
unstructured.UnstructuredList
}

// GetUnstructuredList returns the underlying *unstructured.Unstructured.
func (cr *UnstructuredList) GetUnstructuredList() *unstructured.UnstructuredList {
return &cr.UnstructuredList
}
Loading