Skip to content

Commit c323d36

Browse files
authored
chore: enable dot-imports, duplicated-imports from revive (argoproj#678)
Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
1 parent 782fb85 commit c323d36

23 files changed

+312
-312
lines changed

.golangci.yaml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ linters-settings:
4545
- wrapperFunc #FIXME
4646
goimports:
4747
local-prefixes: github.com/argoproj/gitops-engine
48+
importas:
49+
alias:
50+
- alias: corev1
51+
pkg: k8s.io/api/core/v1
52+
- alias: apierrors
53+
pkg: k8s.io/apimachinery/pkg/api/errors
54+
- alias: metav1
55+
pkg: k8s.io/apimachinery/pkg/apis/meta/v1
56+
- alias: testingutils
57+
pkg: github.com/argoproj/gitops-engine/pkg/utils/testing
4858
perfsprint:
4959
# Optimizes even if it requires an int or uint type cast.
5060
int-conversion: true
@@ -68,9 +78,7 @@ linters-settings:
6878
- name: context-keys-type
6979
disabled: true
7080
- name: dot-imports
71-
disabled: true
7281
- name: duplicated-imports
73-
disabled: true
7482
- name: early-return
7583
arguments:
7684
- 'preserveScope'

pkg/cache/cluster.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ import (
1313
"golang.org/x/sync/semaphore"
1414
authorizationv1 "k8s.io/api/authorization/v1"
1515
v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
16-
"k8s.io/apimachinery/pkg/api/errors"
17-
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
16+
apierrors "k8s.io/apimachinery/pkg/api/errors"
1817
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1918
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2019
"k8s.io/apimachinery/pkg/runtime"
@@ -670,7 +669,7 @@ func (c *clusterCache) watchEvents(ctx context.Context, api kube.APIResourceInfo
670669
w, err := watchutil.NewRetryWatcher(resourceVersion, &cache.ListWatch{
671670
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
672671
res, err := resClient.Watch(ctx, options)
673-
if errors.IsNotFound(err) {
672+
if apierrors.IsNotFound(err) {
674673
c.stopWatching(api.GroupKind, ns)
675674
}
676675
return res, err
@@ -807,7 +806,7 @@ func (c *clusterCache) processApi(client dynamic.Interface, api kube.APIResource
807806

808807
// isRestrictedResource checks if the kube api call is unauthorized or forbidden
809808
func (c *clusterCache) isRestrictedResource(err error) bool {
810-
return c.respectRBAC != RespectRbacDisabled && (k8sErrors.IsForbidden(err) || k8sErrors.IsUnauthorized(err))
809+
return c.respectRBAC != RespectRbacDisabled && (apierrors.IsForbidden(err) || apierrors.IsUnauthorized(err))
811810
}
812811

813812
// checkPermission runs a self subject access review to check if the controller has permissions to list the resource
@@ -1196,7 +1195,7 @@ func (c *clusterCache) IsNamespaced(gk schema.GroupKind) (bool, error) {
11961195
if isNamespaced, ok := c.namespacedResources[gk]; ok {
11971196
return isNamespaced, nil
11981197
}
1199-
return false, errors.NewNotFound(schema.GroupResource{Group: gk.Group}, "")
1198+
return false, apierrors.NewNotFound(schema.GroupResource{Group: gk.Group}, "")
12001199
}
12011200

12021201
func (c *clusterCache) managesNamespace(namespace string) bool {
@@ -1249,7 +1248,7 @@ func (c *clusterCache) GetManagedLiveObjs(targetObjs []*unstructured.Unstructure
12491248
var err error
12501249
managedObj, err = c.kubectl.GetResource(context.TODO(), c.config, targetObj.GroupVersionKind(), existingObj.Ref.Name, existingObj.Ref.Namespace)
12511250
if err != nil {
1252-
if errors.IsNotFound(err) {
1251+
if apierrors.IsNotFound(err) {
12531252
return nil
12541253
}
12551254
return err
@@ -1259,7 +1258,7 @@ func (c *clusterCache) GetManagedLiveObjs(targetObjs []*unstructured.Unstructure
12591258
var err error
12601259
managedObj, err = c.kubectl.GetResource(context.TODO(), c.config, targetObj.GroupVersionKind(), targetObj.GetName(), targetObj.GetNamespace())
12611260
if err != nil {
1262-
if errors.IsNotFound(err) {
1261+
if apierrors.IsNotFound(err) {
12631262
return nil
12641263
}
12651264
return err
@@ -1274,7 +1273,7 @@ func (c *clusterCache) GetManagedLiveObjs(targetObjs []*unstructured.Unstructure
12741273
c.log.V(1).Info(fmt.Sprintf("Failed to convert resource: %v", err))
12751274
managedObj, err = c.kubectl.GetResource(context.TODO(), c.config, targetObj.GroupVersionKind(), managedObj.GetName(), managedObj.GetNamespace())
12761275
if err != nil {
1277-
if errors.IsNotFound(err) {
1276+
if apierrors.IsNotFound(err) {
12781277
return nil
12791278
}
12801279
return err

pkg/cache/cluster_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"github.com/stretchr/testify/require"
1919
appsv1 "k8s.io/api/apps/v1"
2020
corev1 "k8s.io/api/core/v1"
21-
v1 "k8s.io/api/core/v1"
2221
extensionsv1beta1 "k8s.io/api/extensions/v1beta1"
2322
"k8s.io/apimachinery/pkg/api/meta"
2423
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -238,7 +237,7 @@ func TestStatefulSetOwnershipInferred(t *testing.T) {
238237
TypeMeta: metav1.TypeMeta{APIVersion: "apps/v1", Kind: kube.StatefulSetKind},
239238
ObjectMeta: metav1.ObjectMeta{UID: "123", Name: "web", Namespace: "default"},
240239
Spec: appsv1.StatefulSetSpec{
241-
VolumeClaimTemplates: []v1.PersistentVolumeClaim{{
240+
VolumeClaimTemplates: []corev1.PersistentVolumeClaim{{
242241
ObjectMeta: metav1.ObjectMeta{
243242
Name: "www",
244243
},
@@ -249,14 +248,14 @@ func TestStatefulSetOwnershipInferred(t *testing.T) {
249248
tests := []struct {
250249
name string
251250
cluster *clusterCache
252-
pvc *v1.PersistentVolumeClaim
251+
pvc *corev1.PersistentVolumeClaim
253252
expectedRefs []metav1.OwnerReference
254253
expectNoOwner bool
255254
}{
256255
{
257256
name: "STSTemplateNameNotMatching",
258257
cluster: newCluster(t, sts),
259-
pvc: &v1.PersistentVolumeClaim{
258+
pvc: &corev1.PersistentVolumeClaim{
260259
TypeMeta: metav1.TypeMeta{Kind: kube.PersistentVolumeClaimKind},
261260
ObjectMeta: metav1.ObjectMeta{Name: "www1-web-0", Namespace: "default"},
262261
},
@@ -265,7 +264,7 @@ func TestStatefulSetOwnershipInferred(t *testing.T) {
265264
{
266265
name: "MatchingSTSExists",
267266
cluster: newCluster(t, sts),
268-
pvc: &v1.PersistentVolumeClaim{
267+
pvc: &corev1.PersistentVolumeClaim{
269268
TypeMeta: metav1.TypeMeta{Kind: kube.PersistentVolumeClaimKind},
270269
ObjectMeta: metav1.ObjectMeta{Name: "www-web-0", Namespace: "default"},
271270
},
@@ -274,7 +273,7 @@ func TestStatefulSetOwnershipInferred(t *testing.T) {
274273
{
275274
name: "STSTemplateNameNotMatchingWithBatchProcessing",
276275
cluster: newClusterWithOptions(t, opts, sts),
277-
pvc: &v1.PersistentVolumeClaim{
276+
pvc: &corev1.PersistentVolumeClaim{
278277
TypeMeta: metav1.TypeMeta{Kind: kube.PersistentVolumeClaimKind},
279278
ObjectMeta: metav1.ObjectMeta{Name: "www1-web-0", Namespace: "default"},
280279
},
@@ -283,7 +282,7 @@ func TestStatefulSetOwnershipInferred(t *testing.T) {
283282
{
284283
name: "MatchingSTSExistsWithBatchProcessing",
285284
cluster: newClusterWithOptions(t, opts, sts),
286-
pvc: &v1.PersistentVolumeClaim{
285+
pvc: &corev1.PersistentVolumeClaim{
287286
TypeMeta: metav1.TypeMeta{Kind: kube.PersistentVolumeClaimKind},
288287
ObjectMeta: metav1.ObjectMeta{Name: "www-web-0", Namespace: "default"},
289288
},

pkg/cache/predicates_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"github.com/stretchr/testify/assert"
1010
"github.com/stretchr/testify/require"
1111
appsv1 "k8s.io/api/apps/v1"
12-
v1 "k8s.io/api/core/v1"
12+
corev1 "k8s.io/api/core/v1"
1313
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1414
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1515
"k8s.io/client-go/rest"
@@ -25,7 +25,7 @@ func TestResourceOfGroupKind(t *testing.T) {
2525
Name: "deploy",
2626
},
2727
}
28-
service := &v1.Service{
28+
service := &corev1.Service{
2929
TypeMeta: metav1.TypeMeta{
3030
APIVersion: "",
3131
Kind: "Service",

pkg/cache/resource.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55

66
"k8s.io/apimachinery/pkg/types"
77

8-
v1 "k8s.io/api/core/v1"
8+
corev1 "k8s.io/api/core/v1"
99
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1010
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1111

@@ -17,7 +17,7 @@ type Resource struct {
1717
// ResourceVersion holds most recent observed resource version
1818
ResourceVersion string
1919
// Resource reference
20-
Ref v1.ObjectReference
20+
Ref corev1.ObjectReference
2121
// References to resource owners
2222
OwnerRefs []metav1.OwnerReference
2323
// Optional creation timestamp of the resource

pkg/diff/diff_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
"google.golang.org/protobuf/proto"
1818
appsv1 "k8s.io/api/apps/v1"
1919
corev1 "k8s.io/api/core/v1"
20-
v1 "k8s.io/api/core/v1"
2120
"k8s.io/apimachinery/pkg/api/equality"
2221
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2322
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@@ -126,18 +125,18 @@ func newDeployment() *appsv1.Deployment {
126125
"app": "demo",
127126
},
128127
},
129-
Template: v1.PodTemplateSpec{
128+
Template: corev1.PodTemplateSpec{
130129
ObjectMeta: metav1.ObjectMeta{
131130
Labels: map[string]string{
132131
"app": "demo",
133132
},
134133
},
135-
Spec: v1.PodSpec{
136-
Containers: []v1.Container{
134+
Spec: corev1.PodSpec{
135+
Containers: []corev1.Container{
137136
{
138137
Name: "demo",
139138
Image: "gcr.io/kuar-demo/kuard-amd64:1",
140-
Ports: []v1.ContainerPort{
139+
Ports: []corev1.ContainerPort{
141140
{
142141
ContainerPort: 80,
143142
},
@@ -302,7 +301,7 @@ func TestThreeWayDiff(t *testing.T) {
302301
// difference
303302
configBytes, err := json.Marshal(configDep)
304303
require.NoError(t, err)
305-
liveDep.Annotations[v1.LastAppliedConfigAnnotation] = string(configBytes)
304+
liveDep.Annotations[corev1.LastAppliedConfigAnnotation] = string(configBytes)
306305
configUn = mustToUnstructured(configDep)
307306
liveUn = mustToUnstructured(liveDep)
308307
res = diff(t, configUn, liveUn, diffOptionsForTest()...)
@@ -324,7 +323,7 @@ func TestThreeWayDiff(t *testing.T) {
324323
// last-applied-configuration annotation from the live object, and redo the diff. This time,
325324
// the diff will report not modified (because we have no way of knowing what was a defaulted
326325
// field without this annotation)
327-
delete(liveDep.Annotations, v1.LastAppliedConfigAnnotation)
326+
delete(liveDep.Annotations, corev1.LastAppliedConfigAnnotation)
328327
configUn = mustToUnstructured(configDep)
329328
liveUn = mustToUnstructured(liveDep)
330329
res = diff(t, configUn, liveUn, diffOptionsForTest()...)

pkg/sync/hook/delete_policy_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ import (
66
"github.com/stretchr/testify/assert"
77

88
"github.com/argoproj/gitops-engine/pkg/sync/common"
9-
. "github.com/argoproj/gitops-engine/pkg/utils/testing"
9+
testingutils "github.com/argoproj/gitops-engine/pkg/utils/testing"
1010
)
1111

1212
func TestDeletePolicies(t *testing.T) {
13-
assert.Equal(t, []common.HookDeletePolicy{common.HookDeletePolicyBeforeHookCreation}, DeletePolicies(NewPod()))
14-
assert.Equal(t, []common.HookDeletePolicy{common.HookDeletePolicyBeforeHookCreation}, DeletePolicies(Annotate(NewPod(), "argocd.argoproj.io/hook-delete-policy", "garbage")))
15-
assert.Equal(t, []common.HookDeletePolicy{common.HookDeletePolicyBeforeHookCreation}, DeletePolicies(Annotate(NewPod(), "argocd.argoproj.io/hook-delete-policy", "BeforeHookCreation")))
16-
assert.Equal(t, []common.HookDeletePolicy{common.HookDeletePolicyHookSucceeded}, DeletePolicies(Annotate(NewPod(), "argocd.argoproj.io/hook-delete-policy", "HookSucceeded")))
17-
assert.Equal(t, []common.HookDeletePolicy{common.HookDeletePolicyHookFailed}, DeletePolicies(Annotate(NewPod(), "argocd.argoproj.io/hook-delete-policy", "HookFailed")))
13+
assert.Equal(t, []common.HookDeletePolicy{common.HookDeletePolicyBeforeHookCreation}, DeletePolicies(testingutils.NewPod()))
14+
assert.Equal(t, []common.HookDeletePolicy{common.HookDeletePolicyBeforeHookCreation}, DeletePolicies(testingutils.Annotate(testingutils.NewPod(), "argocd.argoproj.io/hook-delete-policy", "garbage")))
15+
assert.Equal(t, []common.HookDeletePolicy{common.HookDeletePolicyBeforeHookCreation}, DeletePolicies(testingutils.Annotate(testingutils.NewPod(), "argocd.argoproj.io/hook-delete-policy", "BeforeHookCreation")))
16+
assert.Equal(t, []common.HookDeletePolicy{common.HookDeletePolicyHookSucceeded}, DeletePolicies(testingutils.Annotate(testingutils.NewPod(), "argocd.argoproj.io/hook-delete-policy", "HookSucceeded")))
17+
assert.Equal(t, []common.HookDeletePolicy{common.HookDeletePolicyHookFailed}, DeletePolicies(testingutils.Annotate(testingutils.NewPod(), "argocd.argoproj.io/hook-delete-policy", "HookFailed")))
1818
// Helm test
19-
assert.Equal(t, []common.HookDeletePolicy{common.HookDeletePolicyHookSucceeded}, DeletePolicies(Annotate(NewPod(), "helm.sh/hook-delete-policy", "hook-succeeded")))
19+
assert.Equal(t, []common.HookDeletePolicy{common.HookDeletePolicyHookSucceeded}, DeletePolicies(testingutils.Annotate(testingutils.NewPod(), "helm.sh/hook-delete-policy", "hook-succeeded")))
2020
}

pkg/sync/hook/helm/delete_policy_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ import (
66
"github.com/stretchr/testify/assert"
77

88
"github.com/argoproj/gitops-engine/pkg/sync/common"
9-
. "github.com/argoproj/gitops-engine/pkg/utils/testing"
9+
testingutils "github.com/argoproj/gitops-engine/pkg/utils/testing"
1010
)
1111

1212
func TestDeletePolicies(t *testing.T) {
13-
assert.Nil(t, DeletePolicies(NewPod()))
14-
assert.Equal(t, []DeletePolicy{BeforeHookCreation}, DeletePolicies(Annotate(NewPod(), "helm.sh/hook-delete-policy", "before-hook-creation")))
15-
assert.Equal(t, []DeletePolicy{HookSucceeded}, DeletePolicies(Annotate(NewPod(), "helm.sh/hook-delete-policy", "hook-succeeded")))
16-
assert.Equal(t, []DeletePolicy{HookFailed}, DeletePolicies(Annotate(NewPod(), "helm.sh/hook-delete-policy", "hook-failed")))
13+
assert.Nil(t, DeletePolicies(testingutils.NewPod()))
14+
assert.Equal(t, []DeletePolicy{BeforeHookCreation}, DeletePolicies(testingutils.Annotate(testingutils.NewPod(), "helm.sh/hook-delete-policy", "before-hook-creation")))
15+
assert.Equal(t, []DeletePolicy{HookSucceeded}, DeletePolicies(testingutils.Annotate(testingutils.NewPod(), "helm.sh/hook-delete-policy", "hook-succeeded")))
16+
assert.Equal(t, []DeletePolicy{HookFailed}, DeletePolicies(testingutils.Annotate(testingutils.NewPod(), "helm.sh/hook-delete-policy", "hook-failed")))
1717
}
1818

1919
func TestDeletePolicy_DeletePolicy(t *testing.T) {

pkg/sync/hook/helm/hook_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import (
55

66
"github.com/stretchr/testify/assert"
77

8-
. "github.com/argoproj/gitops-engine/pkg/utils/testing"
8+
testingutils "github.com/argoproj/gitops-engine/pkg/utils/testing"
99
)
1010

1111
func TestIsHook(t *testing.T) {
12-
assert.False(t, IsHook(NewPod()))
13-
assert.True(t, IsHook(Annotate(NewPod(), "helm.sh/hook", "anything")))
12+
assert.False(t, IsHook(testingutils.NewPod()))
13+
assert.True(t, IsHook(testingutils.Annotate(testingutils.NewPod(), "helm.sh/hook", "anything")))
1414
// helm calls "crd-install" a hook, but it really can't be treated as such
15-
assert.False(t, IsHook(Annotate(NewCRD(), "helm.sh/hook", "crd-install")))
15+
assert.False(t, IsHook(testingutils.Annotate(testingutils.NewCRD(), "helm.sh/hook", "crd-install")))
1616
}

pkg/sync/hook/helm/type_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,22 @@ import (
66
"github.com/stretchr/testify/assert"
77

88
"github.com/argoproj/gitops-engine/pkg/sync/common"
9-
. "github.com/argoproj/gitops-engine/pkg/utils/testing"
9+
testingutils "github.com/argoproj/gitops-engine/pkg/utils/testing"
1010
)
1111

1212
func TestTypes(t *testing.T) {
13-
assert.Nil(t, Types(NewPod()))
14-
assert.Equal(t, []Type{PreInstall}, Types(Annotate(NewPod(), "helm.sh/hook", "pre-install")))
15-
assert.Equal(t, []Type{PreUpgrade}, Types(Annotate(NewPod(), "helm.sh/hook", "pre-upgrade")))
16-
assert.Equal(t, []Type{PostUpgrade}, Types(Annotate(NewPod(), "helm.sh/hook", "post-upgrade")))
17-
assert.Equal(t, []Type{PostInstall}, Types(Annotate(NewPod(), "helm.sh/hook", "post-install")))
13+
assert.Nil(t, Types(testingutils.NewPod()))
14+
assert.Equal(t, []Type{PreInstall}, Types(testingutils.Annotate(testingutils.NewPod(), "helm.sh/hook", "pre-install")))
15+
assert.Equal(t, []Type{PreUpgrade}, Types(testingutils.Annotate(testingutils.NewPod(), "helm.sh/hook", "pre-upgrade")))
16+
assert.Equal(t, []Type{PostUpgrade}, Types(testingutils.Annotate(testingutils.NewPod(), "helm.sh/hook", "post-upgrade")))
17+
assert.Equal(t, []Type{PostInstall}, Types(testingutils.Annotate(testingutils.NewPod(), "helm.sh/hook", "post-install")))
1818
// helm calls "crd-install" a hook, but it really can't be treated as such
19-
assert.Empty(t, Types(Annotate(NewPod(), "helm.sh/hook", "crd-install")))
19+
assert.Empty(t, Types(testingutils.Annotate(testingutils.NewPod(), "helm.sh/hook", "crd-install")))
2020
// we do not consider these supported hooks
21-
assert.Nil(t, Types(Annotate(NewPod(), "helm.sh/hook", "pre-rollback")))
22-
assert.Nil(t, Types(Annotate(NewPod(), "helm.sh/hook", "post-rollback")))
23-
assert.Nil(t, Types(Annotate(NewPod(), "helm.sh/hook", "test-success")))
24-
assert.Nil(t, Types(Annotate(NewPod(), "helm.sh/hook", "test-failure")))
21+
assert.Nil(t, Types(testingutils.Annotate(testingutils.NewPod(), "helm.sh/hook", "pre-rollback")))
22+
assert.Nil(t, Types(testingutils.Annotate(testingutils.NewPod(), "helm.sh/hook", "post-rollback")))
23+
assert.Nil(t, Types(testingutils.Annotate(testingutils.NewPod(), "helm.sh/hook", "test-success")))
24+
assert.Nil(t, Types(testingutils.Annotate(testingutils.NewPod(), "helm.sh/hook", "test-failure")))
2525
}
2626

2727
func TestType_HookType(t *testing.T) {

0 commit comments

Comments
 (0)