From 13023b5f4eec7de1d7e73e5feb350f31bef10d7d Mon Sep 17 00:00:00 2001 From: Gemma Hou Date: Wed, 17 Jul 2024 23:21:18 +0000 Subject: [PATCH] mappers for computeforwardingrule --- .../v1beta1/computeforwardingrule_types.go | 5 + .../direct/compute/mapper.generated.go | 191 ++++++++++++++++++ pkg/controller/direct/compute/mapper.go | 114 +++++++++++ pkg/controller/direct/compute/maputils.go | 87 ++++++++ 4 files changed, 397 insertions(+) create mode 100644 pkg/controller/direct/compute/mapper.generated.go create mode 100644 pkg/controller/direct/compute/mapper.go create mode 100644 pkg/controller/direct/compute/maputils.go diff --git a/apis/compute/v1beta1/computeforwardingrule_types.go b/apis/compute/v1beta1/computeforwardingrule_types.go index 8d8ee8b375..5e5ad74c80 100644 --- a/apis/compute/v1beta1/computeforwardingrule_types.go +++ b/apis/compute/v1beta1/computeforwardingrule_types.go @@ -37,6 +37,7 @@ var ( } ) +// +kcc:proto=google.cloud.compute.v1.MetadataFilterLabelMatch type ForwardingruleFilterLabels struct { /* Immutable. Name of the metadata label. The length must be between 1 and 1024 characters, inclusive. */ @@ -55,6 +56,7 @@ type ForwardingruleIpAddress struct { Ip *string `json:"ip,omitempty"` } +// +kcc:proto=google.cloud.compute.v1.MetadataFilter type ForwardingruleMetadataFilters struct { /* Immutable. The list of label value pairs that must match labels in the provided metadata based on filterMatchCriteria @@ -72,6 +74,7 @@ type ForwardingruleMetadataFilters struct { FilterMatchCriteria string `json:"filterMatchCriteria"` } +// +kcc:proto=google.cloud.compute.v1.ForwardingRuleServiceDirectoryRegistration type ForwardingruleServiceDirectoryRegistrations struct { /* Immutable. Service Directory namespace to register the forwarding rule under. */ // +optional @@ -105,6 +108,7 @@ type ForwardingruleTarget struct { TargetVPNGatewayRef *v1alpha1.ResourceRef `json:"targetVPNGatewayRef,omitempty"` } +// +kcc:proto=google.cloud.compute.v1.ForwardingRule type ComputeForwardingRuleSpec struct { /* Immutable. This field can only be used: * If 'IPProtocol' is one of TCP, UDP, or SCTP. @@ -358,6 +362,7 @@ type ComputeForwardingRuleSpec struct { Target *ForwardingruleTarget `json:"target,omitempty"` } +// +kcc:proto=google.cloud.compute.v1.ForwardingRule type ComputeForwardingRuleStatus struct { /* Conditions represent the latest available observations of the ComputeForwardingRule's current state. */ diff --git a/pkg/controller/direct/compute/mapper.generated.go b/pkg/controller/direct/compute/mapper.generated.go new file mode 100644 index 0000000000..8f8743f400 --- /dev/null +++ b/pkg/controller/direct/compute/mapper.generated.go @@ -0,0 +1,191 @@ +// 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 ( + pb "cloud.google.com/go/compute/apiv1/computepb" + + krm "github.com/GoogleCloudPlatform/k8s-config-connector/apis/compute/v1beta1" +) + +func ComputeForwardingRuleSpec_FromProto(mapCtx *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()) + // MISSING: BaseForwardingRule + // MISSING: CreationTimestamp + out.Description = LazyPtr(in.GetDescription()) + // MISSING: Fingerprint + // MISSING: Id + // MISSING: IpCollection + out.IpVersion = LazyPtr(in.GetIpVersion()) + out.IsMirroringCollector = LazyPtr(in.GetIsMirroringCollector()) + // MISSING: Kind + // MISSING: LabelFingerprint + // MISSING: Labels + out.LoadBalancingScheme = LazyPtr(in.GetLoadBalancingScheme()) + out.MetadataFilters = 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.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()) + // MISSING: ServiceName + out.SourceIpRanges = in.SourceIpRanges + out.SubnetworkRef = 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 { + if in == nil { + return nil + } + out := &pb.ForwardingRule{} + out.IPAddress = ComputeForwardingRuleSpec_IpAddress_ToProto(mapCtx, in.IpAddress) + out.IPProtocol = in.IpProtocol + out.AllPorts = in.AllPorts + out.AllowGlobalAccess = in.AllowGlobalAccess + out.AllowPscGlobalAccess = in.AllowPscGlobalAccess + out.BackendService = ResourceRef_ToProto(mapCtx, in.BackendServiceRef) + // MISSING: BaseForwardingRule + // MISSING: CreationTimestamp + out.Description = in.Description + // MISSING: Fingerprint + // MISSING: Id + // MISSING: IpCollection + out.IpVersion = in.IpVersion + out.IsMirroringCollector = in.IsMirroringCollector + // MISSING: Kind + // MISSING: LabelFingerprint + // MISSING: Labels + out.LoadBalancingScheme = in.LoadBalancingScheme + out.MetadataFilters = Slice_ToProto(mapCtx, in.MetadataFilters, ForwardingruleMetadataFilters_ToProto) + // MISSING: Name + out.Network = ResourceRef_ToProto(mapCtx, in.NetworkRef) + out.NetworkTier = in.NetworkTier + out.NoAutomateDnsZone = in.NoAutomateDnsZone + out.PortRange = in.PortRange + out.Ports = in.Ports + // MISSING: PscConnectionId + // MISSING: PscConnectionStatus + // MISSING: Region + // MISSING: SelfLink + out.ServiceDirectoryRegistrations = 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.Target = ComputeForwardingRuleSpec_Target_ToProto(mapCtx, in.Target) + return out +} +func ComputeForwardingRuleStatus_FromProto(mapCtx *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.PscConnectionId = ComputeForwardingRuleStatus_PscConnectionId_FromProto(mapCtx, in.GetPscConnectionId()) + out.PscConnectionStatus = LazyPtr(in.GetPscConnectionStatus()) + out.SelfLink = LazyPtr(in.GetSelfLink()) + out.ServiceName = LazyPtr(in.GetServiceName()) + return out +} +func ComputeForwardingRuleStatus_ToProto(mapCtx *MapContext, in *krm.ComputeForwardingRuleStatus) *pb.ForwardingRule { + if in == nil { + return nil + } + out := &pb.ForwardingRule{} + out.BaseForwardingRule = in.BaseForwardingRule + out.CreationTimestamp = in.CreationTimestamp + out.LabelFingerprint = in.LabelFingerprint + out.PscConnectionId = ComputeForwardingRuleStatus_PscConnectionId_ToProto(mapCtx, in.PscConnectionId) + out.PscConnectionStatus = in.PscConnectionStatus + out.SelfLink = in.SelfLink + out.ServiceName = in.ServiceName + return out +} +func ForwardingruleFilterLabels_FromProto(mapCtx *MapContext, in *pb.MetadataFilterLabelMatch) *krm.ForwardingruleFilterLabels { + if in == nil { + return nil + } + out := &krm.ForwardingruleFilterLabels{} + out.Name = in.GetName() + out.Value = in.GetValue() + return out +} +func ForwardingruleFilterLabels_ToProto(mapCtx *MapContext, in *krm.ForwardingruleFilterLabels) *pb.MetadataFilterLabelMatch { + if in == nil { + return nil + } + out := &pb.MetadataFilterLabelMatch{} + out.Name = LazyPtr(in.Name) + out.Value = LazyPtr(in.Value) + return out +} +func ForwardingruleMetadataFilters_FromProto(mapCtx *MapContext, in *pb.MetadataFilter) *krm.ForwardingruleMetadataFilters { + if in == nil { + return nil + } + out := &krm.ForwardingruleMetadataFilters{} + out.FilterLabels = Slice_FromProto(mapCtx, in.FilterLabels, ForwardingruleFilterLabels_FromProto) + out.FilterMatchCriteria = in.GetFilterMatchCriteria() + return out +} +func ForwardingruleMetadataFilters_ToProto(mapCtx *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) + return out +} +func ForwardingruleServiceDirectoryRegistrations_FromProto(mapCtx *MapContext, in *pb.ForwardingRuleServiceDirectoryRegistration) *krm.ForwardingruleServiceDirectoryRegistrations { + if in == nil { + return nil + } + out := &krm.ForwardingruleServiceDirectoryRegistrations{} + out.Namespace = LazyPtr(in.GetNamespace()) + out.Service = LazyPtr(in.GetService()) + // MISSING: ServiceDirectoryRegion + return out +} +func ForwardingruleServiceDirectoryRegistrations_ToProto(mapCtx *MapContext, in *krm.ForwardingruleServiceDirectoryRegistrations) *pb.ForwardingRuleServiceDirectoryRegistration { + if in == nil { + return nil + } + out := &pb.ForwardingRuleServiceDirectoryRegistration{} + out.Namespace = in.Namespace + out.Service = in.Service + // MISSING: ServiceDirectoryRegion + return out +} diff --git a/pkg/controller/direct/compute/mapper.go b/pkg/controller/direct/compute/mapper.go new file mode 100644 index 0000000000..2e3dd8445e --- /dev/null +++ b/pkg/controller/direct/compute/mapper.go @@ -0,0 +1,114 @@ +// 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 ( + "strconv" + + 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 { + if in == nil { + return nil + } + + var out *string + if oneof := ResourceRef_ToProto(mapCtx, in.AddressRef); oneof != nil { + out = oneof + } + if in.Ip != nil { + out = in.Ip + } + return out +} + +func ComputeForwardingRuleSpec_IpAddress_FromProto(mapCtx *MapContext, in string) *krm.ForwardingruleIpAddress { + if in == "" { + return nil + } + out := &krm.ForwardingruleIpAddress{} + out.AddressRef = &v1alpha1.ResourceRef{ + External: in, + } + return out +} + +func ComputeForwardingRuleSpec_Target_ToProto(mapCtx *MapContext, in *krm.ForwardingruleTarget) *string { + if in == nil { + return nil + } + + var out *string + if oneof := ResourceRef_ToProto(mapCtx, in.ServiceAttachmentRef); oneof != nil { + out = oneof + } + if oneof := ResourceRef_ToProto(mapCtx, in.TargetGRPCProxyRef); oneof != nil { + out = oneof + } + if oneof := ResourceRef_ToProto(mapCtx, in.TargetHTTPProxyRef); oneof != nil { + out = oneof + } + if oneof := ResourceRef_ToProto(mapCtx, in.TargetHTTPSProxyRef); oneof != nil { + out = oneof + } + if oneof := ResourceRef_ToProto(mapCtx, in.TargetSSLProxyRef); oneof != nil { + out = oneof + } + if oneof := ResourceRef_ToProto(mapCtx, in.TargetTCPProxyRef); oneof != nil { + out = oneof + } + if oneof := ResourceRef_ToProto(mapCtx, in.TargetVPNGatewayRef); oneof != nil { + out = oneof + } + return out +} + +func ComputeForwardingRuleSpec_Target_FromProto(mapCtx *MapContext, in string) *krm.ForwardingruleTarget { + if in == "" { + 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 = &v1alpha1.ResourceRef{ + External: in, + } + return out +} + +func ComputeForwardingRuleStatus_PscConnectionId_FromProto(mapCtx *MapContext, in uint64) *string { + if in == 0 { + return nil + } + strValue := strconv.FormatUint(in, 10) + return &strValue + +} + +func ComputeForwardingRuleStatus_PscConnectionId_ToProto(mapCtx *MapContext, in *string) *uint64 { + if in == nil { + return nil + } + + num, err := strconv.ParseUint(*in, 10, 64) + if err != nil { + mapCtx.Errorf("Error converting string to uint64") + return nil + } + + return &num +} diff --git a/pkg/controller/direct/compute/maputils.go b/pkg/controller/direct/compute/maputils.go new file mode 100644 index 0000000000..4f75d2ab9f --- /dev/null +++ b/pkg/controller/direct/compute/maputils.go @@ -0,0 +1,87 @@ +// 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 +}