github.com/abayer/test-infra@v0.0.5/boskos/crds/resources_config_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 "k8s.io/apimachinery/pkg/apis/meta/v1" 21 "k8s.io/apimachinery/pkg/runtime" 22 "k8s.io/test-infra/boskos/common" 23 "reflect" 24 ) 25 26 var ( 27 resourcesConfigType = Type{ 28 Kind: reflect.TypeOf(ResourcesConfigObject{}).Name(), 29 ListKind: reflect.TypeOf(ResourcesConfigCollection{}).Name(), 30 Singular: "resourcesconfig", 31 Plural: "resourcesconfigs", 32 Object: &ResourcesConfigObject{}, 33 Collection: &ResourcesConfigCollection{}, 34 } 35 ) 36 37 // NewResourceConfigClient creates a CRD rest client for common.Resource 38 func NewResourceConfigClient() (ClientInterface, error) { 39 return newClientFromFlags(resourcesConfigType) 40 } 41 42 // NewTestResourceConfigClient creates a fake CRD rest client for common.Resource 43 func NewTestResourceConfigClient() ClientInterface { 44 return newDummyClient(resourcesConfigType) 45 } 46 47 // ResourcesConfigObject holds generalized configuration information about how the 48 // resource needs to be created. 49 // Some Resource might not have a ResourceConfig (Example Project) 50 type ResourcesConfigObject struct { 51 v1.TypeMeta `json:",inline"` 52 v1.ObjectMeta `json:"metadata,omitempty"` 53 Spec ResourcesConfigSpec `json:"spec"` 54 } 55 56 // ResourcesConfigSpec holds config implementation specific configuration as well as resource needs 57 type ResourcesConfigSpec struct { 58 Config common.ConfigType `json:"config"` 59 Needs common.ResourceNeeds `json:"needs"` 60 } 61 62 // ResourcesConfigCollection implements the Collections interface 63 type ResourcesConfigCollection struct { 64 v1.TypeMeta `json:",inline"` 65 v1.ListMeta `json:"metadata,omitempty"` 66 Items []*ResourcesConfigObject `json:"items"` 67 } 68 69 // GetName implements the Object interface 70 func (in *ResourcesConfigObject) GetName() string { 71 return in.Name 72 } 73 74 func (in *ResourcesConfigObject) deepCopyInto(out *ResourcesConfigObject) { 75 *out = *in 76 out.TypeMeta = in.TypeMeta 77 in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) 78 out.Spec = in.Spec 79 } 80 81 func (in *ResourcesConfigObject) deepCopy() *ResourcesConfigObject { 82 if in == nil { 83 return nil 84 } 85 out := new(ResourcesConfigObject) 86 in.deepCopyInto(out) 87 return out 88 } 89 90 // DeepCopyObject implements the runtime.Object interface 91 func (in *ResourcesConfigObject) DeepCopyObject() runtime.Object { 92 if c := in.deepCopy(); c != nil { 93 return c 94 } 95 return nil 96 } 97 98 func (in *ResourcesConfigObject) toConfig() common.ResourcesConfig { 99 return common.ResourcesConfig{ 100 Name: in.Name, 101 Config: in.Spec.Config, 102 Needs: in.Spec.Needs, 103 } 104 } 105 106 // ToItem implements the Object interface 107 func (in *ResourcesConfigObject) ToItem() common.Item { 108 return in.toConfig() 109 } 110 111 func (in *ResourcesConfigObject) fromConfig(r common.ResourcesConfig) { 112 in.ObjectMeta.Name = r.Name 113 in.Spec.Config = r.Config 114 in.Spec.Needs = r.Needs 115 } 116 117 // FromItem implements the Object interface 118 func (in *ResourcesConfigObject) FromItem(i common.Item) { 119 c, err := common.ItemToResourcesConfig(i) 120 if err == nil { 121 in.fromConfig(c) 122 } 123 } 124 125 // GetItems implements the Collection interface 126 func (in *ResourcesConfigCollection) GetItems() []Object { 127 var items []Object 128 for _, i := range in.Items { 129 items = append(items, i) 130 } 131 return items 132 } 133 134 // SetItems implements the Collection interface 135 func (in *ResourcesConfigCollection) SetItems(objects []Object) { 136 var items []*ResourcesConfigObject 137 for _, b := range objects { 138 items = append(items, b.(*ResourcesConfigObject)) 139 } 140 in.Items = items 141 } 142 143 func (in *ResourcesConfigCollection) deepCopyInto(out *ResourcesConfigCollection) { 144 *out = *in 145 out.TypeMeta = in.TypeMeta 146 in.ListMeta.DeepCopyInto(&out.ListMeta) 147 out.Items = in.Items 148 } 149 150 func (in *ResourcesConfigCollection) deepCopy() *ResourcesConfigCollection { 151 if in == nil { 152 return nil 153 } 154 out := new(ResourcesConfigCollection) 155 in.deepCopyInto(out) 156 return out 157 } 158 159 // DeepCopyObject implements the runtime.Object interface 160 func (in *ResourcesConfigCollection) DeepCopyObject() runtime.Object { 161 if c := in.deepCopy(); c != nil { 162 return c 163 } 164 return nil 165 }