Skip to content

Commit

Permalink
replace collectionutil with samber/lo
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinForReal committed Dec 4, 2024
1 parent c5d1e68 commit 6067108
Show file tree
Hide file tree
Showing 110 changed files with 5,288 additions and 3,834 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ require (
github.com/onsi/ginkgo/v2 v2.22.0
github.com/onsi/gomega v1.36.0
github.com/prometheus/client_golang v1.20.5
github.com/samber/lo v1.47.0
github.com/spf13/cobra v1.8.1
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.10.0
Expand Down Expand Up @@ -121,7 +122,6 @@ require (
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.60.1 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/samber/lo v1.38.1 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
github.com/stoewer/go-strcase v1.2.0 // indirect
github.com/x448/float16 v0.8.4 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@ github.com/redis/go-redis/v9 v9.6.1/go.mod h1:0C0c6ycQsdpVNQpxb1njEQIqkx5UcsM8FJ
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/samber/lo v1.38.1 h1:j2XEAqXKb09Am4ebOg31SpvzUTTs6EN3VfgeLUhPdXM=
github.com/samber/lo v1.38.1/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA=
github.com/samber/lo v1.47.0 h1:z7RynLwP5nbyRscyvcD043DWYoOcYRv3mV8lBeqOCLc=
github.com/samber/lo v1.47.0/go.mod h1:RmDH9Ct32Qy3gduHQuKJ3gW1fMHAnE/fAzQuf6He5cU=
github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8=
github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
Expand Down
5 changes: 2 additions & 3 deletions internal/testutil/fixture/azure_loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@ limitations under the License.
package fixture

import (
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2022-07-01/network"

"k8s.io/utils/ptr"
)

func (f *AzureFixture) LoadBalancer() *AzureLoadBalancerFixture {
return &AzureLoadBalancerFixture{
lb: &network.LoadBalancer{
Name: ptr.To("lb"),
Name: to.Ptr("lb"),
LoadBalancerPropertiesFormat: &network.LoadBalancerPropertiesFormat{
// TODO
},
Expand Down
11 changes: 5 additions & 6 deletions internal/testutil/fixture/azure_publicip.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ package fixture
import (
"fmt"

"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2022-07-01/network"

"k8s.io/utils/ptr"
)

func (f *AzureFixture) PublicIPAddress(name string) *AzurePublicIPAddressFixture {
Expand All @@ -32,8 +31,8 @@ func (f *AzureFixture) PublicIPAddress(name string) *AzurePublicIPAddressFixture

return &AzurePublicIPAddressFixture{
pip: &network.PublicIPAddress{
ID: ptr.To(fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/publicIPAddresses/%s", SubscriptionID, ResourceGroup, name)),
Name: ptr.To(name),
ID: to.Ptr(fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/publicIPAddresses/%s", SubscriptionID, ResourceGroup, name)),
Name: to.Ptr(name),
Tags: make(map[string]*string),
PublicIPAddressPropertiesFormat: &network.PublicIPAddressPropertiesFormat{},
},
Expand All @@ -49,11 +48,11 @@ func (f *AzurePublicIPAddressFixture) Build() network.PublicIPAddress {
}

func (f *AzurePublicIPAddressFixture) WithTag(key, value string) *AzurePublicIPAddressFixture {
f.pip.Tags[key] = ptr.To(value)
f.pip.Tags[key] = to.Ptr(value)
return f
}

func (f *AzurePublicIPAddressFixture) WithAddress(address string) *AzurePublicIPAddressFixture {
f.pip.PublicIPAddressPropertiesFormat.IPAddress = ptr.To(address)
f.pip.PublicIPAddressPropertiesFormat.IPAddress = to.Ptr(address)
return f
}
44 changes: 21 additions & 23 deletions internal/testutil/fixture/azure_securitygroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,10 @@ import (

"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v6"
"k8s.io/utils/ptr"

"github.com/samber/lo"
"sigs.k8s.io/cloud-provider-azure/internal/testutil"
"sigs.k8s.io/cloud-provider-azure/pkg/consts"
"sigs.k8s.io/cloud-provider-azure/pkg/provider/securitygroup"
fnutil "sigs.k8s.io/cloud-provider-azure/pkg/util/collectionutil"
"sigs.k8s.io/cloud-provider-azure/pkg/util/iputil"
)

Expand Down Expand Up @@ -58,17 +56,17 @@ func (f *AzureFixture) NNoiseSecurityRules(nRules int) []*armnetwork.SecurityRul
initPriority := int32(100)
for i := 0; i < nRules; i++ {
rule := &armnetwork.SecurityRule{
Name: ptr.To(fmt.Sprintf("test-security-rule_%d", i)),
Name: to.Ptr(fmt.Sprintf("test-security-rule_%d", i)),
Properties: &armnetwork.SecurityRulePropertiesFormat{
Priority: ptr.To(initPriority),
Priority: to.Ptr(initPriority),
Protocol: protocolByID(i),
Direction: to.Ptr(armnetwork.SecurityRuleDirectionInbound),
Access: to.Ptr(armnetwork.SecurityRuleAccessAllow),
SourceAddressPrefixes: to.SliceOfPtrs(
fmt.Sprintf("140.0.0.%d", i), // NOTE: keep the source IP / destination IP unique to LB ips.
fmt.Sprintf("130.0.50.%d", i),
),
SourcePortRange: ptr.To("*"),
SourcePortRange: to.Ptr("*"),
DestinationPortRanges: to.SliceOfPtrs(
fmt.Sprintf("4000%d", i),
fmt.Sprintf("5000%d", i),
Expand All @@ -83,11 +81,11 @@ func (f *AzureFixture) NNoiseSecurityRules(nRules int) []*armnetwork.SecurityRul
fmt.Sprintf("200.0.50.%d", i),
)
case 1:
rule.Properties.DestinationAddressPrefix = ptr.To(fmt.Sprintf("222.111.0.%d", i))
rule.Properties.DestinationAddressPrefix = to.Ptr(fmt.Sprintf("222.111.0.%d", i))
case 2:
rule.Properties.DestinationApplicationSecurityGroups = []*armnetwork.ApplicationSecurityGroup{
{
ID: ptr.To(fmt.Sprintf("/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/the-rg/providers/Microsoft.Network/applicationSecurityGroups/the-asg-%d", i)),
ID: to.Ptr(fmt.Sprintf("/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/the-rg/providers/Microsoft.Network/applicationSecurityGroups/the-asg-%d", i)),
},
}
}
Expand All @@ -106,7 +104,7 @@ func (f *AzureFixture) NNoiseSecurityRules(nRules int) []*armnetwork.SecurityRul
func (f *AzureFixture) SecurityGroup() *AzureSecurityGroupFixture {
return &AzureSecurityGroupFixture{
sg: &armnetwork.SecurityGroup{
Name: ptr.To("nsg"),
Name: to.Ptr("nsg"),
Properties: &armnetwork.SecurityGroupPropertiesFormat{
SecurityRules: []*armnetwork.SecurityRule{},
},
Expand All @@ -120,25 +118,25 @@ func (f *AzureFixture) AllowSecurityRule(
srcPrefixes []string,
dstPorts []int32,
) *AzureAllowSecurityRuleFixture {
dstPortRanges := fnutil.Map(func(p int32) string { return strconv.FormatInt(int64(p), 10) }, dstPorts)
dstPortRanges := lo.Map(dstPorts, func(p int32, _ int) string { return strconv.FormatInt(int64(p), 10) })
sort.Strings(dstPortRanges)

rv := &AzureAllowSecurityRuleFixture{
rule: &armnetwork.SecurityRule{
Name: ptr.To(securitygroup.GenerateAllowSecurityRuleName(protocol, ipFamily, srcPrefixes, dstPorts)),
Name: to.Ptr(securitygroup.GenerateAllowSecurityRuleName(protocol, ipFamily, srcPrefixes, dstPorts)),
Properties: &armnetwork.SecurityRulePropertiesFormat{
Protocol: to.Ptr(protocol),
Access: to.Ptr(armnetwork.SecurityRuleAccessAllow),
Direction: to.Ptr(armnetwork.SecurityRuleDirectionInbound),
SourcePortRange: ptr.To("*"),
SourcePortRange: to.Ptr("*"),
DestinationPortRanges: to.SliceOfPtrs(dstPortRanges...),
Priority: ptr.To(int32(consts.LoadBalancerMinimumPriority)),
Priority: to.Ptr(int32(consts.LoadBalancerMinimumPriority)),
},
},
}

if len(srcPrefixes) == 1 {
rv.rule.Properties.SourceAddressPrefix = ptr.To(srcPrefixes[0])
rv.rule.Properties.SourceAddressPrefix = to.Ptr(srcPrefixes[0])
} else {
rv.rule.Properties.SourceAddressPrefixes = to.SliceOfPtrs(srcPrefixes...)
}
Expand All @@ -149,15 +147,15 @@ func (f *AzureFixture) AllowSecurityRule(
func (f *AzureFixture) DenyAllSecurityRule(ipFamily iputil.Family) *AzureDenyAllSecurityRuleFixture {
return &AzureDenyAllSecurityRuleFixture{
rule: &armnetwork.SecurityRule{
Name: ptr.To(securitygroup.GenerateDenyAllSecurityRuleName(ipFamily)),
Name: to.Ptr(securitygroup.GenerateDenyAllSecurityRuleName(ipFamily)),
Properties: &armnetwork.SecurityRulePropertiesFormat{
Protocol: to.Ptr(armnetwork.SecurityRuleProtocolAsterisk),
Access: to.Ptr(armnetwork.SecurityRuleAccessDeny),
Direction: to.Ptr(armnetwork.SecurityRuleDirectionInbound),
SourcePortRange: ptr.To("*"),
SourceAddressPrefix: ptr.To("*"),
DestinationPortRange: ptr.To("*"),
Priority: ptr.To(int32(consts.LoadBalancerMaximumPriority)),
SourcePortRange: to.Ptr("*"),
SourceAddressPrefix: to.Ptr("*"),
DestinationPortRange: to.Ptr("*"),
Priority: to.Ptr(int32(consts.LoadBalancerMaximumPriority)),
},
},
}
Expand Down Expand Up @@ -187,13 +185,13 @@ type AzureAllowSecurityRuleFixture struct {
}

func (f *AzureAllowSecurityRuleFixture) WithPriority(p int32) *AzureAllowSecurityRuleFixture {
f.rule.Properties.Priority = ptr.To(p)
f.rule.Properties.Priority = to.Ptr(p)
return f
}

func (f *AzureAllowSecurityRuleFixture) WithDestination(prefixes ...string) *AzureAllowSecurityRuleFixture {
if len(prefixes) == 1 {
f.rule.Properties.DestinationAddressPrefix = ptr.To(prefixes[0])
f.rule.Properties.DestinationAddressPrefix = to.Ptr(prefixes[0])
f.rule.Properties.DestinationAddressPrefixes = nil
} else {
f.rule.Properties.DestinationAddressPrefix = nil
Expand All @@ -213,13 +211,13 @@ type AzureDenyAllSecurityRuleFixture struct {
}

func (f *AzureDenyAllSecurityRuleFixture) WithPriority(p int32) *AzureDenyAllSecurityRuleFixture {
f.rule.Properties.Priority = ptr.To(p)
f.rule.Properties.Priority = to.Ptr(p)
return f
}

func (f *AzureDenyAllSecurityRuleFixture) WithDestination(prefixes ...string) *AzureDenyAllSecurityRuleFixture {
if len(prefixes) == 1 {
f.rule.Properties.DestinationAddressPrefix = ptr.To(prefixes[0])
f.rule.Properties.DestinationAddressPrefix = to.Ptr(prefixes[0])
f.rule.Properties.DestinationAddressPrefixes = nil
} else {
f.rule.Properties.DestinationAddressPrefix = nil
Expand Down
10 changes: 5 additions & 5 deletions internal/testutil/fixture/fixture.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"math/big"
"net/netip"

fnutil "sigs.k8s.io/cloud-provider-azure/pkg/util/collectionutil"
"github.com/samber/lo"
)

type Fixture struct{}
Expand Down Expand Up @@ -69,7 +69,7 @@ func (f *Fixture) RandomIPv4Addresses(n int) []netip.Addr {
}

func (f *Fixture) RandomIPv4AddressStrings(n int) []string {
return fnutil.Map(func(p netip.Addr) string { return p.String() }, f.RandomIPv4Addresses(n))
return lo.Map(f.RandomIPv4Addresses(n), func(p netip.Addr, _ int) string { return p.String() })
}

func (f *Fixture) RandomIPv4Prefixes(n int) []netip.Prefix {
Expand All @@ -91,7 +91,7 @@ func (f *Fixture) RandomIPv4Prefixes(n int) []netip.Prefix {
}

func (f *Fixture) RandomIPv4PrefixStrings(n int) []string {
return fnutil.Map(func(p netip.Prefix) string { return p.String() }, f.RandomIPv4Prefixes(n))
return lo.Map(f.RandomIPv4Prefixes(n), func(p netip.Prefix, _ int) string { return p.String() })
}

func (f *Fixture) RandomIPv6Addresses(n int) []netip.Addr {
Expand All @@ -108,7 +108,7 @@ func (f *Fixture) RandomIPv6Addresses(n int) []netip.Addr {
}

func (f *Fixture) RandomIPv6AddressStrings(n int) []string {
return fnutil.Map(func(p netip.Addr) string { return p.String() }, f.RandomIPv6Addresses(n))
return lo.Map(f.RandomIPv6Addresses(n), func(p netip.Addr, _ int) string { return p.String() })
}

func (f *Fixture) RandomIPv6Prefixes(n int) []netip.Prefix {
Expand All @@ -130,5 +130,5 @@ func (f *Fixture) RandomIPv6Prefixes(n int) []netip.Prefix {
}

func (f *Fixture) RandomIPv6PrefixStrings(n int) []string {
return fnutil.Map(func(p netip.Prefix) string { return p.String() }, f.RandomIPv6Prefixes(n))
return lo.Map(f.RandomIPv6Prefixes(n), func(p netip.Prefix, _ int) string { return p.String() })
}
1 change: 1 addition & 0 deletions kubetest2-aks/deployer/deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

"github.com/octago/sflags/gen/gpflag"
"github.com/spf13/pflag"

"k8s.io/klog"
"sigs.k8s.io/kubetest2/pkg/types"
)
Expand Down
9 changes: 5 additions & 4 deletions kubetest2-aks/deployer/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ import (
"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/streaming"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
armcontainerservicev2 "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/containerservice/armcontainerservice/v2"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources"
"k8s.io/apimachinery/pkg/util/wait"

"k8s.io/klog"
"k8s.io/utils/ptr"

"sigs.k8s.io/kubetest2/pkg/exec"
)
Expand Down Expand Up @@ -152,10 +153,10 @@ func (d *deployer) createResourceGroup(subscriptionID string) (armresources.Reso
now := time.Now()
timestamp := now.Unix()
param := armresources.ResourceGroup{
Location: ptr.To(d.Location),
Location: to.Ptr(d.Location),
Tags: map[string]*string{
"creation_date": ptr.To(fmt.Sprintf("%d", timestamp)),
"usage": ptr.To(usageTag),
"creation_date": to.Ptr(fmt.Sprintf("%d", timestamp)),
"usage": to.Ptr(usageTag),
},
}

Expand Down
2 changes: 1 addition & 1 deletion kubetest2-aks/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ require (
github.com/spf13/pflag v1.0.5
k8s.io/apimachinery v0.31.3
k8s.io/klog v1.0.0
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8
sigs.k8s.io/kubetest2 v0.0.0-20220728001911-c76fb417aa01
)

Expand Down Expand Up @@ -52,4 +51,5 @@ require (
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
k8s.io/klog/v2 v2.130.1 // indirect
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 // indirect
)
12 changes: 6 additions & 6 deletions pkg/consts/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ import (
"strings"
"testing"

"github.com/samber/lo"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/ptr"
)

func TestIsK8sServiceHasHAModeEnabled(t *testing.T) {
Expand Down Expand Up @@ -184,13 +184,13 @@ func Test_extractInt32FromString(t *testing.T) {
wantErr bool
}{
{name: "value is not a number", args: args{val: "cookies"}, wantErr: true},
{name: "value is zero", args: args{val: "0"}, wantErr: false, want: ptr.To(int32(0))},
{name: "value is zero", args: args{val: "0"}, wantErr: false, want: lo.ToPtr(int32(0))},
{name: "value is a float number", args: args{val: "0.1"}, wantErr: true},
{name: "value is a positive integer", args: args{val: "24"}, want: ptr.To(int32(24)), wantErr: false},
{name: "value negative integer", args: args{val: "-6"}, want: ptr.To(int32(-6)), wantErr: false},
{name: "value is a positive integer", args: args{val: "24"}, want: lo.ToPtr(int32(24)), wantErr: false},
{name: "value negative integer", args: args{val: "-6"}, want: lo.ToPtr(int32(-6)), wantErr: false},
{name: "validator is nil", args: args{val: "-6", businessValidator: []Int32BusinessValidator{
nil,
}}, want: ptr.To(int32(-6)), wantErr: false},
}}, want: lo.ToPtr(int32(-6)), wantErr: false},
{name: "validation failed", args: args{val: "-6", businessValidator: []Int32BusinessValidator{
func(_ *int32) error {
return fmt.Errorf("validator failed")
Expand Down Expand Up @@ -349,7 +349,7 @@ func TestGetInt32HealthProbeConfigOfPortFromK8sSvcAnnotation(t *testing.T) {
port: 80,
key: HealthProbeParamsNumOfProbe,
},
want: ptr.To(int32(2)),
want: lo.ToPtr(int32(2)),
wantErr: false,
},
{
Expand Down
Loading

0 comments on commit 6067108

Please sign in to comment.