@@ -20,7 +20,6 @@ import (
2020 serviceusage "google.golang.org/api/serviceusage/v1beta1"
2121 "k8s.io/apimachinery/pkg/util/sets"
2222
23- configv1 "github.com/openshift/api/config/v1"
2423 gcpconsts "github.com/openshift/installer/pkg/constants/gcp"
2524 gcptypes "github.com/openshift/installer/pkg/types/gcp"
2625)
@@ -60,56 +59,76 @@ type API interface {
6059 GetNamespacedTagValue (ctx context.Context , tagNamespacedName string ) (* cloudresourcemanager.TagValue , error )
6160 GetKeyRing (ctx context.Context , kmsKeyRef * gcptypes.KMSKeyReference ) (* kmspb.KeyRing , error )
6261 UpdateDNSPrivateZoneLabels (ctx context.Context , baseDomain , project , zoneName string , labels map [string ]string ) error
62+ GetPrivateServiceConnectEndpoint (ctx context.Context , project string , endpoint * gcptypes.PSCEndpoint ) (* compute.ForwardingRule , error )
6363}
6464
6565// Client makes calls to the GCP API.
6666type Client struct {
67- ssn * Session
68- endpoints []configv1. GCPServiceEndpoint
67+ ssn * Session
68+ endpointName string
6969}
7070
7171// NewClient initializes a client with a session.
72- func NewClient (ctx context.Context , endpoints []configv1. GCPServiceEndpoint ) (* Client , error ) {
72+ func NewClient (ctx context.Context , endpoint * gcptypes. PSCEndpoint ) (* Client , error ) {
7373 ssn , err := GetSession (ctx )
7474 if err != nil {
7575 return nil , errors .Wrap (err , "failed to get session" )
7676 }
7777
78- modifiedEndpoints := FormatGCPEndpointList (endpoints , FormatGCPEndpointInput {SkipPath : false })
78+ endpointName := ""
79+ if gcptypes .ShouldUseEndpointForInstaller (endpoint ) {
80+ endpointName = endpoint .Name
81+ }
7982
8083 client := & Client {
81- ssn : ssn ,
82- endpoints : modifiedEndpoints ,
84+ ssn : ssn ,
85+ endpointName : endpointName ,
8386 }
8487 return client , nil
8588}
8689
8790func (c * Client ) getComputeService (ctx context.Context ) (* compute.Service , error ) {
88- svc , err := GetComputeService (ctx , c .endpoints )
91+ opts := []option.ClientOption {}
92+ if c .endpointName != "" {
93+ opts = append (opts , CreateEndpointOption (c .endpointName , ServiceNameGCPCompute ))
94+ }
95+ svc , err := GetComputeService (ctx , opts ... )
8996 if err != nil {
9097 return nil , fmt .Errorf ("client failed to create compute service: %w" , err )
9198 }
9299 return svc , nil
93100}
94101
95102func (c * Client ) getDNSService (ctx context.Context ) (* dns.Service , error ) {
96- svc , err := GetDNSService (ctx , c .endpoints )
103+ opts := []option.ClientOption {}
104+ if c .endpointName != "" {
105+ opts = append (opts , CreateEndpointOption (c .endpointName , ServiceNameGCPDNS ))
106+ }
107+ svc , err := GetDNSService (ctx , opts ... )
97108 if err != nil {
98109 return nil , fmt .Errorf ("client failed to create dns service: %w" , err )
99110 }
100111 return svc , nil
101112}
102113
103114func (c * Client ) getCloudResourceService (ctx context.Context ) (* cloudresourcemanager.Service , error ) {
104- svc , err := GetCloudResourceService (ctx , c .endpoints )
115+ opts := []option.ClientOption {}
116+ if c .endpointName != "" {
117+ opts = append (opts , CreateEndpointOption (c .endpointName , ServiceNameGCPCloudResource ))
118+ }
119+ svc , err := GetCloudResourceService (ctx , opts ... )
105120 if err != nil {
106121 return nil , fmt .Errorf ("client failed to create cloud resource service: %w" , err )
107122 }
108123 return svc , nil
109124}
110125
111126func (c * Client ) getServiceUsageService (ctx context.Context ) (* serviceusage.APIService , error ) {
112- svc , err := GetServiceUsageService (ctx , c .endpoints )
127+ opts := []option.ClientOption {}
128+ if c .endpointName != "" {
129+ opts = append (opts , CreateEndpointOption (c .endpointName , ServiceNameGCPServiceUsage ))
130+ }
131+ svc , err := GetServiceUsageService (ctx , opts ... )
113132 if err != nil {
114133 return nil , fmt .Errorf ("client failed to create service usage service: %w" , err )
115134 }
@@ -576,7 +595,11 @@ func (c *Client) GetEnabledServices(ctx context.Context, project string) ([]stri
576595
577596// GetServiceAccount retrieves a service account from a project if it exists.
578597func (c * Client ) GetServiceAccount (ctx context.Context , project , serviceAccount string ) (string , error ) {
579- svc , err := GetIAMService (ctx , c .endpoints )
598+ opts := []option.ClientOption {}
599+ if c .endpointName != "" {
600+ opts = append (opts , CreateEndpointOption (c .endpointName , ServiceNameGCPIAM ))
601+ }
602+ svc , err := GetIAMService (ctx , opts ... )
580603 if err != nil {
581604 return "" , errors .Wrapf (err , "failed create IAM service" )
582605 }
@@ -740,3 +763,40 @@ func (c *Client) GetKeyRing(ctx context.Context, kmsKeyRef *gcptypes.KMSKeyRefer
740763 }
741764 return nil , fmt .Errorf ("failed to find kms key ring with name %s" , keyRingName )
742765}
766+
767+ // GetPrivateServiceConnectEndpoint finds the GCP compute forwarding rule that is associated with the endpoint.
768+ func GetPrivateServiceConnectEndpoint (client * compute.Service , project string , endpoint * gcptypes.PSCEndpoint ) (* compute.ForwardingRule , error ) {
769+ if endpoint == nil {
770+ return nil , nil
771+ }
772+
773+ var forwardingRules * compute.ForwardingRuleList
774+ var forwardingRuleErr error
775+ if endpoint .Region != "" {
776+ forwardingRules , forwardingRuleErr = client .ForwardingRules .List (project , endpoint .Region ).Do ()
777+ } else {
778+ forwardingRules , forwardingRuleErr = client .GlobalForwardingRules .List (project ).Do ()
779+ }
780+ if forwardingRuleErr != nil {
781+ return nil , fmt .Errorf ("failed to list forwarding rules: %w" , forwardingRuleErr )
782+ }
783+
784+ if forwardingRules != nil {
785+ // Iterate through forwarding rules to find the PSC endpoint
786+ for _ , rule := range forwardingRules .Items {
787+ if rule .Name == endpoint .Name {
788+ return rule , nil
789+ }
790+ }
791+ }
792+ return nil , fmt .Errorf ("failed to find forwarding rule for private service connect endpoint %s" , endpoint .Name )
793+ }
794+
795+ // GetPrivateServiceConnectEndpoint will get the forwarding rule associated with a private service connect endpoint.
796+ func (c * Client ) GetPrivateServiceConnectEndpoint (ctx context.Context , project string , endpoint * gcptypes.PSCEndpoint ) (* compute.ForwardingRule , error ) {
797+ svc , err := c .getComputeService (ctx )
798+ if err != nil {
799+ return nil , fmt .Errorf ("failed to create Compute service: %w" , err )
800+ }
801+ return GetPrivateServiceConnectEndpoint (svc , project , endpoint )
802+ }
0 commit comments