Skip to content

Commit f5ec1b6

Browse files
ylink-lfsk8s-publishing-bot
authored andcommitted
chore: replace ptr caster with unified ptr.To
Kubernetes-commit: 32053b1d2f3e3be9cdd175ac963cfecc63fe3250
1 parent 63d27f2 commit f5ec1b6

File tree

2 files changed

+14
-20
lines changed

2 files changed

+14
-20
lines changed

pkg/apiserver/validation/ratcheting_test.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,35 +24,36 @@ import (
2424
"k8s.io/apiextensions-apiserver/pkg/apiserver/validation"
2525
"k8s.io/kube-openapi/pkg/validation/spec"
2626
"k8s.io/kube-openapi/pkg/validation/strfmt"
27+
"k8s.io/utils/ptr"
2728
)
2829

2930
var zeroIntSchema *spec.Schema = &spec.Schema{
3031
SchemaProps: spec.SchemaProps{
3132
Type: spec.StringOrArray{"number"},
32-
Minimum: ptr(float64(0)),
33-
Maximum: ptr(float64(0)),
33+
Minimum: ptr.To[float64](0),
34+
Maximum: ptr.To[float64](0),
3435
},
3536
}
3637

3738
var smallIntSchema *spec.Schema = &spec.Schema{
3839
SchemaProps: spec.SchemaProps{
3940
Type: spec.StringOrArray{"number"},
40-
Maximum: ptr(float64(50)),
41+
Maximum: ptr.To[float64](50),
4142
},
4243
}
4344

4445
var mediumIntSchema *spec.Schema = &spec.Schema{
4546
SchemaProps: spec.SchemaProps{
4647
Type: spec.StringOrArray{"number"},
47-
Minimum: ptr(float64(50)),
48-
Maximum: ptr(float64(10000)),
48+
Minimum: ptr.To[float64](50),
49+
Maximum: ptr.To[float64](10000),
4950
},
5051
}
5152

5253
var largeIntSchema *spec.Schema = &spec.Schema{
5354
SchemaProps: spec.SchemaProps{
5455
Type: spec.StringOrArray{"number"},
55-
Minimum: ptr(float64(10000)),
56+
Minimum: ptr.To[float64](10000),
5657
},
5758
}
5859

@@ -130,7 +131,3 @@ func TestObjectObjectFieldsRatcheting(t *testing.T) {
130131
"small": 501,
131132
}}, validation.WithRatcheting(nil)).IsValid())
132133
}
133-
134-
func ptr[T any](v T) *T {
135-
return &v
136-
}

test/integration/ratcheting_test.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ import (
5555
featuregatetesting "k8s.io/component-base/featuregate/testing"
5656
"k8s.io/kube-openapi/pkg/validation/spec"
5757
"k8s.io/kube-openapi/pkg/validation/strfmt"
58+
"k8s.io/utils/ptr"
5859
)
5960

6061
var stringSchema *apiextensionsv1.JSONSchemaProps = &apiextensionsv1.JSONSchemaProps{
@@ -1115,7 +1116,7 @@ func TestRatchetingFunctionality(t *testing.T) {
11151116
Properties: map[string]apiextensionsv1.JSONSchemaProps{
11161117
"field": {
11171118
Type: "array",
1118-
XListType: ptr("map"),
1119+
XListType: ptr.To("map"),
11191120
XListMapKeys: []string{"name", "port"},
11201121
Items: &apiextensionsv1.JSONSchemaPropsOrArray{
11211122
Schema: &apiextensionsv1.JSONSchemaProps{
@@ -1395,7 +1396,7 @@ func TestRatchetingFunctionality(t *testing.T) {
13951396
{
13961397
Rule: "!oldSelf.hasValue()",
13971398
Message: "oldSelf must be null",
1398-
OptionalOldSelf: ptr(true),
1399+
OptionalOldSelf: ptr.To(true),
13991400
},
14001401
},
14011402
},
@@ -1445,7 +1446,7 @@ func TestRatchetingFunctionality(t *testing.T) {
14451446
Operations: []ratchetingTestOperation{
14461447
updateMyCRDV1Beta1Schema{&apiextensionsv1.JSONSchemaProps{
14471448
Type: "object",
1448-
XPreserveUnknownFields: ptr(true),
1449+
XPreserveUnknownFields: ptr.To(true),
14491450
}},
14501451
applyPatchOperation{
14511452
"create instance with strings that do not start with k8s",
@@ -1457,7 +1458,7 @@ func TestRatchetingFunctionality(t *testing.T) {
14571458
},
14581459
updateMyCRDV1Beta1Schema{&apiextensionsv1.JSONSchemaProps{
14591460
Type: "object",
1460-
XPreserveUnknownFields: ptr(true),
1461+
XPreserveUnknownFields: ptr.To(true),
14611462
Properties: map[string]apiextensionsv1.JSONSchemaProps{
14621463
"myStringField": {
14631464
Type: "string",
@@ -1759,10 +1760,6 @@ func TestRatchetingFunctionality(t *testing.T) {
17591760
runTests(t, cases)
17601761
}
17611762

1762-
func ptr[T any](v T) *T {
1763-
return &v
1764-
}
1765-
17661763
type validator func(new, old *unstructured.Unstructured)
17671764

17681765
func newValidator(customResourceValidation *apiextensionsinternal.JSONSchemaProps, kind schema.GroupVersionKind, namespaceScoped bool) (validator, error) {
@@ -2031,7 +2028,7 @@ func TestRatchetingDropFields(t *testing.T) {
20312028
{
20322029
// Results in error if field wasn't dropped
20332030
Rule: "self == oldSelf",
2034-
OptionalOldSelf: ptr(true),
2031+
OptionalOldSelf: ptr.To(true),
20352032
},
20362033
},
20372034
},
@@ -2064,7 +2061,7 @@ func TestRatchetingDropFields(t *testing.T) {
20642061
if err != nil {
20652062
return false, err
20662063
}
2067-
existing.Spec.Versions[0].Schema.OpenAPIV3Schema.Properties["spec"].Properties["field"].XValidations[0].OptionalOldSelf = ptr(true)
2064+
existing.Spec.Versions[0].Schema.OpenAPIV3Schema.Properties["spec"].Properties["field"].XValidations[0].OptionalOldSelf = ptr.To(true)
20682065
updated, err = apiExtensionClient.ApiextensionsV1().CustomResourceDefinitions().Update(context.TODO(), existing, metav1.UpdateOptions{})
20692066
if err != nil {
20702067
if apierrors.IsConflict(err) {

0 commit comments

Comments
 (0)