@@ -31,6 +31,7 @@ import (
3131 "time"
3232
3333 jsonpatch "github.com/evanphx/json-patch"
34+
3435 apiextensionsinternal "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions"
3536 apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
3637 structuralschema "k8s.io/apiextensions-apiserver/pkg/apiserver/schema"
@@ -1949,3 +1950,87 @@ func BenchmarkRatcheting(b *testing.B) {
19491950 })
19501951 }
19511952}
1953+
1954+ func TestRatchetingDropFields (t * testing.T ) {
1955+ tearDown , apiExtensionClient , _ , err := fixtures .StartDefaultServerWithClients (t )
1956+ if err != nil {
1957+ t .Fatal (err )
1958+ }
1959+ defer tearDown ()
1960+
1961+ group := myCRDV1Beta1 .Group
1962+ version := myCRDV1Beta1 .Version
1963+ resource := myCRDV1Beta1 .Resource
1964+ kind := fakeRESTMapper [myCRDV1Beta1 ]
1965+
1966+ myCRD := & apiextensionsv1.CustomResourceDefinition {
1967+ ObjectMeta : metav1.ObjectMeta {Name : resource + "." + group },
1968+ Spec : apiextensionsv1.CustomResourceDefinitionSpec {
1969+ Group : group ,
1970+ Versions : []apiextensionsv1.CustomResourceDefinitionVersion {{
1971+ Name : version ,
1972+ Served : true ,
1973+ Storage : true ,
1974+ Schema : & apiextensionsv1.CustomResourceValidation {
1975+ OpenAPIV3Schema : & apiextensionsv1.JSONSchemaProps {
1976+ Type : "object" ,
1977+ Properties : map [string ]apiextensionsv1.JSONSchemaProps {
1978+ "spec" : {
1979+ Type : "object" ,
1980+ Properties : map [string ]apiextensionsv1.JSONSchemaProps {
1981+ "field" : {
1982+ Type : "string" ,
1983+ XValidations : []apiextensionsv1.ValidationRule {
1984+ {
1985+ Rule : "self == oldSelf" ,
1986+ OptionalOldSelf : ptr (true ),
1987+ },
1988+ },
1989+ },
1990+ },
1991+ },
1992+ },
1993+ },
1994+ },
1995+ }},
1996+ Names : apiextensionsv1.CustomResourceDefinitionNames {
1997+ Plural : resource ,
1998+ Kind : kind ,
1999+ ListKind : kind + "List" ,
2000+ },
2001+ Scope : apiextensionsv1 .NamespaceScoped ,
2002+ },
2003+ }
2004+
2005+ created , err := apiExtensionClient .ApiextensionsV1 ().CustomResourceDefinitions ().Create (context .TODO (), myCRD , metav1.CreateOptions {})
2006+ if err != nil {
2007+ t .Fatal (err )
2008+ }
2009+ if created .Spec .Versions [0 ].Schema .OpenAPIV3Schema .Properties ["spec" ].Properties ["field" ].XValidations [0 ].OptionalOldSelf != nil {
2010+ t .Errorf ("Expected OpeiontalOldSelf field to be dropped for create when feature gate is disabled" )
2011+ }
2012+
2013+ var updated * apiextensionsv1.CustomResourceDefinition
2014+ err = wait .PollUntilContextTimeout (context .TODO (), 100 * time .Millisecond , 5 * time .Second , true , func (ctx context.Context ) (bool , error ) {
2015+ existing , err := apiExtensionClient .ApiextensionsV1 ().CustomResourceDefinitions ().Get (context .TODO (), created .Name , metav1.GetOptions {})
2016+ if err != nil {
2017+ return false , err
2018+ }
2019+ existing .Spec .Versions [0 ].Schema .OpenAPIV3Schema .Properties ["spec" ].Properties ["field" ].XValidations [0 ].OptionalOldSelf = ptr (true )
2020+ updated , err = apiExtensionClient .ApiextensionsV1 ().CustomResourceDefinitions ().Update (context .TODO (), existing , metav1.UpdateOptions {})
2021+ if err != nil {
2022+ if apierrors .IsConflict (err ) {
2023+ return false , nil
2024+ }
2025+ return false , err
2026+ }
2027+ return true , nil
2028+ })
2029+ if err != nil {
2030+ t .Fatalf ("unexpected error waiting for CRD update: %v" , err )
2031+ }
2032+
2033+ if updated .Spec .Versions [0 ].Schema .OpenAPIV3Schema .Properties ["spec" ].Properties ["field" ].XValidations [0 ].OptionalOldSelf != nil {
2034+ t .Errorf ("Expected OpeiontalOldSelf field to be dropped for update when feature gate is disabled" )
2035+ }
2036+ }
0 commit comments