Skip to content

Commit 1573e59

Browse files
mdboothmandre
authored andcommitted
openshift: Add make verify and test
Also fix lint issues hightlighted by these tests.
1 parent b907b20 commit 1573e59

File tree

5 files changed

+239
-27
lines changed

5 files changed

+239
-27
lines changed

openshift/.golangci.yml

+187
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
linters:
2+
disable-all: true
3+
enable:
4+
- asasalint
5+
- asciicheck
6+
- bidichk
7+
- bodyclose
8+
- cyclop
9+
# - depguard
10+
- dogsled
11+
- dupword
12+
- durationcheck
13+
- errcheck
14+
- exportloopref
15+
- forbidigo
16+
- gci
17+
- goconst
18+
- gocritic
19+
- gocyclo
20+
- godot
21+
- gofmt
22+
- gofumpt
23+
- goheader
24+
# - goimports Conflicts with gci
25+
- gomodguard
26+
- goprintffuncname
27+
- gosec
28+
- gosimple
29+
- govet
30+
- importas
31+
- ineffassign
32+
- makezero
33+
- misspell
34+
- nakedret
35+
- nestif
36+
- nilerr
37+
- noctx
38+
- nolintlint
39+
- prealloc
40+
- predeclared
41+
- revive
42+
- rowserrcheck
43+
- sqlclosecheck
44+
- staticcheck
45+
- stylecheck
46+
- thelper
47+
- typecheck
48+
- unconvert
49+
- unparam
50+
- unused
51+
- wastedassign
52+
- whitespace
53+
54+
linters-settings:
55+
cyclop:
56+
# TODO(sbuerin) fix remaining findings and set to 20 afterwards
57+
max-complexity: 30
58+
gci:
59+
sections:
60+
- standard
61+
- default
62+
- prefix(sigs.k8s.io/cluster-api-provider-openstack)
63+
- prefix(github.com/openshift/cluster-api-provider-openstack/openshift)
64+
skip-vendor: true
65+
gocritic:
66+
enabled-tags:
67+
- diagnostic
68+
- experimental
69+
- performance
70+
disabled-checks:
71+
- appendAssign
72+
- dupImport # https://github.com/go-critic/go-critic/issues/845
73+
- evalOrder
74+
- ifElseChain
75+
- octalLiteral
76+
- regexpSimplify
77+
- sloppyReassign
78+
- truncateCmp
79+
- typeDefFirst
80+
- unnamedResult
81+
- unnecessaryDefer
82+
- whyNoLint
83+
- wrapperFunc
84+
- rangeValCopy
85+
- hugeParam
86+
importas:
87+
no-unaliased: true
88+
alias:
89+
# Kubernetes
90+
- pkg: k8s.io/api/core/v1
91+
alias: corev1
92+
- pkg: k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1
93+
alias: apiextensionsv1
94+
- pkg: k8s.io/apimachinery/pkg/apis/meta/v1
95+
alias: metav1
96+
- pkg: k8s.io/apimachinery/pkg/api/errors
97+
alias: apierrors
98+
- pkg: k8s.io/apimachinery/pkg/util/errors
99+
alias: kerrors
100+
# Controller Runtime
101+
- pkg: sigs.k8s.io/controller-runtime
102+
alias: ctrl
103+
# CAPO
104+
- pkg: sigs.k8s.io/cluster-api-provider-openstack/api/v1alpha5
105+
alias: infrav1alpha5
106+
- pkg: sigs.k8s.io/cluster-api-provider-openstack/api/v1alpha6
107+
alias: infrav1alpha6
108+
- pkg: sigs.k8s.io/cluster-api-provider-openstack/api/v1alpha7
109+
alias: infrav1
110+
- pkg: sigs.k8s.io/cluster-api-provider-openstack/pkg/utils/errors
111+
alias: capoerrors
112+
# CAPI
113+
- pkg: sigs.k8s.io/cluster-api/api/v1alpha3
114+
alias: clusterv1alpha3
115+
- pkg: sigs.k8s.io/cluster-api/api/v1alpha4
116+
alias: clusterv1alpha4
117+
- pkg: sigs.k8s.io/cluster-api/api/v1beta1
118+
alias: clusterv1
119+
# CABPK
120+
- pkg: sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1alpha3
121+
alias: bootstrapv1alpha3
122+
- pkg: sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1alpha4
123+
alias: bootstrapv1alpha4
124+
- pkg: sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1
125+
alias: bootstrapv1
126+
# KCP
127+
- pkg: sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1alpha3
128+
alias: controlplanev1alpha3
129+
- pkg: sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1alpha4
130+
alias: controlplanev1alpha4
131+
- pkg: sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1beta1
132+
alias: controlplanev1
133+
134+
nolintlint:
135+
# https://github.com/golangci/golangci-lint/issues/3228
136+
allow-unused: true
137+
staticcheck:
138+
go: "1.17"
139+
stylecheck:
140+
go: "1.17"
141+
nestif:
142+
# minimal complexity of if statements to report, 5 by default
143+
# TODO(sbuerin) fix remaining findings and set to 5 after:
144+
# https://github.com/kubernetes-sigs/cluster-api-provider-openstack/pull/778
145+
min-complexity: 13
146+
147+
issues:
148+
max-same-issues: 0
149+
max-issues-per-linter: 0
150+
# List of regexps of issue texts to exclude, empty list by default.
151+
exclude-rules:
152+
- linters:
153+
- gosec
154+
text: "G108: Profiling endpoint is automatically exposed on /debug/pprof"
155+
- linters:
156+
- gosec
157+
text: "G108: Profiling endpoint is automatically exposed on /debug/pprof"
158+
# This directive allows the embed package to be imported with an underscore everywhere.
159+
- linters:
160+
- revive
161+
source: _ "embed"
162+
- linters:
163+
- revive
164+
- stylecheck
165+
path: (test)/.*.go
166+
text: should not use dot imports
167+
- linters:
168+
- revive
169+
path: test/e2e/shared/defaults.go
170+
text: "exported: exported const .* should have comment \\(or a comment on this block\\) or be unexported"
171+
- linters:
172+
- revive
173+
text: "var-naming: don't use underscores in Go names;"
174+
path: .*(api|types)\/.*\/.*conversion.*\.go$
175+
- linters:
176+
- stylecheck
177+
text: "ST1003: should not use underscores in Go names;"
178+
path: .*(api|types)\/.*\/.*conversion.*\.go$
179+
180+
run:
181+
timeout: 10m
182+
build-tags:
183+
- e2e
184+
185+
skip-files:
186+
- "zz_generated.*\\.go$"
187+
allow-parallel-runners: true

openshift/Makefile

+31-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ manifests_prefix ?= 0000_30_cluster-api-provider-openstack_
1818
TOOLS_DIR=../hack/tools
1919
KUSTOMIZE=$(TOOLS_DIR)/bin/kustomize
2020
CONTROLLER_GEN=$(TOOLS_DIR)/bin/controller-gen
21+
GOLANGCI_LINT=$(TOOLS_DIR)/bin/golangci-lint
2122

2223
define manifest_name
2324
$(addsuffix ".yaml",$(addprefix $(manifests_dir)/$(manifests_prefix),$(1)))
@@ -27,8 +28,8 @@ manifest_names = 00_credentials-request 04_infrastructure-components
2728
infrastructure_components = kustomize/cluster-capi-configmap/infrastructure-components.yaml
2829
infracluster_role = kustomize/infracluster-controller/role.yaml
2930

30-
.PHONY: all_manifests
31-
all_manifests: $(foreach m,$(manifest_names),$(call manifest_name,$(m)))
31+
.PHONY: generate
32+
generate: $(foreach m,$(manifest_names),$(call manifest_name,$(m)))
3233

3334
$(call manifest_name,00_credentials-request): $(KUSTOMIZE) ALWAYS | $(manifests_dir)
3435
$(KUSTOMIZE) build kustomize/credentials-request > $@
@@ -40,7 +41,7 @@ $(call manifest_name,04_infrastructure-components): $(KUSTOMIZE) $(infrastructur
4041
$(KUSTOMIZE) build kustomize/cluster-capi-configmap > $@
4142

4243
$(infracluster_role): $(CONTROLLER_GEN) ALWAYS
43-
$(CONTROLLER_GEN) rbac:roleName=infracluster-controller paths=./pkg/infracluster_controller output:stdout > $@
44+
$(CONTROLLER_GEN) rbac:roleName=infracluster-controller paths=./pkg/infraclustercontroller output:stdout > $@
4445

4546
$(manifests_dir):
4647
mkdir -p $@
@@ -51,5 +52,32 @@ $(KUSTOMIZE):
5152
$(CONTROLLER_GEN):
5253
$(MAKE) -C $(TOOLS_DIR) bin/controller-gen
5354

55+
$(GOLANGCI_LINT):
56+
$(MAKE) -C $(TOOLS_DIR) bin/golangci-lint
57+
58+
.PHONY: verify
59+
verify: lint modules generate
60+
@if !(git diff --quiet HEAD); then \
61+
git diff; \
62+
echo "generated files are out of date, run make generate"; exit 1; \
63+
fi
64+
65+
.PHONY: lint
66+
lint: $(GOLANGCI_LINT) ## Lint codebase
67+
$(GOLANGCI_LINT) run -v --fast=false
68+
69+
.PHONY: lint-update
70+
lint-update: $(GOLANGCI_LINT) ## Lint codebase
71+
$(GOLANGCI_LINT) run -v --fast=false --fix
72+
73+
.PHONY: modules
74+
modules:
75+
go mod tidy
76+
go mod vendor
77+
78+
.PHONY: test
79+
test:
80+
go test ./...
81+
5482
.PHONY: ALWAYS
5583
ALWAYS:

openshift/cmd/manager.go

+14-17
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,25 @@ import (
2020
"flag"
2121
"os"
2222

23+
openshiftconfig "github.com/openshift/api/config/v1"
24+
mapi "github.com/openshift/api/machine/v1beta1"
25+
corev1 "k8s.io/api/core/v1"
26+
"k8s.io/apimachinery/pkg/fields"
2327
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
2428
// to ensure that exec-entrypoint and run can make use of them.
2529
_ "k8s.io/client-go/plugin/pkg/client/auth"
26-
27-
"k8s.io/apimachinery/pkg/fields"
2830
ctrl "sigs.k8s.io/controller-runtime"
2931
"sigs.k8s.io/controller-runtime/pkg/cache"
32+
"sigs.k8s.io/controller-runtime/pkg/client"
3033
"sigs.k8s.io/controller-runtime/pkg/controller"
3134
"sigs.k8s.io/controller-runtime/pkg/healthz"
3235
"sigs.k8s.io/controller-runtime/pkg/log/zap"
3336
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
3437

35-
//+kubebuilder:scaffold:imports
36-
37-
openshiftconfig "github.com/openshift/api/config/v1"
38-
mapi "github.com/openshift/api/machine/v1beta1"
39-
corev1 "k8s.io/api/core/v1"
40-
41-
"github.com/openshift/cluster-api-provider-openstack/openshift/pkg/infracluster_controller"
38+
"github.com/openshift/cluster-api-provider-openstack/openshift/pkg/infraclustercontroller"
4239
caposcheme "github.com/openshift/cluster-api-provider-openstack/openshift/pkg/scheme"
40+
4341
"sigs.k8s.io/cluster-api-provider-openstack/pkg/scope"
44-
"sigs.k8s.io/controller-runtime/pkg/client"
4542
)
4643

4744
var (
@@ -72,7 +69,7 @@ func main() {
7269
HealthProbeBindAddress: probeAddr,
7370
LeaderElection: enableLeaderElection,
7471
LeaderElectionID: "infracluster-leader-election-capo",
75-
LeaderElectionNamespace: infracluster_controller.CAPINamespace,
72+
LeaderElectionNamespace: infraclustercontroller.CAPINamespace,
7673
// LeaderElectionReleaseOnCancel defines if the leader should step down voluntarily
7774
// when the Manager ends. This requires the binary to immediately end when the
7875
// Manager is stopped, otherwise, this setting is unsafe. Setting this significantly
@@ -88,28 +85,28 @@ func main() {
8885
Cache: cache.Options{
8986
// Restrict namespaced watches to the Cluster API namespace
9087
DefaultNamespaces: map[string]cache.Config{
91-
infracluster_controller.CAPINamespace: {},
88+
infraclustercontroller.CAPINamespace: {},
9289
},
9390

9491
ByObject: map[client.Object]cache.ByObject{
9592
// MAPI Machines are in their own namespace
9693
&mapi.Machine{}: {
9794
Namespaces: map[string]cache.Config{
98-
infracluster_controller.MAPINamespace: {},
95+
infraclustercontroller.MAPINamespace: {},
9996
},
10097
},
10198

10299
// We only need to watch a single cluster operator
103100
&openshiftconfig.ClusterOperator{}: {
104-
Field: fields.OneTermEqualSelector("metadata.name", infracluster_controller.ClusterOperatorName),
101+
Field: fields.OneTermEqualSelector("metadata.name", infraclustercontroller.ClusterOperatorName),
105102
},
106103

107104
// We only need to watch a single secret
108105
&corev1.Secret{}: {
109106
Namespaces: map[string]cache.Config{
110-
infracluster_controller.CAPINamespace: {},
107+
infraclustercontroller.CAPINamespace: {},
111108
},
112-
Field: fields.OneTermEqualSelector("metadata.name", infracluster_controller.CredentialsSecretName),
109+
Field: fields.OneTermEqualSelector("metadata.name", infraclustercontroller.CredentialsSecretName),
113110
},
114111
},
115112
},
@@ -130,7 +127,7 @@ func main() {
130127
os.Exit(1)
131128
}
132129

133-
if err := (&infracluster_controller.OpenShiftClusterReconciler{
130+
if err := (&infraclustercontroller.OpenShiftClusterReconciler{
134131
Client: mgr.GetClient(),
135132
Recorder: mgr.GetEventRecorderFor("openshiftcluster-controller"),
136133
ScopeFactory: scope.ScopeFactory,

openshift/pkg/infracluster_controller/openstackcluster.go renamed to openshift/pkg/infraclustercontroller/openstackcluster.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package infracluster_controller
1+
package infraclustercontroller
22

33
import (
44
"context"
@@ -280,7 +280,7 @@ func (r *OpenShiftClusterReconciler) ensureInfraCluster(ctx context.Context, log
280280
openStackCluster.Spec.Network.ID = defaultSubnet.NetworkID
281281
// N.B. Deliberately don't add subnet here: CAPO will use all subnets in network, which should also cover dual stack deployments
282282

283-
routerID, err := r.getDefaultRouterIDFromSubnet(ctx, log, networkClient, defaultSubnet)
283+
routerID, err := r.getDefaultRouterIDFromSubnet(ctx, networkClient, defaultSubnet)
284284
if err != nil {
285285
return nil, err
286286
}
@@ -310,7 +310,7 @@ func (r *OpenShiftClusterReconciler) ensureInfraCluster(ctx context.Context, log
310310
return &openStackCluster, nil
311311
}
312312

313-
func (r *OpenShiftClusterReconciler) getDefaultRouterIDFromSubnet(ctx context.Context, log logr.Logger, networkClient clients.NetworkClient, subnet *subnets.Subnet) (string, error) {
313+
func (r *OpenShiftClusterReconciler) getDefaultRouterIDFromSubnet(_ context.Context, networkClient clients.NetworkClient, subnet *subnets.Subnet) (string, error) {
314314
// Find the port which owns the subnet's gateway IP
315315
ports, err := networkClient.ListPort(ports.ListOpts{
316316
NetworkID: subnet.NetworkID,
@@ -320,7 +320,7 @@ func (r *OpenShiftClusterReconciler) getDefaultRouterIDFromSubnet(ctx context.Co
320320
// XXX: We should search on both subnet and IP
321321
// address here, but can't because of
322322
// https://github.com/gophercloud/gophercloud/issues/2807
323-
//SubnetID: subnet.ID,
323+
// SubnetID: subnet.ID,
324324
},
325325
},
326326
})

openshift/pkg/scheme/scheme.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package scheme
22

33
import (
4+
openshiftconfig "github.com/openshift/api/config/v1"
5+
machinev1beta1 "github.com/openshift/api/machine/v1beta1"
46
"k8s.io/apimachinery/pkg/runtime"
57
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
68
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
9+
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
710

8-
openshiftconfig "github.com/openshift/api/config/v1"
9-
machinev1beta1 "github.com/openshift/api/machine/v1beta1"
1011
infrav1 "sigs.k8s.io/cluster-api-provider-openstack/api/v1alpha7"
11-
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
1212
)
1313

1414
func DefaultScheme() *runtime.Scheme {

0 commit comments

Comments
 (0)