Skip to content

Commit c7dfd6b

Browse files
committed
build: add conversion from url values to build log options
1 parent d857ce4 commit c7dfd6b

File tree

2 files changed

+137
-1
lines changed

2 files changed

+137
-1
lines changed

pkg/build/apis/build/v1/conversion.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package v1
33
import (
44
"net/url"
55

6+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
67
"k8s.io/apimachinery/pkg/conversion"
78
"k8s.io/apimachinery/pkg/runtime"
89

@@ -180,6 +181,90 @@ func Convert_v1_BinaryBuildRequestOptions_To_url_Values(in *v1.BinaryBuildReques
180181
return nil
181182
}
182183

184+
func Convert_url_Values_To_v1_BuildLogOptions(in *url.Values, out *v1.BuildLogOptions, s conversion.Scope) error {
185+
if in == nil || out == nil {
186+
return nil
187+
}
188+
if values, ok := map[string][]string(*in)["container"]; ok && len(values) > 0 {
189+
if err := runtime.Convert_Slice_string_To_string(&values, &out.Container, s); err != nil {
190+
return err
191+
}
192+
} else {
193+
out.Container = ""
194+
}
195+
if values, ok := map[string][]string(*in)["follow"]; ok && len(values) > 0 {
196+
if err := runtime.Convert_Slice_string_To_bool(&values, &out.Follow, s); err != nil {
197+
return err
198+
}
199+
} else {
200+
out.Follow = false
201+
}
202+
if values, ok := map[string][]string(*in)["previous"]; ok && len(values) > 0 {
203+
if err := runtime.Convert_Slice_string_To_bool(&values, &out.Previous, s); err != nil {
204+
return err
205+
}
206+
} else {
207+
out.Previous = false
208+
}
209+
if values, ok := map[string][]string(*in)["sinceSeconds"]; ok && len(values) > 0 {
210+
if err := runtime.Convert_Slice_string_To_Pointer_int64(&values, &out.SinceSeconds, s); err != nil {
211+
return err
212+
}
213+
} else {
214+
out.SinceSeconds = nil
215+
}
216+
if values, ok := map[string][]string(*in)["sinceTime"]; ok && len(values) > 0 {
217+
if err := metav1.Convert_Slice_string_To_Pointer_v1_Time(&values, &out.SinceTime, s); err != nil {
218+
return err
219+
}
220+
} else {
221+
out.SinceTime = nil
222+
}
223+
if values, ok := map[string][]string(*in)["timestamps"]; ok && len(values) > 0 {
224+
if err := runtime.Convert_Slice_string_To_bool(&values, &out.Timestamps, s); err != nil {
225+
return err
226+
}
227+
} else {
228+
out.Timestamps = false
229+
}
230+
if values, ok := map[string][]string(*in)["tailLines"]; ok && len(values) > 0 {
231+
if err := runtime.Convert_Slice_string_To_Pointer_int64(&values, &out.TailLines, s); err != nil {
232+
return err
233+
}
234+
} else {
235+
out.TailLines = nil
236+
}
237+
if values, ok := map[string][]string(*in)["limitBytes"]; ok && len(values) > 0 {
238+
if err := runtime.Convert_Slice_string_To_Pointer_int64(&values, &out.LimitBytes, s); err != nil {
239+
return err
240+
}
241+
} else {
242+
out.LimitBytes = nil
243+
}
244+
if values, ok := map[string][]string(*in)["nowait"]; ok && len(values) > 0 {
245+
if err := runtime.Convert_Slice_string_To_bool(&values, &out.NoWait, s); err != nil {
246+
return err
247+
}
248+
} else {
249+
out.NoWait = false
250+
}
251+
if values, ok := map[string][]string(*in)["version"]; ok && len(values) > 0 {
252+
if err := runtime.Convert_Slice_string_To_Pointer_int64(&values, &out.Version, s); err != nil {
253+
return err
254+
}
255+
} else {
256+
out.Version = nil
257+
}
258+
if values, ok := map[string][]string(*in)["insecureSkipTLSVerifyBackend"]; ok && len(values) > 0 {
259+
if err := runtime.Convert_Slice_string_To_bool(&values, &out.InsecureSkipTLSVerifyBackend, s); err != nil {
260+
return err
261+
}
262+
} else {
263+
out.InsecureSkipTLSVerifyBackend = false
264+
}
265+
return nil
266+
}
267+
183268
// AddCustomConversionFuncs adds conversion functions which cannot be automatically generated.
184269
// This is typically due to the objects not having 1:1 field mappings.
185270
func AddCustomConversionFuncs(scheme *runtime.Scheme) error {
@@ -193,6 +278,11 @@ func AddCustomConversionFuncs(scheme *runtime.Scheme) error {
193278
}); err != nil {
194279
return err
195280
}
281+
if err := scheme.AddConversionFunc((*url.Values)(nil), (*v1.BuildLogOptions)(nil), func(a, b interface{}, scope conversion.Scope) error {
282+
return Convert_url_Values_To_v1_BuildLogOptions(a.(*url.Values), b.(*v1.BuildLogOptions), scope)
283+
}); err != nil {
284+
return err
285+
}
196286
return nil
197287
}
198288

pkg/build/apis/build/v1/conversion_test.go

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@ package v1
22

33
import (
44
"testing"
5+
"time"
56

67
corev1 "k8s.io/api/core/v1"
8+
apiequality "k8s.io/apimachinery/pkg/api/equality"
79
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
810
"k8s.io/apimachinery/pkg/conversion/queryparams"
9-
runtime "k8s.io/apimachinery/pkg/runtime"
11+
"k8s.io/apimachinery/pkg/runtime"
1012
"k8s.io/apimachinery/pkg/runtime/serializer"
13+
"k8s.io/apimachinery/pkg/util/diff"
1114
kinternal "k8s.io/kubernetes/pkg/apis/core"
1215

1316
v1 "github.com/openshift/api/build/v1"
@@ -387,3 +390,46 @@ func TestImageChangeTriggerNilImageChangePointer(t *testing.T) {
387390
}
388391
}
389392
}
393+
394+
func TestBuildLogOptionsOptions(t *testing.T) {
395+
sinceSeconds := int64(1)
396+
sinceTime := metav1.NewTime(time.Date(2000, 1, 1, 12, 34, 56, 0, time.UTC).Local())
397+
tailLines := int64(2)
398+
limitBytes := int64(3)
399+
400+
unversionedBuildLogOptions := &internal.BuildLogOptions{
401+
Container: "mycontainer",
402+
Follow: true,
403+
Previous: true,
404+
SinceSeconds: &sinceSeconds,
405+
SinceTime: &sinceTime,
406+
Timestamps: true,
407+
TailLines: &tailLines,
408+
LimitBytes: &limitBytes,
409+
}
410+
versionedBuildLogOptions, err := scheme.ConvertToVersion(unversionedBuildLogOptions, v1.GroupVersion)
411+
if err != nil {
412+
t.Fatal(err)
413+
}
414+
params, err := queryparams.Convert(versionedBuildLogOptions)
415+
if err != nil {
416+
t.Fatal(err)
417+
}
418+
419+
// checks "query params -> versioned" conversion
420+
{
421+
convertedVersionedBuildLogOptions := &v1.BuildLogOptions{}
422+
if err := scheme.Convert(&params, convertedVersionedBuildLogOptions, nil); err != nil {
423+
t.Fatal(err)
424+
}
425+
426+
// TypeMeta is not filled by the conversion functions
427+
// Setting it manually to simplify testing.
428+
convertedVersionedBuildLogOptions.TypeMeta.Kind = "BuildLogOptions"
429+
convertedVersionedBuildLogOptions.TypeMeta.APIVersion = v1.GroupVersion.String()
430+
431+
if !apiequality.Semantic.DeepEqual(convertedVersionedBuildLogOptions, versionedBuildLogOptions) {
432+
t.Fatalf("Unexpected deserialization:\n%s", diff.ObjectGoPrintSideBySide(versionedBuildLogOptions, convertedVersionedBuildLogOptions))
433+
}
434+
}
435+
}

0 commit comments

Comments
 (0)