@@ -33,6 +33,7 @@ import (
33
33
"github.com/google/go-cmp/cmp/cmpopts"
34
34
"github.com/googleapis/gax-go/v2/apierror"
35
35
"github.com/pkg/errors"
36
+ "google.golang.org/api/iterator"
36
37
"google.golang.org/grpc/codes"
37
38
infrav1exp "sigs.k8s.io/cluster-api-provider-gcp/exp/api/v1beta1"
38
39
"sigs.k8s.io/cluster-api-provider-gcp/util/reconciler"
@@ -171,7 +172,7 @@ func (s *Service) Reconcile(ctx context.Context) (ctrl.Result, error) {
171
172
172
173
identityServiceServer , err := s .getIdentityServiceServer (ctx , & log )
173
174
if err != nil {
174
- log .Error (err , "Failed to retrieve identity service server" )
175
+ log .Error (fmt . Errorf ( "Failed to retrieve identity service: [%w]" , err ) , "Failed to set identity service server, skipping until next reconciliation " )
175
176
} else if identityServiceServer != "" {
176
177
s .scope .GCPManagedControlPlane .Status .IdentityServiceServer = identityServiceServer
177
178
}
@@ -494,27 +495,29 @@ func compareMasterAuthorizedNetworksConfig(a, b *containerpb.MasterAuthorizedNet
494
495
}
495
496
496
497
func (s * Service ) getIdentityServiceServer (ctx context.Context , log * logr.Logger ) (string , error ) {
497
- if s . scope . GCPManagedControlPlane . Spec . EnableIdentityService == nil || ! * s .scope .GCPManagedControlPlane .Spec .EnableIdentityService {
498
+ if ! s .scope .GCPManagedControlPlane .Spec .EnableIdentityService {
498
499
// Identity service is not enabled, return empty string
499
500
return "" , nil
500
501
}
501
502
502
- nodePools , _ , err := s .scope .GetAllNodePools (ctx )
503
- if err != nil {
504
- return "" , err
505
- }
506
503
const (
507
504
indentityServiceFilter = "description:anthos-identity-service"
508
505
instanceFilterFormat = "instance:https://www.googleapis.com/compute/v1/projects/%s/zones/%s/instances/%s"
509
506
)
510
507
508
+ nodePools , _ , err := s .scope .GetAllNodePools (ctx )
509
+ if err != nil {
510
+ return "" , err
511
+ }
512
+
511
513
instanceFilters := []string {}
512
514
for _ , np := range nodePools {
513
515
for _ , providerID := range np .Spec .ProviderIDList {
514
516
parsedProviderID , err := providerid .NewFromResourceURL (providerID )
515
517
if err != nil {
516
518
return "" , err
517
519
}
520
+
518
521
instanceFilters = append (instanceFilters , fmt .Sprintf (instanceFilterFormat , parsedProviderID .Project , parsedProviderID .Location , parsedProviderID .Name ))
519
522
}
520
523
}
@@ -523,31 +526,55 @@ func (s *Service) getIdentityServiceServer(ctx context.Context, log *logr.Logger
523
526
return "" , errors .New ("no instances found" )
524
527
}
525
528
526
- targetPoolName := ""
527
- req := & computepb.ListTargetPoolsRequest {Filter : indentityServiceFilter + " AND (" + strings .Join (instanceFilters , " OR " ) + ")" }
528
- for resp , err := range s .scope .TargetPoolsClient ().List (ctx , req ).All () {
529
- if err != nil {
529
+ var (
530
+ targetPoolName string
531
+ listTargetPoolsRequest = & computepb.ListTargetPoolsRequest {Filter : indentityServiceFilter + " AND (" + strings .Join (instanceFilters , " OR " ) + ")" }
532
+ targetPoolsIT = s .scope .TargetPoolsClient ().List (ctx , listTargetPoolsRequest )
533
+ )
534
+
535
+ for {
536
+ resp , err := targetPoolsIT .Next ()
537
+ if err == iterator .Done {
538
+ break
539
+ } else if err != nil {
530
540
return "" , err
531
541
} else if targetPoolName != "" {
532
542
return "" , errors .New ("multiple target pools found" )
543
+ } else if resp .Name != nil {
544
+ return "" , errors .New ("no target pool name found" )
533
545
}
534
- targetPoolName = resp .Name
546
+
547
+ targetPoolName = * resp .Name
535
548
}
536
- identityServer := ""
537
- nameFilter := "name:" + targetPoolName
538
- req := & computepb.ListForwardingRulesRequest {Filter : instanceFilter + " AND " + nameFilter }
539
- for resp , err := range s .scope .ForwardingRulesClient ().List (ctx , req ).All () {
540
- if err != nil {
541
- return err
549
+
550
+ if targetPoolName == "" {
551
+ return "" , errors .New ("no target pools found" )
552
+ }
553
+
554
+ var (
555
+ identityServer string
556
+ nameFilter = "name:" + targetPoolName
557
+ listForwardingRulesRequest = & computepb.ListForwardingRulesRequest {Filter : indentityServiceFilter + " AND " + nameFilter }
558
+ forwardingRulesIT = s .scope .ForwardingRulesClient ().List (ctx , listForwardingRulesRequest )
559
+ )
560
+
561
+ for {
562
+ resp , err := forwardingRulesIT .Next ()
563
+ if err == iterator .Done {
564
+ break
565
+ } else if err != nil {
566
+ return "" , err
542
567
} else if identityServer != "" {
543
568
return "" , errors .New ("multiple forwarding rules found" )
544
569
} else if len (resp .Ports ) != 1 {
545
570
return "" , fmt .Errorf ("unexpected ports count in forwarding rule: %d" , len (resp .Ports ))
546
571
} else if resp .Ports [0 ] != "443" {
547
572
return "" , fmt .Errorf ("unexpected port in forwarding rule: %d" , resp .Ports [0 ])
573
+ } else if resp .IPAddress == nil {
574
+ return "" , errors .New ("no IP address found" )
548
575
}
549
576
550
- return "https://" + resp .IPAddress + ":" + resp .Ports [0 ], nil
577
+ return "https://" + * resp .IPAddress + ":" + resp .Ports [0 ], nil
551
578
}
552
579
553
580
if identityServer == "" {
0 commit comments