diff --git a/pkg/controller/direct/compute/forwardingrule_controller.go b/pkg/controller/direct/compute/forwardingrule_controller.go index 361b9d60f7..ed2675cd05 100644 --- a/pkg/controller/direct/compute/forwardingrule_controller.go +++ b/pkg/controller/direct/compute/forwardingrule_controller.go @@ -424,8 +424,29 @@ func (a *forwardingRuleAdapter) Update(ctx context.Context, u *unstructured.Unst } func (a *forwardingRuleAdapter) Export(ctx context.Context) (*unstructured.Unstructured, error) { - // TODO(kcc) - return nil, nil + if a.actual == nil { + return nil, fmt.Errorf("forwardingrule %q not found", a.fullyQualifiedName()) + } + + mc := &direct.MapContext{} + spec := ComputeForwardingRuleSpec_FromProto(mc, a.actual) + specObj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(spec) + if err != nil { + return nil, fmt.Errorf("error converting forwardingrule spec to unstructured: %w", err) + } + + u := &unstructured.Unstructured{ + Object: make(map[string]interface{}), + } + u.SetName(a.id.forwardingRule) + u.SetGroupVersionKind(krm.ComputeForwardingRuleGVK) + u.SetLabels(a.actual.Labels) + + if err := unstructured.SetNestedField(u.Object, specObj, "spec"); err != nil { + return nil, fmt.Errorf("setting spec: %w", err) + } + + return u, nil } // Delete implements the Adapter interface. diff --git a/pkg/controller/direct/compute/mapper.go b/pkg/controller/direct/compute/mapper.go index 0b76971b76..2160112a73 100644 --- a/pkg/controller/direct/compute/mapper.go +++ b/pkg/controller/direct/compute/mapper.go @@ -16,6 +16,7 @@ package compute import ( "strconv" + "strings" refs "github.com/GoogleCloudPlatform/k8s-config-connector/apis/refs/v1beta1" @@ -47,9 +48,7 @@ func ComputeForwardingRuleSpec_IpAddress_FromProto(mapCtx *direct.MapContext, in return nil } out := &krm.ForwardingruleIpAddress{} - out.AddressRef = &refs.ComputeAddressRef{ - External: in, - } + out.Ip = direct.LazyPtr(in) return out } @@ -166,10 +165,34 @@ func ComputeForwardingRuleSpec_Target_FromProto(mapCtx *direct.MapContext, in st return nil } out := &krm.ForwardingruleTarget{} - // TODO(yuhou): ForwardingRuleTarget can be one of multiple target objects. We need to determine which one to assign the value to. - // Assign to TargetHTTPProxy temporarily - out.TargetHTTPProxyRef = &refs.ComputeTargetHTTPProxyRef{ - External: in, + if strings.Contains(in, "serviceAttachments") { + out.ServiceAttachmentRef = &refs.ComputeServiceAttachmentRef{ + External: in, + } + } else if strings.Contains(in, "targetGrpcProxies") { + out.TargetGRPCProxyRef = &refs.ComputeTargetGrpcProxyRef{ + External: in, + } + } else if strings.Contains(in, "targetHttpProxies") { + out.TargetHTTPProxyRef = &refs.ComputeTargetHTTPProxyRef{ + External: in, + } + } else if strings.Contains(in, "targetHttpsProxies") { + out.TargetHTTPSProxyRef = &refs.ComputeTargetHTTPSProxyRef{ + External: in, + } + } else if strings.Contains(in, "targetSslProxies") { + out.TargetSSLProxyRef = &refs.ComputeTargetSSLProxyRef{ + External: in, + } + } else if strings.Contains(in, "targetTcpProxies") { + out.TargetTCPProxyRef = &refs.ComputeTargetTCPProxyRef{ + External: in, + } + } else if strings.Contains(in, "targetVpnGateways") { + out.TargetVPNGatewayRef = &refs.ComputeTargetVPNGatewayRef{ + External: in, + } } return out }