Skip to content

Commit 01440d5

Browse files
authored
Enable golangci-lint (#330)
Add golangci-lint to the CI pipeline to help maintain code cleanliness. * Fixup various issues discovered. Signed-off-by: SuperQ <superq@gmail.com>
1 parent 472a099 commit 01440d5

29 files changed

+85
-99
lines changed

.github/workflows/ci.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ jobs:
3838
- name: Code Check
3939
run: |
4040
make code-check
41+
- name: Lint
42+
uses: golangci/golangci-lint-action@1481404843c368bc19ca9406f87d6e0fc97bdcfd # v7.0.0
43+
with:
44+
args: --verbose
45+
version: v2.0.2
4146
unit-test:
4247
name: Unit Test
4348
runs-on: ubuntu-latest

.golangci.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
version: "2"
2+
formatters:
3+
enable:
4+
- goimports
5+
settings:
6+
goimports:
7+
local-prefixes:
8+
- github.com/zilliztech/milvus-operator
9+
linters:
10+
exclusions:
11+
rules:
12+
- linters:
13+
- errcheck
14+
# Taken from the default exclusions in v1.
15+
text: Error return value of .((os\.)?std(out|err)\..*|.*Close|.*Flush|os\.Remove(All)?|.*print(f|ln)?|os\.(Un)?Setenv). is not checked
16+
- linters:
17+
- errcheck
18+
path: _test.go

apis/milvus.io/v1alpha1/milvus_webhook.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,10 @@ package v1alpha1
1919
import (
2020
ctrl "sigs.k8s.io/controller-runtime"
2121
"sigs.k8s.io/controller-runtime/pkg/conversion"
22-
logf "sigs.k8s.io/controller-runtime/pkg/log"
2322

2423
v1beta1 "github.com/zilliztech/milvus-operator/apis/milvus.io/v1beta1"
2524
)
2625

27-
var milvuslog = logf.Log.WithName("milvus-v1alpha1")
28-
2926
func (r *Milvus) SetupWebhookWithManager(mgr ctrl.Manager) error {
3027
return ctrl.NewWebhookManagedBy(mgr).
3128
For(r).

apis/milvus.io/v1beta1/milvus_types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func (ms MilvusSpec) GetServiceComponent() *ServiceComponent {
114114
// GetMilvusVersionByImage returns the version of Milvus by ms.Com.ComponentSpec.Image
115115
func (ms MilvusSpec) GetMilvusVersionByImage() (semver.Version, error) {
116116
// parse format: registry/namespace/image:tag
117-
splited := strings.Split(ms.Com.ComponentSpec.Image, ":")
117+
splited := strings.Split(ms.Com.Image, ":")
118118
if len(splited) != 2 {
119119
return semver.Version{}, errors.Errorf("unknown version of image[%s]", splited[0])
120120
}

apis/milvus.io/v1beta1/milvus_types_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,15 +206,15 @@ func TestGetMilvusVersionByGlobalImage(t *testing.T) {
206206
_, err = m.Spec.GetMilvusVersionByImage()
207207
assert.NoError(t, err)
208208

209-
m.Spec.Com.ComponentSpec.Image = "milvusdb/milvus:v2.3.1-beta1"
209+
m.Spec.Com.Image = "milvusdb/milvus:v2.3.1-beta1"
210210
ver, err := m.Spec.GetMilvusVersionByImage()
211211
assert.NoError(t, err)
212212
assert.Equal(t, uint64(2), ver.Major)
213213
assert.Equal(t, uint64(3), ver.Minor)
214214
assert.Equal(t, uint64(1), ver.Patch)
215215
assert.Equal(t, "beta1", ver.Pre[0].VersionStr)
216216

217-
m.Spec.Com.ComponentSpec.Image = "harbor.milvus.io/milvus/milvus:latest"
217+
m.Spec.Com.Image = "harbor.milvus.io/milvus/milvus:latest"
218218
_, err = m.Spec.GetMilvusVersionByImage()
219219
assert.Error(t, err)
220220
}

apis/milvus.io/v1beta1/milvus_webhook.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
"k8s.io/apimachinery/pkg/runtime/schema"
2828
"k8s.io/apimachinery/pkg/util/validation/field"
2929
ctrl "sigs.k8s.io/controller-runtime"
30-
logf "sigs.k8s.io/controller-runtime/pkg/log"
3130
"sigs.k8s.io/controller-runtime/pkg/webhook"
3231
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
3332

@@ -36,9 +35,6 @@ import (
3635
"github.com/zilliztech/milvus-operator/pkg/util"
3736
)
3837

39-
// log is for logging in this package.
40-
var milvuslog = logf.Log.WithName("milvus-resource")
41-
4238
func (r *Milvus) SetupWebhookWithManager(mgr ctrl.Manager) error {
4339
return ctrl.NewWebhookManagedBy(mgr).
4440
For(r).

apis/milvus.io/v1beta1/persistence_types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,6 @@ type PersistentVolumeClaim struct {
4646

4747
func (p *PersistentVolumeClaim) GetSpec() *corev1.PersistentVolumeClaimSpec {
4848
ret := new(corev1.PersistentVolumeClaimSpec)
49-
p.Spec.AsObject(ret)
49+
p.Spec.AsObject(ret) //nolint:errcheck
5050
return ret
5151
}

main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ func main() {
4848
var enablePprof bool
4949
var probeAddr string
5050
var workDir string
51-
var k8sQps int = 100
52-
var k8sBurst int = 100
51+
var k8sQps = 100
52+
var k8sBurst = 100
5353
var enableWebhook bool
5454
showVersion := flag.Bool("version", false, "Show version")
5555
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")

pkg/config/config.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package config
22

33
import (
4-
"io/ioutil"
54
"os"
65

76
corev1 "k8s.io/api/core/v1"
@@ -75,12 +74,12 @@ func NewConfig(workDir string) (*Config, error) {
7574

7675
templateDir := workDir + TemplateRelativeDir
7776

78-
tmpls, err := ioutil.ReadDir(templateDir)
77+
tmpls, err := os.ReadDir(templateDir)
7978
if err != nil {
8079
return nil, err
8180
}
8281
for _, tmpl := range tmpls {
83-
data, err := ioutil.ReadFile(templateDir + "/" + tmpl.Name())
82+
data, err := os.ReadFile(templateDir + "/" + tmpl.Name())
8483
if err != nil {
8584
return nil, err
8685
}

pkg/controllers/components_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ func TestMilvusComponent_GetReplicas(t *testing.T) {
301301
spec.Com.QueryNode = &v1beta1.MilvusQueryNode{}
302302
com := QueryNode
303303
replica := int32(1)
304-
spec.Com.QueryNode.Component.Replicas = &replica
304+
spec.Com.QueryNode.Replicas = &replica
305305
assert.Equal(t, &replica, com.GetReplicas(spec))
306306
}
307307

@@ -442,7 +442,7 @@ func TestMilvusComponent_GetComponentPort(t *testing.T) {
442442

443443
func TestMilvusComponent_GetComponentSpec(t *testing.T) {
444444
spec := newSpecCluster()
445-
spec.Com.QueryNode.Component.ComponentSpec.Image = "a"
445+
spec.Com.QueryNode.Image = "a"
446446
com := QueryNode
447447
assert.Equal(t, "a", com.GetComponentSpec(spec).Image)
448448
}

pkg/controllers/dependencies.go

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func (l LocalHelmReconciler) NewHelmCfg(namespace string) *action.Configuration
6767
}
6868

6969
// cfg.Init will never return err, only panic if bad driver
70-
cfg.Init(
70+
_ = cfg.Init(
7171
getRESTClientGetterFromClient(l.helmSettings, namespace, l.mgr),
7272
namespace,
7373
os.Getenv("HELM_DRIVER"),
@@ -77,20 +77,6 @@ func (l LocalHelmReconciler) NewHelmCfg(namespace string) *action.Configuration
7777
return cfg
7878
}
7979

80-
func getRESTClientGetterWithNamespace(env *cli.EnvSettings, namespace string) genericclioptions.RESTClientGetter {
81-
return &genericclioptions.ConfigFlags{
82-
Namespace: &namespace,
83-
Context: &env.KubeContext,
84-
BearerToken: &env.KubeToken,
85-
APIServer: &env.KubeAPIServer,
86-
CAFile: &env.KubeCaFile,
87-
KubeConfig: &env.KubeConfig,
88-
Impersonate: &env.KubeAsUser,
89-
ImpersonateGroup: &env.KubeAsGroups,
90-
Insecure: &configFlagInsecure,
91-
}
92-
}
93-
9480
func getRESTClientGetterFromClient(env *cli.EnvSettings, namespace string, mgr manager.Manager) genericclioptions.RESTClientGetter {
9581
return &clientRESTClientGetter{
9682
namespace: namespace,

pkg/controllers/deploy_ctrl.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,8 @@ func (c *DeployControllerBizImpl) HandleCreate(ctx context.Context, mc v1beta1.M
228228
if err == nil {
229229
return nil
230230
}
231-
switch {
232-
case err == ErrNotFound:
231+
switch err {
232+
case ErrNotFound:
233233
err := c.util.MarkMilvusComponentGroupId(ctx, mc, c.component, 0)
234234
if err != nil {
235235
return errors.Wrapf(err, "mark milvus %s group id to %d", c.component.Name, 0)
@@ -238,7 +238,7 @@ func (c *DeployControllerBizImpl) HandleCreate(ctx context.Context, mc v1beta1.M
238238
if err != nil {
239239
return errors.Wrapf(err, "create %s deployment 0", c.component.Name)
240240
}
241-
case err == ErrNoLastDeployment:
241+
case ErrNoLastDeployment:
242242
return c.util.CreateDeploy(ctx, mc, nil, 1)
243243
default:
244244
return errors.Wrapf(err, "get %s deploys", c.component.Name)

pkg/controllers/deploy_ctrl_util.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ func (c *DeployControllerBizUtilImpl) LastRolloutFinished(ctx context.Context, m
261261
return false, nil
262262
}
263263
// make sure all old pods are down
264-
pods, err := c.K8sUtil.ListDeployPods(ctx, lastDeployment, c.component)
264+
pods, err := c.ListDeployPods(ctx, lastDeployment, c.component)
265265
if err != nil {
266266
return false, err
267267
}
@@ -497,7 +497,7 @@ func (c *DeployControllerBizUtilImpl) doScaleAction(ctx context.Context, action
497497
return nil
498498
}
499499
action.deploy.Spec.Replicas = int32Ptr(getDeployReplicas(action.deploy) + action.replicaChange)
500-
return c.K8sUtil.UpdateAndRequeue(ctx, action.deploy)
500+
return c.UpdateAndRequeue(ctx, action.deploy)
501501
}
502502

503503
func (c *DeployControllerBizUtilImpl) markDeployAsCurrent(ctx context.Context, mc v1beta1.Milvus, currentDeployment *appsv1.Deployment) error {
@@ -510,20 +510,20 @@ func (c *DeployControllerBizUtilImpl) markDeployAsCurrent(ctx context.Context, m
510510
}
511511

512512
func (c *DeployControllerBizUtilImpl) checkDeploymentsStable(ctx context.Context, currentDeployment, lastDeployment *appsv1.Deployment) error {
513-
lastDeployPods, err := c.K8sUtil.ListDeployPods(ctx, lastDeployment, c.component)
513+
lastDeployPods, err := c.ListDeployPods(ctx, lastDeployment, c.component)
514514
if err != nil {
515515
return errors.Wrap(err, "list last deploy pods")
516516
}
517-
isStable, reason := c.K8sUtil.DeploymentIsStable(lastDeployment, lastDeployPods)
517+
isStable, reason := c.DeploymentIsStable(lastDeployment, lastDeployPods)
518518
if !isStable {
519519
return errors.Wrapf(ErrRequeue, "last deploy is not stable[%s]", reason)
520520
}
521521

522-
currentDeployPods, err := c.K8sUtil.ListDeployPods(ctx, currentDeployment, c.component)
522+
currentDeployPods, err := c.ListDeployPods(ctx, currentDeployment, c.component)
523523
if err != nil {
524524
return errors.Wrap(err, "list current deploy pods")
525525
}
526-
isStable, reason = c.K8sUtil.DeploymentIsStable(currentDeployment, currentDeployPods)
526+
isStable, reason = c.DeploymentIsStable(currentDeployment, currentDeployPods)
527527
if !isStable {
528528
return errors.Wrapf(ErrRequeue, "current deploy is not stable[%s]", reason)
529529
}

pkg/controllers/deployment_updater.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ func newMilvusDeploymentUpdater(m v1beta1.Milvus, scheme *runtime.Scheme, compon
409409
}
410410

411411
func (m milvusDeploymentUpdater) GetPersistenceConfig() *v1beta1.Persistence {
412-
return m.Milvus.Spec.GetPersistenceConfig()
412+
return m.Spec.GetPersistenceConfig()
413413
}
414414

415415
func (m milvusDeploymentUpdater) GetIntanceName() string {
@@ -455,7 +455,7 @@ func (m milvusDeploymentUpdater) GetInitContainers() []corev1.Container {
455455
}
456456

457457
func (m milvusDeploymentUpdater) GetDeploymentStrategy() appsv1.DeploymentStrategy {
458-
if m.Milvus.Spec.Com.ImageUpdateMode == v1beta1.ImageUpdateModeForce {
458+
if m.Spec.Com.ImageUpdateMode == v1beta1.ImageUpdateModeForce {
459459
all := intstr.FromString("100%")
460460
return appsv1.DeploymentStrategy{
461461
Type: appsv1.RollingUpdateDeploymentStrategyType,
@@ -465,7 +465,7 @@ func (m milvusDeploymentUpdater) GetDeploymentStrategy() appsv1.DeploymentStrate
465465
},
466466
}
467467
}
468-
return m.component.GetDeploymentStrategy(m.Milvus.Spec.Conf.Data)
468+
return m.component.GetDeploymentStrategy(m.Spec.Conf.Data)
469469
}
470470

471471
func (m milvusDeploymentUpdater) GetConfCheckSum() string {
@@ -480,7 +480,7 @@ func (m milvusDeploymentUpdater) GetMergedComponentSpec() ComponentSpec {
480480
}
481481

482482
func (m milvusDeploymentUpdater) GetArgs() []string {
483-
var ret = []string{}
483+
var ret []string
484484
if len(m.GetMergedComponentSpec().Commands) > 0 {
485485
ret = append([]string{RunScriptPath}, m.GetMergedComponentSpec().Commands...)
486486
} else {
@@ -501,7 +501,7 @@ func (m milvusDeploymentUpdater) GetMilvus() *v1beta1.Milvus {
501501
}
502502

503503
func (m milvusDeploymentUpdater) RollingUpdateImageDependencyReady() bool {
504-
if m.Milvus.Status.ObservedGeneration < m.Milvus.Generation {
504+
if m.Status.ObservedGeneration < m.Generation {
505505
return false
506506
}
507507
deps := m.component.GetDependencies(m.Spec)
@@ -514,5 +514,5 @@ func (m milvusDeploymentUpdater) RollingUpdateImageDependencyReady() bool {
514514
}
515515

516516
func (m milvusDeploymentUpdater) HasHookConfig() bool {
517-
return len(m.Milvus.Spec.HookConf.Data) > 0
517+
return len(m.Spec.HookConf.Data) > 0
518518
}

pkg/controllers/deployments.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package controllers
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67

78
appsv1 "k8s.io/api/apps/v1"
@@ -11,7 +12,6 @@ import (
1112
"k8s.io/apimachinery/pkg/labels"
1213
"sigs.k8s.io/controller-runtime/pkg/client"
1314

14-
"github.com/pkg/errors"
1515
pkgerr "github.com/pkg/errors"
1616

1717
"github.com/zilliztech/milvus-operator/apis/milvus.io/v1beta1"
@@ -40,7 +40,7 @@ const (
4040

4141
var (
4242
DefaultConfigMapMode = corev1.ConfigMapVolumeSourceDefaultMode
43-
ErrRequeue = pkgerr.New("requeue")
43+
ErrRequeue = errors.New("requeue")
4444
)
4545

4646
func GetStorageSecretRefEnv(secretRef string) []corev1.EnvVar {
@@ -79,7 +79,7 @@ func (r *MilvusReconciler) updateDeployment(
7979
updater := newMilvusDeploymentUpdater(mc, r.Scheme, component)
8080
hasTerminatingPod, err := CheckComponentHasTerminatingPod(ctx, r.Client, mc, component)
8181
if err != nil {
82-
return errors.Wrap(err, "check component has terminating pod")
82+
return pkgerr.Wrap(err, "check component has terminating pod")
8383
}
8484
if hasTerminatingPod {
8585
return updateDeploymentWithoutPodTemplate(deployment, updater)
@@ -148,9 +148,9 @@ func (r *MilvusReconciler) handleOldInstanceChangingMode(ctx context.Context, mc
148148

149149
mc.Annotations[v1beta1.PodServiceLabelAddedAnnotation] = v1beta1.TrueStr
150150
if err := r.Update(ctx, &mc); err != nil {
151-
return errors.Wrap(err, "update milvus annotation")
151+
return pkgerr.Wrap(err, "update milvus annotation")
152152
}
153-
return errors.Wrap(ErrRequeue, "requeue after updated milvus annotation")
153+
return pkgerr.Wrap(ErrRequeue, "requeue after updated milvus annotation")
154154
}
155155
return nil
156156
}

pkg/controllers/ingress_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func TestMilvusClusterReconciler_ReconcileIngress(t *testing.T) {
9797
}).Return(nil)
9898
mockClient.EXPECT().Update(gomock.Any(), &rendered).Return(mockErr)
9999
err := r.ReconcileIngress(ctx, mc)
100-
assert.Equal(t, finalizers, rendered.ObjectMeta.Finalizers)
100+
assert.Equal(t, finalizers, rendered.Finalizers)
101101
assert.Error(t, err)
102102
})
103103
}

0 commit comments

Comments
 (0)