Skip to content

Commit 2f35b56

Browse files
Merge pull request GoogleCloudPlatform#3252 from gemmahou/skipupdate
fix: Log error when update is not supported for regional TCP Proxy
2 parents 4db732a + c182429 commit 2f35b56

File tree

2 files changed

+31
-23
lines changed

2 files changed

+31
-23
lines changed

apis/compute/v1beta1/targettcpproxy_reference.go

+10-9
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,12 @@ func NewComputeTargetTCPProxyRef(ctx context.Context, reader client.Reader, obj
9797
return nil, fmt.Errorf("cannot resolve project")
9898
}
9999

100-
// Get Region
100+
// Get Location
101101
if obj.Spec.Location == nil {
102-
id.parent = &ComputeTargetTCPProxyParent{ProjectID: projectID, Region: "global"}
102+
id.parent = &ComputeTargetTCPProxyParent{ProjectID: projectID, Location: "global"}
103103
} else {
104-
region := common.ValueOf(obj.Spec.Location)
105-
id.parent = &ComputeTargetTCPProxyParent{ProjectID: projectID, Region: region}
104+
location := common.ValueOf(obj.Spec.Location)
105+
id.parent = &ComputeTargetTCPProxyParent{ProjectID: projectID, Location: location}
106106
}
107107

108108
// Get desired ID
@@ -154,14 +154,14 @@ func (r *ComputeTargetTCPProxyRef) Parent() (*ComputeTargetTCPProxyParent, error
154154

155155
type ComputeTargetTCPProxyParent struct {
156156
ProjectID string
157-
Region string
157+
Location string
158158
}
159159

160160
func (p *ComputeTargetTCPProxyParent) String() string {
161-
if p.Region == "global" {
161+
if p.Location == "global" {
162162
return "projects/" + p.ProjectID + "/global"
163163
} else {
164-
return "projects/" + p.ProjectID + "/regions/" + p.Region
164+
return "projects/" + p.ProjectID + "/regions/" + p.Location
165165
}
166166
}
167167

@@ -172,16 +172,17 @@ func asComputeTargetTCPProxyExternal(parent *ComputeTargetTCPProxyParent, resour
172172
func parseComputeTargetTCPProxyExternal(external string) (parent *ComputeTargetTCPProxyParent, resourceID string, err error) {
173173
external = strings.TrimPrefix(external, "/")
174174
tokens := strings.Split(external, "/")
175-
if len(tokens) == 5 && tokens[0] == "projects" && tokens[3] == "targetTcpProxies" {
175+
if len(tokens) == 5 && tokens[0] == "projects" && tokens[2] == "global" && tokens[3] == "targetTcpProxies" {
176176
parent = &ComputeTargetTCPProxyParent{
177177
ProjectID: tokens[1],
178+
Location: "global",
178179
}
179180
resourceID = tokens[4]
180181
return parent, resourceID, nil
181182
} else if len(tokens) == 6 && tokens[0] == "projects" && tokens[2] == "regions" && tokens[4] == "targetTcpProxies" {
182183
parent = &ComputeTargetTCPProxyParent{
183184
ProjectID: tokens[1],
184-
Region: tokens[3],
185+
Location: tokens[3],
185186
}
186187
resourceID = tokens[5]
187188
return parent, resourceID, nil

pkg/controller/direct/compute/targettcpproxy/targettcpproxy_controller.go

+21-14
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"reflect"
2121
"strings"
2222

23+
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/k8s"
24+
2325
"google.golang.org/api/option"
2426

2527
gcp "cloud.google.com/go/compute/apiv1"
@@ -111,7 +113,7 @@ func (m *targetTCPProxyModel) AdapterForObject(ctx context.Context, reader clien
111113
if err != nil {
112114
return nil, fmt.Errorf("get ComputeTargetTCPProxyAdapter parent %s: %w", computeTargetTCPProxyRef.External, err)
113115
}
114-
location := parent.Region
116+
location := parent.Location
115117

116118
// Handle API/TF default values
117119
if obj.Spec.ProxyBind != nil && *obj.Spec.ProxyBind == false {
@@ -181,13 +183,13 @@ func (a *targetTCPProxyAdapter) Create(ctx context.Context, createOp *directbase
181183
if err != nil {
182184
return fmt.Errorf("get ComputeTargetTCPProxy parent %s: %w", a.id.External, err)
183185
}
184-
region := parent.Region
186+
location := parent.Location
185187

186188
tokens := strings.Split(a.id.External, "/")
187189
targetTCPProxy.Name = direct.LazyPtr(tokens[len(tokens)-1])
188190

189191
op := &gcp.Operation{}
190-
if region == "global" {
192+
if location == "global" {
191193
req := &computepb.InsertTargetTcpProxyRequest{
192194
Project: parent.ProjectID,
193195
TargetTcpProxyResource: targetTCPProxy,
@@ -196,7 +198,7 @@ func (a *targetTCPProxyAdapter) Create(ctx context.Context, createOp *directbase
196198
} else {
197199
req := &computepb.InsertRegionTargetTcpProxyRequest{
198200
Project: parent.ProjectID,
199-
Region: region,
201+
Region: location,
200202
TargetTcpProxyResource: targetTCPProxy,
201203
}
202204
op, err = a.regionalTargetTcpProxiesClient.Insert(ctx, req)
@@ -258,13 +260,18 @@ func (a *targetTCPProxyAdapter) Update(ctx context.Context, updateOp *directbase
258260
if err != nil {
259261
return fmt.Errorf("get ComputeTargetTCPProxy parent %s: %w", a.id.External, err)
260262
}
261-
region := parent.Region
263+
location := parent.Location
264+
265+
// Regional API does not support Update
266+
if location != "global" {
267+
return fmt.Errorf("update operation not supported for resource %v %v",
268+
a.desired.GroupVersionKind(), k8s.GetNamespacedName(a.desired))
269+
}
262270

263271
tokens := strings.Split(a.id.External, "/")
264272
targetTCPProxy.Name = direct.LazyPtr(tokens[len(tokens)-1])
265273

266-
// Regional API does not support Update
267-
if !reflect.DeepEqual(targetTCPProxy.ProxyHeader, a.actual.ProxyHeader) && region == "global" {
274+
if !reflect.DeepEqual(targetTCPProxy.ProxyHeader, a.actual.ProxyHeader) {
268275
setProxyHeaderReq := &computepb.SetProxyHeaderTargetTcpProxyRequest{
269276
Project: parent.ProjectID,
270277
TargetTcpProxiesSetProxyHeaderRequestResource: &computepb.TargetTcpProxiesSetProxyHeaderRequest{ProxyHeader: targetTCPProxy.ProxyHeader},
@@ -283,7 +290,7 @@ func (a *targetTCPProxyAdapter) Update(ctx context.Context, updateOp *directbase
283290
log.V(2).Info("successfully updated ComputeTargetTCPProxy proxy header", "name", a.id.External)
284291
}
285292

286-
if !reflect.DeepEqual(targetTCPProxy.Service, a.actual.Service) && region == "global" {
293+
if !reflect.DeepEqual(targetTCPProxy.Service, a.actual.Service) {
287294
setBackendServiceReq := &computepb.SetBackendServiceTargetTcpProxyRequest{
288295
Project: parent.ProjectID,
289296
TargetTcpProxiesSetBackendServiceRequestResource: &computepb.TargetTcpProxiesSetBackendServiceRequest{Service: targetTCPProxy.Service},
@@ -347,11 +354,11 @@ func (a *targetTCPProxyAdapter) Delete(ctx context.Context, deleteOp *directbase
347354
if err != nil {
348355
return false, fmt.Errorf("get ComputeTargetTcpProxy parent %s: %w", a.id.External, err)
349356
}
350-
region := parent.Region
357+
location := parent.Location
351358

352359
op := &gcp.Operation{}
353360
tokens := strings.Split(a.id.External, "/")
354-
if region == "global" {
361+
if location == "global" {
355362
delReq := &computepb.DeleteTargetTcpProxyRequest{
356363
Project: parent.ProjectID,
357364
TargetTcpProxy: tokens[len(tokens)-1],
@@ -360,7 +367,7 @@ func (a *targetTCPProxyAdapter) Delete(ctx context.Context, deleteOp *directbase
360367
} else {
361368
delReq := &computepb.DeleteRegionTargetTcpProxyRequest{
362369
Project: parent.ProjectID,
363-
Region: region,
370+
Region: location,
364371
TargetTcpProxy: tokens[len(tokens)-1],
365372
}
366373
op, err = a.regionalTargetTcpProxiesClient.Delete(ctx, delReq)
@@ -384,10 +391,10 @@ func (a *targetTCPProxyAdapter) get(ctx context.Context) (*computepb.TargetTcpPr
384391
if err != nil {
385392
return nil, fmt.Errorf("get ComputeTargetTcpProxy parent %s: %w", a.id.External, err)
386393
}
387-
region := parent.Region
394+
location := parent.Location
388395

389396
tokens := strings.Split(a.id.External, "/")
390-
if region == "global" {
397+
if location == "global" {
391398
getReq := &computepb.GetTargetTcpProxyRequest{
392399
Project: parent.ProjectID,
393400
TargetTcpProxy: tokens[len(tokens)-1],
@@ -396,7 +403,7 @@ func (a *targetTCPProxyAdapter) get(ctx context.Context) (*computepb.TargetTcpPr
396403
} else {
397404
getReq := &computepb.GetRegionTargetTcpProxyRequest{
398405
Project: parent.ProjectID,
399-
Region: region,
406+
Region: location,
400407
TargetTcpProxy: tokens[len(tokens)-1],
401408
}
402409
return a.regionalTargetTcpProxiesClient.Get(ctx, getReq)

0 commit comments

Comments
 (0)