Skip to content

Commit 60bb3a5

Browse files
authored
lint: Integrate golangcilint (#68)
* lint: Integrate golangcilint Signed-off-by: Quique Llorente <[email protected]> * lint: Fix deadcode Signed-off-by: Quique Llorente <[email protected]> * lint: Exclude dupl from tests Signed-off-by: Quique Llorente <[email protected]> * lint: fix errcheck Signed-off-by: Quique Llorente <[email protected]> * lint: fix gochecknoinits Signed-off-by: Quique Llorente <[email protected]> * lint: Fix gocritic Signed-off-by: Quique Llorente <[email protected]> * lint: fix gocyclo Signed-off-by: Quique Llorente <[email protected]> * lint: fix gofmt Signed-off-by: Quique Llorente <[email protected]> * lint: fix gomnd Signed-off-by: Quique Llorente <[email protected]> * lint: fix gosec Signed-off-by: Quique Llorente <[email protected]> * lint: fix govet Signed-off-by: Quique Llorente <[email protected]> * lint: fix misspell Signed-off-by: Quique Llorente <[email protected]> * lint: fix stylecheck Signed-off-by: Quique Llorente <[email protected]> * lint: fix unconvert Signed-off-by: Quique Llorente <[email protected]> * lint: fix unparam Signed-off-by: Quique Llorente <[email protected]> * lint: fix whitespace Signed-off-by: Quique Llorente <[email protected]> * lint: fix lint for webhook server Signed-off-by: Quique Llorente <[email protected]> * lint: fix lll Signed-off-by: Quique Llorente <[email protected]> * lint: fix goheader Signed-off-by: Quique Llorente <[email protected]>
1 parent 22a7a67 commit 60bb3a5

28 files changed

+694
-311
lines changed

.github/workflows/main.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,22 @@ on:
55
pull_request:
66
branches: [ main ]
77
jobs:
8+
lint:
9+
name: lint
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Check out code
13+
uses: actions/checkout@v2
14+
- uses: arnested/go-version-action@v1
15+
id: go-version
16+
- name: Set up Go
17+
uses: actions/setup-go@v1
18+
with:
19+
go-version: ${{ steps.go-version.outputs.minimal }}
20+
- name: golangci-lint
21+
uses: golangci/golangci-lint-action@v2
22+
with:
23+
version: v1.42.1
824
build:
925
runs-on: ubuntu-latest
1026
steps:

.golangci.yml

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# Copyright 2021 The NMPolicy Authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
16+
17+
linters-settings:
18+
dupl:
19+
threshold: 100
20+
funlen:
21+
lines: 100
22+
statements: 50
23+
gci:
24+
local-prefixes: github.com/qinqon/kube-admisssion-webhook
25+
goconst:
26+
min-len: 2
27+
min-occurrences: 2
28+
gocritic:
29+
enabled-tags:
30+
- diagnostic
31+
- experimental
32+
- opinionated
33+
- performance
34+
- style
35+
disabled-checks:
36+
- dupImport # https://github.com/go-critic/go-critic/issues/845
37+
- ifElseChain
38+
- octalLiteral
39+
- whyNoLint
40+
- wrapperFunc
41+
gocyclo:
42+
min-complexity: 15
43+
goheader:
44+
values:
45+
regexp:
46+
year-regexp: 2\d\d\d
47+
template-path: header.tpl
48+
goimports:
49+
local-prefixes: github.com/qinqon/kube-admisssion-webhook
50+
gomnd:
51+
settings:
52+
mnd:
53+
# don't include the "operation" and "assign"
54+
checks: argument,case,condition,return
55+
govet:
56+
check-shadowing: true
57+
lll:
58+
line-length: 140
59+
maligned:
60+
suggest-new: true
61+
misspell:
62+
locale: US
63+
nolintlint:
64+
allow-leading-space: true # don't require machine-readable nolint directives (i.e. with no leading space)
65+
allow-unused: false # report any unused nolint directives
66+
require-explanation: false # don't require an explanation for nolint directives
67+
require-specific: false # don't require nolint directives to be specific about which linter is being skipped
68+
69+
issues:
70+
exclude-rules:
71+
- path: _test.go
72+
linters:
73+
- dupl
74+
75+
linters:
76+
disable-all: true
77+
enable:
78+
- bodyclose
79+
- deadcode
80+
- depguard
81+
- dogsled
82+
- dupl
83+
- errcheck
84+
- exportloopref
85+
- exhaustive
86+
- funlen
87+
- gochecknoinits
88+
- goconst
89+
- gocritic
90+
- gocyclo
91+
- gofmt
92+
- goheader
93+
- goimports
94+
- gomnd
95+
- goprintffuncname
96+
- gosec
97+
- gosimple
98+
- govet
99+
- ineffassign
100+
- lll
101+
- misspell
102+
- nakedret
103+
- noctx
104+
- nolintlint
105+
- rowserrcheck
106+
- staticcheck
107+
- structcheck
108+
- stylecheck
109+
- typecheck
110+
- unconvert
111+
- unparam
112+
- unused
113+
- varcheck
114+
- whitespace

Makefile

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,8 @@ WHAT ?= ./pkg/...
44

55
all: test
66

7-
format:
8-
hack/whitespace.sh format
9-
gofmt -w ./pkg
10-
11-
vet:
12-
go vet ./pkg/...
7+
lint:
8+
hack/lint.sh
139

1410
testenv:
1511
hack/setup-testenv.sh

hack/lint.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash -xe
2+
3+
golangci_lint_version=v1.42.1
4+
if [ ! -f $(go env GOPATH)/bin/golangci-lint ]; then
5+
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin $golangci_lint_version
6+
fi
7+
golangci-lint run
8+

header.tpl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
* Copyright {{ year-regexp }} Kube Admission Webhook Authors.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at:
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.

pkg/certificate/certificate_suite_test.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright 2022 Kube Admission Webhook Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at:
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package certificate
218

319
import (
@@ -105,6 +121,8 @@ func deleteResources() {
105121

106122
var _ = BeforeSuite(func() {
107123

124+
klog.InitFlags(nil)
125+
108126
testEnv = &envtest.Environment{
109127
UseExistingCluster: &useCluster,
110128
}
@@ -116,7 +134,7 @@ var _ = BeforeSuite(func() {
116134
Expect(err).ToNot(HaveOccurred(), "should success creating client")
117135

118136
// Ideally we create/delete the namespace at every test but, envtest
119-
// cannot delete namespaces [1] so we just create it at the beggining
137+
// cannot delete namespaces [1] so we just create it at the beginning
120138
// of the test suite.
121139
//
122140
// [1] https://book.kubebuilder.io/reference/testing/envtest.html?highlight=envtest#testing-considerations
@@ -137,10 +155,6 @@ var _ = AfterSuite(func() {
137155
Expect(err).ToNot(HaveOccurred(), "should success stopping testenv")
138156
})
139157

140-
func init() {
141-
klog.InitFlags(nil)
142-
}
143-
144158
func TestCertificate(t *testing.T) {
145159
RegisterFailHandler(Fail)
146160
junitReporter := reporters.NewJUnitReporter("junit.certificate_suite_test.xml")

pkg/certificate/cleanup.go

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright 2022 Kube Admission Webhook Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at:
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package certificate
218

319
import (
@@ -36,13 +52,13 @@ func (m *Manager) earliestElapsedForServiceCertsCleanup() (time.Duration, error)
3652
}
3753

3854
elapsedTimesForCleanup := []time.Duration{}
39-
for service, _ := range services {
40-
55+
for service := range services {
4156
certs, err := m.getTLSCerts(service)
4257
if err != nil {
4358
return time.Duration(0), fmt.Errorf("failed getting TLS keypair from service %s to calculate cleanup next run: %w", service, err)
4459
}
45-
elapsedTimeForCleanup, err := m.earliestElapsedForCleanup(m.log.WithName("earliestElapsedForServiceCertsCleanup").WithValues("service", service), certs)
60+
elapsedTimeForCleanup, err := m.earliestElapsedForCleanup(
61+
m.log.WithName("earliestElapsedForServiceCertsCleanup").WithValues("service", service), certs)
4662
if err != nil {
4763
return time.Duration(0), err
4864
}
@@ -84,7 +100,7 @@ func (m *Manager) earliestCleanupDeadlineForCerts(certificates []*x509.Certifica
84100

85101
func (m *Manager) cleanUpCABundle() error {
86102
m.log.Info("cleanUpCABundle")
87-
_, err := m.updateWebhookCABundleWithFunc(func([]byte) ([]byte, error) {
103+
err := m.updateWebhookCABundleWithFunc(func([]byte) ([]byte, error) {
88104
cas, err := m.getCACertsFromCABundle()
89105
if err != nil {
90106
return nil, errors.Wrap(err, "failed getting ca certs to start cleanup")
@@ -112,23 +128,27 @@ func (m *Manager) cleanUpServiceCerts() error {
112128
return fmt.Errorf("failed getting services to do the cleanup: %w", err)
113129
}
114130

115-
for service, _ := range services {
116-
m.applySecret(service, corev1.SecretTypeTLS, nil, func(secret corev1.Secret, keyPair *triple.KeyPair) (*corev1.Secret, error) {
117-
certPEM, found := secret.Data[corev1.TLSCertKey]
118-
if !found {
119-
return nil, errors.Wrapf(err, "TLS cert not found at secret %s to clean up ", service)
120-
}
121-
122-
certs, err := triple.ParseCertsPEM(certPEM)
123-
if err != nil {
124-
return nil, errors.Wrapf(err, "failed parsing TLS cert PEM at secret %s to clean up", service)
125-
}
126-
127-
cleanedCerts := m.cleanUpCertificates(certs)
128-
pem := triple.EncodeCertsPEM(cleanedCerts)
129-
secret.Data[corev1.TLSCertKey] = pem
130-
return &secret, nil
131-
})
131+
for service := range services {
132+
applyErr := m.applySecret(service, corev1.SecretTypeTLS, nil,
133+
func(secret *corev1.Secret, keyPair *triple.KeyPair) (*corev1.Secret, error) {
134+
certPEM, found := secret.Data[corev1.TLSCertKey]
135+
if !found {
136+
return nil, errors.Wrapf(err, "TLS cert not found at secret %s to clean up ", service)
137+
}
138+
139+
certs, err := triple.ParseCertsPEM(certPEM)
140+
if err != nil {
141+
return nil, errors.Wrapf(err, "failed parsing TLS cert PEM at secret %s to clean up", service)
142+
}
143+
144+
cleanedCerts := m.cleanUpCertificates(certs)
145+
pem := triple.EncodeCertsPEM(cleanedCerts)
146+
secret.Data[corev1.TLSCertKey] = pem
147+
return secret, nil
148+
})
149+
if applyErr != nil {
150+
return applyErr
151+
}
132152
}
133153
return nil
134154
}

pkg/certificate/cleanup_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright 2022 Kube Admission Webhook Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at:
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package certificate
218

319
import (

pkg/certificate/client.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright 2022 Kube Admission Webhook Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at:
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package certificate
218

319
import (
@@ -10,12 +26,17 @@ import (
1026
"sigs.k8s.io/controller-runtime/pkg/client"
1127
)
1228

29+
const (
30+
pollInterval = 5 * time.Second
31+
pollTimeout = 30 * time.Second
32+
)
33+
1334
// get wraps controller-runtime client `Get` to ensure that client cache
1435
// is ready, sometimes after controller-runtime manager is ready the
1536
// cache is still not ready, specially if you webhook or plain runnable
1637
// is being used since it miss some controller bits.
1738
func (m *Manager) get(key types.NamespacedName, value client.Object) error {
18-
return wait.PollImmediate(5*time.Second, 30*time.Second, func() (bool, error) {
39+
return wait.PollImmediate(pollInterval, pollTimeout, func() (bool, error) {
1940
err := m.client.Get(context.TODO(), key, value)
2041
if err != nil {
2142
if _, cacheNotStarted := err.(*cache.ErrCacheNotStarted); cacheNotStarted {

0 commit comments

Comments
 (0)