From 178f56b20b7641bfad6aaa9cf227e4aee2f3cf29 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Tue, 27 Aug 2024 02:16:12 +0000 Subject: [PATCH] Adopt cmp.Or --- pkg/controller/controller.go | 18 ++++++++---------- pkg/controller/util.go | 9 ++++----- pkg/nghttpx/tls.go | 14 +++++--------- 3 files changed, 17 insertions(+), 24 deletions(-) diff --git a/pkg/controller/controller.go b/pkg/controller/controller.go index 59b3dffc..9acdfb6b 100644 --- a/pkg/controller/controller.go +++ b/pkg/controller/controller.go @@ -1274,11 +1274,10 @@ func (lbc *LoadBalancerController) createConfig(ctx context.Context) (*nghttpx.I } slices.SortFunc(upstreams, func(a, b *nghttpx.Upstream) int { - if c := cmp.Compare(a.Host, b.Host); c != 0 { - return c - } - - return cmp.Compare(a.Path, b.Path) + return cmp.Or( + cmp.Compare(a.Host, b.Host), + cmp.Compare(a.Path, b.Path), + ) }) upstreams = removeUpstreamsWithInconsistentBackendParams(ctx, upstreams) @@ -1286,11 +1285,10 @@ func (lbc *LoadBalancerController) createConfig(ctx context.Context) (*nghttpx.I for _, value := range upstreams { backends := value.Backends slices.SortFunc(backends, func(a, b nghttpx.Backend) int { - if c := cmp.Compare(a.Address, b.Address); c != 0 { - return c - } - - return cmp.Compare(a.Port, b.Port) + return cmp.Or( + cmp.Compare(a.Address, b.Address), + cmp.Compare(a.Port, b.Port), + ) }) // remove duplicate Backend diff --git a/pkg/controller/util.go b/pkg/controller/util.go index 4c22a5a1..c7b1eaed 100644 --- a/pkg/controller/util.go +++ b/pkg/controller/util.go @@ -58,11 +58,10 @@ func loadBalancerIngressesIPEqual(a, b []networkingv1.IngressLoadBalancerIngress // sortLoadBalancerIngress sorts a by IP and Hostname in the ascending order. func sortLoadBalancerIngress(lbIngs []networkingv1.IngressLoadBalancerIngress) { slices.SortFunc(lbIngs, func(a, b networkingv1.IngressLoadBalancerIngress) int { - if c := cmp.Compare(a.IP, b.IP); c != 0 { - return c - } - - return cmp.Compare(a.Hostname, b.Hostname) + return cmp.Or( + cmp.Compare(a.IP, b.IP), + cmp.Compare(a.Hostname, b.Hostname), + ) }) } diff --git a/pkg/nghttpx/tls.go b/pkg/nghttpx/tls.go index aebb96b4..9badd242 100644 --- a/pkg/nghttpx/tls.go +++ b/pkg/nghttpx/tls.go @@ -146,15 +146,11 @@ func TLSCredShareSamePaths(a, b *TLSCred) bool { } func TLSCredCompare(a, b *TLSCred) int { - if c := cmp.Compare(a.Key.Path, b.Key.Path); c != 0 { - return c - } - - if c := cmp.Compare(a.Cert.Path, b.Cert.Path); c != 0 { - return c - } - - return cmp.Compare(a.OCSPResp.GetPath(), b.OCSPResp.GetPath()) + return cmp.Or( + cmp.Compare(a.Key.Path, b.Key.Path), + cmp.Compare(a.Cert.Path, b.Cert.Path), + cmp.Compare(a.OCSPResp.GetPath(), b.OCSPResp.GetPath()), + ) } // SortTLSCred sorts creds in ascending order of Key.Path, Cert.Path, and OCSPResp.Path.