/
construct.go
179 lines (152 loc) · 6.08 KB
/
construct.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
/*
Copyright 2022 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 roundtrip
import (
"fmt"
"reflect"
"strconv"
"strings"
"time"
apimeta "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/intstr"
)
func defaultFillFuncs() map[reflect.Type]FillFunc {
funcs := map[reflect.Type]FillFunc{}
funcs[reflect.TypeOf(&runtime.RawExtension{})] = func(s string, i int, obj interface{}) {
// generate a raw object in normalized form
// TODO: test non-normalized round-tripping... YAMLToJSON normalizes and makes exact comparisons fail
obj.(*runtime.RawExtension).Raw = []byte(`{"apiVersion":"example.com/v1","kind":"CustomType","spec":{"replicas":1},"status":{"available":1}}`)
}
funcs[reflect.TypeOf(&metav1.TypeMeta{})] = func(s string, i int, obj interface{}) {
// APIVersion and Kind are not serialized in all formats (notably protobuf), so clear by default for cross-format checking.
obj.(*metav1.TypeMeta).APIVersion = ""
obj.(*metav1.TypeMeta).Kind = ""
}
funcs[reflect.TypeOf(&metav1.FieldsV1{})] = func(s string, i int, obj interface{}) {
obj.(*metav1.FieldsV1).Raw = []byte(`{}`)
}
funcs[reflect.TypeOf(&metav1.Time{})] = func(s string, i int, obj interface{}) {
// use the integer as an offset from the year
obj.(*metav1.Time).Time = time.Date(2000+i, 1, 1, 1, 1, 1, 0, time.UTC)
}
funcs[reflect.TypeOf(&metav1.MicroTime{})] = func(s string, i int, obj interface{}) {
// use the integer as an offset from the year, and as a microsecond
obj.(*metav1.MicroTime).Time = time.Date(2000+i, 1, 1, 1, 1, 1, i*int(time.Microsecond), time.UTC)
}
funcs[reflect.TypeOf(&intstr.IntOrString{})] = func(s string, i int, obj interface{}) {
// use the string as a string value
obj.(*intstr.IntOrString).Type = intstr.String
obj.(*intstr.IntOrString).StrVal = s + "Value"
}
return funcs
}
// CompatibilityTestObject returns a deterministically filled object for the specified GVK
func CompatibilityTestObject(scheme *runtime.Scheme, gvk schema.GroupVersionKind, fillFuncs map[reflect.Type]FillFunc) (runtime.Object, error) {
// Construct the object
obj, err := scheme.New(gvk)
if err != nil {
return nil, err
}
fill("", 0, reflect.TypeOf(obj), reflect.ValueOf(obj), fillFuncs, map[reflect.Type]bool{})
// Set the kind and apiVersion
if typeAcc, err := apimeta.TypeAccessor(obj); err != nil {
return nil, err
} else {
typeAcc.SetKind(gvk.Kind)
typeAcc.SetAPIVersion(gvk.GroupVersion().String())
}
return obj, nil
}
func fill(dataString string, dataInt int, t reflect.Type, v reflect.Value, fillFuncs map[reflect.Type]FillFunc, filledTypes map[reflect.Type]bool) {
if filledTypes[t] {
// we already filled this type, avoid recursing infinitely
return
}
filledTypes[t] = true
defer delete(filledTypes, t)
// if nil, populate pointers with a zero-value instance of the underlying type
if t.Kind() == reflect.Pointer && v.IsNil() {
if v.CanSet() {
v.Set(reflect.New(t.Elem()))
} else if v.IsNil() {
panic(fmt.Errorf("unsettable nil pointer of type %v in field %s", t, dataString))
}
}
if f, ok := fillFuncs[t]; ok {
// use the custom fill function for this type
f(dataString, dataInt, v.Interface())
return
}
switch t.Kind() {
case reflect.Slice:
// populate with a single-item slice
v.Set(reflect.MakeSlice(t, 1, 1))
// recurse to populate the item, preserving the data context
fill(dataString, dataInt, t.Elem(), v.Index(0), fillFuncs, filledTypes)
case reflect.Map:
// construct the key, which must be a string type, possibly converted to a type alias of string
key := reflect.ValueOf(dataString + "Key").Convert(t.Key())
// construct a zero-value item
item := reflect.New(t.Elem())
// recurse to populate the item, preserving the data context
fill(dataString, dataInt, t.Elem(), item.Elem(), fillFuncs, filledTypes)
// store in the map
v.Set(reflect.MakeMap(t))
v.SetMapIndex(key, item.Elem())
case reflect.Struct:
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
if !field.IsExported() {
continue
}
// use the json field name, which must be stable
dataString := strings.Split(field.Tag.Get("json"), ",")[0]
if len(dataString) == 0 {
// fall back to the struct field name if there is no json field name
dataString = "<no json tag> " + field.Name
}
// use the protobuf tag, which must be stable
dataInt := 0
if protobufTagParts := strings.Split(field.Tag.Get("protobuf"), ","); len(protobufTagParts) > 1 {
if tag, err := strconv.Atoi(protobufTagParts[1]); err != nil {
panic(err)
} else {
dataInt = tag
}
}
if dataInt == 0 {
// fall back to the length of dataString as a backup
dataInt = -len(dataString)
}
fieldType := field.Type
fieldValue := v.Field(i)
fill(dataString, dataInt, reflect.PointerTo(fieldType), fieldValue.Addr(), fillFuncs, filledTypes)
}
case reflect.Pointer:
fill(dataString, dataInt, t.Elem(), v.Elem(), fillFuncs, filledTypes)
case reflect.String:
// use Convert to set into string alias types correctly
v.Set(reflect.ValueOf(dataString + "Value").Convert(t))
case reflect.Bool:
// set to true to ensure we serialize omitempty fields
v.Set(reflect.ValueOf(true).Convert(t))
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
// use Convert to set into int alias types and different int widths correctly
v.Set(reflect.ValueOf(dataInt).Convert(t))
default:
panic(fmt.Errorf("unhandled type %v in field %s", t, dataString))
}
}