Skip to content

Commit b131b4b

Browse files
authored
Merge pull request kubernetes#81947 from RainbowMango/pr_cleanup_staticcheck_for_client_p1
Cleanup client-go static analysis issues-phase 1
2 parents 7d4d175 + c8c055b commit b131b4b

File tree

9 files changed

+15
-30
lines changed

9 files changed

+15
-30
lines changed

hack/.staticcheck_failures

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,14 +213,9 @@ vendor/k8s.io/client-go/metadata/fake
213213
vendor/k8s.io/client-go/rest
214214
vendor/k8s.io/client-go/rest/watch
215215
vendor/k8s.io/client-go/restmapper
216-
vendor/k8s.io/client-go/testing
217216
vendor/k8s.io/client-go/tools/cache
218-
vendor/k8s.io/client-go/tools/clientcmd
219217
vendor/k8s.io/client-go/tools/leaderelection
220-
vendor/k8s.io/client-go/tools/portforward
221-
vendor/k8s.io/client-go/tools/watch
222218
vendor/k8s.io/client-go/transport
223-
vendor/k8s.io/client-go/util/jsonpath
224219
vendor/k8s.io/cloud-provider/service/helpers
225220
vendor/k8s.io/code-generator/cmd/client-gen/generators/fake
226221
vendor/k8s.io/code-generator/cmd/client-gen/generators/util

staging/src/k8s.io/client-go/testing/actions.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,8 +439,8 @@ func (a ActionImpl) GetSubresource() string {
439439
return a.Subresource
440440
}
441441
func (a ActionImpl) Matches(verb, resource string) bool {
442-
return strings.ToLower(verb) == strings.ToLower(a.Verb) &&
443-
strings.ToLower(resource) == strings.ToLower(a.Resource.Resource)
442+
return strings.EqualFold(verb, a.Verb) &&
443+
strings.EqualFold(resource, a.Resource.Resource)
444444
}
445445
func (a ActionImpl) DeepCopy() Action {
446446
ret := a

staging/src/k8s.io/client-go/testing/fixture_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,8 @@ func TestWatchCallMultipleInvocation(t *testing.T) {
204204
event := <-w.ResultChan()
205205
accessor, err := meta.Accessor(event.Object)
206206
if err != nil {
207-
t.Fatalf("unexpected error: %v", err)
207+
t.Errorf("unexpected error: %v", err)
208+
break
208209
}
209210
assert.Equal(t, c.op, event.Type, "watch event mismatched")
210211
assert.Equal(t, c.name, accessor.GetName(), "watched object mismatch")

staging/src/k8s.io/client-go/tools/clientcmd/client_config_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,6 @@ func TestCreateCleanDefaultCluster(t *testing.T) {
458458
}
459459

460460
func TestCreateMissingContextNoDefault(t *testing.T) {
461-
const expectedErrorContains = "Context was not found for specified context"
462461
config := createValidTestConfig()
463462
clientBuilder := NewNonInteractiveClientConfig(*config, "not-present", &ConfigOverrides{}, nil)
464463

staging/src/k8s.io/client-go/tools/clientcmd/merged_client_builder_test.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,6 @@ import (
2424
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
2525
)
2626

27-
type testLoader struct {
28-
ClientConfigLoader
29-
30-
called bool
31-
config *clientcmdapi.Config
32-
err error
33-
}
34-
35-
func (l *testLoader) Load() (*clientcmdapi.Config, error) {
36-
l.called = true
37-
return l.config, l.err
38-
}
39-
4027
type testClientConfig struct {
4128
rawconfig *clientcmdapi.Config
4229
config *restclient.Config

staging/src/k8s.io/client-go/tools/clientcmd/validation.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,10 @@ func validateClusterInfo(clusterName string, clusterInfo clientcmdapi.Cluster) [
185185
}
186186
if len(clusterInfo.CertificateAuthority) != 0 {
187187
clientCertCA, err := os.Open(clusterInfo.CertificateAuthority)
188-
defer clientCertCA.Close()
189188
if err != nil {
190189
validationErrors = append(validationErrors, fmt.Errorf("unable to read certificate-authority %v for %v due to %v", clusterInfo.CertificateAuthority, clusterName, err))
190+
} else {
191+
defer clientCertCA.Close()
191192
}
192193
}
193194

@@ -223,16 +224,18 @@ func validateAuthInfo(authInfoName string, authInfo clientcmdapi.AuthInfo) []err
223224

224225
if len(authInfo.ClientCertificate) != 0 {
225226
clientCertFile, err := os.Open(authInfo.ClientCertificate)
226-
defer clientCertFile.Close()
227227
if err != nil {
228228
validationErrors = append(validationErrors, fmt.Errorf("unable to read client-cert %v for %v due to %v", authInfo.ClientCertificate, authInfoName, err))
229+
} else {
230+
defer clientCertFile.Close()
229231
}
230232
}
231233
if len(authInfo.ClientKey) != 0 {
232234
clientKeyFile, err := os.Open(authInfo.ClientKey)
233-
defer clientKeyFile.Close()
234235
if err != nil {
235236
validationErrors = append(validationErrors, fmt.Errorf("unable to read client-key %v for %v due to %v", authInfo.ClientKey, authInfoName, err))
237+
} else {
238+
defer clientKeyFile.Close()
236239
}
237240
}
238241
}

staging/src/k8s.io/client-go/tools/portforward/portforward_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,9 @@ func TestGetPortsReturnsDynamicallyAssignedLocalPort(t *testing.T) {
376376
<-pf.Ready
377377

378378
ports, err := pf.GetPorts()
379+
if err != nil {
380+
t.Fatalf("Failed to get ports. error: %v", err)
381+
}
379382

380383
if len(ports) != 1 {
381384
t.Fatalf("expected 1 port, got %d", len(ports))

staging/src/k8s.io/client-go/tools/watch/until_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import (
3535
)
3636

3737
type fakePod struct {
38-
name string
3938
}
4039

4140
func (obj *fakePod) GetObjectKind() schema.ObjectKind { return schema.EmptyObjectKind }

staging/src/k8s.io/client-go/util/jsonpath/parser.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ type Parser struct {
3737
Name string
3838
Root *ListNode
3939
input string
40-
cur *ListNode
4140
pos int
4241
start int
4342
width int
@@ -186,8 +185,7 @@ func (p *Parser) parseInsideAction(cur *ListNode) error {
186185
func (p *Parser) parseRightDelim(cur *ListNode) error {
187186
p.pos += len(rightDelim)
188187
p.consumeText()
189-
cur = p.Root
190-
return p.parseText(cur)
188+
return p.parseText(p.Root)
191189
}
192190

193191
// parseIdentifier scans build-in keywords, like "range" "end"
@@ -231,7 +229,7 @@ func (p *Parser) parseRecursive(cur *ListNode) error {
231229
func (p *Parser) parseNumber(cur *ListNode) error {
232230
r := p.peek()
233231
if r == '+' || r == '-' {
234-
r = p.next()
232+
p.next()
235233
}
236234
for {
237235
r = p.next()

0 commit comments

Comments
 (0)