diff --git a/apis/compute/v1beta1/computefirewallpolicyrule_reference.go b/apis/compute/v1beta1/computefirewallpolicyrule_reference.go index 014a9abb43..a4b142bed8 100644 --- a/apis/compute/v1beta1/computefirewallpolicyrule_reference.go +++ b/apis/compute/v1beta1/computefirewallpolicyrule_reference.go @@ -20,6 +20,7 @@ import ( "strconv" "strings" + "github.com/GoogleCloudPlatform/k8s-config-connector/apis/common" refsv1beta1 "github.com/GoogleCloudPlatform/k8s-config-connector/apis/refs/v1beta1" "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/k8s" apierrors "k8s.io/apimachinery/pkg/api/errors" @@ -105,7 +106,7 @@ func NewComputeFirewallPolicyRuleRef(ctx context.Context, reader client.Reader, priority := obj.Spec.Priority // Use approved External - externalRef := valueOf(obj.Status.ExternalRef) + externalRef := common.ValueOf(obj.Status.ExternalRef) if externalRef == "" { id.External = asComputeFirewallPolicyRuleExternal(id.parent, priority) return id, nil diff --git a/apis/compute/v1beta1/targettcpproxy_identity.go b/apis/compute/v1beta1/targettcpproxy_identity.go new file mode 100644 index 0000000000..9a99c0ff6d --- /dev/null +++ b/apis/compute/v1beta1/targettcpproxy_identity.go @@ -0,0 +1,92 @@ +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package v1beta1 + +import ( + "context" + "fmt" + + "github.com/GoogleCloudPlatform/k8s-config-connector/apis/common" + refsv1beta1 "github.com/GoogleCloudPlatform/k8s-config-connector/apis/refs/v1beta1" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "sigs.k8s.io/controller-runtime/pkg/client" +) + +type TargetTCPProxyIdentity struct { + id string + parent *TargetTCPProxyParent +} + +func (i *TargetTCPProxyIdentity) String() string { + return i.parent.String() + "/targetTcpProxies/" + i.id +} + +func (r *TargetTCPProxyIdentity) Parent() *TargetTCPProxyParent { + return r.parent +} + +func (r *TargetTCPProxyIdentity) ID() string { + return r.id +} + +type TargetTCPProxyParent struct { + ProjectID string + Location string +} + +func (p *TargetTCPProxyParent) String() string { + if p.Location == "global" { + return "projects/" + p.ProjectID + "/global" + } else { + return "projects/" + p.ProjectID + "/regions/" + p.Location + } +} + +func NewTargetTCPProxyIdentity(ctx context.Context, reader client.Reader, obj *ComputeTargetTCPProxy, u *unstructured.Unstructured) (*TargetTCPProxyIdentity, error) { + // Get Parent + projectID, err := refsv1beta1.ResolveProjectID(ctx, reader, u) + if err != nil { + return nil, err + } + // Get desired ID + resourceID := common.ValueOf(obj.Spec.ResourceID) + if resourceID == "" { + resourceID = obj.GetName() + } + if resourceID == "" { + return nil, fmt.Errorf("cannot resolve resource ID") + } + + // Use approved External + externalRef := common.ValueOf(obj.Status.ExternalRef) + if externalRef != "" { + actualIdentity, err := parseTargetTCPProxyExternal(externalRef) + if err != nil { + return nil, err + } + if actualIdentity.parent.ProjectID != projectID { + return nil, fmt.Errorf("spec.projectRef changed, expect %s, got %s", actualIdentity.parent.ProjectID, projectID) + } + if actualIdentity.id != resourceID { + return nil, fmt.Errorf("cannot reset `metadata.name` or `spec.resourceID` to %s, since it has already assigned to %s", + resourceID, actualIdentity.id) + } + } + + return &TargetTCPProxyIdentity{ + parent: &TargetTCPProxyParent{ProjectID: projectID}, + id: resourceID, + }, nil +} diff --git a/apis/compute/v1beta1/targettcpproxy_reference.go b/apis/compute/v1beta1/targettcpproxy_reference.go index 84f2fbc3fd..7563c973e8 100644 --- a/apis/compute/v1beta1/targettcpproxy_reference.go +++ b/apis/compute/v1beta1/targettcpproxy_reference.go @@ -19,7 +19,6 @@ import ( "fmt" "strings" - "github.com/GoogleCloudPlatform/k8s-config-connector/apis/common" refsv1beta1 "github.com/GoogleCloudPlatform/k8s-config-connector/apis/refs/v1beta1" "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/k8s" apierrors "k8s.io/apimachinery/pkg/api/errors" @@ -28,11 +27,11 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" ) -var _ refsv1beta1.ExternalNormalizer = &ComputeTargetTCPProxyRef{} +var _ refsv1beta1.ExternalNormalizer = &TargetTCPProxyRef{} -// ComputeTargetTCPProxyRef defines the resource reference to ComputeTargetTCPProxy, which "External" field +// TargetTCPProxyRef defines the resource reference to ComputeTargetTCPProxy, which "External" field // holds the GCP identifier for the KRM object. -type ComputeTargetTCPProxyRef struct { +type TargetTCPProxyRef struct { // A reference to an externally managed ComputeTargetTCPProxy resource. // Should be in the format "projects/{{projectID}}/global/targetTcpProxies/{{targettcpproxyID}}" // or "projects/{{projectID}}/regions/{{region}}/targetTcpProxies/{{targettcpproxyID}}". @@ -43,20 +42,18 @@ type ComputeTargetTCPProxyRef struct { // The namespace of a ComputeTargetTCPProxy resource. Namespace string `json:"namespace,omitempty"` - - parent *ComputeTargetTCPProxyParent } // NormalizedExternal provision the "External" value for other resource that depends on ComputeTargetTCPProxy. // If the "External" is given in the other resource's spec.ComputeTargetTCPProxyRef, the given value will be used. // Otherwise, the "Name" and "Namespace" will be used to query the actual ComputeTargetTCPProxy object from the cluster. -func (r *ComputeTargetTCPProxyRef) NormalizedExternal(ctx context.Context, reader client.Reader, otherNamespace string) (string, error) { +func (r *TargetTCPProxyRef) NormalizedExternal(ctx context.Context, reader client.Reader, otherNamespace string) (string, error) { if r.External != "" && r.Name != "" { return "", fmt.Errorf("cannot specify both name and external on %s reference", ComputeTargetTCPProxyGVK.Kind) } // From given External if r.External != "" { - if _, _, err := parseComputeTargetTCPProxyExternal(r.External); err != nil { + if _, err := parseTargetTCPProxyExternal(r.External); err != nil { return "", err } return r.External, nil @@ -87,114 +84,19 @@ func (r *ComputeTargetTCPProxyRef) NormalizedExternal(ctx context.Context, reade return r.External, nil } -// New builds a ComputeTargetTCPProxyRef from the Config Connector ComputeTargetTCPProxy object. -func NewComputeTargetTCPProxyRef(ctx context.Context, reader client.Reader, obj *ComputeTargetTCPProxy, u *unstructured.Unstructured) (*ComputeTargetTCPProxyRef, error) { - id := &ComputeTargetTCPProxyRef{} - - // Get Parent - projectID, err := refsv1beta1.ResolveProjectID(ctx, reader, u) - if projectID == "" { - return nil, fmt.Errorf("cannot resolve project") - } - - // Get Location - if obj.Spec.Location == nil { - id.parent = &ComputeTargetTCPProxyParent{ProjectID: projectID, Location: "global"} - } else { - location := common.ValueOf(obj.Spec.Location) - id.parent = &ComputeTargetTCPProxyParent{ProjectID: projectID, Location: location} - } - - // Get desired ID - resourceID := valueOf(obj.Spec.ResourceID) - if resourceID == "" { - resourceID = obj.GetName() - } - if resourceID == "" { - return nil, fmt.Errorf("cannot resolve resource ID") - } - - // Use approved External - externalRef := valueOf(obj.Status.ExternalRef) - if externalRef == "" { - id.External = asComputeTargetTCPProxyExternal(id.parent, resourceID) - return id, nil - } - - // Validate desired with actual - actualParent, actualResourceID, err := parseComputeTargetTCPProxyExternal(externalRef) - if err != nil { - return nil, err - } - if actualParent.ProjectID != projectID { - return nil, fmt.Errorf("spec.projectRef changed, expect %s, got %s", actualParent.ProjectID, projectID) - } - - if actualResourceID != resourceID { - return nil, fmt.Errorf("cannot reset `metadata.name` or `spec.resourceID` to %s, since it has already assigned to %s", - resourceID, actualResourceID) - } - id.External = externalRef - return id, nil -} - -func (r *ComputeTargetTCPProxyRef) Parent() (*ComputeTargetTCPProxyParent, error) { - if r.parent != nil { - return r.parent, nil - } - if r.External != "" { - parent, _, err := parseComputeTargetTCPProxyExternal(r.External) - if err != nil { - return nil, err - } - return parent, nil - } - return nil, fmt.Errorf("ComputeTargetTCPProxyRef not initialized from `NewComputeTargetTCPProxyRef` or `NormalizedExternal`") -} - -type ComputeTargetTCPProxyParent struct { - ProjectID string - Location string -} - -func (p *ComputeTargetTCPProxyParent) String() string { - if p.Location == "global" { - return "projects/" + p.ProjectID + "/global" - } else { - return "projects/" + p.ProjectID + "/regions/" + p.Location - } -} - -func asComputeTargetTCPProxyExternal(parent *ComputeTargetTCPProxyParent, resourceID string) (external string) { - return parent.String() + "/targetTcpProxies/" + resourceID -} - -func parseComputeTargetTCPProxyExternal(external string) (parent *ComputeTargetTCPProxyParent, resourceID string, err error) { +func parseTargetTCPProxyExternal(external string) (*TargetTCPProxyIdentity, error) { external = strings.TrimPrefix(external, "/") tokens := strings.Split(external, "/") if len(tokens) == 5 && tokens[0] == "projects" && tokens[2] == "global" && tokens[3] == "targetTcpProxies" { - parent = &ComputeTargetTCPProxyParent{ - ProjectID: tokens[1], - Location: "global", - } - resourceID = tokens[4] - return parent, resourceID, nil + return &TargetTCPProxyIdentity{ + parent: &TargetTCPProxyParent{ProjectID: tokens[1], Location: "global"}, + id: tokens[4], + }, nil } else if len(tokens) == 6 && tokens[0] == "projects" && tokens[2] == "regions" && tokens[4] == "targetTcpProxies" { - parent = &ComputeTargetTCPProxyParent{ - ProjectID: tokens[1], - Location: tokens[3], - } - resourceID = tokens[5] - return parent, resourceID, nil - } - return nil, "", fmt.Errorf("ExternalRef format invalid: %s", external) - -} - -func valueOf[T any](t *T) T { - var zeroVal T - if t == nil { - return zeroVal + return &TargetTCPProxyIdentity{ + parent: &TargetTCPProxyParent{ProjectID: tokens[1], Location: tokens[3]}, + id: tokens[5], + }, nil } - return *t + return nil, fmt.Errorf("format of ComputeTargetTCPProxy external=%q was not known (use projects/{{projectID}}/global/targetTcpProxies/{{targettcpproxyID}} or projects/{{projectID}}/regions/{{region}}/targetTcpProxies/{{targettcpproxyID}})", external) } diff --git a/apis/compute/v1beta1/zz_generated.deepcopy.go b/apis/compute/v1beta1/zz_generated.deepcopy.go index ca24003cfc..d412f59f0b 100644 --- a/apis/compute/v1beta1/zz_generated.deepcopy.go +++ b/apis/compute/v1beta1/zz_generated.deepcopy.go @@ -519,41 +519,6 @@ func (in *ComputeTargetTCPProxyObservedState) DeepCopy() *ComputeTargetTCPProxyO return out } -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *ComputeTargetTCPProxyParent) DeepCopyInto(out *ComputeTargetTCPProxyParent) { - *out = *in -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ComputeTargetTCPProxyParent. -func (in *ComputeTargetTCPProxyParent) DeepCopy() *ComputeTargetTCPProxyParent { - if in == nil { - return nil - } - out := new(ComputeTargetTCPProxyParent) - in.DeepCopyInto(out) - return out -} - -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *ComputeTargetTCPProxyRef) DeepCopyInto(out *ComputeTargetTCPProxyRef) { - *out = *in - if in.parent != nil { - in, out := &in.parent, &out.parent - *out = new(ComputeTargetTCPProxyParent) - **out = **in - } -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ComputeTargetTCPProxyRef. -func (in *ComputeTargetTCPProxyRef) DeepCopy() *ComputeTargetTCPProxyRef { - if in == nil { - return nil - } - out := new(ComputeTargetTCPProxyRef) - in.DeepCopyInto(out) - return out -} - // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *ComputeTargetTCPProxySpec) DeepCopyInto(out *ComputeTargetTCPProxySpec) { *out = *in @@ -875,3 +840,53 @@ func (in *ForwardingruleTarget) DeepCopy() *ForwardingruleTarget { in.DeepCopyInto(out) return out } + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *TargetTCPProxyIdentity) DeepCopyInto(out *TargetTCPProxyIdentity) { + *out = *in + if in.parent != nil { + in, out := &in.parent, &out.parent + *out = new(TargetTCPProxyParent) + **out = **in + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new TargetTCPProxyIdentity. +func (in *TargetTCPProxyIdentity) DeepCopy() *TargetTCPProxyIdentity { + if in == nil { + return nil + } + out := new(TargetTCPProxyIdentity) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *TargetTCPProxyParent) DeepCopyInto(out *TargetTCPProxyParent) { + *out = *in +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new TargetTCPProxyParent. +func (in *TargetTCPProxyParent) DeepCopy() *TargetTCPProxyParent { + if in == nil { + return nil + } + out := new(TargetTCPProxyParent) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *TargetTCPProxyRef) DeepCopyInto(out *TargetTCPProxyRef) { + *out = *in +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new TargetTCPProxyRef. +func (in *TargetTCPProxyRef) DeepCopy() *TargetTCPProxyRef { + if in == nil { + return nil + } + out := new(TargetTCPProxyRef) + in.DeepCopyInto(out) + return out +} diff --git a/pkg/controller/direct/compute/targettcpproxy/targettcpproxy_controller.go b/pkg/controller/direct/compute/targettcpproxy/targettcpproxy_controller.go index 781a8eb8ee..56343a3a06 100644 --- a/pkg/controller/direct/compute/targettcpproxy/targettcpproxy_controller.go +++ b/pkg/controller/direct/compute/targettcpproxy/targettcpproxy_controller.go @@ -75,7 +75,7 @@ type targetTCPProxyModel struct { var _ directbase.Model = &targetTCPProxyModel{} type targetTCPProxyAdapter struct { - id *krm.ComputeTargetTCPProxyRef + id *krm.TargetTCPProxyIdentity targetTcpProxiesClient *gcp.TargetTcpProxiesClient regionalTargetTcpProxiesClient *gcp.RegionTargetTcpProxiesClient desired *krm.ComputeTargetTCPProxy @@ -117,22 +117,19 @@ func (m *targetTCPProxyModel) AdapterForObject(ctx context.Context, reader clien return nil, fmt.Errorf("error converting to %T: %w", obj, err) } - computeTargetTCPProxyRef, err := krm.NewComputeTargetTCPProxyRef(ctx, reader, obj, u) + id, err := krm.NewTargetTCPProxyIdentity(ctx, reader, obj, u) if err != nil { return nil, err } targetTCPProxyAdapter := &targetTCPProxyAdapter{ - id: computeTargetTCPProxyRef, + id: id, desired: obj, reader: reader, } // Get location - parent, err := computeTargetTCPProxyRef.Parent() - if err != nil { - return nil, fmt.Errorf("get ComputeTargetTCPProxyAdapter parent %s: %w", computeTargetTCPProxyRef.External, err) - } + parent := id.Parent() location := parent.Location // Handle API/TF default values @@ -167,14 +164,14 @@ func (m *targetTCPProxyModel) AdapterForURL(ctx context.Context, url string) (di func (a *targetTCPProxyAdapter) Find(ctx context.Context) (bool, error) { log := klog.FromContext(ctx) - log.V(2).Info("getting ComputeTargetTCPProxy", "name", a.id.External) + log.V(2).Info("getting ComputeTargetTCPProxy", "name", a.id) targetTCPProxy, err := a.get(ctx) if err != nil { if direct.IsNotFound(err) { return false, nil } - return false, fmt.Errorf("getting ComputeTargetTCPProxy %q: %w", a.id.External, err) + return false, fmt.Errorf("getting ComputeTargetTCPProxy %q: %w", a.id, err) } a.actual = targetTCPProxy return true, nil @@ -189,7 +186,7 @@ func (a *targetTCPProxyAdapter) Create(ctx context.Context, createOp *directbase } log := klog.FromContext(ctx) - log.V(2).Info("creating ComputeTargetTCPProxy", "name", a.id.External) + log.V(2).Info("creating ComputeTargetTCPProxy", "name", a.id) mapCtx := &direct.MapContext{} desired := a.desired.DeepCopy() @@ -199,13 +196,10 @@ func (a *targetTCPProxyAdapter) Create(ctx context.Context, createOp *directbase return mapCtx.Err() } - parent, err := a.id.Parent() - if err != nil { - return fmt.Errorf("get ComputeTargetTCPProxy parent %s: %w", a.id.External, err) - } + parent := a.id.Parent() location := parent.Location - tokens := strings.Split(a.id.External, "/") + tokens := strings.Split(a.id.String(), "/") targetTCPProxy.Name = direct.LazyPtr(tokens[len(tokens)-1]) op := &gcp.Operation{} @@ -225,30 +219,27 @@ func (a *targetTCPProxyAdapter) Create(ctx context.Context, createOp *directbase } if err != nil { - return fmt.Errorf("creating ComputeTargetTCPProxy %s: %w", a.id.External, err) + return fmt.Errorf("creating ComputeTargetTCPProxy %s: %w", a.id, err) } if !op.Done() { err = op.Wait(ctx) if err != nil { - return fmt.Errorf("waiting ComputeTargetTCPProxy %s create failed: %w", a.id.External, err) + return fmt.Errorf("waiting ComputeTargetTCPProxy %s create failed: %w", a.id, err) } } - log.V(2).Info("successfully created ComputeTargetTCPProxy", "name", a.id.External) + log.V(2).Info("successfully created ComputeTargetTCPProxy", "name", a.id) // Get the created resource created := &computepb.TargetTcpProxy{} created, err = a.get(ctx) if err != nil { - return fmt.Errorf("getting ComputeTargetTCPProxy %s: %w", a.id.External, err) + return fmt.Errorf("getting ComputeTargetTCPProxy %s: %w", a.id, err) } status := &krm.ComputeTargetTCPProxyStatus{} status = ComputeTargetTCPProxyStatus_FromProto(mapCtx, created) - parent, err = a.id.Parent() - if err != nil { - return err - } + parent = a.id.Parent() externalRef := parent.String() + "/targetTcpProxies/" + direct.ValueOf(created.Name) status.ExternalRef = &externalRef @@ -264,7 +255,7 @@ func (a *targetTCPProxyAdapter) Update(ctx context.Context, updateOp *directbase } log := klog.FromContext(ctx).WithName(ctrlName) - log.V(2).Info("updating ComputeTargetTCPProxy", "name", a.id.External) + log.V(2).Info("updating ComputeTargetTCPProxy", "name", a.id) mapCtx := &direct.MapContext{} desired := a.desired.DeepCopy() @@ -276,10 +267,7 @@ func (a *targetTCPProxyAdapter) Update(ctx context.Context, updateOp *directbase op := &gcp.Operation{} updated := &computepb.TargetTcpProxy{} - parent, err := a.id.Parent() - if err != nil { - return fmt.Errorf("get ComputeTargetTCPProxy parent %s: %w", a.id.External, err) - } + parent := a.id.Parent() location := parent.Location // Regional API does not support Update @@ -288,7 +276,7 @@ func (a *targetTCPProxyAdapter) Update(ctx context.Context, updateOp *directbase a.desired.GroupVersionKind(), k8s.GetNamespacedName(a.desired)) } - tokens := strings.Split(a.id.External, "/") + tokens := strings.Split(a.id.String(), "/") targetTCPProxy.Name = direct.LazyPtr(tokens[len(tokens)-1]) if !reflect.DeepEqual(targetTCPProxy.ProxyHeader, a.actual.ProxyHeader) { @@ -299,15 +287,15 @@ func (a *targetTCPProxyAdapter) Update(ctx context.Context, updateOp *directbase } op, err = a.targetTcpProxiesClient.SetProxyHeader(ctx, setProxyHeaderReq) if err != nil { - return fmt.Errorf("updating ComputeTargetTCPProxy proxy header %s: %w", a.id.External, err) + return fmt.Errorf("updating ComputeTargetTCPProxy proxy header %s: %w", a.id, err) } if !op.Done() { err = op.Wait(ctx) if err != nil { - return fmt.Errorf("waiting ComputeTargetTCPProxy proxy header %s update failed: %w", a.id.External, err) + return fmt.Errorf("waiting ComputeTargetTCPProxy proxy header %s update failed: %w", a.id, err) } } - log.V(2).Info("successfully updated ComputeTargetTCPProxy proxy header", "name", a.id.External) + log.V(2).Info("successfully updated ComputeTargetTCPProxy proxy header", "name", a.id) } if !reflect.DeepEqual(targetTCPProxy.Service, a.actual.Service) { @@ -318,22 +306,22 @@ func (a *targetTCPProxyAdapter) Update(ctx context.Context, updateOp *directbase } op, err = a.targetTcpProxiesClient.SetBackendService(ctx, setBackendServiceReq) if err != nil { - return fmt.Errorf("updating ComputeTargetTCPProxy backend service %s: %w", a.id.External, err) + return fmt.Errorf("updating ComputeTargetTCPProxy backend service %s: %w", a.id, err) } if !op.Done() { err = op.Wait(ctx) if err != nil { - return fmt.Errorf("waiting ComputeTargetTCPProxy backend service %s update failed: %w", a.id.External, err) + return fmt.Errorf("waiting ComputeTargetTCPProxy backend service %s update failed: %w", a.id, err) } } - log.V(2).Info("successfully updated ComputeTargetTCPProxy backend service", "name", a.id.External) + log.V(2).Info("successfully updated ComputeTargetTCPProxy backend service", "name", a.id) } // Get the updated resource updated, err = a.get(ctx) if err != nil { - return fmt.Errorf("getting ComputeTargetTCPProxy %s: %w", a.id.External, err) + return fmt.Errorf("getting ComputeTargetTCPProxy %s: %w", a.id, err) } status := &krm.ComputeTargetTCPProxyStatus{} @@ -343,7 +331,7 @@ func (a *targetTCPProxyAdapter) Update(ctx context.Context, updateOp *directbase func (a *targetTCPProxyAdapter) Export(ctx context.Context) (*unstructured.Unstructured, error) { if a.actual == nil { - return nil, fmt.Errorf("targetTcpProxy %s not found", a.id.External) + return nil, fmt.Errorf("targetTcpProxy %s not found", a.id) } mc := &direct.MapContext{} @@ -368,16 +356,14 @@ func (a *targetTCPProxyAdapter) Export(ctx context.Context) (*unstructured.Unstr // Delete implements the Adapter interface. func (a *targetTCPProxyAdapter) Delete(ctx context.Context, deleteOp *directbase.DeleteOperation) (bool, error) { log := klog.FromContext(ctx) - log.V(2).Info("deleting ComputeTargetTcpProxy", "name", a.id.External) + log.V(2).Info("deleting ComputeTargetTcpProxy", "name", a.id) - parent, err := a.id.Parent() - if err != nil { - return false, fmt.Errorf("get ComputeTargetTcpProxy parent %s: %w", a.id.External, err) - } + parent := a.id.Parent() location := parent.Location + var err error op := &gcp.Operation{} - tokens := strings.Split(a.id.External, "/") + tokens := strings.Split(a.id.String(), "/") if location == "global" { delReq := &computepb.DeleteTargetTcpProxyRequest{ Project: parent.ProjectID, @@ -394,26 +380,23 @@ func (a *targetTCPProxyAdapter) Delete(ctx context.Context, deleteOp *directbase } if err != nil { - return false, fmt.Errorf("deleting ComputeTargetTcpProxy %s: %w", a.id.External, err) + return false, fmt.Errorf("deleting ComputeTargetTcpProxy %s: %w", a.id, err) } if !op.Done() { err = op.Wait(ctx) if err != nil { - return false, fmt.Errorf("waiting ComputeTargetTcpProxy %s delete failed: %w", a.id.External, err) + return false, fmt.Errorf("waiting ComputeTargetTcpProxy %s delete failed: %w", a.id, err) } } - log.V(2).Info("successfully deleted ComputeTargetTcpProxy", "name", a.id.External) + log.V(2).Info("successfully deleted ComputeTargetTcpProxy", "name", a.id) return true, nil } func (a *targetTCPProxyAdapter) get(ctx context.Context) (*computepb.TargetTcpProxy, error) { - parent, err := a.id.Parent() - if err != nil { - return nil, fmt.Errorf("get ComputeTargetTcpProxy parent %s: %w", a.id.External, err) - } + parent := a.id.Parent() location := parent.Location - tokens := strings.Split(a.id.External, "/") + tokens := strings.Split(a.id.String(), "/") if location == "global" { getReq := &computepb.GetTargetTcpProxyRequest{ Project: parent.ProjectID,