Skip to content

Commit

Permalink
move resolvers out of adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
gemmahou committed Sep 20, 2024
1 parent ad50e6d commit c6bec2b
Show file tree
Hide file tree
Showing 6 changed files with 376 additions and 147 deletions.
2 changes: 1 addition & 1 deletion config/tests/samples/create/harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ func MaybeSkip(t *testing.T, name string, resources []*unstructured.Unstructured
case schema.GroupKind{Group: "compute.cnrm.cloud.google.com", Kind: "ComputeNodeGroup"}:
case schema.GroupKind{Group: "compute.cnrm.cloud.google.com", Kind: "ComputeNodeTemplate"}:
case schema.GroupKind{Group: "compute.cnrm.cloud.google.com", Kind: "ComputeManagedSSLCertificate"}:
//case schema.GroupKind{Group: "compute.cnrm.cloud.google.com", Kind: "ComputeServiceAttachment"}:
case schema.GroupKind{Group: "compute.cnrm.cloud.google.com", Kind: "ComputeServiceAttachment"}:
case schema.GroupKind{Group: "compute.cnrm.cloud.google.com", Kind: "ComputeSSLCertificate"}:
case schema.GroupKind{Group: "compute.cnrm.cloud.google.com", Kind: "ComputeSubnetwork"}:
case schema.GroupKind{Group: "compute.cnrm.cloud.google.com", Kind: "ComputeTargetHTTPProxy"}:
Expand Down
119 changes: 14 additions & 105 deletions pkg/controller/direct/compute/forwardingrule_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type forwardingRuleAdapter struct {
globalForwardingRulesClient *gcp.GlobalForwardingRulesClient
desired *krm.ComputeForwardingRule
actual *computepb.ForwardingRule
reader client.Reader
}

var _ directbase.Adapter = &forwardingRuleAdapter{}
Expand All @@ -80,109 +81,6 @@ func (m *forwardingRuleModel) AdapterForObject(ctx context.Context, reader clien
return nil, err
}

// Get network
if obj.Spec.NetworkRef != nil {
networkRef, err := ResolveComputeNetwork(ctx, reader, obj, obj.Spec.NetworkRef)
if err != nil {
return nil, err

}
obj.Spec.NetworkRef.External = networkRef.External
}

// Get subnetwork
if obj.Spec.SubnetworkRef != nil {
subnetworkRef, err := ResolveComputeSubnetwork(ctx, reader, obj, obj.Spec.SubnetworkRef)
if err != nil {
return nil, err

}
obj.Spec.SubnetworkRef.External = subnetworkRef.External
}

// Get backend service
if obj.Spec.BackendServiceRef != nil {
backendServiceRef, err := ResolveComputeBackendService(ctx, reader, obj, obj.Spec.BackendServiceRef)
if err != nil {
return nil, err

}
obj.Spec.BackendServiceRef.External = backendServiceRef.External
}

// Get ip address, ip address is optional
if obj.Spec.IpAddress != nil && obj.Spec.IpAddress.AddressRef != nil {
computeAddressRef, err := ResolveComputeAddress(ctx, reader, obj, obj.Spec.IpAddress.AddressRef)
if err != nil {
return nil, err

}
obj.Spec.IpAddress.AddressRef.External = computeAddressRef.External
}

// Get target, target is optional
if obj.Spec.Target != nil {
// Get target ServiceAttachment
if obj.Spec.Target.ServiceAttachmentRef != nil {
serviceAttachmentRef, err := ResolveComputeServiceAttachment(ctx, reader, obj, obj.Spec.Target.ServiceAttachmentRef)
if err != nil {
return nil, err

}
obj.Spec.Target.ServiceAttachmentRef.External = serviceAttachmentRef.External
}

// Get target ComputeTargetHTTPProxy
if obj.Spec.Target.TargetHTTPProxyRef != nil {
targetHTTPProxyRef, err := ResolveComputeTargetHTTPProxy(ctx, reader, obj, obj.Spec.Target.TargetHTTPProxyRef)
if err != nil {
return nil, err

}
obj.Spec.Target.TargetHTTPProxyRef.External = targetHTTPProxyRef.External
}

// Get target ComputeTargetHTTPSProxy
if obj.Spec.Target.TargetHTTPSProxyRef != nil {
targetHTTPSProxyRef, err := ResolveComputeTargetHTTPSProxy(ctx, reader, obj, obj.Spec.Target.TargetHTTPSProxyRef)
if err != nil {
return nil, err

}
obj.Spec.Target.TargetHTTPSProxyRef.External = targetHTTPSProxyRef.External
}

// Get target TargetVPNGateway
if obj.Spec.Target.TargetVPNGatewayRef != nil {
targetVPNGatewayRef, err := ResolveComputeTargetVPNGateway(ctx, reader, obj, obj.Spec.Target.TargetVPNGatewayRef)
if err != nil {
return nil, err

}
obj.Spec.Target.TargetVPNGatewayRef.External = targetVPNGatewayRef.External
}

// Get target SSLProxy
if obj.Spec.Target.TargetSSLProxyRef != nil {
targetSSLProxyRef, err := ResolveComputeTargetSSLProxy(ctx, reader, obj, obj.Spec.Target.TargetSSLProxyRef)
if err != nil {
return nil, err

}
obj.Spec.Target.TargetSSLProxyRef.External = targetSSLProxyRef.External
}

// Get target TCPProxy
if obj.Spec.Target.TargetTCPProxyRef != nil {
targetTCPProxyRef, err := ResolveComputeTargetTCPProxy(ctx, reader, obj, obj.Spec.Target.TargetTCPProxyRef)
if err != nil {
return nil, err

}
obj.Spec.Target.TargetTCPProxyRef.External = targetTCPProxyRef.External
}
}

// Get location
location := obj.Spec.Location

Expand Down Expand Up @@ -223,6 +121,7 @@ func (m *forwardingRuleModel) AdapterForObject(ctx context.Context, reader clien
forwardingRuleAdapter := &forwardingRuleAdapter{
id: id,
desired: obj,
reader: reader,
}

// Get GCP client
Expand Down Expand Up @@ -274,6 +173,7 @@ func (a *forwardingRuleAdapter) Find(ctx context.Context) (bool, error) {

func (a *forwardingRuleAdapter) Create(ctx context.Context, createOp *directbase.CreateOperation) error {
u := createOp.GetUnstructured()
var err error

if a.id.project == "" {
return fmt.Errorf("project is empty")
Expand All @@ -282,6 +182,11 @@ func (a *forwardingRuleAdapter) Create(ctx context.Context, createOp *directbase
return fmt.Errorf("resourceID is empty")
}

err = resolveDependencies(ctx, a.reader, a.desired)
if err != nil {
return err
}

log := klog.FromContext(ctx).WithName(ctrlName)
log.V(2).Info("creating ComputeForwardingRule", "name", a.id.forwardingRule)
mapCtx := &direct.MapContext{}
Expand All @@ -296,7 +201,6 @@ func (a *forwardingRuleAdapter) Create(ctx context.Context, createOp *directbase
forwardingRule.Labels = desired.Labels

// Create forwarding rule(labels are not set during Insert)
var err error
op := &gcp.Operation{}
if a.id.location == "global" {
req := &computepb.InsertGlobalForwardingRuleRequest{
Expand Down Expand Up @@ -362,11 +266,17 @@ func (a *forwardingRuleAdapter) Create(ctx context.Context, createOp *directbase

func (a *forwardingRuleAdapter) Update(ctx context.Context, updateOp *directbase.UpdateOperation) error {
u := updateOp.GetUnstructured()
var err error

if a.id.forwardingRule == "" {
return fmt.Errorf("resourceID is empty")
}

err = resolveDependencies(ctx, a.reader, a.desired)
if err != nil {
return err
}

log := klog.FromContext(ctx).WithName(ctrlName)
log.V(2).Info("updating ComputeForwardingRule", "name", a.id.forwardingRule)
mapCtx := &direct.MapContext{}
Expand All @@ -381,7 +291,6 @@ func (a *forwardingRuleAdapter) Update(ctx context.Context, updateOp *directbase

// Patch only support update on networkTier field, which KCC does not support yet.
// Use setTarget and setLabels to update target and labels fields.
var err error
op := &gcp.Operation{}
updated := &computepb.ForwardingRule{}
if !reflect.DeepEqual(forwardingRule.Labels, a.actual.Labels) {
Expand Down
108 changes: 108 additions & 0 deletions pkg/controller/direct/compute/refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"context"
"fmt"

krm "github.com/GoogleCloudPlatform/k8s-config-connector/apis/compute/v1beta1"

refs "github.com/GoogleCloudPlatform/k8s-config-connector/apis/refs/v1beta1"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/k8s"
apierrors "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -521,3 +523,109 @@ func resolveResourceName(ctx context.Context, reader client.Reader, key client.O

return resource, nil
}

func resolveDependencies(ctx context.Context, reader client.Reader, obj *krm.ComputeForwardingRule) error {
// Get network
if obj.Spec.NetworkRef != nil {
networkRef, err := ResolveComputeNetwork(ctx, reader, obj, obj.Spec.NetworkRef)
if err != nil {
return err

}
obj.Spec.NetworkRef.External = networkRef.External
}

// Get subnetwork
if obj.Spec.SubnetworkRef != nil {
subnetworkRef, err := ResolveComputeSubnetwork(ctx, reader, obj, obj.Spec.SubnetworkRef)
if err != nil {
return err

}
obj.Spec.SubnetworkRef.External = subnetworkRef.External
}

// Get backend service
if obj.Spec.BackendServiceRef != nil {
backendServiceRef, err := ResolveComputeBackendService(ctx, reader, obj, obj.Spec.BackendServiceRef)
if err != nil {
return err

}
obj.Spec.BackendServiceRef.External = backendServiceRef.External
}

// Get ip address, ip address is optional
if obj.Spec.IpAddress != nil && obj.Spec.IpAddress.AddressRef != nil {
computeAddressRef, err := ResolveComputeAddress(ctx, reader, obj, obj.Spec.IpAddress.AddressRef)
if err != nil {
return err

}
obj.Spec.IpAddress.AddressRef.External = computeAddressRef.External
}

// Get target, target is optional
if obj.Spec.Target != nil {
// Get target ServiceAttachment
if obj.Spec.Target.ServiceAttachmentRef != nil {
serviceAttachmentRef, err := ResolveComputeServiceAttachment(ctx, reader, obj, obj.Spec.Target.ServiceAttachmentRef)
if err != nil {
return err

}
obj.Spec.Target.ServiceAttachmentRef.External = serviceAttachmentRef.External
}

// Get target ComputeTargetHTTPProxy
if obj.Spec.Target.TargetHTTPProxyRef != nil {
targetHTTPProxyRef, err := ResolveComputeTargetHTTPProxy(ctx, reader, obj, obj.Spec.Target.TargetHTTPProxyRef)
if err != nil {
return err

}
obj.Spec.Target.TargetHTTPProxyRef.External = targetHTTPProxyRef.External
}

// Get target ComputeTargetHTTPSProxy
if obj.Spec.Target.TargetHTTPSProxyRef != nil {
targetHTTPSProxyRef, err := ResolveComputeTargetHTTPSProxy(ctx, reader, obj, obj.Spec.Target.TargetHTTPSProxyRef)
if err != nil {
return err

}
obj.Spec.Target.TargetHTTPSProxyRef.External = targetHTTPSProxyRef.External
}

// Get target TargetVPNGateway
if obj.Spec.Target.TargetVPNGatewayRef != nil {
targetVPNGatewayRef, err := ResolveComputeTargetVPNGateway(ctx, reader, obj, obj.Spec.Target.TargetVPNGatewayRef)
if err != nil {
return err

}
obj.Spec.Target.TargetVPNGatewayRef.External = targetVPNGatewayRef.External
}

// Get target SSLProxy
if obj.Spec.Target.TargetSSLProxyRef != nil {
targetSSLProxyRef, err := ResolveComputeTargetSSLProxy(ctx, reader, obj, obj.Spec.Target.TargetSSLProxyRef)
if err != nil {
return err

}
obj.Spec.Target.TargetSSLProxyRef.External = targetSSLProxyRef.External
}

// Get target TCPProxy
if obj.Spec.Target.TargetTCPProxyRef != nil {
targetTCPProxyRef, err := ResolveComputeTargetTCPProxy(ctx, reader, obj, obj.Spec.Target.TargetTCPProxyRef)
if err != nil {
return err

}
obj.Spec.Target.TargetTCPProxyRef.External = targetTCPProxyRef.External
}
}
return nil
}
Loading

0 comments on commit c6bec2b

Please sign in to comment.