Last active
January 8, 2025 12:11
-
-
Save sttts/7868e32dd70efa13a4972136147594b8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/staging/src/k8s.io/apiextensions-apiserver/test/integration/ratcheting_test.go b/staging/src/k8s.io/apiextensions-apiserver/test/integration/ratcheting_test.go | |
index b00b97b29e3..ca61e7b7ab1 100644 | |
--- a/staging/src/k8s.io/apiextensions-apiserver/test/integration/ratcheting_test.go | |
+++ b/staging/src/k8s.io/apiextensions-apiserver/test/integration/ratcheting_test.go | |
@@ -45,7 +45,6 @@ import ( | |
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | |
"k8s.io/apimachinery/pkg/runtime" | |
"k8s.io/apimachinery/pkg/runtime/schema" | |
- utilerrors "k8s.io/apimachinery/pkg/util/errors" | |
"k8s.io/apimachinery/pkg/util/uuid" | |
"k8s.io/apimachinery/pkg/util/wait" | |
utilyaml "k8s.io/apimachinery/pkg/util/yaml" | |
@@ -82,7 +81,7 @@ type ratchetingTestContext struct { | |
*testing.T | |
DynamicClient dynamic.Interface | |
APIExtensionsClient clientset.Interface | |
- SkipStatus bool | |
+ StatusSubresource bool | |
} | |
type ratchetingTestOperation interface { | |
@@ -97,12 +96,6 @@ type expectError struct { | |
func (e expectError) Do(ctx *ratchetingTestContext) error { | |
err := e.op.Do(ctx) | |
if err != nil { | |
- // If there is an error, it should happen when updating the CR | |
- // and also when updating the CR status subresource. | |
- if agg, ok := err.(utilerrors.Aggregate); ok && len(agg.Errors()) != 2 { | |
- return fmt.Errorf("expected 2 errors, got %v", len(agg.Errors())) | |
- } | |
- | |
return nil | |
} | |
return errors.New("expected error") | |
@@ -182,67 +175,22 @@ func (a applyPatchOperation) Do(ctx *ratchetingTestContext) error { | |
return fmt.Errorf("invalid patch type: %T", a.patch) | |
} | |
+ if ctx.StatusSubresource { | |
+ patch.Object = map[string]interface{}{"status": patch.Object} | |
+ } | |
+ | |
patch.SetKind(kind) | |
patch.SetAPIVersion(a.gvr.GroupVersion().String()) | |
patch.SetName(a.name) | |
patch.SetNamespace("default") | |
- _, err := ctx.DynamicClient. | |
- Resource(a.gvr). | |
- Namespace(patch.GetNamespace()). | |
- Apply( | |
- context.TODO(), | |
- patch.GetName(), | |
- patch, | |
- metav1.ApplyOptions{ | |
- FieldManager: "manager", | |
- }) | |
- | |
- if ctx.SkipStatus { | |
+ c := ctx.DynamicClient.Resource(a.gvr).Namespace(patch.GetNamespace()) | |
+ if ctx.StatusSubresource { | |
+ _, err := c.ApplyStatus(context.TODO(), patch.GetName(), patch, metav1.ApplyOptions{FieldManager: "manager"}) | |
return err | |
} | |
- | |
- errs := []error{} | |
- | |
- if err != nil { | |
- errs = append(errs, err) | |
- } | |
- | |
- statusPatch := &unstructured.Unstructured{ | |
- Object: map[string]interface{}{ | |
- "status": patch.Object, | |
- }, | |
- } | |
- | |
- statusPatch.SetKind(kind) | |
- statusPatch.SetAPIVersion(a.gvr.GroupVersion().String()) | |
- statusPatch.SetName(a.name) | |
- statusPatch.SetNamespace("default") | |
- | |
- delete(patch.Object, "metadata") | |
- delete(patch.Object, "apiVersion") | |
- delete(patch.Object, "kind") | |
- | |
- _, err = ctx.DynamicClient. | |
- Resource(a.gvr). | |
- Namespace(statusPatch.GetNamespace()). | |
- ApplyStatus( | |
- context.TODO(), | |
- statusPatch.GetName(), | |
- statusPatch, | |
- metav1.ApplyOptions{ | |
- FieldManager: "manager", | |
- }) | |
- | |
- if err != nil { | |
- errs = append(errs, err) | |
- } | |
- | |
- if len(errs) > 0 { | |
- return utilerrors.NewAggregate(errs) | |
- } | |
- | |
- return nil | |
+ _, err := c.Apply(context.TODO(), patch.GetName(), patch, metav1.ApplyOptions{FieldManager: "manager"}) | |
+ return err | |
} | |
func (a applyPatchOperation) Description() string { | |
@@ -279,10 +227,13 @@ func (u updateMyCRDV1Beta1Schema) Do(ctx *ratchetingTestContext) error { | |
}}, | |
} | |
- if !ctx.SkipStatus { | |
- // Duplicate the schema as a status schema | |
- statusSchema := sch.DeepCopy() | |
- sch.Properties["status"] = *statusSchema | |
+ if ctx.StatusSubresource { | |
+ sch = &apiextensionsv1.JSONSchemaProps{ | |
+ Type: "object", | |
+ Properties: map[string]apiextensionsv1.JSONSchemaProps{ | |
+ "status": *sch, | |
+ }, | |
+ } | |
} | |
for _, v := range myCRD.Spec.Versions { | |
@@ -344,18 +295,15 @@ type patchMyCRDV1Beta1Schema struct { | |
} | |
func (p patchMyCRDV1Beta1Schema) Do(ctx *ratchetingTestContext) error { | |
- var err error | |
- patchJSON, err := json.Marshal(p.patch) | |
- if err != nil { | |
- return err | |
+ patch := p.patch | |
+ if ctx.StatusSubresource { | |
+ patch = map[string]interface{}{ | |
+ "status": p, | |
+ } | |
} | |
- statusPatch := map[string]interface{}{ | |
- "properties": map[string]interface{}{ | |
- "status": p.patch, | |
- }, | |
- } | |
- statusPatchJSON, err := json.Marshal(statusPatch) | |
+ var err error | |
+ patchJSON, err := json.Marshal(patch) | |
if err != nil { | |
return err | |
} | |
@@ -380,13 +328,8 @@ func (p patchMyCRDV1Beta1Schema) Do(ctx *ratchetingTestContext) error { | |
return err | |
} | |
- mergedStatus, err := jsonpatch.MergePatch(merged, statusPatchJSON) | |
- if err != nil { | |
- return err | |
- } | |
- | |
var parsed apiextensionsv1.JSONSchemaProps | |
- if err := json.Unmarshal(mergedStatus, &parsed); err != nil { | |
+ if err := json.Unmarshal(merged, &parsed); err != nil { | |
return err | |
} | |
@@ -478,14 +421,7 @@ func runTests(t *testing.T, cases []ratchetingTestCase) { | |
continue | |
} | |
- t.Run(c.Name, func(t *testing.T) { | |
- ctx := &ratchetingTestContext{ | |
- T: t, | |
- DynamicClient: dynamicClient, | |
- APIExtensionsClient: apiExtensionClient, | |
- SkipStatus: c.SkipStatus, | |
- } | |
- | |
+ run := func(t *testing.T, ctx *ratchetingTestContext) { | |
for i, op := range c.Operations { | |
t.Logf("Performing Operation: %v", op.Description()) | |
if err := op.Do(ctx); err != nil { | |
@@ -498,7 +434,26 @@ func runTests(t *testing.T, cases []ratchetingTestCase) { | |
if err != nil { | |
t.Fatal(err) | |
} | |
+ } | |
+ | |
+ t.Run(c.Name, func(t *testing.T) { | |
+ run(t, &ratchetingTestContext{ | |
+ T: t, | |
+ DynamicClient: dynamicClient, | |
+ APIExtensionsClient: apiExtensionClient, | |
+ }) | |
}) | |
+ | |
+ if !c.SkipStatus { | |
+ t.Run(c.Name + " (status)", func(t *testing.T) { | |
+ run(t, &ratchetingTestContext{ | |
+ T: t, | |
+ DynamicClient: dynamicClient, | |
+ APIExtensionsClient: apiExtensionClient, | |
+ StatusSubresource: true, | |
+ }) | |
+ }) | |
+ } | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment