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