@@ -31,11 +31,12 @@ import (
31
31
pkgruntime "k8s.io/apimachinery/pkg/runtime"
32
32
"k8s.io/apimachinery/pkg/runtime/schema"
33
33
"k8s.io/apimachinery/pkg/types"
34
+ jsonutil "k8s.io/apimachinery/pkg/util/json"
34
35
"k8s.io/client-go/dynamic"
35
36
"k8s.io/client-go/kubernetes"
36
37
37
38
fedcorev1a1 "github.com/kubewharf/kubeadmiral/pkg/apis/core/v1alpha1"
38
- controllerutil "github.com/kubewharf/kubeadmiral/pkg/controllers/ util"
39
+ "github.com/kubewharf/kubeadmiral/pkg/util/naming "
39
40
"github.com/kubewharf/kubeadmiral/test/e2e/framework"
40
41
"github.com/kubewharf/kubeadmiral/test/e2e/framework/policies"
41
42
"github.com/kubewharf/kubeadmiral/test/e2e/framework/util"
@@ -71,14 +72,13 @@ type resourceClient[T k8sObject] interface {
71
72
}
72
73
73
74
type resourceStatusCollectionTestConfig struct {
74
- // GVR of the federatedstatus.
75
- gvr schema.GroupVersionResource
76
75
// Path to a field in the resource whose value should be collected by status collection.
77
76
path string
78
77
}
79
78
80
79
type resourcePropagationTestConfig [T k8sObject ] struct {
81
80
gvr schema.GroupVersionResource
81
+ gvk schema.GroupVersionKind
82
82
statusCollection * resourceStatusCollectionTestConfig
83
83
// Returns an object template with the given name.
84
84
objectFactory func (name string ) T
@@ -153,18 +153,17 @@ func resourcePropagationTest[T k8sObject](
153
153
})
154
154
155
155
ginkgo .By ("Updating the source object" , func () {
156
- patch := []map [string ]interface {}{
157
- {
158
- "op" : "add" ,
159
- // escape the / in annotation key
160
- "path" : "/metadata/annotations/" + strings .Replace (resourceUpdateTestAnnotationKey , "/" , "~1" , 1 ),
161
- "value" : resourceUpdateTestAnnotationValue ,
156
+ patch := map [string ]interface {}{
157
+ "metadata" : map [string ]interface {}{
158
+ "annotations" : map [string ]interface {}{
159
+ resourceUpdateTestAnnotationKey : resourceUpdateTestAnnotationValue ,
160
+ },
162
161
},
163
162
}
164
163
patchBytes , err := json .Marshal (patch )
165
164
gomega .Expect (err ).NotTo (gomega .HaveOccurred (), framework .MessageUnexpectedError )
166
165
167
- object , err = hostClient .Patch (ctx , object .GetName (), types .JSONPatchType , patchBytes , metav1.PatchOptions {})
166
+ object , err = hostClient .Patch (ctx , object .GetName (), types .StrategicMergePatchType , patchBytes , metav1.PatchOptions {})
168
167
gomega .Expect (err ).NotTo (gomega .HaveOccurred (), framework .MessageUnexpectedError )
169
168
})
170
169
@@ -245,29 +244,33 @@ func resourcePropagationTest[T k8sObject](
245
244
actualFieldByCluster [cluster .Name ] = actualField
246
245
}
247
246
248
- fedStatusUns , err := f .HostDynamicClient ().Resource (config .statusCollection .gvr ).Namespace (object .GetNamespace ()).Get (
249
- ctx , object .GetName (), metav1.GetOptions {})
247
+ ftc , exists := f .FTCManager ().GetResourceFTC (config .gvk )
248
+ g .Expect (exists ).To (gomega .BeTrue ())
249
+
250
+ collectedStatusName := naming .GenerateFederatedObjectName (object .GetName (), ftc .Name )
251
+ fedStatus , err := f .HostFedClient ().CoreV1alpha1 ().CollectedStatuses (object .GetNamespace ()).Get (
252
+ ctx , collectedStatusName , metav1.GetOptions {})
250
253
if err != nil && apierrors .IsNotFound (err ) {
251
254
// status might not have been created yet, use local g to fail only this attempt
252
255
g .Expect (err ).NotTo (gomega .HaveOccurred (), "Federated status object has not been created" )
253
256
}
254
257
gomega .Expect (err ).NotTo (gomega .HaveOccurred (), framework .MessageUnexpectedError )
255
258
256
- fedStatus := controllerutil.FederatedResource {}
257
- err = pkgruntime .DefaultUnstructuredConverter .FromUnstructured (fedStatusUns .Object , & fedStatus )
258
- gomega .Expect (err ).NotTo (gomega .HaveOccurred (), framework .MessageUnexpectedError )
259
-
260
- g .Expect (fedStatus .ClusterStatus ).
259
+ g .Expect (fedStatus .Clusters ).
261
260
To (gomega .HaveLen (len (actualFieldByCluster )), "Collected status has wrong number of clusters" )
262
- for _ , clusterStatus := range fedStatus .ClusterStatus {
263
- actualField , exists := actualFieldByCluster [clusterStatus .ClusterName ]
264
- g .Expect (exists ).To (gomega .BeTrue (), fmt .Sprintf ("collected from unexpected cluster %s" , clusterStatus .ClusterName ))
265
-
266
- collectedField , exists , err := unstructured .NestedFieldNoCopy (clusterStatus .CollectedFields , pathSegments ... )
261
+ for _ , clusterStatus := range fedStatus .Clusters {
262
+ actualField , exists := actualFieldByCluster [clusterStatus .Cluster ]
263
+ g .Expect (exists ).
264
+ To (gomega .BeTrue (), fmt .Sprintf ("collected from unexpected cluster %s" , clusterStatus .Cluster ))
265
+
266
+ collectedFields := & map [string ]interface {}{}
267
+ err := jsonutil .Unmarshal (clusterStatus .CollectedFields .Raw , collectedFields )
268
+ gomega .Expect (err ).ToNot (gomega .HaveOccurred ())
269
+ collectedField , exists , err := unstructured .NestedFieldNoCopy (* collectedFields , pathSegments ... )
267
270
gomega .Expect (err ).NotTo (gomega .HaveOccurred (), framework .MessageUnexpectedError )
268
271
g .Expect (exists ).To (
269
272
gomega .BeTrue (),
270
- fmt .Sprintf ("collected fields does not contain %q for cluster %s" , config .statusCollection .path , clusterStatus .ClusterName ),
273
+ fmt .Sprintf ("collected fields does not contain %q for cluster %s" , config .statusCollection .path , clusterStatus .Cluster ),
271
274
)
272
275
g .Expect (collectedField ).To (gomega .Equal (actualField ), "collected and actual fields differ" )
273
276
}
0 commit comments