Skip to content

Commit

Permalink
Merge pull request #105 from turkenh/release-1.13-backport-usage
Browse files Browse the repository at this point in the history
[release-1.13] Backport Usage API and Convert from Json Transform
  • Loading branch information
phisco authored Sep 12, 2023
2 parents ce30956 + c7bf47a commit 141e541
Show file tree
Hide file tree
Showing 29 changed files with 3,336 additions and 36 deletions.
14 changes: 10 additions & 4 deletions apis/apiextensions/v1/composition_transforms.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,12 +445,15 @@ const (
TransformIOTypeInt TransformIOType = "int"
TransformIOTypeInt64 TransformIOType = "int64"
TransformIOTypeFloat64 TransformIOType = "float64"

TransformIOTypeObject TransformIOType = "object"
TransformIOTypeArray TransformIOType = "array"
)

// IsValid checks if the given TransformIOType is valid.
func (c TransformIOType) IsValid() bool {
switch c {
case TransformIOTypeString, TransformIOTypeBool, TransformIOTypeInt, TransformIOTypeInt64, TransformIOTypeFloat64:
case TransformIOTypeString, TransformIOTypeBool, TransformIOTypeInt, TransformIOTypeInt64, TransformIOTypeFloat64, TransformIOTypeObject, TransformIOTypeArray:
return true
}
return false
Expand All @@ -464,12 +467,13 @@ type ConvertTransformFormat string
const (
ConvertTransformFormatNone ConvertTransformFormat = "none"
ConvertTransformFormatQuantity ConvertTransformFormat = "quantity"
ConvertTransformFormatJSON ConvertTransformFormat = "json"
)

// IsValid returns true if the format is valid.
func (c ConvertTransformFormat) IsValid() bool {
switch c {
case ConvertTransformFormatNone, ConvertTransformFormatQuantity:
case ConvertTransformFormatNone, ConvertTransformFormatQuantity, ConvertTransformFormatJSON:
return true
}
return false
Expand All @@ -478,17 +482,19 @@ func (c ConvertTransformFormat) IsValid() bool {
// A ConvertTransform converts the input into a new object whose type is supplied.
type ConvertTransform struct {
// ToType is the type of the output of this transform.
// +kubebuilder:validation:Enum=string;int;int64;bool;float64
// +kubebuilder:validation:Enum=string;int;int64;bool;float64;object;list
ToType TransformIOType `json:"toType"`

// The expected input format.
//
// * `quantity` - parses the input as a K8s [`resource.Quantity`](https://pkg.go.dev/k8s.io/apimachinery/pkg/api/resource#Quantity).
// Only used during `string -> float64` conversions.
// * `json` - parses the input as a JSON string.
// Only used during `string -> object` or `string -> list` conversions.
//
// If this property is null, the default conversion is applied.
//
// +kubebuilder:validation:Enum=none;quantity
// +kubebuilder:validation:Enum=none;quantity;json
// +kubebuilder:validation:Default=none
Format *ConvertTransformFormat `json:"format,omitempty"`
}
Expand Down
9 changes: 9 additions & 0 deletions apis/apiextensions/v1alpha1/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ var (
EnvironmentConfigGroupVersionKind = SchemeGroupVersion.WithKind(EnvironmentConfigKind)
)

// Usage type metadata.
var (
UsageKind = reflect.TypeOf(Usage{}).Name()
UsageGroupKind = schema.GroupKind{Group: Group, Kind: UsageKind}.String()
UsageKindAPIVersion = UsageKind + "." + SchemeGroupVersion.String()
UsageGroupVersionKind = SchemeGroupVersion.WithKind(UsageKind)
)

func init() {
SchemeBuilder.Register(&EnvironmentConfig{}, &EnvironmentConfigList{})
SchemeBuilder.Register(&Usage{}, &UsageList{})
}
101 changes: 101 additions & 0 deletions apis/apiextensions/v1alpha1/usage_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
Copyright 2023 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 v1alpha1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
)

// ResourceRef is a reference to a resource.
type ResourceRef struct {
// Name of the referent.
Name string `json:"name"`
}

// ResourceSelector is a selector to a resource.
type ResourceSelector struct {
// MatchLabels ensures an object with matching labels is selected.
MatchLabels map[string]string `json:"matchLabels,omitempty"`

// MatchControllerRef ensures an object with the same controller reference
// as the selecting object is selected.
MatchControllerRef *bool `json:"matchControllerRef,omitempty"`
}

// Resource defines a cluster-scoped resource.
type Resource struct {
// API version of the referent.
// +optional
APIVersion string `json:"apiVersion,omitempty"`
// Kind of the referent.
// More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
// +optional
Kind string `json:"kind,omitempty"`
// Reference to the resource.
// +optional
ResourceRef *ResourceRef `json:"resourceRef,omitempty"`
// Selector to the resource.
// This field will be ignored if ResourceRef is set.
// +optional
ResourceSelector *ResourceSelector `json:"resourceSelector,omitempty"`
}

// UsageSpec defines the desired state of Usage.
type UsageSpec struct {
// Of is the resource that is "being used".
// +kubebuilder:validation:XValidation:rule="has(self.resourceRef) || has(self.resourceSelector)",message="either a resource reference or a resource selector should be set."
Of Resource `json:"of"`
// By is the resource that is "using the other resource".
// +optional
// +kubebuilder:validation:XValidation:rule="has(self.resourceRef) || has(self.resourceSelector)",message="either a resource reference or a resource selector should be set."
By *Resource `json:"by,omitempty"`
// Reason is the reason for blocking deletion of the resource.
// +optional
Reason *string `json:"reason,omitempty"`
}

// UsageStatus defines the observed state of Usage.
type UsageStatus struct {
xpv1.ConditionedStatus `json:",inline"`
}

// A Usage defines a deletion blocking relationship between two resources.
// +kubebuilder:object:root=true
// +kubebuilder:storageversion
// +kubebuilder:printcolumn:name="DETAILS",type="string",JSONPath=".metadata.annotations.crossplane\\.io/usage-details"
// +kubebuilder:printcolumn:name="READY",type="string",JSONPath=".status.conditions[?(@.type=='Ready')].status"
// +kubebuilder:printcolumn:name="AGE",type="date",JSONPath=".metadata.creationTimestamp"
// +kubebuilder:resource:scope=Cluster,categories=crossplane
// +kubebuilder:subresource:status
type Usage struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
// +kubebuilder:validation:XValidation:rule="has(self.by) || has(self.reason)",message="either \"spec.by\" or \"spec.reason\" must be specified."
Spec UsageSpec `json:"spec"`
Status UsageStatus `json:"status,omitempty"`
}

// +kubebuilder:object:root=true

// UsageList contains a list of Usage.
type UsageList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []Usage `json:"items"`
}
168 changes: 168 additions & 0 deletions apis/apiextensions/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 10 additions & 4 deletions apis/apiextensions/v1beta1/zz_generated.composition_transforms.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,12 +447,15 @@ const (
TransformIOTypeInt TransformIOType = "int"
TransformIOTypeInt64 TransformIOType = "int64"
TransformIOTypeFloat64 TransformIOType = "float64"

TransformIOTypeObject TransformIOType = "object"
TransformIOTypeArray TransformIOType = "array"
)

// IsValid checks if the given TransformIOType is valid.
func (c TransformIOType) IsValid() bool {
switch c {
case TransformIOTypeString, TransformIOTypeBool, TransformIOTypeInt, TransformIOTypeInt64, TransformIOTypeFloat64:
case TransformIOTypeString, TransformIOTypeBool, TransformIOTypeInt, TransformIOTypeInt64, TransformIOTypeFloat64, TransformIOTypeObject, TransformIOTypeArray:
return true
}
return false
Expand All @@ -466,12 +469,13 @@ type ConvertTransformFormat string
const (
ConvertTransformFormatNone ConvertTransformFormat = "none"
ConvertTransformFormatQuantity ConvertTransformFormat = "quantity"
ConvertTransformFormatJSON ConvertTransformFormat = "json"
)

// IsValid returns true if the format is valid.
func (c ConvertTransformFormat) IsValid() bool {
switch c {
case ConvertTransformFormatNone, ConvertTransformFormatQuantity:
case ConvertTransformFormatNone, ConvertTransformFormatQuantity, ConvertTransformFormatJSON:
return true
}
return false
Expand All @@ -480,17 +484,19 @@ func (c ConvertTransformFormat) IsValid() bool {
// A ConvertTransform converts the input into a new object whose type is supplied.
type ConvertTransform struct {
// ToType is the type of the output of this transform.
// +kubebuilder:validation:Enum=string;int;int64;bool;float64
// +kubebuilder:validation:Enum=string;int;int64;bool;float64;object;list
ToType TransformIOType `json:"toType"`

// The expected input format.
//
// * `quantity` - parses the input as a K8s [`resource.Quantity`](https://pkg.go.dev/k8s.io/apimachinery/pkg/api/resource#Quantity).
// Only used during `string -> float64` conversions.
// * `json` - parses the input as a JSON string.
// Only used during `string -> object` or `string -> list` conversions.
//
// If this property is null, the default conversion is applied.
//
// +kubebuilder:validation:Enum=none;quantity
// +kubebuilder:validation:Enum=none;quantity;json
// +kubebuilder:validation:Default=none
Format *ConvertTransformFormat `json:"format,omitempty"`
}
Expand Down
Loading

0 comments on commit 141e541

Please sign in to comment.