github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/pkg/runtime/testing/types.go (about) 1 /* 2 Copyright 2016 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 testing 18 19 import ( 20 "fmt" 21 22 "github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/runtime" 23 "github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/runtime/schema" 24 "github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/util/json" 25 ) 26 27 // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object 28 type EmbeddedTest struct { 29 runtime.TypeMeta 30 ID string 31 Object runtime.Object 32 EmptyObject runtime.Object 33 } 34 35 // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object 36 type EmbeddedTestExternal struct { 37 runtime.TypeMeta `json:",inline"` 38 ID string `json:"id,omitempty"` 39 Object runtime.RawExtension `json:"object,omitempty"` 40 EmptyObject runtime.RawExtension `json:"emptyObject,omitempty"` 41 } 42 43 // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object 44 type ObjectTest struct { 45 runtime.TypeMeta 46 47 ID string 48 Items []runtime.Object 49 } 50 51 // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object 52 type ObjectTestExternal struct { 53 runtime.TypeMeta `yaml:",inline" json:",inline"` 54 55 ID string `json:"id,omitempty"` 56 Items []runtime.RawExtension `json:"items,omitempty"` 57 } 58 59 // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object 60 type InternalSimple struct { 61 runtime.TypeMeta `json:",inline"` 62 TestString string `json:"testString"` 63 } 64 65 // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object 66 type ExternalSimple struct { 67 runtime.TypeMeta `json:",inline"` 68 TestString string `json:"testString"` 69 } 70 71 // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object 72 type ExtensionA struct { 73 runtime.TypeMeta `json:",inline"` 74 TestString string `json:"testString"` 75 } 76 77 // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object 78 type ExtensionB struct { 79 runtime.TypeMeta `json:",inline"` 80 TestString string `json:"testString"` 81 } 82 83 // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object 84 type ExternalExtensionType struct { 85 runtime.TypeMeta `json:",inline"` 86 Extension runtime.RawExtension `json:"extension"` 87 } 88 89 // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object 90 type InternalExtensionType struct { 91 runtime.TypeMeta `json:",inline"` 92 Extension runtime.Object `json:"extension"` 93 } 94 95 // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object 96 type ExternalOptionalExtensionType struct { 97 runtime.TypeMeta `json:",inline"` 98 Extension runtime.RawExtension `json:"extension,omitempty"` 99 } 100 101 // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object 102 type InternalOptionalExtensionType struct { 103 runtime.TypeMeta `json:",inline"` 104 Extension runtime.Object `json:"extension,omitempty"` 105 } 106 107 // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object 108 type InternalComplex struct { 109 runtime.TypeMeta 110 String string 111 Integer int 112 Integer64 int64 113 Int64 int64 114 Bool bool 115 } 116 117 // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object 118 type ExternalComplex struct { 119 runtime.TypeMeta `json:",inline"` 120 String string `json:"string" description:"testing"` 121 Integer int `json:"int"` 122 Integer64 int64 `json:",omitempty"` 123 Int64 int64 124 Bool bool `json:"bool"` 125 } 126 127 // Test a weird version/kind embedding format. 128 // +k8s:deepcopy-gen=false 129 type MyWeirdCustomEmbeddedVersionKindField struct { 130 ID string `json:"ID,omitempty"` 131 APIVersion string `json:"myVersionKey,omitempty"` 132 ObjectKind string `json:"myKindKey,omitempty"` 133 Z string `json:"Z,omitempty"` 134 Y uint64 `json:"Y,omitempty"` 135 } 136 137 // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object 138 type TestType1 struct { 139 MyWeirdCustomEmbeddedVersionKindField `json:",inline"` 140 A string `json:"A,omitempty"` 141 B int `json:"B,omitempty"` 142 C int8 `json:"C,omitempty"` 143 D int16 `json:"D,omitempty"` 144 E int32 `json:"E,omitempty"` 145 F int64 `json:"F,omitempty"` 146 G uint `json:"G,omitempty"` 147 H uint8 `json:"H,omitempty"` 148 I uint16 `json:"I,omitempty"` 149 J uint32 `json:"J,omitempty"` 150 K uint64 `json:"K,omitempty"` 151 L bool `json:"L,omitempty"` 152 M map[string]int `json:"M,omitempty"` 153 N map[string]TestType2 `json:"N,omitempty"` 154 O *TestType2 `json:"O,omitempty"` 155 P []TestType2 `json:"Q,omitempty"` 156 } 157 158 // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object 159 type TestType2 struct { 160 A string `json:"A,omitempty"` 161 B int `json:"B,omitempty"` 162 } 163 164 // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object 165 type ExternalTestType2 struct { 166 A string `json:"A,omitempty"` 167 B int `json:"B,omitempty"` 168 } 169 170 // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object 171 type ExternalTestType1 struct { 172 MyWeirdCustomEmbeddedVersionKindField `json:",inline"` 173 A string `json:"A,omitempty"` 174 B int `json:"B,omitempty"` 175 C int8 `json:"C,omitempty"` 176 D int16 `json:"D,omitempty"` 177 E int32 `json:"E,omitempty"` 178 F int64 `json:"F,omitempty"` 179 G uint `json:"G,omitempty"` 180 H uint8 `json:"H,omitempty"` 181 I uint16 `json:"I,omitempty"` 182 J uint32 `json:"J,omitempty"` 183 K uint64 `json:"K,omitempty"` 184 L bool `json:"L,omitempty"` 185 M map[string]int `json:"M,omitempty"` 186 N map[string]ExternalTestType2 `json:"N,omitempty"` 187 O *ExternalTestType2 `json:"O,omitempty"` 188 P []ExternalTestType2 `json:"Q,omitempty"` 189 } 190 191 // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object 192 type ExternalInternalSame struct { 193 MyWeirdCustomEmbeddedVersionKindField `json:",inline"` 194 A TestType2 `json:"A,omitempty"` 195 } 196 197 // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object 198 type UnversionedType struct { 199 MyWeirdCustomEmbeddedVersionKindField `json:",inline"` 200 A string `json:"A,omitempty"` 201 } 202 203 // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object 204 type UnknownType struct { 205 MyWeirdCustomEmbeddedVersionKindField `json:",inline"` 206 A string `json:"A,omitempty"` 207 } 208 209 func (obj *MyWeirdCustomEmbeddedVersionKindField) GetObjectKind() schema.ObjectKind { return obj } 210 func (obj *MyWeirdCustomEmbeddedVersionKindField) SetGroupVersionKind(gvk schema.GroupVersionKind) { 211 obj.APIVersion, obj.ObjectKind = gvk.ToAPIVersionAndKind() 212 } 213 func (obj *MyWeirdCustomEmbeddedVersionKindField) GroupVersionKind() schema.GroupVersionKind { 214 return schema.FromAPIVersionAndKind(obj.APIVersion, obj.ObjectKind) 215 } 216 217 func (obj *TestType2) GetObjectKind() schema.ObjectKind { return schema.EmptyObjectKind } 218 func (obj *ExternalTestType2) GetObjectKind() schema.ObjectKind { return schema.EmptyObjectKind } 219 220 // +k8s:deepcopy-gen=false 221 type Unstructured struct { 222 // Object is a JSON compatible map with string, float, int, bool, []interface{}, or 223 // map[string]interface{} 224 // children. 225 Object map[string]interface{} 226 } 227 228 var _ runtime.Unstructured = &Unstructured{} 229 230 func (obj *Unstructured) GetObjectKind() schema.ObjectKind { return obj } 231 232 func (obj *Unstructured) IsList() bool { 233 if obj.Object != nil { 234 _, ok := obj.Object["items"] 235 return ok 236 } 237 return false 238 } 239 240 func (obj *Unstructured) EachListItem(fn func(runtime.Object) error) error { 241 if obj.Object == nil { 242 return fmt.Errorf("content is not a list") 243 } 244 field, ok := obj.Object["items"] 245 if !ok { 246 return fmt.Errorf("content is not a list") 247 } 248 items, ok := field.([]interface{}) 249 if !ok { 250 return nil 251 } 252 for _, item := range items { 253 child, ok := item.(map[string]interface{}) 254 if !ok { 255 return fmt.Errorf("items member is not an object") 256 } 257 if err := fn(&Unstructured{Object: child}); err != nil { 258 return err 259 } 260 } 261 return nil 262 } 263 264 func (obj *Unstructured) NewEmptyInstance() runtime.Unstructured { 265 out := new(Unstructured) 266 if obj != nil { 267 out.SetGroupVersionKind(obj.GroupVersionKind()) 268 } 269 return out 270 } 271 272 func (obj *Unstructured) UnstructuredContent() map[string]interface{} { 273 if obj.Object == nil { 274 return make(map[string]interface{}) 275 } 276 return obj.Object 277 } 278 279 func (obj *Unstructured) SetUnstructuredContent(content map[string]interface{}) { 280 obj.Object = content 281 } 282 283 // MarshalJSON ensures that the unstructured object produces proper 284 // JSON when passed to Go's standard JSON library. 285 func (u *Unstructured) MarshalJSON() ([]byte, error) { 286 return json.Marshal(u.Object) 287 } 288 289 // UnmarshalJSON ensures that the unstructured object properly decodes 290 // JSON when passed to Go's standard JSON library. 291 func (u *Unstructured) UnmarshalJSON(b []byte) error { 292 return json.Unmarshal(b, &u.Object) 293 } 294 295 func (in *Unstructured) DeepCopyObject() runtime.Object { 296 return in.DeepCopy() 297 } 298 299 func (in *Unstructured) DeepCopy() *Unstructured { 300 if in == nil { 301 return nil 302 } 303 out := new(Unstructured) 304 *out = *in 305 out.Object = runtime.DeepCopyJSON(in.Object) 306 return out 307 } 308 309 func (u *Unstructured) GroupVersionKind() schema.GroupVersionKind { 310 apiVersion, ok := u.Object["apiVersion"].(string) 311 if !ok { 312 return schema.GroupVersionKind{} 313 } 314 gv, err := schema.ParseGroupVersion(apiVersion) 315 if err != nil { 316 return schema.GroupVersionKind{} 317 } 318 kind, ok := u.Object["kind"].(string) 319 if ok { 320 return gv.WithKind(kind) 321 } 322 return schema.GroupVersionKind{} 323 } 324 325 func (u *Unstructured) SetGroupVersionKind(gvk schema.GroupVersionKind) { 326 if u.Object == nil { 327 u.Object = make(map[string]interface{}) 328 } 329 u.Object["apiVersion"] = gvk.GroupVersion().String() 330 u.Object["kind"] = gvk.Kind 331 }