Skip to content

Commit aaa63d7

Browse files
author
louisgong
committed
add DeleteOptions conversion
1 parent 7b7aa58 commit aaa63d7

File tree

6 files changed

+101
-15
lines changed

6 files changed

+101
-15
lines changed

staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/conversion.go

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package v1
1818

1919
import (
2020
"fmt"
21+
"net/url"
2122
"strconv"
2223
"strings"
2324

@@ -26,6 +27,7 @@ import (
2627
"k8s.io/apimachinery/pkg/fields"
2728
"k8s.io/apimachinery/pkg/labels"
2829
"k8s.io/apimachinery/pkg/runtime"
30+
"k8s.io/apimachinery/pkg/types"
2931
"k8s.io/apimachinery/pkg/util/intstr"
3032
)
3133

@@ -81,7 +83,7 @@ func AddConversionFuncs(scheme *runtime.Scheme) error {
8183

8284
Convert_Slice_string_To_Slice_int32,
8385

84-
Convert_Slice_string_To_v1_DeletionPropagation,
86+
Convert_Slice_string_To_Pointer_v1_DeletionPropagation,
8587

8688
Convert_Slice_string_To_v1_IncludeObjectPolicy,
8789
)
@@ -352,13 +354,16 @@ func Convert_Slice_string_To_Slice_int32(in *[]string, out *[]int32, s conversio
352354
return nil
353355
}
354356

355-
// Convert_Slice_string_To_v1_DeletionPropagation allows converting a URL query parameter propagationPolicy
356-
func Convert_Slice_string_To_v1_DeletionPropagation(in *[]string, out *DeletionPropagation, s conversion.Scope) error {
357+
// Convert_Slice_string_To_Pointer_v1_DeletionPropagation allows converting a URL query parameter propagationPolicy
358+
func Convert_Slice_string_To_Pointer_v1_DeletionPropagation(in *[]string, out **DeletionPropagation, s conversion.Scope) error {
359+
var str string
357360
if len(*in) > 0 {
358-
*out = DeletionPropagation((*in)[0])
361+
str = (*in)[0]
359362
} else {
360-
*out = ""
363+
str = ""
361364
}
365+
temp := DeletionPropagation(str)
366+
*out = &temp
362367
return nil
363368
}
364369

@@ -369,3 +374,33 @@ func Convert_Slice_string_To_v1_IncludeObjectPolicy(in *[]string, out *IncludeOb
369374
}
370375
return nil
371376
}
377+
378+
// Convert_url_Values_To_v1_DeleteOptions allows converting a URL to DeleteOptions.
379+
func Convert_url_Values_To_v1_DeleteOptions(in *url.Values, out *DeleteOptions, s conversion.Scope) error {
380+
if err := autoConvert_url_Values_To_v1_DeleteOptions(in, out, s); err != nil {
381+
return err
382+
}
383+
384+
uid := types.UID("")
385+
if values, ok := (*in)["uid"]; ok && len(values) > 0 {
386+
uid = types.UID(values[0])
387+
}
388+
389+
resourceVersion := ""
390+
if values, ok := (*in)["resourceVersion"]; ok && len(values) > 0 {
391+
resourceVersion = values[0]
392+
}
393+
394+
if len(uid) > 0 || len(resourceVersion) > 0 {
395+
if out.Preconditions == nil {
396+
out.Preconditions = &Preconditions{}
397+
}
398+
if len(uid) > 0 {
399+
out.Preconditions.UID = &uid
400+
}
401+
if len(resourceVersion) > 0 {
402+
out.Preconditions.ResourceVersion = &resourceVersion
403+
}
404+
}
405+
return nil
406+
}

staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/conversion_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,13 @@ func TestConvertSliceStringToDeletionPropagation(t *testing.T) {
7575
}
7676

7777
for _, tc := range tcs {
78-
var dp v1.DeletionPropagation
79-
if err := v1.Convert_Slice_string_To_v1_DeletionPropagation(&tc.Input, &dp, nil); err != nil {
80-
t.Errorf("Convert_Slice_string_To_v1_DeletionPropagation(%#v): %v", tc.Input, err)
78+
var dpPtr *v1.DeletionPropagation
79+
if err := v1.Convert_Slice_string_To_Pointer_v1_DeletionPropagation(&tc.Input, &dpPtr, nil); err != nil {
80+
t.Errorf("Convert_Slice_string_To_Pointer_v1_DeletionPropagation(%#v): %v", tc.Input, err)
8181
continue
8282
}
83-
if !apiequality.Semantic.DeepEqual(dp, tc.Output) {
84-
t.Errorf("slice string to DeletionPropagation conversion failed: got %v; want %v", dp, tc.Output)
83+
if !apiequality.Semantic.DeepEqual(dpPtr, &tc.Output) {
84+
t.Errorf("slice string to DeletionPropagation conversion failed: got %v; want %v", *dpPtr, tc.Output)
8585
}
8686
}
8787
}

staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/generated.proto

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,7 @@ const (
455455
DryRunAll = "All"
456456
)
457457

458+
// +k8s:conversion-gen:explicit-from=net/url.Values
458459
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
459460

460461
// DeleteOptions may be provided when deleting an API object.
@@ -470,6 +471,7 @@ type DeleteOptions struct {
470471

471472
// Must be fulfilled before a deletion is carried out. If not possible, a 409 Conflict status will be
472473
// returned.
474+
// +k8s:conversion-gen=false
473475
// +optional
474476
Preconditions *Preconditions `json:"preconditions,omitempty" protobuf:"bytes,2,opt,name=preconditions"`
475477

staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/zz_generated.conversion.go

Lines changed: 48 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

staging/src/k8s.io/code-generator/cmd/conversion-gen/generators/conversion.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,6 +1068,11 @@ func (g *genConversion) generateFromUrlValues(inType, outType *types.Type, sw *g
10681068
}
10691069
sw.Do("func auto"+nameTmpl+"(in *$.inType|raw$, out *$.outType|raw$, s $.Scope|raw$) error {\n", args)
10701070
for _, outMember := range outType.Members {
1071+
if tagvals := extractTag(outMember.CommentLines); tagvals != nil && tagvals[0] == "false" {
1072+
// This field is excluded from conversion.
1073+
sw.Do("// INFO: in."+outMember.Name+" opted out of conversion generation\n", nil)
1074+
continue
1075+
}
10711076
jsonTag := reflect.StructTag(outMember.Tags).Get("json")
10721077
index := strings.Index(jsonTag, ",")
10731078
if index == -1 {

0 commit comments

Comments
 (0)