Skip to content

Commit 0abee6b

Browse files
authored
Merge pull request kubernetes#131488 from sivchari/apply-slices-package
kubeadm: use slices package to reduce the codes
2 parents a19c0ad + efcceee commit 0abee6b

File tree

10 files changed

+28
-93
lines changed

10 files changed

+28
-93
lines changed

cmd/kubeadm/app/cmd/init.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"io"
2222
"os"
2323
"path/filepath"
24+
"slices"
2425

2526
"github.com/pkg/errors"
2627
"github.com/spf13/cobra"
@@ -643,10 +644,5 @@ func manageSkippedAddons(cfg *kubeadmapi.ClusterConfiguration, skipPhases []stri
643644
}
644645

645646
func isPhaseInSkipPhases(phase string, skipPhases []string) bool {
646-
for _, item := range skipPhases {
647-
if item == phase {
648-
return true
649-
}
650-
}
651-
return false
647+
return slices.Contains(skipPhases, phase)
652648
}

cmd/kubeadm/app/phases/controlplane/manifests.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"net"
2222
"os"
2323
"path/filepath"
24+
"slices"
2425
"strconv"
2526
"strings"
2627

@@ -295,12 +296,7 @@ func isValidAuthzMode(authzMode string) bool {
295296
kubeadmconstants.ModeAlwaysDeny,
296297
}
297298

298-
for _, mode := range allModes {
299-
if authzMode == mode {
300-
return true
301-
}
302-
}
303-
return false
299+
return slices.Contains(allModes, authzMode)
304300
}
305301

306302
// getControllerManagerCommand builds the right controller manager command from the given config object and version

cmd/kubeadm/app/phases/etcd/local.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"net"
2222
"os"
2323
"path/filepath"
24+
"slices"
2425
"strconv"
2526
"strings"
2627
"time"
@@ -105,11 +106,9 @@ func RemoveStackedEtcdMemberFromCluster(client clientset.Interface, cfg *kubeadm
105106
// is not needed.
106107
if len(members) == 1 {
107108
etcdClientAddress := etcdutil.GetClientURL(&cfg.LocalAPIEndpoint)
108-
for _, endpoint := range etcdClient.Endpoints {
109-
if endpoint == etcdClientAddress {
110-
klog.V(1).Info("[etcd] This is the only remaining etcd member in the etcd cluster, skip removing it")
111-
return nil
112-
}
109+
if slices.Contains(etcdClient.Endpoints, etcdClientAddress) {
110+
klog.V(1).Info("[etcd] This is the only remaining etcd member in the etcd cluster, skip removing it")
111+
return nil
113112
}
114113
}
115114

cmd/kubeadm/app/phases/kubeconfig/kubeconfig.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"io"
2626
"os"
2727
"path/filepath"
28+
"slices"
2829
"time"
2930

3031
"github.com/pkg/errors"
@@ -280,15 +281,7 @@ func validateKubeConfig(outDir, filename string, config *clientcmdapi.Config) er
280281
currentCaCert := currentCACerts[0]
281282

282283
// Find a common trust anchor
283-
trustAnchorFound := false
284-
for _, expectedCaCert := range expectedCACerts {
285-
// Compare the current CA cert to the expected CA cert.
286-
// If the certificates match then a common trust anchor was found.
287-
if currentCaCert.Equal(expectedCaCert) {
288-
trustAnchorFound = true
289-
break
290-
}
291-
}
284+
trustAnchorFound := slices.ContainsFunc(expectedCACerts, currentCaCert.Equal)
292285
if !trustAnchorFound {
293286
return errors.Errorf("a kubeconfig file %q exists but does not contain a trusted CA in its current context's "+
294287
"cluster. Total CA certificates found: %d", kubeConfigFilePath, len(currentCACerts))

cmd/kubeadm/app/phases/markcontrolplane/markcontrolplane.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package markcontrolplane
1818

1919
import (
2020
"fmt"
21+
"slices"
2122

2223
v1 "k8s.io/api/core/v1"
2324
clientset "k8s.io/client-go/kubernetes"
@@ -50,23 +51,13 @@ func MarkControlPlane(client clientset.Interface, controlPlaneName string, taint
5051
})
5152
}
5253

53-
func taintExists(taint v1.Taint, taints []v1.Taint) bool {
54-
for _, t := range taints {
55-
if t == taint {
56-
return true
57-
}
58-
}
59-
60-
return false
61-
}
62-
6354
func markControlPlaneNode(n *v1.Node, taints []v1.Taint) {
6455
for _, label := range labelsToAdd {
6556
n.ObjectMeta.Labels[label] = ""
6657
}
6758

6859
for _, nt := range n.Spec.Taints {
69-
if !taintExists(nt, taints) {
60+
if !slices.Contains(taints, nt) {
7061
taints = append(taints, nt)
7162
}
7263
}

cmd/kubeadm/app/util/certs/util.go

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"crypto/x509"
2323
"net"
2424
"path/filepath"
25+
"slices"
2526
"testing"
2627
"time"
2728

@@ -115,21 +116,17 @@ func AssertCertificateHasOrganizations(t *testing.T, cert *x509.Certificate, org
115116
// AssertCertificateHasClientAuthUsage is a utility function for kubeadm testing that asserts if a given certificate has
116117
// the expected ExtKeyUsageClientAuth
117118
func AssertCertificateHasClientAuthUsage(t *testing.T, cert *x509.Certificate) {
118-
for i := range cert.ExtKeyUsage {
119-
if cert.ExtKeyUsage[i] == x509.ExtKeyUsageClientAuth {
120-
return
121-
}
119+
if slices.Contains(cert.ExtKeyUsage, x509.ExtKeyUsageClientAuth) {
120+
return
122121
}
123122
t.Error("cert has not ClientAuth usage as expected")
124123
}
125124

126125
// AssertCertificateHasServerAuthUsage is a utility function for kubeadm testing that asserts if a given certificate has
127126
// the expected ExtKeyUsageServerAuth
128127
func AssertCertificateHasServerAuthUsage(t *testing.T, cert *x509.Certificate) {
129-
for i := range cert.ExtKeyUsage {
130-
if cert.ExtKeyUsage[i] == x509.ExtKeyUsageServerAuth {
131-
return
132-
}
128+
if slices.Contains(cert.ExtKeyUsage, x509.ExtKeyUsageServerAuth) {
129+
return
133130
}
134131
t.Error("cert is not a ServerAuth")
135132
}
@@ -138,13 +135,7 @@ func AssertCertificateHasServerAuthUsage(t *testing.T, cert *x509.Certificate) {
138135
// the expected DNSNames
139136
func AssertCertificateHasDNSNames(t *testing.T, cert *x509.Certificate, DNSNames ...string) {
140137
for _, DNSName := range DNSNames {
141-
found := false
142-
for _, val := range cert.DNSNames {
143-
if val == DNSName {
144-
found = true
145-
break
146-
}
147-
}
138+
found := slices.Contains(cert.DNSNames, DNSName)
148139

149140
if !found {
150141
t.Errorf("cert does not contain DNSName %s", DNSName)

cmd/kubeadm/app/util/patches/patches.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"os"
2525
"path/filepath"
2626
"regexp"
27+
"slices"
2728
"strings"
2829
"sync"
2930

@@ -170,14 +171,7 @@ func (pm *PatchManager) ApplyPatchesToTarget(patchTarget *PatchTarget) error {
170171
var err error
171172
var patchedData []byte
172173

173-
var found bool
174-
for _, pt := range pm.knownTargets {
175-
if pt == patchTarget.Name {
176-
found = true
177-
break
178-
}
179-
}
180-
if !found {
174+
if !slices.Contains(pm.knownTargets, patchTarget.Name) {
181175
return errors.Errorf("unknown patch target name %q, must be one of %v", patchTarget.Name, pm.knownTargets)
182176
}
183177

cmd/kubeadm/app/util/pkiutil/pki_helpers.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"net"
3333
"os"
3434
"path/filepath"
35+
"slices"
3536
"time"
3637

3738
"github.com/pkg/errors"
@@ -127,12 +128,7 @@ func NewCSRAndKey(config *CertConfig) (*x509.CertificateRequest, crypto.Signer,
127128

128129
// HasServerAuth returns true if the given certificate is a ServerAuth
129130
func HasServerAuth(cert *x509.Certificate) bool {
130-
for i := range cert.ExtKeyUsage {
131-
if cert.ExtKeyUsage[i] == x509.ExtKeyUsageServerAuth {
132-
return true
133-
}
134-
}
135-
return false
131+
return slices.Contains(cert.ExtKeyUsage, x509.ExtKeyUsageServerAuth)
136132
}
137133

138134
// WriteCertAndKey stores certificate and key at the specified location

cmd/kubeadm/app/util/pkiutil/pki_helpers_test.go

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"os"
2828
"path/filepath"
2929
"reflect"
30+
"slices"
3031
"testing"
3132

3233
certutil "k8s.io/client-go/util/cert"
@@ -634,13 +635,7 @@ func TestGetAPIServerAltNames(t *testing.T) {
634635
}
635636

636637
for _, DNSName := range rt.expectedDNSNames {
637-
found := false
638-
for _, val := range altNames.DNSNames {
639-
if val == DNSName {
640-
found = true
641-
break
642-
}
643-
}
638+
found := slices.Contains(altNames.DNSNames, DNSName)
644639

645640
if !found {
646641
t.Errorf("%s: altNames does not contain DNSName %s but %v", rt.name, DNSName, altNames.DNSNames)
@@ -696,13 +691,7 @@ func TestGetEtcdAltNames(t *testing.T) {
696691
expectedDNSNames := []string{"myNode", "localhost", proxy}
697692
for _, DNSName := range expectedDNSNames {
698693
t.Run(DNSName, func(t *testing.T) {
699-
found := false
700-
for _, val := range altNames.DNSNames {
701-
if val == DNSName {
702-
found = true
703-
break
704-
}
705-
}
694+
found := slices.Contains(altNames.DNSNames, DNSName)
706695

707696
if !found {
708697
t.Errorf("altNames does not contain DNSName %s", DNSName)
@@ -758,13 +747,7 @@ func TestGetEtcdPeerAltNames(t *testing.T) {
758747
expectedDNSNames := []string{hostname, proxy}
759748
for _, DNSName := range expectedDNSNames {
760749
t.Run(DNSName, func(t *testing.T) {
761-
found := false
762-
for _, val := range altNames.DNSNames {
763-
if val == DNSName {
764-
found = true
765-
break
766-
}
767-
}
750+
found := slices.Contains(altNames.DNSNames, DNSName)
768751

769752
if !found {
770753
t.Errorf("altNames does not contain DNSName %s", DNSName)

cmd/kubeadm/app/util/runtime/runtime_test.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"net"
2222
"os"
2323
"runtime"
24+
"slices"
2425
"testing"
2526

2627
"github.com/stretchr/testify/assert"
@@ -447,12 +448,7 @@ func TestDetectCRISocketImpl(t *testing.T) {
447448
for _, test := range tests {
448449
t.Run(test.name, func(t *testing.T) {
449450
socket, err := detectCRISocketImpl(func(path string) bool {
450-
for _, existing := range test.existingSockets {
451-
if path == existing {
452-
return true
453-
}
454-
}
455-
return false
451+
return slices.Contains(test.existingSockets, path)
456452
}, test.existingSockets)
457453

458454
if (err != nil) != test.expectedError {

0 commit comments

Comments
 (0)