@@ -40,6 +40,7 @@ import (
40
40
"github.com/unikorn-cloud/kubernetes/pkg/provisioners/helmapplications/vcluster"
41
41
"github.com/unikorn-cloud/kubernetes/pkg/server/handler/clustermanager"
42
42
"github.com/unikorn-cloud/kubernetes/pkg/server/handler/common"
43
+ "github.com/unikorn-cloud/kubernetes/pkg/server/handler/identity"
43
44
"github.com/unikorn-cloud/kubernetes/pkg/server/handler/region"
44
45
regionapi "github.com/unikorn-cloud/region/pkg/openapi"
45
46
@@ -92,14 +93,14 @@ type Client struct {
92
93
options * Options
93
94
94
95
// identity is a client to access the identity service.
95
- identity identityapi. ClientWithResponsesInterface
96
+ identity * identity. Client
96
97
97
98
// region is a client to access regions.
98
- region regionapi. ClientWithResponsesInterface
99
+ region * region. Client
99
100
}
100
101
101
102
// NewClient returns a new client with required parameters.
102
- func NewClient (client client.Client , namespace string , options * Options , identity identityapi. ClientWithResponsesInterface , region regionapi. ClientWithResponsesInterface ) * Client {
103
+ func NewClient (client client.Client , namespace string , options * Options , identity * identity. Client , region * region. Client ) * Client {
103
104
return & Client {
104
105
client : client ,
105
106
namespace : namespace ,
@@ -200,7 +201,7 @@ func (c *Client) GetKubeconfig(ctx context.Context, organizationID, projectID, c
200
201
}
201
202
202
203
func (c * Client ) generateAllocations (ctx context.Context , organizationID string , resource * unikornv1.KubernetesCluster ) (* identityapi.AllocationWrite , error ) {
203
- flavors , err := region .Flavors (ctx , c . region , organizationID , resource .Spec .RegionID )
204
+ flavors , err := c . region .Flavors (ctx , organizationID , resource .Spec .RegionID )
204
205
if err != nil {
205
206
return nil , err
206
207
}
@@ -280,7 +281,12 @@ func (c *Client) createAllocation(ctx context.Context, organizationID, projectID
280
281
return nil , err
281
282
}
282
283
283
- resp , err := c .identity .PostApiV1OrganizationsOrganizationIDProjectsProjectIDAllocationsWithResponse (ctx , organizationID , projectID , * allocations )
284
+ client , err := c .identity .Client (ctx )
285
+ if err != nil {
286
+ return nil , err
287
+ }
288
+
289
+ resp , err := client .PostApiV1OrganizationsOrganizationIDProjectsProjectIDAllocationsWithResponse (ctx , organizationID , projectID , * allocations )
284
290
if err != nil {
285
291
return nil , err
286
292
}
@@ -298,7 +304,12 @@ func (c *Client) updateAllocation(ctx context.Context, organizationID, projectID
298
304
return err
299
305
}
300
306
301
- resp , err := c .identity .PutApiV1OrganizationsOrganizationIDProjectsProjectIDAllocationsAllocationIDWithResponse (ctx , organizationID , projectID , resource .Annotations [constants .AllocationAnnotation ], * allocations )
307
+ client , err := c .identity .Client (ctx )
308
+ if err != nil {
309
+ return err
310
+ }
311
+
312
+ resp , err := client .PutApiV1OrganizationsOrganizationIDProjectsProjectIDAllocationsAllocationIDWithResponse (ctx , organizationID , projectID , resource .Annotations [constants .AllocationAnnotation ], * allocations )
302
313
if err != nil {
303
314
return err
304
315
}
@@ -311,7 +322,12 @@ func (c *Client) updateAllocation(ctx context.Context, organizationID, projectID
311
322
}
312
323
313
324
func (c * Client ) deleteAllocation (ctx context.Context , organizationID , projectID , allocationID string ) error {
314
- resp , err := c .identity .DeleteApiV1OrganizationsOrganizationIDProjectsProjectIDAllocationsAllocationIDWithResponse (ctx , organizationID , projectID , allocationID )
325
+ client , err := c .identity .Client (ctx )
326
+ if err != nil {
327
+ return err
328
+ }
329
+
330
+ resp , err := client .DeleteApiV1OrganizationsOrganizationIDProjectsProjectIDAllocationsAllocationIDWithResponse (ctx , organizationID , projectID , allocationID )
315
331
if err != nil {
316
332
return err
317
333
}
@@ -342,7 +358,12 @@ func (c *Client) createIdentity(ctx context.Context, organizationID, projectID,
342
358
},
343
359
}
344
360
345
- resp , err := c .region .PostApiV1OrganizationsOrganizationIDProjectsProjectIDIdentitiesWithResponse (ctx , organizationID , projectID , request )
361
+ client , err := c .region .Client (ctx )
362
+ if err != nil {
363
+ return nil , errors .OAuth2ServerError ("unable to create region client" ).WithError (err )
364
+ }
365
+
366
+ resp , err := client .PostApiV1OrganizationsOrganizationIDProjectsProjectIDIdentitiesWithResponse (ctx , organizationID , projectID , request )
346
367
if err != nil {
347
368
return nil , errors .OAuth2ServerError ("unable to create identity" ).WithError (err )
348
369
}
@@ -380,7 +401,12 @@ func (c *Client) createPhysicalNetworkOpenstack(ctx context.Context, organizatio
380
401
},
381
402
}
382
403
383
- resp , err := c .region .PostApiV1OrganizationsOrganizationIDProjectsProjectIDIdentitiesIdentityIDNetworksWithResponse (ctx , organizationID , projectID , identity .Metadata .Id , request )
404
+ client , err := c .region .Client (ctx )
405
+ if err != nil {
406
+ return nil , errors .OAuth2ServerError ("unable to create region client" ).WithError (err )
407
+ }
408
+
409
+ resp , err := client .PostApiV1OrganizationsOrganizationIDProjectsProjectIDIdentitiesIdentityIDNetworksWithResponse (ctx , organizationID , projectID , identity .Metadata .Id , request )
384
410
if err != nil {
385
411
return nil , errors .OAuth2ServerError ("unable to physical network" ).WithError (err )
386
412
}
@@ -392,30 +418,6 @@ func (c *Client) createPhysicalNetworkOpenstack(ctx context.Context, organizatio
392
418
return resp .JSON201 , nil
393
419
}
394
420
395
- func (c * Client ) getRegion (ctx context.Context , organizationID , regionID string ) (* regionapi.RegionRead , error ) {
396
- // TODO: Need a straight get interface rather than a list.
397
- resp , err := c .region .GetApiV1OrganizationsOrganizationIDRegionsWithResponse (ctx , organizationID )
398
- if err != nil {
399
- return nil , errors .OAuth2ServerError ("unable to get region" ).WithError (err )
400
- }
401
-
402
- if resp .StatusCode () != http .StatusOK {
403
- return nil , errors .OAuth2ServerError ("unable to get region" )
404
- }
405
-
406
- results := * resp .JSON200
407
-
408
- index := slices .IndexFunc (results , func (region regionapi.RegionRead ) bool {
409
- return region .Metadata .Id == regionID
410
- })
411
-
412
- if index < 0 {
413
- return nil , errors .OAuth2ServerError ("unable to get region" )
414
- }
415
-
416
- return & results [index ], nil
417
- }
418
-
419
421
func (c * Client ) applyCloudSpecificConfiguration (ctx context.Context , organizationID , projectID , regionID string , allocation * identityapi.AllocationRead , identity * regionapi.IdentityRead , cluster * unikornv1.KubernetesCluster ) error {
420
422
// Save the identity ID for later cleanup.
421
423
if cluster .Annotations == nil {
@@ -426,7 +428,7 @@ func (c *Client) applyCloudSpecificConfiguration(ctx context.Context, organizati
426
428
cluster .Annotations [constants .IdentityAnnotation ] = identity .Metadata .Id
427
429
428
430
// Apply any region specific configuration based on feature flags.
429
- region , err := c .getRegion (ctx , organizationID , regionID )
431
+ region , err := c .region . Get (ctx , organizationID , regionID )
430
432
if err != nil {
431
433
return err
432
434
}
@@ -576,10 +578,6 @@ func (c *Client) Update(ctx context.Context, organizationID, projectID, clusterI
576
578
return errors .OAuth2ServerError ("failed to merge annotations" ).WithError (err )
577
579
}
578
580
579
- if err := c .updateAllocation (ctx , organizationID , projectID , required ); err != nil {
580
- return errors .OAuth2ServerError ("failed to update quota allocation" ).WithError (err )
581
- }
582
-
583
581
// Preserve networking options as if they change it'll be fairly catastrophic.
584
582
required .Spec .Network = current .Spec .Network
585
583
@@ -590,6 +588,10 @@ func (c *Client) Update(ctx context.Context, organizationID, projectID, clusterI
590
588
updated .Annotations = required .Annotations
591
589
updated .Spec = required .Spec
592
590
591
+ if err := c .updateAllocation (ctx , organizationID , projectID , updated ); err != nil {
592
+ return errors .OAuth2ServerError ("failed to update quota allocation" ).WithError (err )
593
+ }
594
+
593
595
if err := c .client .Patch (ctx , updated , client .MergeFrom (current )); err != nil {
594
596
return errors .OAuth2ServerError ("failed to patch cluster" ).WithError (err )
595
597
}
0 commit comments