crossplane-runtime/pkg/resource/unstructured/composed/composed.go

93 lines
3.0 KiB
Go

/*
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.
*/
// Package composed contains an unstructured composed resource.
package composed
import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"github.com/crossplane/crossplane-runtime/apis/core/v1alpha1"
"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)
}
}
// New returns a new unstructured composed resource.
func New(opts ...Option) *Unstructured {
cr := &Unstructured{}
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 v1alpha1.ConditionType) v1alpha1.Condition {
conditioned := v1alpha1.ConditionedStatus{}
// The path is directly `status` because conditions are inline.
if err := fieldpath.Pave(cr.Object).GetValueInto("status", &conditioned); err != nil {
return v1alpha1.Condition{}
}
return conditioned.GetCondition(ct)
}
// SetConditions of this Composed resource.
func (cr *Unstructured) SetConditions(c ...v1alpha1.Condition) {
conditioned := v1alpha1.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() *v1alpha1.SecretReference {
out := &v1alpha1.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 *v1alpha1.SecretReference) {
_ = fieldpath.Pave(cr.Object).SetValue("spec.writeConnectionSecretToRef", r)
}