@@ -23,6 +23,7 @@ import (
23
23
"net/url"
24
24
"path"
25
25
"reflect"
26
+ "sort"
26
27
"strings"
27
28
"testing"
28
29
"time"
@@ -49,6 +50,7 @@ import (
49
50
apisv1alpha2 "github.com/kcp-dev/kcp/sdk/apis/apis/v1alpha2"
50
51
"github.com/kcp-dev/kcp/sdk/apis/core"
51
52
corev1alpha1 "github.com/kcp-dev/kcp/sdk/apis/core/v1alpha1"
53
+ tenancyv1alpha1 "github.com/kcp-dev/kcp/sdk/apis/tenancy/v1alpha1"
52
54
"github.com/kcp-dev/kcp/sdk/apis/third_party/conditions/util/conditions"
53
55
kcpclientset "github.com/kcp-dev/kcp/sdk/client/clientset/versioned/cluster"
54
56
kcptesting "github.com/kcp-dev/kcp/sdk/testing"
@@ -163,21 +165,47 @@ func TestAPIBinding(t *testing.T) {
163
165
t .Parallel ()
164
166
165
167
server := kcptesting .SharedKcpServer (t )
168
+ cfg := server .BaseConfig (t )
166
169
167
170
ctx , cancel := context .WithCancel (context .Background ())
168
171
t .Cleanup (cancel )
169
172
173
+ t .Logf ("Check if we can access shards" )
174
+ var shards * corev1alpha1.ShardList
175
+ {
176
+ kcpClusterClient , err := kcpclientset .NewForConfig (cfg )
177
+ require .NoError (t , err , "failed to construct kcp cluster client for server" )
178
+
179
+ shards , err = kcpClusterClient .Cluster (core .RootCluster .Path ()).CoreV1alpha1 ().Shards ().List (ctx , metav1.ListOptions {})
180
+ require .NoError (t , err , "failed to list shards" )
181
+ }
182
+
183
+ t .Logf ("Shards: %v" , shards .Items )
184
+
170
185
orgPath , _ := framework .NewOrganizationFixture (t , server ) //nolint:staticcheck // TODO: switch to NewWorkspaceFixture.
171
- provider1Path , provider1 := kcptesting .NewWorkspaceFixture (t , server , orgPath , kcptesting .WithName ("service-provider-1" ))
186
+ var provider1Path , provider2Path logicalcluster.Path
187
+ var provider1 , provider2 * tenancyv1alpha1.Workspace
188
+ if len (shards .Items ) == 1 {
189
+ provider1Path , provider1 = kcptesting .NewWorkspaceFixture (t , server , orgPath , kcptesting .WithName ("service-provider-1" ), kcptesting .WithShard (shards .Items [0 ].Name ))
190
+ provider2Path , provider2 = kcptesting .NewWorkspaceFixture (t , server , orgPath , kcptesting .WithName ("service-provider-2" ), kcptesting .WithShard (shards .Items [0 ].Name ))
191
+ } else {
192
+ provider1Path , provider1 = kcptesting .NewWorkspaceFixture (t , server , orgPath , kcptesting .WithName ("service-provider-1" ), kcptesting .WithShard (shards .Items [0 ].Name ))
193
+ provider2Path , provider2 = kcptesting .NewWorkspaceFixture (t , server , orgPath , kcptesting .WithName ("service-provider-2" ), kcptesting .WithShard (shards .Items [1 ].Name ))
194
+ }
172
195
provider1ClusterName := logicalcluster .Name (provider1 .Spec .Cluster )
173
- provider2Path , provider2 := kcptesting .NewWorkspaceFixture (t , server , orgPath , kcptesting .WithName ("service-provider-2" ))
174
196
provider2ClusterName := logicalcluster .Name (provider2 .Spec .Cluster )
175
- consumer1Path , _ := kcptesting .NewWorkspaceFixture (t , server , orgPath , kcptesting .WithName ("consumer-1-bound-against-1" ))
176
- consumer2Path , _ := kcptesting .NewWorkspaceFixture (t , server , orgPath , kcptesting .WithName ("consumer-2-bound-against-1" ))
177
- consumer3Path , consumer3Workspace := kcptesting .NewWorkspaceFixture (t , server , orgPath , kcptesting .WithName ("consumer-3-bound-against-2" ))
178
- consumer3ClusterName := logicalcluster .Name (consumer3Workspace .Spec .Cluster )
179
197
180
- cfg := server .BaseConfig (t )
198
+ consumer1Path , _ := kcptesting .NewWorkspaceFixture (t , server , orgPath , kcptesting .WithName ("consumer-1-bound-against-1" ), kcptesting .WithShard (shards .Items [0 ].Name ))
199
+ var consumer2Path , consumer3Path logicalcluster.Path
200
+ var consumer3Workspace * tenancyv1alpha1.Workspace
201
+ if len (shards .Items ) == 1 {
202
+ consumer2Path , _ = kcptesting .NewWorkspaceFixture (t , server , orgPath , kcptesting .WithName ("consumer-2-bound-against-1" ), kcptesting .WithShard (shards .Items [0 ].Name ))
203
+ consumer3Path , consumer3Workspace = kcptesting .NewWorkspaceFixture (t , server , orgPath , kcptesting .WithName ("consumer-3-bound-against-2" ), kcptesting .WithShard (shards .Items [0 ].Name ))
204
+ } else {
205
+ consumer2Path , _ = kcptesting .NewWorkspaceFixture (t , server , orgPath , kcptesting .WithName ("consumer-2-bound-against-1" ), kcptesting .WithShard (shards .Items [1 ].Name ))
206
+ consumer3Path , consumer3Workspace = kcptesting .NewWorkspaceFixture (t , server , orgPath , kcptesting .WithName ("consumer-3-bound-against-2" ), kcptesting .WithShard (shards .Items [1 ].Name ))
207
+ }
208
+ consumer3ClusterName := logicalcluster .Name (consumer3Workspace .Spec .Cluster )
181
209
182
210
kcpClusterClient , err := kcpclientset .NewForConfig (cfg )
183
211
require .NoError (t , err , "failed to construct kcp cluster client for server" )
@@ -187,8 +215,6 @@ func TestAPIBinding(t *testing.T) {
187
215
188
216
shardVirtualWorkspaceURLs := sets .New [string ]()
189
217
t .Logf ("Getting a list of VirtualWorkspaceURLs assigned to Shards" )
190
- shards , err := kcpClusterClient .Cluster (core .RootCluster .Path ()).CoreV1alpha1 ().Shards ().List (ctx , metav1.ListOptions {})
191
- require .NoError (t , err )
192
218
// Filtering out shards that are not schedulable
193
219
var shardItems []corev1alpha1.Shard
194
220
for _ , s := range shards .Items {
@@ -365,25 +391,27 @@ func TestAPIBinding(t *testing.T) {
365
391
expectedURLs = append (expectedURLs , u .String ())
366
392
}
367
393
368
- t .Logf ("Make sure the APIExport gets status.virtualWorkspaceURLs set" )
394
+ t .Logf ("Make sure the APIExportEndpointSlice gets status.virtualWorkspaceURLs set" )
369
395
kcptestinghelpers .Eventually (t , func () (bool , string ) {
370
- e , err := kcpClusterClient .Cluster (serviceProviderClusterName .Path ()).ApisV1alpha2 ().APIExports ().Get (ctx , exportName , metav1.GetOptions {})
396
+ apiExportEndpointSlice , err := kcpClusterClient .Cluster (serviceProviderClusterName .Path ()).ApisV1alpha1 ().APIExportEndpointSlices ().Get (ctx , exportName , metav1.GetOptions {})
371
397
if err != nil {
372
- t .Logf ("Unexpected error getting APIExport %s|%s: %v" , serviceProviderClusterName .Path (), exportName , err )
398
+ t .Logf ("Unexpected error getting APIExportEndpointSlice %s|%s: %v" , serviceProviderClusterName .Path (), exportName , err )
373
399
}
374
400
375
401
var actualURLs []string
376
- //nolint:staticcheck // SA1019 VirtualWorkspaces is deprecated but not removed yet
377
- for _ , u := range e .Status .VirtualWorkspaces {
402
+ for _ , u := range apiExportEndpointSlice .Status .APIExportEndpoints {
378
403
actualURLs = append (actualURLs , u .URL )
379
404
}
380
-
405
+ sort .Strings (expectedURLs )
406
+ sort .Strings (actualURLs )
407
+ t .Logf ("Expected URLs: %v" , expectedURLs )
408
+ t .Logf ("Actual URLs: %v" , actualURLs )
381
409
if ! reflect .DeepEqual (expectedURLs , actualURLs ) {
382
410
return false , fmt .Sprintf ("Unexpected URLs. Diff: %s" , cmp .Diff (expectedURLs , actualURLs ))
383
411
}
384
412
385
413
return true , ""
386
- }, wait .ForeverTestTimeout , 100 * time .Millisecond , "APIExport %s|%s didn't get status.virtualWorkspaceURLs set correctly" ,
414
+ }, wait .ForeverTestTimeout , 100 * time .Millisecond , "APIExportEndpointSlices %s|%s didn't get status.virtualWorkspaceURLs set correctly" ,
387
415
serviceProviderClusterName , exportName )
388
416
}
389
417
@@ -438,15 +466,14 @@ func TestAPIBinding(t *testing.T) {
438
466
t .Logf ("=== Verify that %s|%s export virtual workspace shows cowboys" , provider2Path , exportName )
439
467
rawConfig , err := server .RawConfig ()
440
468
require .NoError (t , err )
441
- export2 , err := kcpClusterClient .Cluster (provider2Path ).ApisV1alpha2 ().APIExports ().Get (ctx , exportName , metav1.GetOptions {})
469
+ apiExportEndpointSlice2 , err := kcpClusterClient .Cluster (provider2Path ).ApisV1alpha1 ().APIExportEndpointSlices ().Get (ctx , exportName , metav1.GetOptions {})
442
470
require .NoError (t , err )
443
471
444
472
kcptestinghelpers .Eventually (t , func () (bool , string ) {
445
473
foundOnShards := 0
446
474
var listErrs []error
447
475
448
- //nolint:staticcheck // SA1019 VirtualWorkspaces is deprecated but not removed yet
449
- for _ , vw := range export2 .Status .VirtualWorkspaces {
476
+ for _ , vw := range apiExportEndpointSlice2 .Status .APIExportEndpoints {
450
477
vw2ClusterClient , err := kcpdynamic .NewForConfig (apiexportVWConfig (t , rawConfig , vw .URL ))
451
478
require .NoError (t , err )
452
479
@@ -476,8 +503,7 @@ func TestAPIBinding(t *testing.T) {
476
503
return false , "couldn't list via virtual workspaces because the user is not ready yet"
477
504
}
478
505
479
- //nolint:staticcheck // SA1019 VirtualWorkspaces is deprecated but not removed yet
480
- require .Equal (t , 1 , foundOnShards , "cowboys not found exactly on one shard, but on %d/%d" , foundOnShards , len (export2 .Status .VirtualWorkspaces ))
506
+ require .Equal (t , 1 , foundOnShards , "cowboys not found exactly on one shard, but on %d/%d" , foundOnShards , len (apiExportEndpointSlice2 .Status .APIExportEndpoints ))
481
507
482
508
return true , ""
483
509
}, wait .ForeverTestTimeout , 100 * time .Millisecond , "expected to have cowboys exactly on one shard" )
0 commit comments