Skip to content

Commit

Permalink
Implement direct export
Browse files Browse the repository at this point in the history
  • Loading branch information
gemmahou committed Aug 29, 2024
1 parent 316f97f commit dda945a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 9 deletions.
25 changes: 23 additions & 2 deletions pkg/controller/direct/compute/forwardingrule_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
37 changes: 30 additions & 7 deletions pkg/controller/direct/compute/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package compute

import (
"strconv"
"strings"

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

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit dda945a

Please sign in to comment.