-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Workaround for Usage - we don't have Crossplane Runtime fork
Signed-off-by: Hasan Turken <turkenh@gmail.com>
- Loading branch information
Showing
7 changed files
with
182 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
176 changes: 176 additions & 0 deletions
176
internal/controller/apiextensions/usage/composed/composed.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
/* | ||
Copyright 2020 The Crossplane Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
// Note(turkenh): This file is copied from https://github.com/crossplane/crossplane-runtime/blob/9d37f2639b182b4da5f832f1467d831dfa68ff7f/pkg/resource/unstructured/composed/composed.go\ | ||
// to be able to back-port the Usage PR here without having these changes | ||
// in the release branch of crossplane-runtime. | ||
// Ideally, we should maintain a fork of crossplane-runtime as | ||
// upbound/crossplane-runtime and depend on it so that we can backport | ||
// changes/fixes that depend on crossplane-runtime. | ||
// Considering this is a rare requirement (encountered only once so far), | ||
// we are using this as a workaround for now. | ||
|
||
// Package composed contains an unstructured composed resource. | ||
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" | ||
) | ||
|
||
// An Option modifies an unstructured composed resource. | ||
type Option func(resource *Unstructured) | ||
|
||
// FromReference returns an Option that propagates the metadata in the supplied | ||
// reference to an unstructured composed resource. | ||
func FromReference(ref corev1.ObjectReference) Option { | ||
return func(cr *Unstructured) { | ||
cr.SetGroupVersionKind(ref.GroupVersionKind()) | ||
cr.SetName(ref.Name) | ||
cr.SetNamespace(ref.Namespace) | ||
cr.SetUID(ref.UID) | ||
} | ||
} | ||
|
||
// WithConditions returns an Option that sets the supplied conditions on an | ||
// unstructured composed resource. | ||
func WithConditions(c ...xpv1.Condition) Option { | ||
return func(cr *Unstructured) { | ||
cr.SetConditions(c...) | ||
} | ||
} | ||
|
||
// New returns a new unstructured composed resource. | ||
func New(opts ...Option) *Unstructured { | ||
cr := &Unstructured{unstructured.Unstructured{Object: make(map[string]any)}} | ||
for _, f := range opts { | ||
f(cr) | ||
} | ||
return cr | ||
} | ||
|
||
// An Unstructured composed resource. | ||
type Unstructured struct { | ||
unstructured.Unstructured | ||
} | ||
|
||
// GetUnstructured returns the underlying *unstructured.Unstructured. | ||
func (cr *Unstructured) GetUnstructured() *unstructured.Unstructured { | ||
return &cr.Unstructured | ||
} | ||
|
||
// GetCondition of this Composed resource. | ||
func (cr *Unstructured) GetCondition(ct xpv1.ConditionType) xpv1.Condition { | ||
conditioned := xpv1.ConditionedStatus{} | ||
// The path is directly `status` because conditions are inline. | ||
if err := fieldpath.Pave(cr.Object).GetValueInto("status", &conditioned); err != nil { | ||
return xpv1.Condition{} | ||
} | ||
return conditioned.GetCondition(ct) | ||
} | ||
|
||
// SetConditions of this Composed resource. | ||
func (cr *Unstructured) SetConditions(c ...xpv1.Condition) { | ||
conditioned := xpv1.ConditionedStatus{} | ||
// The path is directly `status` because conditions are inline. | ||
_ = fieldpath.Pave(cr.Object).GetValueInto("status", &conditioned) | ||
conditioned.SetConditions(c...) | ||
_ = fieldpath.Pave(cr.Object).SetValue("status.conditions", conditioned.Conditions) | ||
} | ||
|
||
// GetWriteConnectionSecretToReference of this Composed resource. | ||
func (cr *Unstructured) GetWriteConnectionSecretToReference() *xpv1.SecretReference { | ||
out := &xpv1.SecretReference{} | ||
if err := fieldpath.Pave(cr.Object).GetValueInto("spec.writeConnectionSecretToRef", out); err != nil { | ||
return nil | ||
} | ||
return out | ||
} | ||
|
||
// SetWriteConnectionSecretToReference of this Composed resource. | ||
func (cr *Unstructured) SetWriteConnectionSecretToReference(r *xpv1.SecretReference) { | ||
_ = fieldpath.Pave(cr.Object).SetValue("spec.writeConnectionSecretToRef", r) | ||
} | ||
|
||
// GetPublishConnectionDetailsTo of this Composed resource. | ||
func (cr *Unstructured) GetPublishConnectionDetailsTo() *xpv1.PublishConnectionDetailsTo { | ||
out := &xpv1.PublishConnectionDetailsTo{} | ||
if err := fieldpath.Pave(cr.Object).GetValueInto("spec.publishConnectionDetailsTo", out); err != nil { | ||
return nil | ||
} | ||
return out | ||
} | ||
|
||
// SetPublishConnectionDetailsTo of this Composed resource. | ||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters