From de7154259992a15bf946114cc2fd930c21d4db2b Mon Sep 17 00:00:00 2001 From: Gemma Hou Date: Wed, 17 Jul 2024 23:45:00 +0000 Subject: [PATCH] use direct map util --- .../direct/compute/mapper.generated.go | 91 ++++++++++--------- pkg/controller/direct/compute/mapper.go | 30 +++--- pkg/controller/direct/compute/maputils.go | 87 ------------------ pkg/controller/direct/maputils.go | 21 +++++ 4 files changed, 83 insertions(+), 146 deletions(-) delete mode 100644 pkg/controller/direct/compute/maputils.go diff --git a/pkg/controller/direct/compute/mapper.generated.go b/pkg/controller/direct/compute/mapper.generated.go index 8f8743f400..3aac5476f4 100644 --- a/pkg/controller/direct/compute/mapper.generated.go +++ b/pkg/controller/direct/compute/mapper.generated.go @@ -16,53 +16,54 @@ package compute import ( pb "cloud.google.com/go/compute/apiv1/computepb" + "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct" krm "github.com/GoogleCloudPlatform/k8s-config-connector/apis/compute/v1beta1" ) -func ComputeForwardingRuleSpec_FromProto(mapCtx *MapContext, in *pb.ForwardingRule) *krm.ComputeForwardingRuleSpec { +func ComputeForwardingRuleSpec_FromProto(mapCtx *direct.MapContext, in *pb.ForwardingRule) *krm.ComputeForwardingRuleSpec { if in == nil { return nil } out := &krm.ComputeForwardingRuleSpec{} out.IpAddress = ComputeForwardingRuleSpec_IpAddress_FromProto(mapCtx, in.GetIPAddress()) - out.IpProtocol = LazyPtr(in.GetIPProtocol()) - out.AllPorts = LazyPtr(in.GetAllPorts()) - out.AllowGlobalAccess = LazyPtr(in.GetAllowGlobalAccess()) - out.AllowPscGlobalAccess = LazyPtr(in.GetAllowPscGlobalAccess()) - out.BackendServiceRef = ResourceRef_FromProto(mapCtx, in.GetBackendService()) + out.IpProtocol = direct.LazyPtr(in.GetIPProtocol()) + out.AllPorts = direct.LazyPtr(in.GetAllPorts()) + out.AllowGlobalAccess = direct.LazyPtr(in.GetAllowGlobalAccess()) + out.AllowPscGlobalAccess = direct.LazyPtr(in.GetAllowPscGlobalAccess()) + out.BackendServiceRef = direct.ResourceRef_FromProto(mapCtx, in.GetBackendService()) // MISSING: BaseForwardingRule // MISSING: CreationTimestamp - out.Description = LazyPtr(in.GetDescription()) + out.Description = direct.LazyPtr(in.GetDescription()) // MISSING: Fingerprint // MISSING: Id // MISSING: IpCollection - out.IpVersion = LazyPtr(in.GetIpVersion()) - out.IsMirroringCollector = LazyPtr(in.GetIsMirroringCollector()) + out.IpVersion = direct.LazyPtr(in.GetIpVersion()) + out.IsMirroringCollector = direct.LazyPtr(in.GetIsMirroringCollector()) // MISSING: Kind // MISSING: LabelFingerprint // MISSING: Labels - out.LoadBalancingScheme = LazyPtr(in.GetLoadBalancingScheme()) - out.MetadataFilters = Slice_FromProto(mapCtx, in.MetadataFilters, ForwardingruleMetadataFilters_FromProto) + out.LoadBalancingScheme = direct.LazyPtr(in.GetLoadBalancingScheme()) + out.MetadataFilters = direct.Slice_FromProto(mapCtx, in.MetadataFilters, ForwardingruleMetadataFilters_FromProto) // MISSING: Name - out.NetworkRef = ResourceRef_FromProto(mapCtx, in.GetNetwork()) - out.NetworkTier = LazyPtr(in.GetNetworkTier()) - out.NoAutomateDnsZone = LazyPtr(in.GetNoAutomateDnsZone()) - out.PortRange = LazyPtr(in.GetPortRange()) + out.NetworkRef = direct.ResourceRef_FromProto(mapCtx, in.GetNetwork()) + out.NetworkTier = direct.LazyPtr(in.GetNetworkTier()) + out.NoAutomateDnsZone = direct.LazyPtr(in.GetNoAutomateDnsZone()) + out.PortRange = direct.LazyPtr(in.GetPortRange()) out.Ports = in.Ports // MISSING: PscConnectionId // MISSING: PscConnectionStatus // MISSING: Region // MISSING: SelfLink - out.ServiceDirectoryRegistrations = Slice_FromProto(mapCtx, in.ServiceDirectoryRegistrations, ForwardingruleServiceDirectoryRegistrations_FromProto) - out.ServiceLabel = LazyPtr(in.GetServiceLabel()) + out.ServiceDirectoryRegistrations = direct.Slice_FromProto(mapCtx, in.ServiceDirectoryRegistrations, ForwardingruleServiceDirectoryRegistrations_FromProto) + out.ServiceLabel = direct.LazyPtr(in.GetServiceLabel()) // MISSING: ServiceName out.SourceIpRanges = in.SourceIpRanges - out.SubnetworkRef = ResourceRef_FromProto(mapCtx, in.GetSubnetwork()) + out.SubnetworkRef = direct.ResourceRef_FromProto(mapCtx, in.GetSubnetwork()) out.Target = ComputeForwardingRuleSpec_Target_FromProto(mapCtx, in.GetTarget()) return out } -func ComputeForwardingRuleSpec_ToProto(mapCtx *MapContext, in *krm.ComputeForwardingRuleSpec) *pb.ForwardingRule { +func ComputeForwardingRuleSpec_ToProto(mapCtx *direct.MapContext, in *krm.ComputeForwardingRuleSpec) *pb.ForwardingRule { if in == nil { return nil } @@ -72,7 +73,7 @@ func ComputeForwardingRuleSpec_ToProto(mapCtx *MapContext, in *krm.ComputeForwar out.AllPorts = in.AllPorts out.AllowGlobalAccess = in.AllowGlobalAccess out.AllowPscGlobalAccess = in.AllowPscGlobalAccess - out.BackendService = ResourceRef_ToProto(mapCtx, in.BackendServiceRef) + out.BackendService = direct.ResourceRef_ToProto(mapCtx, in.BackendServiceRef) // MISSING: BaseForwardingRule // MISSING: CreationTimestamp out.Description = in.Description @@ -85,9 +86,9 @@ func ComputeForwardingRuleSpec_ToProto(mapCtx *MapContext, in *krm.ComputeForwar // MISSING: LabelFingerprint // MISSING: Labels out.LoadBalancingScheme = in.LoadBalancingScheme - out.MetadataFilters = Slice_ToProto(mapCtx, in.MetadataFilters, ForwardingruleMetadataFilters_ToProto) + out.MetadataFilters = direct.Slice_ToProto(mapCtx, in.MetadataFilters, ForwardingruleMetadataFilters_ToProto) // MISSING: Name - out.Network = ResourceRef_ToProto(mapCtx, in.NetworkRef) + out.Network = direct.ResourceRef_ToProto(mapCtx, in.NetworkRef) out.NetworkTier = in.NetworkTier out.NoAutomateDnsZone = in.NoAutomateDnsZone out.PortRange = in.PortRange @@ -96,30 +97,30 @@ func ComputeForwardingRuleSpec_ToProto(mapCtx *MapContext, in *krm.ComputeForwar // MISSING: PscConnectionStatus // MISSING: Region // MISSING: SelfLink - out.ServiceDirectoryRegistrations = Slice_ToProto(mapCtx, in.ServiceDirectoryRegistrations, ForwardingruleServiceDirectoryRegistrations_ToProto) + out.ServiceDirectoryRegistrations = direct.Slice_ToProto(mapCtx, in.ServiceDirectoryRegistrations, ForwardingruleServiceDirectoryRegistrations_ToProto) out.ServiceLabel = in.ServiceLabel // MISSING: ServiceName out.SourceIpRanges = in.SourceIpRanges - out.Subnetwork = ResourceRef_ToProto(mapCtx, in.SubnetworkRef) + out.Subnetwork = direct.ResourceRef_ToProto(mapCtx, in.SubnetworkRef) out.Target = ComputeForwardingRuleSpec_Target_ToProto(mapCtx, in.Target) return out } -func ComputeForwardingRuleStatus_FromProto(mapCtx *MapContext, in *pb.ForwardingRule) *krm.ComputeForwardingRuleStatus { +func ComputeForwardingRuleStatus_FromProto(mapCtx *direct.MapContext, in *pb.ForwardingRule) *krm.ComputeForwardingRuleStatus { if in == nil { return nil } out := &krm.ComputeForwardingRuleStatus{} - out.BaseForwardingRule = LazyPtr(in.GetBaseForwardingRule()) - out.CreationTimestamp = LazyPtr(in.GetCreationTimestamp()) - out.LabelFingerprint = LazyPtr(in.GetLabelFingerprint()) + out.BaseForwardingRule = direct.LazyPtr(in.GetBaseForwardingRule()) + out.CreationTimestamp = direct.LazyPtr(in.GetCreationTimestamp()) + out.LabelFingerprint = direct.LazyPtr(in.GetLabelFingerprint()) out.PscConnectionId = ComputeForwardingRuleStatus_PscConnectionId_FromProto(mapCtx, in.GetPscConnectionId()) - out.PscConnectionStatus = LazyPtr(in.GetPscConnectionStatus()) - out.SelfLink = LazyPtr(in.GetSelfLink()) - out.ServiceName = LazyPtr(in.GetServiceName()) + out.PscConnectionStatus = direct.LazyPtr(in.GetPscConnectionStatus()) + out.SelfLink = direct.LazyPtr(in.GetSelfLink()) + out.ServiceName = direct.LazyPtr(in.GetServiceName()) return out } -func ComputeForwardingRuleStatus_ToProto(mapCtx *MapContext, in *krm.ComputeForwardingRuleStatus) *pb.ForwardingRule { +func ComputeForwardingRuleStatus_ToProto(mapCtx *direct.MapContext, in *krm.ComputeForwardingRuleStatus) *pb.ForwardingRule { if in == nil { return nil } @@ -133,7 +134,7 @@ func ComputeForwardingRuleStatus_ToProto(mapCtx *MapContext, in *krm.ComputeForw out.ServiceName = in.ServiceName return out } -func ForwardingruleFilterLabels_FromProto(mapCtx *MapContext, in *pb.MetadataFilterLabelMatch) *krm.ForwardingruleFilterLabels { +func ForwardingruleFilterLabels_FromProto(mapCtx *direct.MapContext, in *pb.MetadataFilterLabelMatch) *krm.ForwardingruleFilterLabels { if in == nil { return nil } @@ -142,44 +143,44 @@ func ForwardingruleFilterLabels_FromProto(mapCtx *MapContext, in *pb.MetadataFil out.Value = in.GetValue() return out } -func ForwardingruleFilterLabels_ToProto(mapCtx *MapContext, in *krm.ForwardingruleFilterLabels) *pb.MetadataFilterLabelMatch { +func ForwardingruleFilterLabels_ToProto(mapCtx *direct.MapContext, in *krm.ForwardingruleFilterLabels) *pb.MetadataFilterLabelMatch { if in == nil { return nil } out := &pb.MetadataFilterLabelMatch{} - out.Name = LazyPtr(in.Name) - out.Value = LazyPtr(in.Value) + out.Name = direct.LazyPtr(in.Name) + out.Value = direct.LazyPtr(in.Value) return out } -func ForwardingruleMetadataFilters_FromProto(mapCtx *MapContext, in *pb.MetadataFilter) *krm.ForwardingruleMetadataFilters { +func ForwardingruleMetadataFilters_FromProto(mapCtx *direct.MapContext, in *pb.MetadataFilter) *krm.ForwardingruleMetadataFilters { if in == nil { return nil } out := &krm.ForwardingruleMetadataFilters{} - out.FilterLabels = Slice_FromProto(mapCtx, in.FilterLabels, ForwardingruleFilterLabels_FromProto) + out.FilterLabels = direct.Slice_FromProto(mapCtx, in.FilterLabels, ForwardingruleFilterLabels_FromProto) out.FilterMatchCriteria = in.GetFilterMatchCriteria() return out } -func ForwardingruleMetadataFilters_ToProto(mapCtx *MapContext, in *krm.ForwardingruleMetadataFilters) *pb.MetadataFilter { +func ForwardingruleMetadataFilters_ToProto(mapCtx *direct.MapContext, in *krm.ForwardingruleMetadataFilters) *pb.MetadataFilter { if in == nil { return nil } out := &pb.MetadataFilter{} - out.FilterLabels = Slice_ToProto(mapCtx, in.FilterLabels, ForwardingruleFilterLabels_ToProto) - out.FilterMatchCriteria = LazyPtr(in.FilterMatchCriteria) + out.FilterLabels = direct.Slice_ToProto(mapCtx, in.FilterLabels, ForwardingruleFilterLabels_ToProto) + out.FilterMatchCriteria = direct.LazyPtr(in.FilterMatchCriteria) return out } -func ForwardingruleServiceDirectoryRegistrations_FromProto(mapCtx *MapContext, in *pb.ForwardingRuleServiceDirectoryRegistration) *krm.ForwardingruleServiceDirectoryRegistrations { +func ForwardingruleServiceDirectoryRegistrations_FromProto(mapCtx *direct.MapContext, in *pb.ForwardingRuleServiceDirectoryRegistration) *krm.ForwardingruleServiceDirectoryRegistrations { if in == nil { return nil } out := &krm.ForwardingruleServiceDirectoryRegistrations{} - out.Namespace = LazyPtr(in.GetNamespace()) - out.Service = LazyPtr(in.GetService()) + out.Namespace = direct.LazyPtr(in.GetNamespace()) + out.Service = direct.LazyPtr(in.GetService()) // MISSING: ServiceDirectoryRegion return out } -func ForwardingruleServiceDirectoryRegistrations_ToProto(mapCtx *MapContext, in *krm.ForwardingruleServiceDirectoryRegistrations) *pb.ForwardingRuleServiceDirectoryRegistration { +func ForwardingruleServiceDirectoryRegistrations_ToProto(mapCtx *direct.MapContext, in *krm.ForwardingruleServiceDirectoryRegistrations) *pb.ForwardingRuleServiceDirectoryRegistration { if in == nil { return nil } diff --git a/pkg/controller/direct/compute/mapper.go b/pkg/controller/direct/compute/mapper.go index 2e3dd8445e..93bc48a048 100644 --- a/pkg/controller/direct/compute/mapper.go +++ b/pkg/controller/direct/compute/mapper.go @@ -17,17 +17,19 @@ package compute import ( "strconv" + "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct" + krm "github.com/GoogleCloudPlatform/k8s-config-connector/apis/compute/v1beta1" "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/clients/generated/apis/k8s/v1alpha1" ) -func ComputeForwardingRuleSpec_IpAddress_ToProto(mapCtx *MapContext, in *krm.ForwardingruleIpAddress) *string { +func ComputeForwardingRuleSpec_IpAddress_ToProto(mapCtx *direct.MapContext, in *krm.ForwardingruleIpAddress) *string { if in == nil { return nil } var out *string - if oneof := ResourceRef_ToProto(mapCtx, in.AddressRef); oneof != nil { + if oneof := direct.ResourceRef_ToProto(mapCtx, in.AddressRef); oneof != nil { out = oneof } if in.Ip != nil { @@ -36,7 +38,7 @@ func ComputeForwardingRuleSpec_IpAddress_ToProto(mapCtx *MapContext, in *krm.For return out } -func ComputeForwardingRuleSpec_IpAddress_FromProto(mapCtx *MapContext, in string) *krm.ForwardingruleIpAddress { +func ComputeForwardingRuleSpec_IpAddress_FromProto(mapCtx *direct.MapContext, in string) *krm.ForwardingruleIpAddress { if in == "" { return nil } @@ -47,37 +49,37 @@ func ComputeForwardingRuleSpec_IpAddress_FromProto(mapCtx *MapContext, in string return out } -func ComputeForwardingRuleSpec_Target_ToProto(mapCtx *MapContext, in *krm.ForwardingruleTarget) *string { +func ComputeForwardingRuleSpec_Target_ToProto(mapCtx *direct.MapContext, in *krm.ForwardingruleTarget) *string { if in == nil { return nil } var out *string - if oneof := ResourceRef_ToProto(mapCtx, in.ServiceAttachmentRef); oneof != nil { + if oneof := direct.ResourceRef_ToProto(mapCtx, in.ServiceAttachmentRef); oneof != nil { out = oneof } - if oneof := ResourceRef_ToProto(mapCtx, in.TargetGRPCProxyRef); oneof != nil { + if oneof := direct.ResourceRef_ToProto(mapCtx, in.TargetGRPCProxyRef); oneof != nil { out = oneof } - if oneof := ResourceRef_ToProto(mapCtx, in.TargetHTTPProxyRef); oneof != nil { + if oneof := direct.ResourceRef_ToProto(mapCtx, in.TargetHTTPProxyRef); oneof != nil { out = oneof } - if oneof := ResourceRef_ToProto(mapCtx, in.TargetHTTPSProxyRef); oneof != nil { + if oneof := direct.ResourceRef_ToProto(mapCtx, in.TargetHTTPSProxyRef); oneof != nil { out = oneof } - if oneof := ResourceRef_ToProto(mapCtx, in.TargetSSLProxyRef); oneof != nil { + if oneof := direct.ResourceRef_ToProto(mapCtx, in.TargetSSLProxyRef); oneof != nil { out = oneof } - if oneof := ResourceRef_ToProto(mapCtx, in.TargetTCPProxyRef); oneof != nil { + if oneof := direct.ResourceRef_ToProto(mapCtx, in.TargetTCPProxyRef); oneof != nil { out = oneof } - if oneof := ResourceRef_ToProto(mapCtx, in.TargetVPNGatewayRef); oneof != nil { + if oneof := direct.ResourceRef_ToProto(mapCtx, in.TargetVPNGatewayRef); oneof != nil { out = oneof } return out } -func ComputeForwardingRuleSpec_Target_FromProto(mapCtx *MapContext, in string) *krm.ForwardingruleTarget { +func ComputeForwardingRuleSpec_Target_FromProto(mapCtx *direct.MapContext, in string) *krm.ForwardingruleTarget { if in == "" { return nil } @@ -90,7 +92,7 @@ func ComputeForwardingRuleSpec_Target_FromProto(mapCtx *MapContext, in string) * return out } -func ComputeForwardingRuleStatus_PscConnectionId_FromProto(mapCtx *MapContext, in uint64) *string { +func ComputeForwardingRuleStatus_PscConnectionId_FromProto(mapCtx *direct.MapContext, in uint64) *string { if in == 0 { return nil } @@ -99,7 +101,7 @@ func ComputeForwardingRuleStatus_PscConnectionId_FromProto(mapCtx *MapContext, i } -func ComputeForwardingRuleStatus_PscConnectionId_ToProto(mapCtx *MapContext, in *string) *uint64 { +func ComputeForwardingRuleStatus_PscConnectionId_ToProto(mapCtx *direct.MapContext, in *string) *uint64 { if in == nil { return nil } diff --git a/pkg/controller/direct/compute/maputils.go b/pkg/controller/direct/compute/maputils.go deleted file mode 100644 index 4f75d2ab9f..0000000000 --- a/pkg/controller/direct/compute/maputils.go +++ /dev/null @@ -1,87 +0,0 @@ -// 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 compute - -import ( - "errors" - "fmt" - - "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/clients/generated/apis/k8s/v1alpha1" -) - -type MapContext struct { - errs []error -} - -func (c *MapContext) Errorf(msg string, args ...interface{}) { - c.errs = append(c.errs, fmt.Errorf(msg, args...)) -} - -func (c *MapContext) Err() error { - return errors.Join(c.errs...) -} - -func Slice_ToProto[T, U any](mapCtx *MapContext, in []T, mapper func(mapCtx *MapContext, in *T) *U) []*U { - if in == nil { - return nil - } - - outSlice := make([]*U, 0, len(in)) - for _, inItem := range in { - outItem := mapper(mapCtx, &inItem) - outSlice = append(outSlice, outItem) - } - return outSlice -} - -func Slice_FromProto[T, U any](mapCtx *MapContext, in []*T, mapper func(mapCtx *MapContext, in *T) *U) []U { - if in == nil { - return nil - } - - outSlice := make([]U, 0, len(in)) - for _, inItem := range in { - outItem := mapper(mapCtx, inItem) - outSlice = append(outSlice, *outItem) - } - return outSlice -} - -func ResourceRef_FromProto(mapCtx *MapContext, in string) *v1alpha1.ResourceRef { - if in == "" { - return nil - } - return &v1alpha1.ResourceRef{ - External: in, - } -} - -func ResourceRef_ToProto(mapCtx *MapContext, in *v1alpha1.ResourceRef) *string { - if in == nil { - return nil - } - if in.External == "" { - mapCtx.Errorf("reference was not pre-resolved") - } - return LazyPtr(in.External) -} - -func LazyPtr[V comparable](v V) *V { - var defaultV V - if v == defaultV { - return nil - } - return &v -} diff --git a/pkg/controller/direct/maputils.go b/pkg/controller/direct/maputils.go index eb105a0bb8..36c3a8eb4f 100644 --- a/pkg/controller/direct/maputils.go +++ b/pkg/controller/direct/maputils.go @@ -21,6 +21,8 @@ import ( "strings" "time" + "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/clients/generated/apis/k8s/v1alpha1" + "github.com/googleapis/gax-go/v2/apierror" "google.golang.org/protobuf/reflect/protoreflect" "google.golang.org/protobuf/types/known/timestamppb" @@ -127,6 +129,25 @@ func Enum_FromProto[U ProtoEnum](mapCtx *MapContext, v U) *string { return &s } +func ResourceRef_FromProto(mapCtx *MapContext, in string) *v1alpha1.ResourceRef { + if in == "" { + return nil + } + return &v1alpha1.ResourceRef{ + External: in, + } +} + +func ResourceRef_ToProto(mapCtx *MapContext, in *v1alpha1.ResourceRef) *string { + if in == nil { + return nil + } + if in.External == "" { + mapCtx.Errorf("reference was not pre-resolved") + } + return LazyPtr(in.External) +} + func LazyPtr[V comparable](v V) *V { var defaultV V if v == defaultV {