Skip to content

Commit 2f4984c

Browse files
authored
Enable gosec/golangci-lint, then fix reported errors (#927)
Signed-off-by: Jonathan West <jonwest@redhat.com>
1 parent cef3375 commit 2f4984c

File tree

73 files changed

+429
-375
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+429
-375
lines changed

.github/workflows/codegen.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,22 @@ jobs:
5858
- name: Ensure there is no diff in manifests
5959
run: |
6060
git diff --ignore-matching-lines='.*createdAt:.*' --exit-code -- .
61+
62+
gosec:
63+
name: Ensure that code passes gosec and golint
64+
runs-on: ubuntu-latest
65+
steps:
66+
- name: Checkout code
67+
uses: actions/checkout@v4
68+
- name: Setup Golang
69+
uses: actions/setup-go@v5
70+
with:
71+
go-version-file: 'go.mod'
72+
73+
- name: "Ensure code passes 'go-sec' - run 'make gosec' to identify issues"
74+
run: |
75+
make gosec
76+
77+
- name: "Ensure code passes 'golangci-lint' - run 'make lint' to identify issues"
78+
run: |
79+
make lint

.golangci.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
version: "2"
2+
linters:
3+
enable:
4+
- ginkgolinter # https://github.com/nunnatsa/ginkgolinter
5+
exclusions:
6+
generated: lax
7+
presets:
8+
- comments
9+
- common-false-positives
10+
- legacy
11+
- std-error-handling
12+
paths:
13+
- third_party$
14+
- builtin$
15+
- examples$
16+
17+
formatters:
18+
exclusions:
19+
generated: lax
20+
paths:
21+
- third_party$
22+
- builtin$
23+
- examples$

Makefile

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,6 @@ endif
329329
endif
330330

331331

332-
333332
# A comma-separated list of bundle images (e.g. make catalog-build BUNDLE_IMGS=example.com/operator-bundle:v0.1.0,example.com/operator-bundle:v0.2.0).
334333
# These images MUST exist in a registry and be pull-able.
335334
BUNDLE_IMGS ?= $(BUNDLE_IMG)
@@ -353,3 +352,37 @@ catalog-build: opm ## Build a catalog image.
353352
.PHONY: catalog-push
354353
catalog-push: ## Push a catalog image.
355354
$(MAKE) docker-push IMG=$(CATALOG_IMG)
355+
356+
357+
.PHONY: gosec
358+
gosec: go_sec
359+
$(GO_SEC) --exclude-dir "hack/upgrade-rollouts-manager" ./...
360+
361+
.PHONY: lint
362+
lint: golangci_lint
363+
$(GOLANGCI_LINT) --version
364+
GOMAXPROCS=2 $(GOLANGCI_LINT) run --fix --verbose --timeout 300s
365+
366+
367+
GO_SEC = $(shell pwd)/bin/gosec
368+
go_sec: ## Download gosec locally if necessary.
369+
$(call go-get-tool,$(GO_SEC),github.com/securego/gosec/v2/cmd/gosec@latest)
370+
371+
GOLANGCI_LINT = $(shell pwd)/bin/golangci-lint
372+
golangci_lint: ## Download golangci-lint locally if necessary.
373+
$(call go-get-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/v2/cmd/golangci-lint@latest)
374+
375+
376+
# go-get-tool will 'go install' any package $2 and install it to $1.
377+
PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))
378+
define go-get-tool
379+
@[ -f $(1) ] || { \
380+
set -e ;\
381+
TMP_DIR=$$(mktemp -d) ;\
382+
cd $$TMP_DIR ;\
383+
go mod init tmp ;\
384+
echo "Downloading $(2)" ;\
385+
GOBIN=$(PROJECT_DIR)/bin go install $(2) ;\
386+
rm -rf $$TMP_DIR ;\
387+
}
388+
endef

cmd/main.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ import (
4747
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
4848
crdv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
4949
"k8s.io/apimachinery/pkg/labels"
50-
"k8s.io/apimachinery/pkg/runtime"
50+
5151
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
5252
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
5353
ctrl "sigs.k8s.io/controller-runtime"
@@ -57,8 +57,6 @@ import (
5757
"sigs.k8s.io/controller-runtime/pkg/manager"
5858
"sigs.k8s.io/controller-runtime/pkg/webhook"
5959

60-
"github.com/argoproj-labs/argocd-operator/controllers/argocd"
61-
6260
pipelinesv1alpha1 "github.com/redhat-developer/gitops-operator/api/v1alpha1"
6361
"github.com/redhat-developer/gitops-operator/common"
6462
"github.com/redhat-developer/gitops-operator/controllers"
@@ -70,7 +68,7 @@ import (
7068
)
7169

7270
var (
73-
scheme = runtime.NewScheme()
71+
scheme = k8sruntime.NewScheme()
7472
setupLog = ctrl.Log.WithName("setup")
7573
)
7674

@@ -262,7 +260,7 @@ func main() {
262260
os.Exit(1)
263261
}
264262

265-
argocd.Register(openshift.ReconcilerHook)
263+
argocdprovisioner.Register(openshift.ReconcilerHook)
266264

267265
setupLog.Info("starting manager")
268266
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {

controllers/argocd/argocd_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ func TestArgoCD(t *testing.T) {
7474
v1.ResourceCPU: resourcev1.MustParse("500m"),
7575
},
7676
}
77-
assert.DeepEqual(t, testArgoCD.Spec.Grafana.Resources, testGrafanaResources)
77+
78+
//lint:ignore SA1019 known to be deprecated
79+
assert.DeepEqual(t, testArgoCD.Spec.Grafana.Resources, testGrafanaResources) //nolint:staticcheck // SA1019: We must test deprecated fields.
7880

7981
testHAResources := &v1.ResourceRequirements{
8082
Requests: v1.ResourceList{

controllers/argocd/openshift/clusterconfig_test.go

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -97,29 +97,18 @@ func makeTestDeployment() *appsv1.Deployment {
9797
}
9898
}
9999

100-
func makeTestRoleBinding() *rbacv1.RoleBinding {
101-
return &rbacv1.RoleBinding{
102-
ObjectMeta: metav1.ObjectMeta{
103-
Name: testArgoCDName,
104-
Namespace: testNamespace,
105-
},
106-
Subjects: []rbacv1.Subject{},
107-
RoleRef: rbacv1.RoleRef{},
108-
}
109-
}
110-
111100
func newStatefulSetWithSuffix(suffix string, component string, cr *argoapp.ArgoCD) *appsv1.StatefulSet {
112101
return newStatefulSetWithName(fmt.Sprintf("%s-%s", cr.Name, suffix), component, cr)
113102
}
114103

115104
func newStatefulSetWithName(name string, component string, cr *argoapp.ArgoCD) *appsv1.StatefulSet {
116105
ss := newStatefulSet(cr)
117-
ss.ObjectMeta.Name = name
106+
ss.Name = name
118107

119-
lbls := ss.ObjectMeta.Labels
108+
lbls := ss.Labels
120109
lbls[common.ArgoCDKeyName] = name
121110
lbls[common.ArgoCDKeyComponent] = component
122-
ss.ObjectMeta.Labels = lbls
111+
ss.Labels = lbls
123112

124113
return ss
125114
}

controllers/argocd/openshift/openshift.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,16 @@ func ReconcilerHook(cr *argoapp.ArgoCD, v interface{}, hint string) error {
3131
logv := log.WithValues("ArgoCD Namespace", cr.Namespace, "ArgoCD Name", cr.Name)
3232
switch o := v.(type) {
3333
case *rbacv1.ClusterRole:
34-
if o.ObjectMeta.Name == argocd.GenerateUniqueResourceName("argocd-application-controller", cr) {
34+
if o.Name == argocd.GenerateUniqueResourceName("argocd-application-controller", cr) {
3535
logv.Info("configuring openshift cluster config policy rules")
3636
o.Rules = policyRulesForClusterConfig()
3737
}
3838
case *appsv1.Deployment:
39-
if o.ObjectMeta.Name == cr.ObjectMeta.Name+"-redis" {
39+
switch o.Name {
40+
case cr.Name + "-redis":
4041
logv.Info("configuring openshift redis")
4142
o.Spec.Template.Spec.Containers[0].Args = append(getArgsForRedhatRedis(), o.Spec.Template.Spec.Containers[0].Args...)
42-
} else if o.ObjectMeta.Name == cr.ObjectMeta.Name+"-redis-ha-haproxy" {
43+
case cr.Name + "-redis-ha-haproxy":
4344
logv.Info("configuring openshift redis haproxy")
4445
o.Spec.Template.Spec.Containers[0].Command = append(getCommandForRedhatRedisHaProxy(), o.Spec.Template.Spec.Containers[0].Command...)
4546
version := hint
@@ -55,13 +56,14 @@ func ReconcilerHook(cr *argoapp.ArgoCD, v interface{}, hint string) error {
5556
}
5657
}
5758
case *appsv1.StatefulSet:
58-
if o.ObjectMeta.Name == cr.ObjectMeta.Name+"-redis-ha-server" {
59+
if o.Name == cr.Name+"-redis-ha-server" {
5960
logv.Info("configuring openshift redis-ha-server stateful set")
6061
for index := range o.Spec.Template.Spec.Containers {
61-
if o.Spec.Template.Spec.Containers[index].Name == "redis" {
62+
switch o.Spec.Template.Spec.Containers[index].Name {
63+
case "redis":
6264
o.Spec.Template.Spec.Containers[index].Args = getArgsForRedhatHaRedisServer()
6365
o.Spec.Template.Spec.Containers[index].Command = []string{}
64-
} else if o.Spec.Template.Spec.Containers[index].Name == "sentinel" {
66+
case "sentinel":
6567
o.Spec.Template.Spec.Containers[index].Args = getArgsForRedhatHaRedisSentinel()
6668
o.Spec.Template.Spec.Containers[index].Command = []string{}
6769
}
@@ -70,12 +72,12 @@ func ReconcilerHook(cr *argoapp.ArgoCD, v interface{}, hint string) error {
7072
o.Spec.Template.Spec.InitContainers[0].Command = []string{}
7173
}
7274
case *corev1.Secret:
73-
if allowedNamespace(cr.ObjectMeta.Namespace, os.Getenv("ARGOCD_CLUSTER_CONFIG_NAMESPACES")) {
75+
if allowedNamespace(cr.Namespace, os.Getenv("ARGOCD_CLUSTER_CONFIG_NAMESPACES")) {
7476
logv.Info("configuring cluster secret with empty namespaces to allow cluster resources")
7577
delete(o.Data, "namespaces")
7678
}
7779
case *rbacv1.Role:
78-
if o.ObjectMeta.Name == cr.Name+"-"+"argocd-application-controller" {
80+
if o.Name == cr.Name+"-"+"argocd-application-controller" {
7981
logv.Info("configuring policy rule for Application Controller")
8082

8183
// can move this to somewhere common eventually, maybe init()

controllers/argocd/openshift/openshift_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,13 @@ func TestReconcileArgoCD_reconcileRedisDeployment(t *testing.T) {
107107
a := makeTestArgoCD()
108108
testDeployment := makeTestDeployment()
109109

110-
testDeployment.ObjectMeta.Name = a.Name + "-" + "redis"
110+
testDeployment.Name = a.Name + "-" + "redis"
111111
want := append(getArgsForRedhatRedis(), testDeployment.Spec.Template.Spec.Containers[0].Args...)
112112

113113
assert.NoError(t, ReconcilerHook(a, testDeployment, ""))
114114
assert.Equal(t, testDeployment.Spec.Template.Spec.Containers[0].Args, want)
115115

116-
testDeployment.ObjectMeta.Name = a.Name + "-" + "not-redis"
116+
testDeployment.Name = a.Name + "-" + "not-redis"
117117
want = testDeployment.Spec.Template.Spec.Containers[0].Args
118118

119119
assert.NoError(t, ReconcilerHook(a, testDeployment, ""))
@@ -124,7 +124,7 @@ func TestReconcileArgoCD_reconcileRedisHaProxyDeployment(t *testing.T) {
124124
a := makeTestArgoCD()
125125
testDeployment := makeTestDeployment()
126126

127-
testDeployment.ObjectMeta.Name = a.Name + "-redis-ha-haproxy"
127+
testDeployment.Name = a.Name + "-redis-ha-haproxy"
128128
testDeployment.Spec.Template.Spec.Containers[0].SecurityContext = &corev1.SecurityContext{
129129
Capabilities: &corev1.Capabilities{},
130130
}
@@ -141,7 +141,7 @@ func TestReconcileArgoCD_reconcileRedisHaProxyDeployment(t *testing.T) {
141141
assert.Equal(t, wantc, *testDeployment.Spec.Template.Spec.Containers[0].SecurityContext.Capabilities)
142142

143143
testDeployment = makeTestDeployment()
144-
testDeployment.ObjectMeta.Name = a.Name + "-redis-ha-haproxy"
144+
testDeployment.Name = a.Name + "-redis-ha-haproxy"
145145
testDeployment.Spec.Template.Spec.Containers[0].SecurityContext = &corev1.SecurityContext{
146146
Capabilities: &corev1.Capabilities{},
147147
}
@@ -150,7 +150,7 @@ func TestReconcileArgoCD_reconcileRedisHaProxyDeployment(t *testing.T) {
150150
assert.Nil(t, testDeployment.Spec.Template.Spec.Containers[0].SecurityContext.Capabilities)
151151

152152
testDeployment = makeTestDeployment()
153-
testDeployment.ObjectMeta.Name = a.Name + "-" + "not-redis-ha-haproxy"
153+
testDeployment.Name = a.Name + "-" + "not-redis-ha-haproxy"
154154
want = testDeployment.Spec.Template.Spec.Containers[0].Command
155155

156156
assert.NoError(t, ReconcilerHook(a, testDeployment, ""))

controllers/argocd_controller_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ func TestReconcile_delete_consolelink(t *testing.T) {
118118
t.Run(test.name, func(t *testing.T) {
119119
reconcileArgoCD, fakeClient := newFakeReconcileArgoCD(argoCDRoute, consoleLink)
120120
consoleLink := newConsoleLink("https://test.com", "Cluster Argo CD")
121-
fakeClient.Create(context.TODO(), consoleLink)
121+
err := fakeClient.Create(context.TODO(), consoleLink)
122+
assert.NilError(t, err)
122123

123124
if test.setEnvVarFunc != nil {
124125
test.setEnvVarFunc(t, test.envVar)

controllers/argocd_metrics_controller_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,8 @@ func TestReconcile_add_service_monitors(t *testing.T) {
223223
assert.Assert(t, is.Len(serviceMonitor.OwnerReferences, 1))
224224
assert.Equal(t, serviceMonitor.OwnerReferences[0].Kind, argocdKind)
225225
assert.Equal(t, serviceMonitor.OwnerReferences[0].Name, tc.instanceName)
226-
assert.Equal(t, len(serviceMonitor.ObjectMeta.Labels), 1)
227-
assert.Equal(t, serviceMonitor.ObjectMeta.Labels["release"], "prometheus-operator")
226+
assert.Equal(t, len(serviceMonitor.Labels), 1)
227+
assert.Equal(t, serviceMonitor.Labels["release"], "prometheus-operator")
228228
assert.Equal(t, len(serviceMonitor.Spec.Selector.MatchLabels), 1)
229229
assert.Equal(t, serviceMonitor.Spec.Selector.MatchLabels["app.kubernetes.io/name"], matchLabel)
230230
assert.Equal(t, len(serviceMonitor.Spec.Endpoints), 1)
@@ -238,8 +238,8 @@ func TestReconcile_add_service_monitors(t *testing.T) {
238238
assert.Assert(t, is.Len(serviceMonitor.OwnerReferences, 1))
239239
assert.Equal(t, serviceMonitor.OwnerReferences[0].Kind, argocdKind)
240240
assert.Equal(t, serviceMonitor.OwnerReferences[0].Name, tc.instanceName)
241-
assert.Equal(t, len(serviceMonitor.ObjectMeta.Labels), 1)
242-
assert.Equal(t, serviceMonitor.ObjectMeta.Labels["release"], "prometheus-operator")
241+
assert.Equal(t, len(serviceMonitor.Labels), 1)
242+
assert.Equal(t, serviceMonitor.Labels["release"], "prometheus-operator")
243243
assert.Equal(t, len(serviceMonitor.Spec.Selector.MatchLabels), 1)
244244
assert.Equal(t, serviceMonitor.Spec.Selector.MatchLabels["app.kubernetes.io/name"], matchLabel)
245245
assert.Equal(t, len(serviceMonitor.Spec.Endpoints), 1)
@@ -253,8 +253,8 @@ func TestReconcile_add_service_monitors(t *testing.T) {
253253
assert.Assert(t, is.Len(serviceMonitor.OwnerReferences, 1))
254254
assert.Equal(t, serviceMonitor.OwnerReferences[0].Kind, argocdKind)
255255
assert.Equal(t, serviceMonitor.OwnerReferences[0].Name, tc.instanceName)
256-
assert.Equal(t, len(serviceMonitor.ObjectMeta.Labels), 1)
257-
assert.Equal(t, serviceMonitor.ObjectMeta.Labels["release"], "prometheus-operator")
256+
assert.Equal(t, len(serviceMonitor.Labels), 1)
257+
assert.Equal(t, serviceMonitor.Labels["release"], "prometheus-operator")
258258
assert.Equal(t, len(serviceMonitor.Spec.Selector.MatchLabels), 1)
259259
assert.Equal(t, serviceMonitor.Spec.Selector.MatchLabels["app.kubernetes.io/name"], matchLabel)
260260
assert.Equal(t, len(serviceMonitor.Spec.Endpoints), 1)
@@ -390,7 +390,7 @@ func TestReconciler_add_dashboard(t *testing.T) {
390390
err = r.Client.Get(context.TODO(), types.NamespacedName{Name: name, Namespace: dashboardNamespace}, dashboard)
391391
assert.NilError(t, err)
392392

393-
assert.Assert(t, dashboard.ObjectMeta.Labels["console.openshift.io/dashboard"] == "true")
393+
assert.Assert(t, dashboard.Labels["console.openshift.io/dashboard"] == "true")
394394
assert.Assert(t, dashboard.Data[entry.Name()] == string(content))
395395
}
396396
}

0 commit comments

Comments
 (0)