diff --git a/config/tests/samples/create/harness.go b/config/tests/samples/create/harness.go index a26f699584..0f64ad3245 100644 --- a/config/tests/samples/create/harness.go +++ b/config/tests/samples/create/harness.go @@ -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"}: diff --git a/pkg/controller/direct/compute/forwardingrule_controller.go b/pkg/controller/direct/compute/forwardingrule_controller.go index 0e422d8c51..dc2da1d4c7 100644 --- a/pkg/controller/direct/compute/forwardingrule_controller.go +++ b/pkg/controller/direct/compute/forwardingrule_controller.go @@ -58,6 +58,7 @@ type forwardingRuleAdapter struct { globalForwardingRulesClient *gcp.GlobalForwardingRulesClient desired *krm.ComputeForwardingRule actual *computepb.ForwardingRule + reader client.Reader } var _ directbase.Adapter = &forwardingRuleAdapter{} @@ -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 @@ -223,6 +121,7 @@ func (m *forwardingRuleModel) AdapterForObject(ctx context.Context, reader clien forwardingRuleAdapter := &forwardingRuleAdapter{ id: id, desired: obj, + reader: reader, } // Get GCP client @@ -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") @@ -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{} @@ -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{ @@ -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{} @@ -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) { diff --git a/pkg/controller/direct/compute/forwardingrule_externalresource.go b/pkg/controller/direct/compute/forwardingrule_externalresource.go index f9cbd5f0c2..941b9d8109 100644 --- a/pkg/controller/direct/compute/forwardingrule_externalresource.go +++ b/pkg/controller/direct/compute/forwardingrule_externalresource.go @@ -17,8 +17,12 @@ limitations under the License. package compute import ( + "context" "fmt" "strings" + + krm "github.com/GoogleCloudPlatform/k8s-config-connector/apis/compute/v1beta1" + "sigs.k8s.io/controller-runtime/pkg/client" ) const ( @@ -78,3 +82,109 @@ func BuildID(project, location, forwardingRule string) *ForwardingRuleIdentity { forwardingRule: forwardingRule, } } + +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 +}