Skip to content

Commit

Permalink
Workaround for Usage - we don't have Crossplane Runtime fork
Browse files Browse the repository at this point in the history
Signed-off-by: Hasan Turken <turkenh@gmail.com>
  • Loading branch information
turkenh committed Sep 12, 2023
1 parent 112b02d commit 391d78a
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 6 deletions.
2 changes: 1 addition & 1 deletion cluster/crds/apiextensions.crossplane.io_usages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.13.0
controller-gen.kubebuilder.io/version: v0.12.1
name: usages.apiextensions.crossplane.io
spec:
group: apiextensions.crossplane.io
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ import (
"github.com/crossplane/crossplane-runtime/pkg/resource/unstructured/composed"

v1 "github.com/crossplane/crossplane/apis/apiextensions/v1"
"github.com/crossplane/crossplane/internal/controller/apiextensions/usage"
env "github.com/crossplane/crossplane/internal/controller/apiextensions/composite/environment"
"github.com/crossplane/crossplane/internal/controller/apiextensions/usage"
"github.com/crossplane/crossplane/internal/xcrd"
)

Expand Down
176 changes: 176 additions & 0 deletions internal/controller/apiextensions/usage/composed/composed.go
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
}
2 changes: 1 addition & 1 deletion internal/controller/apiextensions/usage/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ import (
"github.com/crossplane/crossplane-runtime/pkg/ratelimiter"
xpresource "github.com/crossplane/crossplane-runtime/pkg/resource"
"github.com/crossplane/crossplane-runtime/pkg/resource/unstructured"
"github.com/crossplane/crossplane-runtime/pkg/resource/unstructured/composed"

"github.com/crossplane/crossplane/apis/apiextensions/v1alpha1"
apiextensionscontroller "github.com/crossplane/crossplane/internal/controller/apiextensions/controller"
"github.com/crossplane/crossplane/internal/controller/apiextensions/usage/composed"
"github.com/crossplane/crossplane/internal/usage"
)

Expand Down
2 changes: 1 addition & 1 deletion internal/controller/apiextensions/usage/reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ import (
"github.com/crossplane/crossplane-runtime/pkg/errors"
xpresource "github.com/crossplane/crossplane-runtime/pkg/resource"
"github.com/crossplane/crossplane-runtime/pkg/resource/fake"
"github.com/crossplane/crossplane-runtime/pkg/resource/unstructured/composed"
"github.com/crossplane/crossplane-runtime/pkg/test"

"github.com/crossplane/crossplane/apis/apiextensions/v1alpha1"
"github.com/crossplane/crossplane/internal/controller/apiextensions/usage/composed"
"github.com/crossplane/crossplane/internal/xcrd"
)

Expand Down
2 changes: 1 addition & 1 deletion internal/controller/apiextensions/usage/selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ import (

"github.com/crossplane/crossplane-runtime/pkg/errors"
"github.com/crossplane/crossplane-runtime/pkg/meta"
"github.com/crossplane/crossplane-runtime/pkg/resource/unstructured/composed"

"github.com/crossplane/crossplane/apis/apiextensions/v1alpha1"
"github.com/crossplane/crossplane/internal/controller/apiextensions/usage/composed"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion internal/controller/apiextensions/usage/selector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/crossplane/crossplane-runtime/pkg/errors"
"github.com/crossplane/crossplane-runtime/pkg/resource/unstructured/composed"
"github.com/crossplane/crossplane-runtime/pkg/test"

"github.com/crossplane/crossplane/apis/apiextensions/v1alpha1"
"github.com/crossplane/crossplane/internal/controller/apiextensions/usage/composed"
)

var errBoom = errors.New("boom")
Expand Down

0 comments on commit 391d78a

Please sign in to comment.