Skip to content

Commit a4032e7

Browse files
authored
enable and fix lint settings in golangci.yaml (#49)
1 parent 9376fd7 commit a4032e7

23 files changed

+161
-125
lines changed

go.work

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
go 1.22.3
22

33
use (
4-
./operator/
5-
./pkg/
6-
./resolver/
4+
./operator
5+
./pkg
6+
./resolver
77
)

golangci.yaml

+10-10
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,23 @@ linters:
1313
- dogsled
1414
- dupl
1515
- durationcheck
16-
# - errcheck
16+
- errcheck
1717
- errchkjson
1818
- errname
19-
# - errorlint
19+
- errorlint
2020
- exhaustive
2121
# - forcetypeassert
2222
- gocognit
23-
# - goconst
24-
# - gocritic
23+
- goconst
24+
- gocritic
2525
- gocyclo
2626
- gofmt
2727
- goheader
2828
- goimports
2929
# - gomoddirectives
3030
- gomodguard
3131
- goprintffuncname
32-
# - gosec
32+
- gosec
3333
- gosimple
3434
- govet
3535
- grouper
@@ -46,17 +46,17 @@ linters:
4646
- nolintlint
4747
# - nonamedreturns
4848
- nosprintfhostport
49-
# - predeclared
49+
- predeclared
5050
# - promlinter
51-
# - revive
51+
- revive
5252
- staticcheck
53-
# - stylecheck
53+
- stylecheck
5454
- tenv
5555
- thelper
5656
- tparallel
5757
- typecheck
5858
- unconvert
59-
# - unparam
59+
- unparam
6060
- unused
6161
- whitespace
62-
# - wrapcheck
62+
- wrapcheck

operator/cmd/main.go

+26-12
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@ package main
1919
import (
2020
"crypto/tls"
2121
"flag"
22+
"fmt"
2223
"os"
2324
"time"
2425

2526
"github.com/getsentry/sentry-go"
2627

27-
"truefoundry/elasti/operator/internal/elastiServer"
28+
"truefoundry/elasti/operator/internal/elastiserver"
2829

29-
"truefoundry/elasti/operator/internal/crdDirectory"
30+
"truefoundry/elasti/operator/internal/crddirectory"
3031
"truefoundry/elasti/operator/internal/informer"
3132

3233
tfLogger "github.com/truefoundry/elasti/pkg/logger"
@@ -66,6 +67,13 @@ const (
6667
)
6768

6869
func main() {
70+
err := mainWithError()
71+
if err != nil {
72+
os.Exit(1)
73+
}
74+
}
75+
76+
func mainWithError() error {
6977
zapLogger, err := tfLogger.NewLogger("dev")
7078
if err != nil {
7179
setupLog.Error(err, "unable to create logger")
@@ -154,43 +162,49 @@ func main() {
154162
})
155163
if err != nil {
156164
setupLog.Error(err, "unable to start manager")
157-
os.Exit(1)
165+
sentry.CaptureException(err)
166+
return fmt.Errorf("main: %w", err)
158167
}
159168

160169
// Start the shared CRD Directory
161-
crdDirectory.INITDirectory(zapLogger)
170+
crddirectory.INITDirectory(zapLogger)
162171
// Initiate and start the shared Informer manager
163172
Informer := informer.NewInformerManager(zapLogger, mgr.GetConfig())
164173
Informer.Start()
165174

166175
// Set up the ElastiService controller
167176
if err = (&controller.ElastiServiceReconciler{
168-
Client: mgr.GetClient(),
169-
Scheme: mgr.GetScheme(),
170-
Logger: zapLogger,
177+
Client: mgr.GetClient(),
178+
Scheme: mgr.GetScheme(), Logger: zapLogger,
171179
Informer: Informer,
172180
}).SetupWithManager(mgr); err != nil {
173181
setupLog.Error(err, "unable to create controller", "controller", "ElastiService")
174-
os.Exit(1)
182+
sentry.CaptureException(err)
183+
return fmt.Errorf("main: %w", err)
175184
}
176185

177186
// Start the elasti server
178-
eServer := elastiServer.NewServer(zapLogger, mgr.GetConfig(), 30*time.Second)
187+
eServer := elastiserver.NewServer(zapLogger, mgr.GetConfig(), 30*time.Second)
179188
go eServer.Start(elastiServerPort)
180189

181190
//+kubebuilder:scaffold:builder
182191
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
183192
setupLog.Error(err, "unable to set up health check")
184-
os.Exit(1)
193+
sentry.CaptureException(err)
194+
return fmt.Errorf("main: %w", err)
185195
}
186196
if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil {
187197
setupLog.Error(err, "unable to set up ready check")
188-
os.Exit(1)
198+
sentry.CaptureException(err)
199+
return fmt.Errorf("main: %w", err)
189200
}
190201

191202
setupLog.Info("starting manager")
192203
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
193204
setupLog.Error(err, "problem running manager")
194-
os.Exit(1)
205+
sentry.CaptureException(err)
206+
return fmt.Errorf("main: %w", err)
195207
}
208+
209+
return nil
196210
}

operator/internal/controller/elastiservice_controller.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ package controller
22

33
import (
44
"context"
5+
"fmt"
56
"sync"
67
"time"
78

89
"github.com/getsentry/sentry-go"
910

10-
"truefoundry/elasti/operator/internal/crdDirectory"
11+
"truefoundry/elasti/operator/internal/crddirectory"
1112
"truefoundry/elasti/operator/internal/informer"
1213
"truefoundry/elasti/operator/internal/prom"
1314

@@ -114,7 +115,7 @@ func (r *ElastiServiceReconciler) Reconcile(ctx context.Context, req ctrl.Reques
114115

115116
// We add the CRD details to service directory, so when elasti server received a request,
116117
// we can find the right resource to scale up
117-
crdDirectory.CRDDirectory.AddCRD(es.Spec.Service, &crdDirectory.CRDDetails{
118+
crddirectory.CRDDirectory.AddCRD(es.Spec.Service, &crddirectory.CRDDetails{
118119
CRDName: es.Name,
119120
Spec: es.Spec,
120121
})
@@ -123,9 +124,10 @@ func (r *ElastiServiceReconciler) Reconcile(ctx context.Context, req ctrl.Reques
123124
}
124125

125126
func (r *ElastiServiceReconciler) SetupWithManager(mgr ctrl.Manager) error {
126-
return ctrl.NewControllerManagedBy(mgr).
127+
err := ctrl.NewControllerManagedBy(mgr).
127128
For(&v1alpha1.ElastiService{}).
128129
Complete(r)
130+
return fmt.Errorf("SetupWithManager: %w", err)
129131
}
130132

131133
func (r *ElastiServiceReconciler) getMutexForReconcile(key string) *sync.Mutex {

operator/internal/controller/opsCRD.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"sync"
88

99
"truefoundry/elasti/operator/api/v1alpha1"
10-
"truefoundry/elasti/operator/internal/crdDirectory"
10+
"truefoundry/elasti/operator/internal/crddirectory"
1111
"truefoundry/elasti/operator/internal/informer"
1212
"truefoundry/elasti/operator/internal/prom"
1313

@@ -109,7 +109,7 @@ func (r *ElastiServiceReconciler) finalizeCRD(ctx context.Context, es *v1alpha1.
109109
}()
110110
wg.Wait()
111111
// Remove CRD details from service directory
112-
crdDirectory.CRDDirectory.RemoveCRD(es.Spec.Service)
112+
crddirectory.CRDDirectory.RemoveCRD(es.Spec.Service)
113113
r.Logger.Info("[Done] CRD removed from service directory", zap.String("es", req.String()))
114114

115115
if err1 != nil || err2 != nil {
@@ -128,7 +128,7 @@ func (r *ElastiServiceReconciler) watchScaleTargetRef(ctx context.Context, es *v
128128
return fmt.Errorf("scaleTargetRef is incomplete: %w", k8shelper.ErrNoScaleTargetFound)
129129
}
130130

131-
crd, found := crdDirectory.CRDDirectory.GetCRD(es.Spec.Service)
131+
crd, found := crddirectory.CRDDirectory.GetCRD(es.Spec.Service)
132132
if found {
133133
if es.Spec.ScaleTargetRef.Name != crd.Spec.ScaleTargetRef.Name ||
134134
es.Spec.ScaleTargetRef.Kind != crd.Spec.ScaleTargetRef.Kind ||
@@ -201,7 +201,7 @@ func (r *ElastiServiceReconciler) watchPublicService(ctx context.Context, es *v1
201201
return nil
202202
}
203203

204-
func (r *ElastiServiceReconciler) finalizeCRDIfDeleted(ctx context.Context, es *v1alpha1.ElastiService, req ctrl.Request) (delete bool, err error) {
204+
func (r *ElastiServiceReconciler) finalizeCRDIfDeleted(ctx context.Context, es *v1alpha1.ElastiService, req ctrl.Request) (isDeleted bool, err error) {
205205
// If the ElastiService is being deleted, we need to clean up the resources
206206
if !es.ObjectMeta.DeletionTimestamp.IsZero() {
207207
defer func() {

operator/internal/controller/opsEndpointslices.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func (r *ElastiServiceReconciler) getIPsForResolver(ctx context.Context) ([]stri
2121
"kubernetes.io/service-name": resolverServiceName,
2222
}); err != nil {
2323
r.Logger.Error("Failed to get Resolver endpoint slices", zap.Error(err))
24-
return nil, err
24+
return nil, fmt.Errorf("getIPsForResolver: %w", err)
2525
}
2626
var resolverPodIPs []string
2727
for _, endpointSlice := range resolverSlices.Items {
@@ -70,7 +70,7 @@ func (r *ElastiServiceReconciler) createOrUpdateEndpointsliceToResolver(ctx cont
7070
sliceToResolver := &networkingv1.EndpointSlice{}
7171
if err := r.Get(ctx, EndpointsliceNamespacedName, sliceToResolver); err != nil && !errors.IsNotFound(err) {
7272
r.Logger.Debug("Error getting a endpoint slice to Resolver", zap.String("endpointslice", EndpointsliceNamespacedName.String()), zap.Error(err))
73-
return err
73+
return fmt.Errorf("createOrUpdateEndpointsliceToResolver: %w", err)
7474
} else if errors.IsNotFound(err) {
7575
// TODO: This can be handled better
7676
// This is a similar case as seen in resolver informer
@@ -101,7 +101,7 @@ func (r *ElastiServiceReconciler) createOrUpdateEndpointsliceToResolver(ctx cont
101101
},
102102
}
103103

104-
//sliceToResolver.DeepCopy()
104+
// sliceToResolver.DeepCopy()
105105

106106
for _, ip := range resolverPodIPs {
107107
newEndpointSlice.Endpoints = append(newEndpointSlice.Endpoints, networkingv1.Endpoint{
@@ -112,14 +112,14 @@ func (r *ElastiServiceReconciler) createOrUpdateEndpointsliceToResolver(ctx cont
112112
if isResolverSliceFound {
113113
if err := r.Update(ctx, newEndpointSlice); err != nil {
114114
r.Logger.Error("failed to update sliceToResolver", zap.String("endpointslice", EndpointsliceNamespacedName.String()), zap.Error(err))
115-
return err
115+
return fmt.Errorf("createOrUpdateEndpointsliceToResolver: %w", err)
116116
}
117117
r.Logger.Info("EndpointSlice updated successfully", zap.String("endpointslice", EndpointsliceNamespacedName.String()))
118118
} else {
119119
// TODOS: Make sure the private service is owned by the ElastiService
120120
if err := r.Create(ctx, newEndpointSlice); err != nil {
121121
r.Logger.Error("failed to create sliceToResolver", zap.String("endpointslice", EndpointsliceNamespacedName.String()), zap.Error(err))
122-
return err
122+
return fmt.Errorf("createOrUpdateEndpointsliceToResolver: %w", err)
123123
}
124124
r.Logger.Info("EndpointSlice created successfully", zap.String("endpointslice", EndpointsliceNamespacedName.String()))
125125
}

operator/internal/controller/opsInformer.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ func (r *ElastiServiceReconciler) getResolverChangeHandler(ctx context.Context,
5858
}
5959
prom.InformerHandlerCounter.WithLabelValues(req.String(), key, errStr).Inc()
6060
},
61-
UpdateFunc: func(old, new interface{}) {
61+
UpdateFunc: func(_, newObj interface{}) {
6262
errStr := values.Success
63-
err := r.handleResolverChanges(ctx, new, es.Spec.Service, req.Namespace)
63+
err := r.handleResolverChanges(ctx, newObj, es.Spec.Service, req.Namespace)
6464
if err != nil {
6565
errStr = err.Error()
6666
r.Logger.Error("Failed to handle resolver changes", zap.Error(err))
@@ -69,7 +69,7 @@ func (r *ElastiServiceReconciler) getResolverChangeHandler(ctx context.Context,
6969
}
7070
prom.InformerHandlerCounter.WithLabelValues(req.String(), key, errStr).Inc()
7171
},
72-
DeleteFunc: func(obj interface{}) {
72+
DeleteFunc: func(_ interface{}) {
7373
// TODO: Handle deletion of resolver deployment
7474
// We can do two things here
7575
// 1. We can move to the serve mode
@@ -103,9 +103,9 @@ func (r *ElastiServiceReconciler) getPublicServiceChangeHandler(ctx context.Cont
103103
}
104104
prom.InformerHandlerCounter.WithLabelValues(req.String(), key, errStr).Inc()
105105
},
106-
UpdateFunc: func(old, new interface{}) {
106+
UpdateFunc: func(_, newObj interface{}) {
107107
errStr := values.Success
108-
err := r.handlePublicServiceChanges(ctx, new, es.Spec.Service, req.Namespace)
108+
err := r.handlePublicServiceChanges(ctx, newObj, es.Spec.Service, req.Namespace)
109109
if err != nil {
110110
errStr = err.Error()
111111
r.Logger.Error("Failed to handle public service changes", zap.Error(err))
@@ -114,7 +114,7 @@ func (r *ElastiServiceReconciler) getPublicServiceChangeHandler(ctx context.Cont
114114
}
115115
prom.InformerHandlerCounter.WithLabelValues(req.String(), key, errStr).Inc()
116116
},
117-
DeleteFunc: func(obj interface{}) {
117+
DeleteFunc: func(_ interface{}) {
118118
r.Logger.Debug("public deployment deleted",
119119
zap.String("es", req.String()),
120120
zap.String("service", es.Spec.Service))
@@ -130,9 +130,9 @@ func (r *ElastiServiceReconciler) getScaleTargetRefChangeHandler(ctx context.Con
130130
Resource: es.Spec.ScaleTargetRef.Name,
131131
})
132132
return cache.ResourceEventHandlerFuncs{
133-
UpdateFunc: func(old, new interface{}) {
133+
UpdateFunc: func(_, newObj interface{}) {
134134
errStr := values.Success
135-
err := r.handleScaleTargetRefChanges(ctx, new, es, req)
135+
err := r.handleScaleTargetRefChanges(ctx, newObj, es, req)
136136
if err != nil {
137137
errStr = err.Error()
138138
r.Logger.Error("Failed to handle ScaleTargetRef changes", zap.Error(err))

operator/internal/controller/opsServices.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717

1818
func (r *ElastiServiceReconciler) deletePrivateService(ctx context.Context, publichServiceNamespacedName types.NamespacedName) (err error) {
1919
privateServiceNamespacedName := publichServiceNamespacedName
20-
privateServiceNamespacedName.Name = utils.GetPrivateSerivceName(publichServiceNamespacedName.Name)
20+
privateServiceNamespacedName.Name = utils.GetPrivateServiceName(publichServiceNamespacedName.Name)
2121
privateSVC := &v1.Service{}
2222
if err := r.Get(ctx, privateServiceNamespacedName, privateSVC); err != nil && !errors.IsNotFound(err) {
2323
return fmt.Errorf("failed to get private service: %w", err)
@@ -31,9 +31,9 @@ func (r *ElastiServiceReconciler) deletePrivateService(ctx context.Context, publ
3131
return nil
3232
}
3333

34-
func (r *ElastiServiceReconciler) checkAndCreatePrivateService(ctx context.Context, publicSVC *v1.Service, es *v1alpha1.ElastiService) (PVTName string, err error) {
35-
PVTName = utils.GetPrivateSerivceName(publicSVC.Name)
36-
privateServiceNamespacedName := types.NamespacedName{Name: PVTName, Namespace: publicSVC.Namespace}
34+
func (r *ElastiServiceReconciler) checkAndCreatePrivateService(ctx context.Context, publicSVC *v1.Service, es *v1alpha1.ElastiService) (privateServiceName string, err error) {
35+
privateServiceName = utils.GetPrivateServiceName(publicSVC.Name)
36+
privateServiceNamespacedName := types.NamespacedName{Name: privateServiceName, Namespace: publicSVC.Namespace}
3737
// See if private service already exist
3838
privateSVC := &v1.Service{}
3939
if err := r.Get(ctx, privateServiceNamespacedName, privateSVC); err != nil && !errors.IsNotFound(err) {
@@ -42,11 +42,11 @@ func (r *ElastiServiceReconciler) checkAndCreatePrivateService(ctx context.Conte
4242
r.Logger.Info("Private service not found, creating one", zap.String("private-service", privateServiceNamespacedName.String()))
4343
} else {
4444
r.Logger.Info("Private service already exists", zap.String("private-service", privateServiceNamespacedName.String()))
45-
return PVTName, nil
45+
return privateServiceName, nil
4646
}
4747

4848
privateSVC = publicSVC.DeepCopy()
49-
privateSVC.SetName(PVTName)
49+
privateSVC.SetName(privateServiceName)
5050
// We must remove the cluster IP and node port, as it already exists for the public service
5151
privateSVC.Spec.ClusterIP = ""
5252
privateSVC.Spec.ClusterIPs = nil
@@ -58,14 +58,14 @@ func (r *ElastiServiceReconciler) checkAndCreatePrivateService(ctx context.Conte
5858

5959
// Make sure the private service is owned by the ElastiService
6060
if err := controllerutil.SetControllerReference(es, privateSVC, r.Scheme); err != nil {
61-
return PVTName, err
61+
return privateServiceName, fmt.Errorf("checkAndCreatePrivateService: %w", err)
6262
}
6363
err = r.Create(ctx, privateSVC)
6464
if err != nil {
6565
r.Logger.Error("Failed to create private service", zap.String("private-service", privateServiceNamespacedName.String()), zap.Error(err))
66-
return PVTName, err
66+
return privateServiceName, fmt.Errorf("checkAndCreatePrivateService: %w", err)
6767
}
68-
return PVTName, nil
68+
return privateServiceName, nil
6969
}
7070

7171
// handlePublicServiceChanges handles the changes in the public service, and sync those changes in the private service
@@ -81,7 +81,7 @@ func (r *ElastiServiceReconciler) handlePublicServiceChanges(ctx context.Context
8181
return fmt.Errorf("public service is not same as mentioned in CRD; informer misconfigured")
8282
}
8383
// Get Private Service
84-
PVTName := utils.GetPrivateSerivceName(publicSVC.Name)
84+
PVTName := utils.GetPrivateServiceName(publicSVC.Name)
8585
privateServiceNamespacedName := types.NamespacedName{Name: PVTName, Namespace: publicSVC.Namespace}
8686
privateSVC := &v1.Service{}
8787
if err := r.Get(ctx, privateServiceNamespacedName, privateSVC); err != nil && !errors.IsNotFound(err) {

operator/internal/crdDirectory/directory.go operator/internal/crddirectory/directory.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package crdDirectory
1+
package crddirectory
22

33
import (
44
"sync"

0 commit comments

Comments
 (0)