forked from kubernetes/test-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresource_crd.go
181 lines (154 loc) · 4.53 KB
/
resource_crd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package crds
import (
"time"
"k8s.io/test-infra/boskos/common"
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"reflect"
)
var (
resourceType = Type{
Kind: reflect.TypeOf(ResourceObject{}).Name(),
ListKind: reflect.TypeOf(ResourceCollection{}).Name(),
Singular: "resource",
Plural: "resources",
Object: &ResourceObject{},
Collection: &ResourceCollection{},
}
)
// NewResourceClient creates a CRD rest client for common.Resource
func NewResourceClient() (ClientInterface, error) {
return newClientFromFlags(resourceType)
}
// NewTestResourceClient creates a fake CRD rest client for common.Resource
func NewTestResourceClient() ClientInterface {
return newDummyClient(resourceType)
}
// ResourceObject represents common.ResourceObject. It implements the Object interface.
type ResourceObject struct {
v1.TypeMeta `json:",inline"`
v1.ObjectMeta `json:"metadata,omitempty"`
Spec ResourceSpec `json:"spec,omitempty"`
Status ResourceStatus `json:"status,omitempty"`
}
// ResourceCollection is the Collection implementation
type ResourceCollection struct {
v1.TypeMeta `json:",inline"`
v1.ListMeta `json:"metadata,omitempty"`
Items []*ResourceObject `json:"items"`
}
// ResourceSpec holds information that are not likely to change
type ResourceSpec struct {
Type string `json:"type"`
}
// ResourceStatus holds information that are likely to change
type ResourceStatus struct {
State string `json:"state,omitempty"`
Owner string `json:"owner"`
LastUpdate time.Time `json:"lastUpdate,omitempty"`
UserData common.UserData `json:"userData,omitempty"`
}
// GetName returns a unique identifier for a given resource
func (in *ResourceObject) GetName() string {
return in.Name
}
func (in *ResourceObject) deepCopyInto(out *ResourceObject) {
*out = *in
out.TypeMeta = in.TypeMeta
in.ObjectMeta.DeepCopyInto(&out.ObjectMeta)
out.Spec = in.Spec
out.Status = in.Status
}
func (in *ResourceObject) deepCopy() *ResourceObject {
if in == nil {
return nil
}
out := new(ResourceObject)
in.deepCopyInto(out)
return out
}
// DeepCopyObject implements runtime.Object interface
func (in *ResourceObject) DeepCopyObject() runtime.Object {
if c := in.deepCopy(); c != nil {
return c
}
return nil
}
func (in *ResourceObject) toResource() common.Resource {
return common.Resource{
Name: in.Name,
Type: in.Spec.Type,
Owner: in.Status.Owner,
State: in.Status.State,
LastUpdate: in.Status.LastUpdate,
UserData: in.Status.UserData,
}
}
// ToItem implements Object interface
func (in *ResourceObject) ToItem() common.Item {
return in.toResource()
}
func (in *ResourceObject) fromResource(r common.Resource) {
in.Name = r.Name
in.Spec.Type = r.Type
in.Status.Owner = r.Owner
in.Status.State = r.State
in.Status.LastUpdate = r.LastUpdate
in.Status.UserData = r.UserData
}
// FromItem implements Object interface
func (in *ResourceObject) FromItem(i common.Item) {
r, err := common.ItemToResource(i)
if err == nil {
in.fromResource(r)
}
}
// GetItems implements Collection interface
func (in *ResourceCollection) GetItems() []Object {
var items []Object
for _, i := range in.Items {
items = append(items, i)
}
return items
}
// SetItems implements Collection interface
func (in *ResourceCollection) SetItems(objects []Object) {
var items []*ResourceObject
for _, b := range objects {
items = append(items, b.(*ResourceObject))
}
in.Items = items
}
func (in *ResourceCollection) deepCopyInto(out *ResourceCollection) {
*out = *in
out.TypeMeta = in.TypeMeta
in.ListMeta.DeepCopyInto(&out.ListMeta)
out.Items = in.Items
}
func (in *ResourceCollection) deepCopy() *ResourceCollection {
if in == nil {
return nil
}
out := new(ResourceCollection)
in.deepCopyInto(out)
return out
}
// DeepCopyObject implements Collection interface
func (in *ResourceCollection) DeepCopyObject() runtime.Object {
if c := in.deepCopy(); c != nil {
return c
}
return nil
}