forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
conversion.go
256 lines (227 loc) · 8.03 KB
/
conversion.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package v1
import (
"sort"
"strings"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/unversioned"
v1 "k8s.io/kubernetes/pkg/api/v1"
"k8s.io/kubernetes/pkg/conversion"
"k8s.io/kubernetes/pkg/runtime"
oapi "github.com/openshift/origin/pkg/api"
newer "github.com/openshift/origin/pkg/image/api"
)
// The docker metadata must be cast to a version
func convert_api_Image_To_v1_Image(in *newer.Image, out *Image, s conversion.Scope) error {
if err := s.Convert(&in.ObjectMeta, &out.ObjectMeta, 0); err != nil {
return err
}
out.DockerImageReference = in.DockerImageReference
out.DockerImageManifest = in.DockerImageManifest
gvString := in.DockerImageMetadataVersion
if len(gvString) == 0 {
gvString = "1.0"
}
if !strings.Contains(gvString, "/") {
gvString = "/" + gvString
}
version, err := unversioned.ParseGroupVersion(gvString)
if err != nil {
return err
}
data, err := runtime.Encode(api.Codecs.LegacyCodec(version), &in.DockerImageMetadata, version)
if err != nil {
return err
}
out.DockerImageMetadata.RawJSON = data
out.DockerImageMetadataVersion = version.Version
if in.DockerImageLayers != nil {
out.DockerImageLayers = make([]ImageLayer, len(in.DockerImageLayers))
for i := range in.DockerImageLayers {
if err := s.Convert(&in.DockerImageLayers[i], &out.DockerImageLayers[i], 0); err != nil {
return err
}
}
} else {
out.DockerImageLayers = nil
}
return nil
}
func convert_v1_Image_To_api_Image(in *Image, out *newer.Image, s conversion.Scope) error {
if err := s.Convert(&in.ObjectMeta, &out.ObjectMeta, 0); err != nil {
return err
}
out.DockerImageReference = in.DockerImageReference
out.DockerImageManifest = in.DockerImageManifest
version := in.DockerImageMetadataVersion
if len(version) == 0 {
version = "1.0"
}
if len(in.DockerImageMetadata.RawJSON) > 0 {
// TODO: add a way to default the expected kind and version of an object if not set
obj, err := api.Scheme.New(unversioned.GroupVersionKind{Version: version, Kind: "DockerImage"})
if err != nil {
return err
}
if err := runtime.DecodeInto(api.Codecs.UniversalDecoder(), in.DockerImageMetadata.RawJSON, obj); err != nil {
return err
}
if err := s.Convert(obj, &out.DockerImageMetadata, 0); err != nil {
return err
}
}
out.DockerImageMetadataVersion = version
if in.DockerImageLayers != nil {
out.DockerImageLayers = make([]newer.ImageLayer, len(in.DockerImageLayers))
for i := range in.DockerImageLayers {
if err := s.Convert(&in.DockerImageLayers[i], &out.DockerImageLayers[i], 0); err != nil {
return err
}
}
} else {
out.DockerImageLayers = nil
}
return nil
}
func convert_v1_ImageStreamSpec_To_api_ImageStreamSpec(in *ImageStreamSpec, out *newer.ImageStreamSpec, s conversion.Scope) error {
out.DockerImageRepository = in.DockerImageRepository
out.Tags = make(map[string]newer.TagReference)
return s.Convert(&in.Tags, &out.Tags, 0)
}
func convert_api_ImageStreamSpec_To_v1_ImageStreamSpec(in *newer.ImageStreamSpec, out *ImageStreamSpec, s conversion.Scope) error {
out.DockerImageRepository = in.DockerImageRepository
if len(in.DockerImageRepository) > 0 {
// ensure that stored image references have no tag or ID, which was possible from 1.0.0 until 1.0.7
if ref, err := newer.ParseDockerImageReference(in.DockerImageRepository); err == nil {
if len(ref.Tag) > 0 || len(ref.ID) > 0 {
ref.Tag, ref.ID = "", ""
out.DockerImageRepository = ref.Exact()
}
}
}
out.Tags = make([]TagReference, 0, 0)
return s.Convert(&in.Tags, &out.Tags, 0)
}
func convert_v1_ImageStreamStatus_To_api_ImageStreamStatus(in *ImageStreamStatus, out *newer.ImageStreamStatus, s conversion.Scope) error {
out.DockerImageRepository = in.DockerImageRepository
out.Tags = make(map[string]newer.TagEventList)
return s.Convert(&in.Tags, &out.Tags, 0)
}
func convert_api_ImageStreamStatus_To_v1_ImageStreamStatus(in *newer.ImageStreamStatus, out *ImageStreamStatus, s conversion.Scope) error {
out.DockerImageRepository = in.DockerImageRepository
if len(in.DockerImageRepository) > 0 {
// ensure that stored image references have no tag or ID, which was possible from 1.0.0 until 1.0.7
if ref, err := newer.ParseDockerImageReference(in.DockerImageRepository); err == nil {
if len(ref.Tag) > 0 || len(ref.ID) > 0 {
ref.Tag, ref.ID = "", ""
out.DockerImageRepository = ref.Exact()
}
}
}
out.Tags = make([]NamedTagEventList, 0, 0)
return s.Convert(&in.Tags, &out.Tags, 0)
}
func convert_api_ImageStreamMapping_To_v1_ImageStreamMapping(in *newer.ImageStreamMapping, out *ImageStreamMapping, s conversion.Scope) error {
return s.DefaultConvert(in, out, conversion.DestFromSource)
}
func convert_v1_ImageStreamMapping_To_api_ImageStreamMapping(in *ImageStreamMapping, out *newer.ImageStreamMapping, s conversion.Scope) error {
return s.DefaultConvert(in, out, conversion.SourceToDest)
}
func addConversionFuncs(scheme *runtime.Scheme) {
err := scheme.AddDefaultingFuncs(
func(obj *ImageImportSpec) {
if obj.To == nil {
if ref, err := newer.ParseDockerImageReference(obj.From.Name); err == nil {
if len(ref.Tag) > 0 {
obj.To = &v1.LocalObjectReference{Name: ref.Tag}
}
}
}
})
if err != nil {
// If one of the default functions is malformed, detect it immediately.
panic(err)
}
err = scheme.AddConversionFuncs(
func(in *[]NamedTagEventList, out *map[string]newer.TagEventList, s conversion.Scope) error {
for _, curr := range *in {
newTagEventList := newer.TagEventList{}
if err := s.Convert(&curr.Conditions, &newTagEventList.Conditions, 0); err != nil {
return err
}
if err := s.Convert(&curr.Items, &newTagEventList.Items, 0); err != nil {
return err
}
(*out)[curr.Tag] = newTagEventList
}
return nil
},
func(in *map[string]newer.TagEventList, out *[]NamedTagEventList, s conversion.Scope) error {
allKeys := make([]string, 0, len(*in))
for key := range *in {
allKeys = append(allKeys, key)
}
sort.Strings(allKeys)
for _, key := range allKeys {
newTagEventList := (*in)[key]
oldTagEventList := &NamedTagEventList{Tag: key}
if err := s.Convert(&newTagEventList.Conditions, &oldTagEventList.Conditions, 0); err != nil {
return err
}
if err := s.Convert(&newTagEventList.Items, &oldTagEventList.Items, 0); err != nil {
return err
}
*out = append(*out, *oldTagEventList)
}
return nil
},
func(in *[]TagReference, out *map[string]newer.TagReference, s conversion.Scope) error {
for _, curr := range *in {
r := newer.TagReference{}
if err := s.Convert(&curr, &r, 0); err != nil {
return err
}
(*out)[curr.Name] = r
}
return nil
},
func(in *map[string]newer.TagReference, out *[]TagReference, s conversion.Scope) error {
allTags := make([]string, 0, len(*in))
for tag := range *in {
allTags = append(allTags, tag)
}
sort.Strings(allTags)
for _, tag := range allTags {
newTagReference := (*in)[tag]
oldTagReference := TagReference{}
if err := s.Convert(&newTagReference, &oldTagReference, 0); err != nil {
return err
}
oldTagReference.Name = tag
*out = append(*out, oldTagReference)
}
return nil
},
convert_api_Image_To_v1_Image,
convert_v1_Image_To_api_Image,
convert_v1_ImageStreamSpec_To_api_ImageStreamSpec,
convert_api_ImageStreamSpec_To_v1_ImageStreamSpec,
convert_v1_ImageStreamStatus_To_api_ImageStreamStatus,
convert_api_ImageStreamStatus_To_v1_ImageStreamStatus,
convert_api_ImageStreamMapping_To_v1_ImageStreamMapping,
convert_v1_ImageStreamMapping_To_api_ImageStreamMapping,
)
if err != nil {
// If one of the conversion functions is malformed, detect it immediately.
panic(err)
}
if err := scheme.AddFieldLabelConversionFunc("v1", "Image",
oapi.GetFieldLabelConversionFunc(newer.ImageToSelectableFields(&newer.Image{}), nil),
); err != nil {
panic(err)
}
if err := scheme.AddFieldLabelConversionFunc("v1", "ImageStream",
oapi.GetFieldLabelConversionFunc(newer.ImageStreamToSelectableFields(&newer.ImageStream{}), map[string]string{"name": "metadata.name"}),
); err != nil {
panic(err)
}
}