Skip to content

Commit dcb9c0b

Browse files
committed
** Update the clients to use the endpoints for each service if/when they are supplied
via the Cluster Spec.
1 parent 917ba48 commit dcb9c0b

File tree

5 files changed

+36
-14
lines changed

5 files changed

+36
-14
lines changed

cloud/scope/clients.go

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,16 @@ func defaultClientOptions(ctx context.Context, credentialsRef *infrav1.ObjectRef
8989
return opts, nil
9090
}
9191

92-
func newComputeService(ctx context.Context, credentialsRef *infrav1.ObjectReference, crClient client.Client) (*compute.Service, error) {
92+
func newComputeService(ctx context.Context, credentialsRef *infrav1.ObjectReference, crClient client.Client, endpoints *infrav1.ServiceEndpoints) (*compute.Service, error) {
9393
opts, err := defaultClientOptions(ctx, credentialsRef, crClient)
9494
if err != nil {
9595
return nil, fmt.Errorf("getting default gcp client options: %w", err)
9696
}
9797

98+
if endpoints != nil && endpoints.ComputeServiceEndpoint != "" {
99+
opts = append(opts, option.WithEndpoint(endpoints.ComputeServiceEndpoint))
100+
}
101+
98102
computeSvc, err := compute.NewService(ctx, opts...)
99103
if err != nil {
100104
return nil, fmt.Errorf("creating new compute service instance: %w", err)
@@ -103,12 +107,16 @@ func newComputeService(ctx context.Context, credentialsRef *infrav1.ObjectRefere
103107
return computeSvc, nil
104108
}
105109

106-
func newClusterManagerClient(ctx context.Context, credentialsRef *infrav1.ObjectReference, crClient client.Client) (*container.ClusterManagerClient, error) {
110+
func newClusterManagerClient(ctx context.Context, credentialsRef *infrav1.ObjectReference, crClient client.Client, endpoints *infrav1.ServiceEndpoints) (*container.ClusterManagerClient, error) {
107111
opts, err := defaultClientOptions(ctx, credentialsRef, crClient)
108112
if err != nil {
109113
return nil, fmt.Errorf("getting default gcp client options: %w", err)
110114
}
111115

116+
if endpoints != nil && endpoints.ContainerServiceEndpoint != "" {
117+
opts = append(opts, option.WithEndpoint(endpoints.ContainerServiceEndpoint))
118+
}
119+
112120
managedClusterClient, err := container.NewClusterManagerClient(ctx, opts...)
113121
if err != nil {
114122
return nil, errors.Errorf("failed to create gcp cluster manager client: %v", err)
@@ -117,12 +125,16 @@ func newClusterManagerClient(ctx context.Context, credentialsRef *infrav1.Object
117125
return managedClusterClient, nil
118126
}
119127

120-
func newIamCredentialsClient(ctx context.Context, credentialsRef *infrav1.ObjectReference, crClient client.Client) (*credentials.IamCredentialsClient, error) {
128+
func newIamCredentialsClient(ctx context.Context, credentialsRef *infrav1.ObjectReference, crClient client.Client, endpoints *infrav1.ServiceEndpoints) (*credentials.IamCredentialsClient, error) {
121129
opts, err := defaultClientOptions(ctx, credentialsRef, crClient)
122130
if err != nil {
123131
return nil, fmt.Errorf("getting default gcp client options: %w", err)
124132
}
125133

134+
if endpoints != nil && endpoints.IAMServiceEndpoint != "" {
135+
opts = append(opts, option.WithEndpoint(endpoints.IAMServiceEndpoint))
136+
}
137+
126138
credentialsClient, err := credentials.NewIamCredentialsClient(ctx, opts...)
127139
if err != nil {
128140
return nil, errors.Errorf("failed to create gcp ciam credentials client: %v", err)
@@ -131,12 +143,16 @@ func newIamCredentialsClient(ctx context.Context, credentialsRef *infrav1.Object
131143
return credentialsClient, nil
132144
}
133145

134-
func newInstanceGroupManagerClient(ctx context.Context, credentialsRef *infrav1.ObjectReference, crClient client.Client) (*computerest.InstanceGroupManagersClient, error) {
146+
func newInstanceGroupManagerClient(ctx context.Context, credentialsRef *infrav1.ObjectReference, crClient client.Client, endpoints *infrav1.ServiceEndpoints) (*computerest.InstanceGroupManagersClient, error) {
135147
opts, err := defaultClientOptions(ctx, credentialsRef, crClient)
136148
if err != nil {
137149
return nil, fmt.Errorf("getting default gcp client options: %w", err)
138150
}
139151

152+
if endpoints != nil && endpoints.ComputeServiceEndpoint != "" {
153+
opts = append(opts, option.WithEndpoint(endpoints.ComputeServiceEndpoint))
154+
}
155+
140156
instanceGroupManagersClient, err := computerest.NewInstanceGroupManagersRESTClient(ctx, opts...)
141157
if err != nil {
142158
return nil, errors.Errorf("failed to create gcp instance group managers rest client: %v", err)
@@ -145,10 +161,16 @@ func newInstanceGroupManagerClient(ctx context.Context, credentialsRef *infrav1.
145161
return instanceGroupManagersClient, nil
146162
}
147163

148-
func newTagBindingsClient(ctx context.Context, credentialsRef *infrav1.ObjectReference, crClient client.Client, location string) (*resourcemanager.TagBindingsClient, error) {
164+
func newTagBindingsClient(ctx context.Context, credentialsRef *infrav1.ObjectReference, crClient client.Client, location string, endpoints *infrav1.ServiceEndpoints) (*resourcemanager.TagBindingsClient, error) {
149165
opts, err := defaultClientOptions(ctx, credentialsRef, crClient)
150-
endpoint := location + "-cloudresourcemanager.googleapis.com:443"
151-
opts = append(opts, option.WithEndpoint(endpoint))
166+
167+
if endpoints != nil && endpoints.ResourceManagerServiceEndpoint != "" {
168+
opts = append(opts, option.WithEndpoint(endpoints.ResourceManagerServiceEndpoint))
169+
} else {
170+
endpoint := location + "-cloudresourcemanager.googleapis.com:443"
171+
opts = append(opts, option.WithEndpoint(endpoint))
172+
}
173+
152174
if err != nil {
153175
return nil, fmt.Errorf("getting default gcp client options: %w", err)
154176
}

cloud/scope/cluster.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func NewClusterScope(ctx context.Context, params ClusterScopeParams) (*ClusterSc
5151
}
5252

5353
if params.GCPServices.Compute == nil {
54-
computeSvc, err := newComputeService(ctx, params.GCPCluster.Spec.CredentialsRef, params.Client)
54+
computeSvc, err := newComputeService(ctx, params.GCPCluster.Spec.CredentialsRef, params.Client, params.GCPCluster.Spec.ServiceEndpoints)
5555
if err != nil {
5656
return nil, errors.Errorf("failed to create gcp compute client: %v", err)
5757
}

cloud/scope/managedcluster.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func NewManagedClusterScope(ctx context.Context, params ManagedClusterScopeParam
5252
}
5353

5454
if params.GCPServices.Compute == nil {
55-
computeSvc, err := newComputeService(ctx, params.GCPManagedCluster.Spec.CredentialsRef, params.Client)
55+
computeSvc, err := newComputeService(ctx, params.GCPManagedCluster.Spec.CredentialsRef, params.Client, params.GCPManagedCluster.Spec.ServiceEndpoints)
5656
if err != nil {
5757
return nil, errors.Errorf("failed to create gcp compute client: %v", err)
5858
}

cloud/scope/managedcontrolplane.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,22 +70,22 @@ func NewManagedControlPlaneScope(ctx context.Context, params ManagedControlPlane
7070
}
7171

7272
if params.ManagedClusterClient == nil {
73-
managedClusterClient, err := newClusterManagerClient(ctx, params.GCPManagedCluster.Spec.CredentialsRef, params.Client)
73+
managedClusterClient, err := newClusterManagerClient(ctx, params.GCPManagedCluster.Spec.CredentialsRef, params.Client, params.GCPManagedCluster.Spec.ServiceEndpoints)
7474
if err != nil {
7575
return nil, errors.Errorf("failed to create gcp managed cluster client: %v", err)
7676
}
7777
params.ManagedClusterClient = managedClusterClient
7878
}
7979
if params.TagBindingsClient == nil {
80-
tagBindingsClient, err := newTagBindingsClient(ctx, params.GCPManagedCluster.Spec.CredentialsRef, params.Client, params.GCPManagedCluster.Spec.Region)
80+
tagBindingsClient, err := newTagBindingsClient(ctx, params.GCPManagedCluster.Spec.CredentialsRef, params.Client, params.GCPManagedCluster.Spec.Region, params.GCPManagedCluster.Spec.ServiceEndpoints)
8181
if err != nil {
8282
return nil, errors.Errorf("failed to create gcp tag bindings client: %v", err)
8383
}
8484
params.TagBindingsClient = tagBindingsClient
8585
}
8686
if params.CredentialsClient == nil {
8787
var credentialsClient *credentials.IamCredentialsClient
88-
credentialsClient, err = newIamCredentialsClient(ctx, params.GCPManagedCluster.Spec.CredentialsRef, params.Client)
88+
credentialsClient, err = newIamCredentialsClient(ctx, params.GCPManagedCluster.Spec.CredentialsRef, params.Client, params.GCPManagedCluster.Spec.ServiceEndpoints)
8989
if err != nil {
9090
return nil, errors.Errorf("failed to create gcp credentials client: %v", err)
9191
}

cloud/scope/managedmachinepool.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,14 @@ func NewManagedMachinePoolScope(ctx context.Context, params ManagedMachinePoolSc
7171
}
7272

7373
if params.ManagedClusterClient == nil {
74-
managedClusterClient, err := newClusterManagerClient(ctx, params.GCPManagedCluster.Spec.CredentialsRef, params.Client)
74+
managedClusterClient, err := newClusterManagerClient(ctx, params.GCPManagedCluster.Spec.CredentialsRef, params.Client, params.GCPManagedCluster.Spec.ServiceEndpoints)
7575
if err != nil {
7676
return nil, errors.Errorf("failed to create gcp managed cluster client: %v", err)
7777
}
7878
params.ManagedClusterClient = managedClusterClient
7979
}
8080
if params.InstanceGroupManagersClient == nil {
81-
instanceGroupManagersClient, err := newInstanceGroupManagerClient(ctx, params.GCPManagedCluster.Spec.CredentialsRef, params.Client)
81+
instanceGroupManagersClient, err := newInstanceGroupManagerClient(ctx, params.GCPManagedCluster.Spec.CredentialsRef, params.Client, params.GCPManagedCluster.Spec.ServiceEndpoints)
8282
if err != nil {
8383
return nil, errors.Errorf("failed to create gcp instance group manager client: %v", err)
8484
}

0 commit comments

Comments
 (0)