Skip to content

Commit b83c3a5

Browse files
authored
Merge pull request #1409 from barbacbd/CORS-3841
CORS-3841: Add custom Endpoints to CAPG
2 parents c832018 + dcb9c0b commit b83c3a5

13 files changed

+210
-14
lines changed

api/v1beta1/endpoints.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1beta1
18+
19+
// ServiceEndpoints contains all the gcp service endpoints that the user may override. Each field corresponds to
20+
// a service where the expected value is the url that is used to override the default API endpoint.
21+
type ServiceEndpoints struct {
22+
// ComputeServiceEndpoint is the custom endpoint url for the Compute Service
23+
// +kubebuilder:validation:Type=string
24+
// +kubebuilder:validation:Format=uri
25+
// +kubebuilder:validation:Pattern=`^https://`
26+
// +optional
27+
ComputeServiceEndpoint string `json:"compute,omitempty"`
28+
29+
// ContainerServiceEndpoint is the custom endpoint url for the Container Service
30+
// +kubebuilder:validation:Type=string
31+
// +kubebuilder:validation:Format=uri
32+
// +kubebuilder:validation:Pattern=`^https://`
33+
// +optional
34+
ContainerServiceEndpoint string `json:"container,omitempty"`
35+
36+
// IAMServiceEndpoint is the custom endpoint url for the IAM Service
37+
// +kubebuilder:validation:Type=string
38+
// +kubebuilder:validation:Format=uri
39+
// +kubebuilder:validation:Pattern=`^https://`
40+
// +optional
41+
IAMServiceEndpoint string `json:"iam,omitempty"`
42+
43+
// ResourceManagerServiceEndpoint is the custom endpoint url for the Resource Manager Service
44+
// +kubebuilder:validation:Type=string
45+
// +kubebuilder:validation:Format=uri
46+
// +kubebuilder:validation:Pattern=`^https://`
47+
// +optional
48+
ResourceManagerServiceEndpoint string `json:"resourceManager,omitempty"`
49+
}

api/v1beta1/gcpcluster_types.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ type GCPClusterSpec struct {
6868
// LoadBalancer contains configuration for one or more LoadBalancers.
6969
// +optional
7070
LoadBalancer LoadBalancerSpec `json:"loadBalancer,omitempty"`
71+
72+
// ServiceEndpoints contains the custom GCP Service Endpoint urls for each applicable service.
73+
// For instance, the user can specify a new endpoint for the compute service.
74+
// +optional
75+
ServiceEndpoints *ServiceEndpoints `json:"serviceEndpoints,omitempty"`
7176
}
7277

7378
// GCPClusterStatus defines the observed state of GCPCluster.

api/v1beta1/zz_generated.deepcopy.go

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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
}

config/crd/bases/infrastructure.cluster.x-k8s.io_gcpclusters.yaml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,36 @@ spec:
324324
- value
325325
type: object
326326
type: array
327+
serviceEndpoints:
328+
description: |-
329+
ServiceEndpoints contains the custom GCP Service Endpoint urls for each applicable service.
330+
For instance, the user can specify a new endpoint for the compute service.
331+
properties:
332+
compute:
333+
description: ComputeServiceEndpoint is the custom endpoint url
334+
for the Compute Service
335+
format: uri
336+
pattern: ^https://
337+
type: string
338+
container:
339+
description: ContainerServiceEndpoint is the custom endpoint url
340+
for the Container Service
341+
format: uri
342+
pattern: ^https://
343+
type: string
344+
iam:
345+
description: IAMServiceEndpoint is the custom endpoint url for
346+
the IAM Service
347+
format: uri
348+
pattern: ^https://
349+
type: string
350+
resourceManager:
351+
description: ResourceManagerServiceEndpoint is the custom endpoint
352+
url for the Resource Manager Service
353+
format: uri
354+
pattern: ^https://
355+
type: string
356+
type: object
327357
required:
328358
- project
329359
- region

config/crd/bases/infrastructure.cluster.x-k8s.io_gcpclustertemplates.yaml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,36 @@ spec:
341341
- value
342342
type: object
343343
type: array
344+
serviceEndpoints:
345+
description: |-
346+
ServiceEndpoints contains the custom GCP Service Endpoint urls for each applicable service.
347+
For instance, the user can specify a new endpoint for the compute service.
348+
properties:
349+
compute:
350+
description: ComputeServiceEndpoint is the custom endpoint
351+
url for the Compute Service
352+
format: uri
353+
pattern: ^https://
354+
type: string
355+
container:
356+
description: ContainerServiceEndpoint is the custom endpoint
357+
url for the Container Service
358+
format: uri
359+
pattern: ^https://
360+
type: string
361+
iam:
362+
description: IAMServiceEndpoint is the custom endpoint
363+
url for the IAM Service
364+
format: uri
365+
pattern: ^https://
366+
type: string
367+
resourceManager:
368+
description: ResourceManagerServiceEndpoint is the custom
369+
endpoint url for the Resource Manager Service
370+
format: uri
371+
pattern: ^https://
372+
type: string
373+
type: object
344374
required:
345375
- project
346376
- region

0 commit comments

Comments
 (0)