github.com/abayer/test-infra@v0.0.5/boskos/crds/resource_crd.go (about) 1 /* 2 Copyright 2017 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 crds 18 19 import ( 20 "time" 21 22 "k8s.io/test-infra/boskos/common" 23 24 "k8s.io/apimachinery/pkg/apis/meta/v1" 25 "k8s.io/apimachinery/pkg/runtime" 26 "reflect" 27 ) 28 29 var ( 30 resourceType = Type{ 31 Kind: reflect.TypeOf(ResourceObject{}).Name(), 32 ListKind: reflect.TypeOf(ResourceCollection{}).Name(), 33 Singular: "resource", 34 Plural: "resources", 35 Object: &ResourceObject{}, 36 Collection: &ResourceCollection{}, 37 } 38 ) 39 40 // NewResourceClient creates a CRD rest client for common.Resource 41 func NewResourceClient() (ClientInterface, error) { 42 return newClientFromFlags(resourceType) 43 } 44 45 // NewTestResourceClient creates a fake CRD rest client for common.Resource 46 func NewTestResourceClient() ClientInterface { 47 return newDummyClient(resourceType) 48 } 49 50 // ResourceObject represents common.ResourceObject. It implements the Object interface. 51 type ResourceObject struct { 52 v1.TypeMeta `json:",inline"` 53 v1.ObjectMeta `json:"metadata,omitempty"` 54 Spec ResourceSpec `json:"spec,omitempty"` 55 Status ResourceStatus `json:"status,omitempty"` 56 } 57 58 // ResourceCollection is the Collection implementation 59 type ResourceCollection struct { 60 v1.TypeMeta `json:",inline"` 61 v1.ListMeta `json:"metadata,omitempty"` 62 Items []*ResourceObject `json:"items"` 63 } 64 65 // ResourceSpec holds information that are not likely to change 66 type ResourceSpec struct { 67 Type string `json:"type"` 68 } 69 70 // ResourceStatus holds information that are likely to change 71 type ResourceStatus struct { 72 State string `json:"state,omitempty"` 73 Owner string `json:"owner"` 74 LastUpdate time.Time `json:"lastUpdate,omitempty"` 75 UserData *common.UserData `json:"userData,omitempty"` 76 } 77 78 // GetName returns a unique identifier for a given resource 79 func (in *ResourceObject) GetName() string { 80 return in.Name 81 } 82 83 func (in *ResourceObject) deepCopyInto(out *ResourceObject) { 84 *out = *in 85 out.TypeMeta = in.TypeMeta 86 in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) 87 out.Spec = in.Spec 88 out.Status = in.Status 89 } 90 91 func (in *ResourceObject) deepCopy() *ResourceObject { 92 if in == nil { 93 return nil 94 } 95 out := new(ResourceObject) 96 in.deepCopyInto(out) 97 return out 98 } 99 100 // DeepCopyObject implements runtime.Object interface 101 func (in *ResourceObject) DeepCopyObject() runtime.Object { 102 if c := in.deepCopy(); c != nil { 103 return c 104 } 105 return nil 106 } 107 108 func (in *ResourceObject) toResource() common.Resource { 109 return common.Resource{ 110 Name: in.Name, 111 Type: in.Spec.Type, 112 Owner: in.Status.Owner, 113 State: in.Status.State, 114 LastUpdate: in.Status.LastUpdate, 115 UserData: in.Status.UserData, 116 } 117 } 118 119 // ToItem implements Object interface 120 func (in *ResourceObject) ToItem() common.Item { 121 return in.toResource() 122 } 123 124 func (in *ResourceObject) fromResource(r common.Resource) { 125 in.Name = r.Name 126 in.Spec.Type = r.Type 127 in.Status.Owner = r.Owner 128 in.Status.State = r.State 129 in.Status.LastUpdate = r.LastUpdate 130 in.Status.UserData = r.UserData 131 } 132 133 // FromItem implements Object interface 134 func (in *ResourceObject) FromItem(i common.Item) { 135 r, err := common.ItemToResource(i) 136 if err == nil { 137 in.fromResource(r) 138 } 139 } 140 141 // GetItems implements Collection interface 142 func (in *ResourceCollection) GetItems() []Object { 143 var items []Object 144 for _, i := range in.Items { 145 items = append(items, i) 146 } 147 return items 148 } 149 150 // SetItems implements Collection interface 151 func (in *ResourceCollection) SetItems(objects []Object) { 152 var items []*ResourceObject 153 for _, b := range objects { 154 items = append(items, b.(*ResourceObject)) 155 } 156 in.Items = items 157 } 158 159 func (in *ResourceCollection) deepCopyInto(out *ResourceCollection) { 160 *out = *in 161 out.TypeMeta = in.TypeMeta 162 in.ListMeta.DeepCopyInto(&out.ListMeta) 163 out.Items = in.Items 164 } 165 166 func (in *ResourceCollection) deepCopy() *ResourceCollection { 167 if in == nil { 168 return nil 169 } 170 out := new(ResourceCollection) 171 in.deepCopyInto(out) 172 return out 173 } 174 175 // DeepCopyObject implements Collection interface 176 func (in *ResourceCollection) DeepCopyObject() runtime.Object { 177 if c := in.deepCopy(); c != nil { 178 return c 179 } 180 return nil 181 }