sigs.k8s.io/cluster-api@v1.6.3/exp/addons/api/v1alpha4/clusterresourcesetbinding_types.go (about) 1 /* 2 Copyright 2020 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package v1alpha4 18 19 import ( 20 "reflect" 21 22 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 23 ) 24 25 // ANCHOR: ResourceBinding 26 27 // ResourceBinding shows the status of a resource that belongs to a ClusterResourceSet matched by the owner cluster of the ClusterResourceSetBinding object. 28 type ResourceBinding struct { 29 // ResourceRef specifies a resource. 30 ResourceRef `json:",inline"` 31 32 // Hash is the hash of a resource's data. This can be used to decide if a resource is changed. 33 // For "ApplyOnce" ClusterResourceSet.spec.strategy, this is no-op as that strategy does not act on change. 34 Hash string `json:"hash,omitempty"` 35 36 // LastAppliedTime identifies when this resource was last applied to the cluster. 37 // +optional 38 LastAppliedTime *metav1.Time `json:"lastAppliedTime,omitempty"` 39 40 // Applied is to track if a resource is applied to the cluster or not. 41 Applied bool `json:"applied"` 42 } 43 44 // ANCHOR_END: ResourceBinding 45 46 // ResourceSetBinding keeps info on all of the resources in a ClusterResourceSet. 47 type ResourceSetBinding struct { 48 // ClusterResourceSetName is the name of the ClusterResourceSet that is applied to the owner cluster of the binding. 49 ClusterResourceSetName string `json:"clusterResourceSetName"` 50 51 // Resources is a list of resources that the ClusterResourceSet has. 52 Resources []ResourceBinding `json:"resources,omitempty"` 53 } 54 55 // IsApplied returns true if the resource is applied to the cluster by checking the cluster's binding. 56 func (r *ResourceSetBinding) IsApplied(resourceRef ResourceRef) bool { 57 for _, resource := range r.Resources { 58 if reflect.DeepEqual(resource.ResourceRef, resourceRef) { 59 if resource.Applied { 60 return true 61 } 62 } 63 } 64 return false 65 } 66 67 // SetBinding sets resourceBinding for a resource in resourceSetbinding either by updating the existing one or 68 // creating a new one. 69 func (r *ResourceSetBinding) SetBinding(resourceBinding ResourceBinding) { 70 for i := range r.Resources { 71 if reflect.DeepEqual(r.Resources[i].ResourceRef, resourceBinding.ResourceRef) { 72 r.Resources[i] = resourceBinding 73 return 74 } 75 } 76 r.Resources = append(r.Resources, resourceBinding) 77 } 78 79 // GetOrCreateBinding returns the ResourceSetBinding for a given ClusterResourceSet if exists, 80 // otherwise creates one and updates ClusterResourceSet with it. 81 func (c *ClusterResourceSetBinding) GetOrCreateBinding(clusterResourceSet *ClusterResourceSet) *ResourceSetBinding { 82 for _, binding := range c.Spec.Bindings { 83 if binding.ClusterResourceSetName == clusterResourceSet.Name { 84 return binding 85 } 86 } 87 binding := &ResourceSetBinding{ClusterResourceSetName: clusterResourceSet.Name, Resources: []ResourceBinding{}} 88 c.Spec.Bindings = append(c.Spec.Bindings, binding) 89 return binding 90 } 91 92 // DeleteBinding removes the ClusterResourceSet from the ClusterResourceSetBinding Bindings list. 93 func (c *ClusterResourceSetBinding) DeleteBinding(clusterResourceSet *ClusterResourceSet) { 94 for i, binding := range c.Spec.Bindings { 95 if binding.ClusterResourceSetName == clusterResourceSet.Name { 96 copy(c.Spec.Bindings[i:], c.Spec.Bindings[i+1:]) 97 c.Spec.Bindings = c.Spec.Bindings[:len(c.Spec.Bindings)-1] 98 break 99 } 100 } 101 } 102 103 // +kubebuilder:object:root=true 104 // +kubebuilder:unservedversion 105 // +kubebuilder:deprecatedversion 106 // +kubebuilder:resource:path=clusterresourcesetbindings,scope=Namespaced,categories=cluster-api 107 // +kubebuilder:subresource:status 108 // +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp",description="Time duration since creation of ClusterResourceSetBinding" 109 110 // ClusterResourceSetBinding lists all matching ClusterResourceSets with the cluster it belongs to. 111 // 112 // Deprecated: This type will be removed in one of the next releases. 113 type ClusterResourceSetBinding struct { 114 metav1.TypeMeta `json:",inline"` 115 metav1.ObjectMeta `json:"metadata,omitempty"` 116 Spec ClusterResourceSetBindingSpec `json:"spec,omitempty"` 117 } 118 119 // ANCHOR: ClusterResourceSetBindingSpec 120 121 // ClusterResourceSetBindingSpec defines the desired state of ClusterResourceSetBinding. 122 type ClusterResourceSetBindingSpec struct { 123 // Bindings is a list of ClusterResourceSets and their resources. 124 Bindings []*ResourceSetBinding `json:"bindings,omitempty"` 125 } 126 127 // ANCHOR_END: ClusterResourceSetBindingSpec 128 129 // +kubebuilder:object:root=true 130 131 // ClusterResourceSetBindingList contains a list of ClusterResourceSetBinding. 132 // 133 // Deprecated: This type will be removed in one of the next releases. 134 type ClusterResourceSetBindingList struct { 135 metav1.TypeMeta `json:",inline"` 136 metav1.ListMeta `json:"metadata,omitempty"` 137 Items []ClusterResourceSetBinding `json:"items"` 138 } 139 140 func init() { 141 objectTypes = append(objectTypes, &ClusterResourceSetBinding{}, &ClusterResourceSetBindingList{}) 142 }