Skip to content

Commit c98b7fd

Browse files
authored
Merge pull request #2191 from vincepri/component-config-readd
🌱 Re-add tests for component config
2 parents 9f4e5ed + c0a72ba commit c98b7fd

File tree

14 files changed

+495
-121
lines changed

14 files changed

+495
-121
lines changed

.golangci.yml

Lines changed: 103 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,44 @@
11
linters:
22
disable-all: true
33
enable:
4-
- asasalint
5-
- asciicheck
6-
- bidichk
7-
- bodyclose
8-
- depguard
9-
- dogsled
10-
- dupl
11-
- errcheck
12-
- errchkjson
13-
- errorlint
14-
- exhaustive
15-
- exportloopref
16-
- goconst
17-
- gocritic
18-
- gocyclo
19-
- gofmt
20-
- goimports
21-
- goprintffuncname
22-
- gosec
23-
- gosimple
24-
- govet
25-
- importas
26-
- ineffassign
27-
- makezero
28-
- misspell
29-
- nakedret
30-
- nilerr
31-
- nolintlint
32-
- prealloc
33-
- revive
34-
- staticcheck
35-
- stylecheck
36-
- tagliatelle
37-
- typecheck
38-
- unconvert
39-
- unparam
40-
- unused
41-
- whitespace
4+
- asasalint
5+
- asciicheck
6+
- bidichk
7+
- bodyclose
8+
- depguard
9+
- dogsled
10+
- dupl
11+
- errcheck
12+
- errchkjson
13+
- errorlint
14+
- exhaustive
15+
- exportloopref
16+
- goconst
17+
- gocritic
18+
- gocyclo
19+
- gofmt
20+
- goimports
21+
- goprintffuncname
22+
- gosec
23+
- gosimple
24+
- govet
25+
- importas
26+
- ineffassign
27+
- makezero
28+
- misspell
29+
- nakedret
30+
- nilerr
31+
- nolintlint
32+
- prealloc
33+
- revive
34+
- staticcheck
35+
- stylecheck
36+
- tagliatelle
37+
- typecheck
38+
- unconvert
39+
- unparam
40+
- unused
41+
- whitespace
4242

4343
linters-settings:
4444
importas:
@@ -75,71 +75,74 @@ issues:
7575
exclude-use-default: false
7676
# List of regexps of issue texts to exclude, empty list by default.
7777
exclude:
78-
# The following are being worked on to remove their exclusion. This list should be reduced or go away all together over time.
79-
# If it is decided they will not be addressed they should be moved above this comment.
80-
- Subprocess launch(ed with variable|ing should be audited)
81-
- (G204|G104|G307)
82-
- "ST1000: at least one file in a package should have a package comment"
78+
# The following are being worked on to remove their exclusion. This list should be reduced or go away all together over time.
79+
# If it is decided they will not be addressed they should be moved above this comment.
80+
- Subprocess launch(ed with variable|ing should be audited)
81+
- (G204|G104|G307)
82+
- "ST1000: at least one file in a package should have a package comment"
8383
exclude-rules:
84-
- linters:
85-
- gosec
86-
text: "G108: Profiling endpoint is automatically exposed on /debug/pprof"
87-
- linters:
88-
- revive
89-
text: "exported: exported method .*\\.(Reconcile|SetupWithManager|SetupWebhookWithManager) should have comment or be unexported"
90-
- linters:
91-
- errcheck
92-
text: Error return value of .((os\.)?std(out|err)\..*|.*Close|.*Flush|os\.Remove(All)?|.*print(f|ln)?|os\.(Un)?Setenv). is not checked
93-
# With Go 1.16, the new embed directive can be used with an un-named import,
94-
# revive (previously, golint) only allows these to be imported in a main.go, which wouldn't work for us.
95-
# This directive allows the embed package to be imported with an underscore everywhere.
96-
- linters:
97-
- revive
98-
source: _ "embed"
99-
# Exclude some packages or code to require comments, for example test code, or fake clients.
100-
- linters:
101-
- revive
102-
text: exported (method|function|type|const) (.+) should have comment or be unexported
103-
source: (func|type).*Fake.*
104-
- linters:
105-
- revive
106-
text: exported (method|function|type|const) (.+) should have comment or be unexported
107-
path: fake_\.go
108-
# Disable unparam "always receives" which might not be really
109-
# useful when building libraries.
110-
- linters:
111-
- unparam
112-
text: always receives
113-
# Dot imports for gomega and ginkgo are allowed
114-
# within test files.
115-
- path: _test\.go
116-
text: should not use dot imports
117-
- path: _test\.go
118-
text: cyclomatic complexity
119-
- path: _test\.go
120-
text: "G107: Potential HTTP request made with variable url"
121-
# Append should be able to assign to a different var/slice.
122-
- linters:
123-
- gocritic
124-
text: "appendAssign: append result not assigned to the same slice"
125-
- linters:
126-
- gocritic
127-
text: "singleCaseSwitch: should rewrite switch statement to if statement"
128-
# It considers all file access to a filename that comes from a variable problematic,
129-
# which is naiv at best.
130-
- linters:
131-
- gosec
132-
text: "G304: Potential file inclusion via variable"
133-
- linters:
134-
- revive
135-
text: "package-comments: should have a package comment"
136-
- linters:
137-
- dupl
138-
path: _test\.go
84+
- linters:
85+
- gosec
86+
text: "G108: Profiling endpoint is automatically exposed on /debug/pprof"
87+
- linters:
88+
- revive
89+
text: "exported: exported method .*\\.(Reconcile|SetupWithManager|SetupWebhookWithManager) should have comment or be unexported"
90+
- linters:
91+
- errcheck
92+
text: Error return value of .((os\.)?std(out|err)\..*|.*Close|.*Flush|os\.Remove(All)?|.*print(f|ln)?|os\.(Un)?Setenv). is not checked
93+
- linters:
94+
- staticcheck
95+
text: "SA1019: .* The component config package has been deprecated and will be removed in a future release."
96+
# With Go 1.16, the new embed directive can be used with an un-named import,
97+
# revive (previously, golint) only allows these to be imported in a main.go, which wouldn't work for us.
98+
# This directive allows the embed package to be imported with an underscore everywhere.
99+
- linters:
100+
- revive
101+
source: _ "embed"
102+
# Exclude some packages or code to require comments, for example test code, or fake clients.
103+
- linters:
104+
- revive
105+
text: exported (method|function|type|const) (.+) should have comment or be unexported
106+
source: (func|type).*Fake.*
107+
- linters:
108+
- revive
109+
text: exported (method|function|type|const) (.+) should have comment or be unexported
110+
path: fake_\.go
111+
# Disable unparam "always receives" which might not be really
112+
# useful when building libraries.
113+
- linters:
114+
- unparam
115+
text: always receives
116+
# Dot imports for gomega and ginkgo are allowed
117+
# within test files.
118+
- path: _test\.go
119+
text: should not use dot imports
120+
- path: _test\.go
121+
text: cyclomatic complexity
122+
- path: _test\.go
123+
text: "G107: Potential HTTP request made with variable url"
124+
# Append should be able to assign to a different var/slice.
125+
- linters:
126+
- gocritic
127+
text: "appendAssign: append result not assigned to the same slice"
128+
- linters:
129+
- gocritic
130+
text: "singleCaseSwitch: should rewrite switch statement to if statement"
131+
# It considers all file access to a filename that comes from a variable problematic,
132+
# which is naiv at best.
133+
- linters:
134+
- gosec
135+
text: "G304: Potential file inclusion via variable"
136+
- linters:
137+
- revive
138+
text: "package-comments: should have a package comment"
139+
- linters:
140+
- dupl
141+
path: _test\.go
139142

140143
run:
141144
timeout: 10m
142145
skip-files:
143-
- "zz_generated.*\\.go$"
144-
- ".*conversion.*\\.go$"
146+
- "zz_generated.*\\.go$"
147+
- ".*conversion.*\\.go$"
145148
allow-parallel-runners: true

alias.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ var (
101101
// the manager.
102102
//
103103
// Deprecated: This is deprecated in favor of using Options directly.
104-
ConfigFile = cfg.File //nolint:staticcheck
104+
ConfigFile = cfg.File
105105

106106
// NewControllerManagedBy returns a new controller builder that will be started by the provided Manager.
107107
NewControllerManagedBy = builder.ControllerManagedBy

pkg/config/config.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,24 @@ import (
2424
"k8s.io/apimachinery/pkg/runtime"
2525
"k8s.io/apimachinery/pkg/runtime/serializer"
2626
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
27-
"sigs.k8s.io/controller-runtime/pkg/config/v1alpha1" //nolint:staticcheck
27+
"sigs.k8s.io/controller-runtime/pkg/config/v1alpha1"
2828
)
2929

3030
// ControllerManagerConfiguration defines the functions necessary to parse a config file
3131
// and to configure the Options struct for the ctrl.Manager.
3232
//
33-
// Deprecated: This package has been deprecated and will be removed in a future release.
33+
// Deprecated: The component config package has been deprecated and will be removed in a future release. Users should migrate to their own config implementation, please share feedback in https://github.com/kubernetes-sigs/controller-runtime/issues/895.
3434
type ControllerManagerConfiguration interface {
3535
runtime.Object
3636

3737
// Complete returns the versioned configuration
38-
Complete() (v1alpha1.ControllerManagerConfigurationSpec, error) //nolint:staticcheck
38+
Complete() (v1alpha1.ControllerManagerConfigurationSpec, error)
3939
}
4040

4141
// DeferredFileLoader is used to configure the decoder for loading controller
4242
// runtime component config types.
4343
//
44-
// Deprecated: This package has been deprecated and will be removed in a future release.
44+
// Deprecated: The component config package has been deprecated and will be removed in a future release. Users should migrate to their own config implementation, please share feedback in https://github.com/kubernetes-sigs/controller-runtime/issues/895.
4545
type DeferredFileLoader struct {
4646
ControllerManagerConfiguration
4747
path string
@@ -57,7 +57,7 @@ type DeferredFileLoader struct {
5757
// * Path: "./config.yaml"
5858
// * Kind: GenericControllerManagerConfiguration
5959
//
60-
// Deprecated: This package has been deprecated and will be removed in a future release.
60+
// Deprecated: The component config package has been deprecated and will be removed in a future release. Users should migrate to their own config implementation, please share feedback in https://github.com/kubernetes-sigs/controller-runtime/issues/895.
6161
func File() *DeferredFileLoader {
6262
scheme := runtime.NewScheme()
6363
utilruntime.Must(v1alpha1.AddToScheme(scheme))
@@ -69,10 +69,10 @@ func File() *DeferredFileLoader {
6969
}
7070

7171
// Complete will use sync.Once to set the scheme.
72-
func (d *DeferredFileLoader) Complete() (v1alpha1.ControllerManagerConfigurationSpec, error) { //nolint:staticcheck
72+
func (d *DeferredFileLoader) Complete() (v1alpha1.ControllerManagerConfigurationSpec, error) {
7373
d.once.Do(d.loadFile)
7474
if d.err != nil {
75-
return v1alpha1.ControllerManagerConfigurationSpec{}, d.err //nolint:staticcheck
75+
return v1alpha1.ControllerManagerConfigurationSpec{}, d.err
7676
}
7777
return d.ControllerManagerConfiguration.Complete()
7878
}

pkg/config/config_suite_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
Copyright 2018 The Kubernetes Authors.
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+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package config_test
15+
16+
import (
17+
"testing"
18+
19+
. "github.com/onsi/ginkgo/v2"
20+
. "github.com/onsi/gomega"
21+
)
22+
23+
func TestScheme(t *testing.T) {
24+
RegisterFailHandler(Fail)
25+
RunSpecs(t, "Config Suite")
26+
}

pkg/config/config_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
Copyright 2020 The Kubernetes Authors.
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+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package config_test
15+
16+
import (
17+
. "github.com/onsi/ginkgo/v2"
18+
. "github.com/onsi/gomega"
19+
"sigs.k8s.io/controller-runtime/pkg/config"
20+
"sigs.k8s.io/controller-runtime/pkg/config/v1alpha1"
21+
)
22+
23+
var _ = Describe("config", func() {
24+
Describe("File", func() {
25+
26+
It("should error loading from non existent file", func() {
27+
loader := config.File()
28+
_, err := loader.Complete()
29+
Expect(err).ToNot(BeNil())
30+
})
31+
32+
It("should load a config from file", func() {
33+
conf := v1alpha1.ControllerManagerConfiguration{}
34+
loader := config.File().AtPath("./testdata/config.yaml").OfKind(&conf)
35+
Expect(conf.CacheNamespace).To(Equal(""))
36+
37+
_, err := loader.Complete()
38+
Expect(err).To(BeNil())
39+
40+
Expect(*conf.LeaderElection.LeaderElect).To(Equal(true))
41+
Expect(conf.CacheNamespace).To(Equal("default"))
42+
Expect(conf.Metrics.BindAddress).To(Equal(":8081"))
43+
})
44+
})
45+
})

0 commit comments

Comments
 (0)