Skip to content

Commit 056a425

Browse files
Merge pull request #133180 from ylink-lfs/chore/ptr_cast_replace
chore: replace ptr caster with unified ptr.To Kubernetes-commit: 9409af4b80b9dee917e6d043194afc012013f097
2 parents 63d27f2 + f5ec1b6 commit 056a425

File tree

4 files changed

+18
-22
lines changed

4 files changed

+18
-22
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,5 @@ require (
126126
k8s.io/kms v0.0.0-20250716213631-bbefe5cb7a2e // indirect
127127
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.31.2 // indirect
128128
)
129+
130+
replace k8s.io/api => k8s.io/api v0.0.0-20250725024535-9bc89ec036ec

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,8 @@ gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYs
291291
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
292292
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
293293
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
294-
k8s.io/api v0.0.0-20250725024535-b95b43d5b95d h1:OdC1L69BYvh/4HX6Wgg7DeL0A++gD3gadKfaWT4IdbA=
295-
k8s.io/api v0.0.0-20250725024535-b95b43d5b95d/go.mod h1:wKZv1VB6nzJ6L449TteVelrBfRsawhrthiOsEylKo8U=
294+
k8s.io/api v0.0.0-20250725024535-9bc89ec036ec h1:Hr5ELh7ZNxCxze+o+zSYW8H7+Jhc/zsqpNP+g8vYzAE=
295+
k8s.io/api v0.0.0-20250725024535-9bc89ec036ec/go.mod h1:wKZv1VB6nzJ6L449TteVelrBfRsawhrthiOsEylKo8U=
296296
k8s.io/apimachinery v0.0.0-20250725024258-04507a37f6a4 h1:N25HX4lRPTvLHSUPoCMFP+B/oEcOmPESB+BRkYMD8Io=
297297
k8s.io/apimachinery v0.0.0-20250725024258-04507a37f6a4/go.mod h1:/GwIlEcWuTX9zKIg2mbw0LRFIsXwrfoVxn+ef0X13lw=
298298
k8s.io/apiserver v0.0.0-20250725030654-b0d4f2b90481 h1:WiDL92SUsqJ27AP3O/OQjWXFiSMWiqeOAGoSA30pLmU=

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)