Skip to content

Commit f4ab698

Browse files
authored
Add more linters and fix linter errors (#81)
* Add more linters and fix linter errors * Specify golangci-lint version * Fix punctuations at the end of error messages * More punctuation fixes * Specify the version of staticcheck Co-authored-by: Wonkun Kim <[email protected]>
1 parent 165ba6b commit f4ab698

19 files changed

+104
-96
lines changed

.golangci.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,16 @@ linters-settings:
1717
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1818
See the License for the specific language governing permissions and
1919
limitations under the License.
20+
gocyclo:
21+
min-complexity: 15
2022

2123
linters:
2224
enable:
2325
- gosec
2426
- goheader
2527
- revive
28+
- gocyclo
29+
- misspell
2630

2731
run:
2832
issues-exit-code: 1

Makefile

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,11 @@ vet: ## Run go vet on the whole project.
114114
go vet ./...
115115

116116
.PHONY: lint
117-
lint: bin/golangci-lint generate-mocks ## Run linting for the project.
117+
lint: bin/golangci-lint bin/staticcheck generate-mocks ## Run linting for the project.
118118
go fmt ./...
119119
go vet ./...
120120
golangci-lint run -v --timeout 360s ./...
121+
staticcheck ./...
121122
@ # The below string of commands checks that ginkgo isn't present in the controllers.
122123
@(grep ginkgo ${PROJECT_DIR}/controllers/cloudstack*_controller.go && \
123124
echo "Remove ginkgo from controllers. This is probably an artifact of testing." \
@@ -137,11 +138,13 @@ undeploy: bin/kustomize ## Undeploy controller from the K8s cluster specified in
137138
##@ Binaries
138139

139140
.PHONY: binaries
140-
binaries: bin/controller-gen bin/kustomize bin/ginkgo bin/golangci-lint bin/mockgen bin/kubectl ## Locally install all needed bins.
141+
binaries: bin/controller-gen bin/kustomize bin/ginkgo bin/golangci-lint bin/staticcheck bin/mockgen bin/kubectl ## Locally install all needed bins.
141142
bin/controller-gen: ## Install controller-gen to bin.
142143
GOBIN=$(PROJECT_DIR)/bin go install sigs.k8s.io/controller-tools/cmd/[email protected]
143144
bin/golangci-lint: ## Install golangci-lint to bin.
144-
GOBIN=$(PROJECT_DIR)/bin go install github.com/golangci/golangci-lint/cmd/[email protected]
145+
GOBIN=$(PROJECT_DIR)/bin go install github.com/golangci/golangci-lint/cmd/[email protected]
146+
bin/staticcheck: ## Install staticcheck to bin.
147+
GOBIN=$(PROJECT_DIR)/bin go install honnef.co/go/tools/cmd/[email protected]
145148
bin/ginkgo: ## Install ginkgo to bin.
146149
GOBIN=$(PROJECT_DIR)/bin go install github.com/onsi/ginkgo/[email protected]
147150
bin/mockgen:

config/crd/bases/infrastructure.cluster.x-k8s.io_cloudstackmachinestatecheckers.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ spec:
3535
metadata:
3636
type: object
3737
spec:
38-
description: CloudStackMachineStateCheckerSpec defines the desired state
39-
of CloudStackMachineStateChecker
38+
description: CloudStackMachineStateCheckerSpec
4039
properties:
4140
instanceID:
4241
description: CloudStack machine instance ID

controllers/cloudstackcluster_controller.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func (r *CloudStackClusterReconciliationRunner) SetFailureDomains() (ctrl.Result
126126
return ctrl.Result{}, nil
127127
}
128128

129-
// ReconcileDelete cleans up resources used by the cluster and finaly removes the CloudStackCluster's finalizers.
129+
// ReconcileDelete cleans up resources used by the cluster and finally removes the CloudStackCluster's finalizers.
130130
func (r *CloudStackClusterReconciliationRunner) ReconcileDelete() (ctrl.Result, error) {
131131
r.Log.Info("Deleting CloudStackCluster.")
132132
if res, err := r.GetZones(r.Zones)(); r.ShouldReturn(res, err) {
@@ -171,7 +171,7 @@ func (reconciler *CloudStackClusterReconciler) SetupWithManager(mgr ctrl.Manager
171171
},
172172
).Build(reconciler)
173173
if err != nil {
174-
return errors.Wrap(err, "building CloudStackCluster controller:")
174+
return errors.Wrap(err, "building CloudStackCluster controller")
175175
}
176176

177177
// Add a watch on CAPI Cluster objects for unpause and ready events.
@@ -187,5 +187,5 @@ func (reconciler *CloudStackClusterReconciler) SetupWithManager(mgr ctrl.Manager
187187
},
188188
DeleteFunc: func(e event.DeleteEvent) bool { return false },
189189
CreateFunc: func(e event.CreateEvent) bool { return false }})
190-
return errors.Wrap(err, "building CloudStackCluster controller:")
190+
return errors.Wrap(err, "building CloudStackCluster controller")
191191
}

controllers/cloudstackisolatednetwork_controller.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,17 @@ func (r *CloudStackIsoNetReconciliationRunner) Reconcile() (retRes ctrl.Result,
7777
// Set endpoint of CloudStackCluster if it is not currently set. (uses patcher to do so)
7878
csClusterPatcher, err := patch.NewHelper(r.CSCluster, r.K8sClient)
7979
if err != nil {
80-
return r.ReturnWrappedError(retErr, "setting up CloudStackCluster patcher:")
80+
return r.ReturnWrappedError(retErr, "setting up CloudStackCluster patcher")
8181
}
8282
if err := r.CSUser.GetOrCreateIsolatedNetwork(r.Zone, r.ReconciliationSubject, r.CSCluster); err != nil {
8383
return ctrl.Result{}, err
8484
}
8585
// Tag the created network.
8686
if err := r.CSUser.AddClusterTag(cloud.ResourceTypeNetwork, r.ReconciliationSubject.Spec.ID, r.CSCluster); err != nil {
87-
return ctrl.Result{}, errors.Wrapf(err, "tagging network with id %s:", r.ReconciliationSubject.Spec.ID)
87+
return ctrl.Result{}, errors.Wrapf(err, "tagging network with id %s", r.ReconciliationSubject.Spec.ID)
8888
}
8989
if err := csClusterPatcher.Patch(r.RequestCtx, r.CSCluster); err != nil {
90-
return r.ReturnWrappedError(err, "patching endpoint update to CloudStackCluster:")
90+
return r.ReturnWrappedError(err, "patching endpoint update to CloudStackCluster")
9191
}
9292

9393
r.ReconciliationSubject.Status.Ready = true

controllers/cloudstackmachine_controller.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import (
3535

3636
infrav1 "github.com/aws/cluster-api-provider-cloudstack/api/v1beta1"
3737
"github.com/aws/cluster-api-provider-cloudstack/controllers/utils"
38-
csCtrlrUtils "github.com/aws/cluster-api-provider-cloudstack/controllers/utils"
3938
"github.com/aws/cluster-api-provider-cloudstack/pkg/cloud"
4039
capiv1 "sigs.k8s.io/cluster-api/api/v1beta1"
4140
)
@@ -49,7 +48,7 @@ import (
4948

5049
// CloudStackMachineReconciliationRunner is a ReconciliationRunner with extensions specific to CloudStack machine reconciliation.
5150
type CloudStackMachineReconciliationRunner struct {
52-
csCtrlrUtils.ReconciliationRunner
51+
utils.ReconciliationRunner
5352
ReconciliationSubject *infrav1.CloudStackMachine
5453
CAPIMachine *capiv1.Machine
5554
StateChecker *infrav1.CloudStackMachineStateChecker
@@ -61,7 +60,7 @@ type CloudStackMachineReconciliationRunner struct {
6160

6261
// CloudStackMachineReconciler reconciles a CloudStackMachine object
6362
type CloudStackMachineReconciler struct {
64-
csCtrlrUtils.ReconcilerBase
63+
utils.ReconcilerBase
6564
}
6665

6766
// Initialize a new CloudStackMachine reconciliation runner with concrete types and initialized member fields.
@@ -75,7 +74,7 @@ func NewCSMachineReconciliationRunner() *CloudStackMachineReconciliationRunner {
7574
r.AffinityGroup = &infrav1.CloudStackAffinityGroup{}
7675
r.FailureDomain = &infrav1.CloudStackZone{}
7776
// Setup the base runner. Initializes pointers and links reconciliation methods.
78-
r.ReconciliationRunner = csCtrlrUtils.NewRunner(r, r.ReconciliationSubject)
77+
r.ReconciliationRunner = utils.NewRunner(r, r.ReconciliationSubject)
7978
return r
8079
}
8180

@@ -112,9 +111,9 @@ func (r *CloudStackMachineReconciliationRunner) ConsiderAffinity() (ctrl.Result,
112111
return ctrl.Result{}, nil
113112
}
114113

115-
agName, err := csCtrlrUtils.GenerateAffinityGroupName(*r.ReconciliationSubject, r.CAPIMachine)
114+
agName, err := utils.GenerateAffinityGroupName(*r.ReconciliationSubject, r.CAPIMachine)
116115
if err != nil {
117-
r.Log.Info("getting affinity group name:", err)
116+
r.Log.Info("getting affinity group name", err)
118117
}
119118

120119
if res, err := r.GetOrCreateAffinityGroup(agName, r.ReconciliationSubject.Spec.Affinity, r.AffinityGroup)(); r.ShouldReturn(res, err) {
@@ -241,7 +240,7 @@ func (r *CloudStackMachineReconciliationRunner) GetOrCreateMachineStateChecker()
241240
Status: infrav1.CloudStackMachineStateCheckerStatus{Ready: false},
242241
}
243242

244-
if err := r.K8sClient.Create(r.RequestCtx, csMachineStateChecker); err != nil && !csCtrlrUtils.ContainsAlreadyExistsSubstring(err) {
243+
if err := r.K8sClient.Create(r.RequestCtx, csMachineStateChecker); err != nil && !utils.ContainsAlreadyExistsSubstring(err) {
245244
return r.ReturnWrappedError(err, "error encountered when creating CloudStackMachineStateChecker")
246245
}
247246

controllers/cloudstackzone_controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,11 @@ func (r *CloudStackZoneReconciliationRunner) Reconcile() (retRes ctrl.Result, re
8282
r.Log.V(1).Info("Reconciling CloudStackZone.", "zoneSpec", r.ReconciliationSubject.Spec)
8383
// Start by purely data fetching information about the zone and specified network.
8484
if err := r.CSUser.ResolveZone(r.ReconciliationSubject); err != nil {
85-
return ctrl.Result{}, errors.Wrap(err, "resolving CloudStack zone information:")
85+
return ctrl.Result{}, errors.Wrap(err, "resolving CloudStack zone information")
8686
}
8787
if err := r.CSUser.ResolveNetworkForZone(r.ReconciliationSubject); err != nil &&
8888
!csCtrlrUtils.ContainsNoMatchSubstring(err) {
89-
return ctrl.Result{}, errors.Wrap(err, "resolving Cloudstack network information:")
89+
return ctrl.Result{}, errors.Wrap(err, "resolving Cloudstack network information")
9090
}
9191

9292
// Address Isolated Networks.

controllers/utils/affinity_group.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func (r *ReconciliationRunner) GetOrCreateAffinityGroup(name string, affinityTyp
6767
}
6868

6969
if err := r.K8sClient.Create(r.RequestCtx, ag); err != nil && !ContainsAlreadyExistsSubstring(err) {
70-
return r.ReturnWrappedError(err, "creating affinity group CRD:")
70+
return r.ReturnWrappedError(err, "creating affinity group CRD")
7171
}
7272
return ctrl.Result{}, nil
7373
}

controllers/utils/base_reconciler.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func (r *ReconciliationRunner) GetCAPICluster() (ctrl.Result, error) {
146146
Name: name,
147147
}
148148
if err := r.K8sClient.Get(r.RequestCtx, key, r.CAPICluster); err != nil {
149-
return ctrl.Result{}, errors.Wrapf(client.IgnoreNotFound(err), "getting CAPI Cluster %s:", name)
149+
return ctrl.Result{}, errors.Wrapf(client.IgnoreNotFound(err), "getting CAPI Cluster %s", name)
150150
} else if r.CAPICluster.Name == "" {
151151
return r.RequeueWithMessage("Cluster not fetched.")
152152
}
@@ -167,7 +167,7 @@ func (r *ReconciliationRunner) GetCSCluster() (ctrl.Result, error) {
167167
Name: name,
168168
}
169169
err := r.K8sClient.Get(r.RequestCtx, key, r.CSCluster)
170-
return ctrl.Result{}, errors.Wrapf(client.IgnoreNotFound(err), "getting CAPI Cluster %s:", name)
170+
return ctrl.Result{}, errors.Wrapf(client.IgnoreNotFound(err), "getting CAPI Cluster %s", name)
171171
}
172172

173173
// CheckOwnedCRDsForReadiness queries for the readiness of CRDs of GroupVersionKind passed.
@@ -181,7 +181,7 @@ func (r *ReconciliationRunner) CheckOwnedCRDsForReadiness(gvks ...schema.GroupVe
181181
potentiallyOnwedObjs.SetGroupVersionKind(gvk)
182182
err := r.K8sClient.List(r.RequestCtx, potentiallyOnwedObjs)
183183
if err != nil {
184-
return ctrl.Result{}, errors.Wrapf(err, "requesting owned objects with gvk %s:", gvk)
184+
return ctrl.Result{}, errors.Wrapf(err, "requesting owned objects with gvk %s", gvk)
185185
}
186186

187187
// Filter objects not actually owned by reconciliation subject via owner reference UID.
@@ -199,10 +199,10 @@ func (r *ReconciliationRunner) CheckOwnedCRDsForReadiness(gvks ...schema.GroupVe
199199
// Check that found objects are ready.
200200
for _, owned := range ownedObjs {
201201
if ready, found, err := unstructured.NestedBool(owned.Object, "status", "ready"); err != nil {
202-
return ctrl.Result{}, errors.Wrapf(err, "parsing ready for object %s:", owned)
202+
return ctrl.Result{}, errors.Wrapf(err, "parsing ready for object %s", owned)
203203
} else if !found || !ready {
204204
if name, found, err := unstructured.NestedString(owned.Object, "metadata", "name"); err != nil {
205-
return ctrl.Result{}, errors.Wrapf(err, "parsing name for object %s:", owned)
205+
return ctrl.Result{}, errors.Wrapf(err, "parsing name for object %s", owned)
206206
} else if !found {
207207
return r.RequeueWithMessage(
208208
fmt.Sprintf(
@@ -257,14 +257,14 @@ func (r *ReconciliationRunner) RequeueIfCloudStackClusterNotReady() (ctrl.Result
257257
func (r *ReconciliationRunner) SetupPatcher() (res ctrl.Result, retErr error) {
258258
r.Log.V(1).Info("Setting up patcher.")
259259
r.Patcher, retErr = patch.NewHelper(r.ReconciliationSubject, r.K8sClient)
260-
return res, errors.Wrapf(retErr, "setting up patcher:")
260+
return res, errors.Wrapf(retErr, "setting up patcher")
261261
}
262262

263263
// PatchChangesBackToAPI patches changes to the ReconciliationSubject back to the appropriate API.
264264
func (r *ReconciliationRunner) PatchChangesBackToAPI() (res ctrl.Result, retErr error) {
265265
r.Log.V(1).Info("Patching changes back to api.")
266266
err := r.Patcher.Patch(r.RequestCtx, r.ReconciliationSubject)
267-
return res, errors.Wrapf(err, "patching reconciliation subject:")
267+
return res, errors.Wrapf(err, "patching reconciliation subject")
268268
}
269269

270270
// RequeueWithMessage is a convenience method to log requeue message and then return a result with RequeueAfter set.
@@ -358,7 +358,7 @@ func (r *ReconciliationRunner) GetReconciliationSubject() (res ctrl.Result, rete
358358
r.SetReturnEarly()
359359
}
360360
if err != nil {
361-
return ctrl.Result{}, errors.Wrap(err, "fetching reconciliation subject:")
361+
return ctrl.Result{}, errors.Wrap(err, "fetching reconciliation subject")
362362
}
363363
return r.SetupPatcher()
364364
}

controllers/utils/isolated_network.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func (r *ReconciliationRunner) GenerateIsolatedNetwork(name string) CloudStackRe
4040
csIsoNet.Spec.ControlPlaneEndpoint.Port = r.CSCluster.Spec.ControlPlaneEndpoint.Port
4141

4242
if err := r.K8sClient.Create(r.RequestCtx, csIsoNet); err != nil && !ContainsAlreadyExistsSubstring(err) {
43-
return r.ReturnWrappedError(err, "creating isolated network CRD:")
43+
return r.ReturnWrappedError(err, "creating isolated network CRD")
4444
}
4545
return ctrl.Result{}, nil
4646
}

0 commit comments

Comments
 (0)