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