Skip to content
This repository has been archived by the owner on Oct 12, 2023. It is now read-only.

Add .golangci.yml from Argo CD, and fix corresponding linter failures #63

Merged
merged 1 commit into from
Dec 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
run:
timeout: 2m
skip-files:
skip-dirs:
linters:
enable:
- vet
- deadcode
- goimports
- varcheck
- structcheck
- ineffassign
- unconvert
- unparam
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ DOCKER_PUSH?=true
build:
CGO_ENABLED=0 go build -ldflags="-w -s" -o ./dist/argocd-applicationset .

.PHONY: test
test:
go test -race -count=1 `go list ./...`

.PHONY: image
image:
docker build -t $(IMAGE) .
Expand All @@ -22,6 +26,11 @@ manifests:
controller-gen paths=./api/... crd:trivialVersions=true output:dir=./manifests/crds/
controller-gen object paths=./api/...

lint:
golangci-lint --version
GOMAXPROCS=2 golangci-lint run --fix --verbose --timeout 300s


# Run go fmt against code
fmt:
go fmt ./...
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"flag"

"github.com/argoproj-labs/applicationset/pkg/generators"
"github.com/argoproj-labs/applicationset/pkg/services"
"github.com/argoproj-labs/applicationset/pkg/utils"
Expand Down
5 changes: 3 additions & 2 deletions pkg/controllers/applicationset_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,8 @@ func (r *ApplicationSetReconciler) createInCluster(ctx context.Context, applicat
return r.createOrUpdateInCluster(ctx, applicationSet, createApps)
}

func (r *ApplicationSetReconciler) getCurrentApplications(ctx context.Context, applicationSet argoprojiov1alpha1.ApplicationSet) ([]argov1alpha1.Application, error) {
func (r *ApplicationSetReconciler) getCurrentApplications(_ context.Context, applicationSet argoprojiov1alpha1.ApplicationSet) ([]argov1alpha1.Application, error) {
// TODO: Should this use the context param?
var current argov1alpha1.ApplicationList
err := r.Client.List(context.Background(), &current, client.MatchingFields{".metadata.controller": applicationSet.Name})

Expand Down Expand Up @@ -313,7 +314,7 @@ func (r *ApplicationSetReconciler) deleteInCluster(ctx context.Context, applicat
appLog := log.WithFields(log.Fields{"app": app.Name, "appSet": applicationSet.Name})
_, exists := m[app.Name]

if exists == false {
if !exists {
err := r.Client.Delete(ctx, &app)
if err != nil {
appLog.WithError(err).Error("failed to delete Application")
Expand Down
65 changes: 44 additions & 21 deletions pkg/controllers/applicationset_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"context"
"errors"
"fmt"
"testing"
"time"

"github.com/argoproj-labs/applicationset/pkg/generators"
argov1alpha1 "github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"
"github.com/stretchr/testify/assert"
Expand All @@ -14,8 +17,6 @@ import (
crtclient "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"testing"
"time"

argoprojiov1alpha1 "github.com/argoproj-labs/applicationset/api/v1alpha1"
)
Expand Down Expand Up @@ -53,8 +54,11 @@ func (r *rendererMock) RenderTemplateParams(tmpl *argov1alpha1.Application, para

func TestExtractApplications(t *testing.T) {
scheme := runtime.NewScheme()
argoprojiov1alpha1.AddToScheme(scheme)
argov1alpha1.AddToScheme(scheme)
err := argoprojiov1alpha1.AddToScheme(scheme)
assert.Nil(t, err)

err = argov1alpha1.AddToScheme(scheme)
assert.Nil(t, err)

client := fake.NewFakeClientWithScheme(scheme)

Expand Down Expand Up @@ -174,8 +178,11 @@ func TestExtractApplications(t *testing.T) {
func TestCreateOrUpdateInCluster(t *testing.T) {

scheme := runtime.NewScheme()
argoprojiov1alpha1.AddToScheme(scheme)
argov1alpha1.AddToScheme(scheme)
err := argoprojiov1alpha1.AddToScheme(scheme)
assert.Nil(t, err)

err = argov1alpha1.AddToScheme(scheme)
assert.Nil(t, err)

for _, c := range []struct {
appSet argoprojiov1alpha1.ApplicationSet
Expand Down Expand Up @@ -329,7 +336,8 @@ func TestCreateOrUpdateInCluster(t *testing.T) {
} {
initObjs := []runtime.Object{&c.appSet}
for _, a := range c.existsApps {
controllerutil.SetControllerReference(&c.appSet, &a, scheme)
err = controllerutil.SetControllerReference(&c.appSet, &a, scheme)
assert.Nil(t, err)
initObjs = append(initObjs, &a)
}

Expand All @@ -341,7 +349,8 @@ func TestCreateOrUpdateInCluster(t *testing.T) {
Recorder: record.NewFakeRecorder(len(initObjs) + len(c.expected)),
}

r.createOrUpdateInCluster(context.TODO(), c.appSet, c.apps)
err = r.createOrUpdateInCluster(context.TODO(), c.appSet, c.apps)
assert.Nil(t, err)

for _, obj := range c.expected {
got := &argov1alpha1.Application{}
Expand All @@ -350,7 +359,8 @@ func TestCreateOrUpdateInCluster(t *testing.T) {
Name: obj.Name,
}, got)

controllerutil.SetControllerReference(&c.appSet, &obj, r.Scheme)
err = controllerutil.SetControllerReference(&c.appSet, &obj, r.Scheme)
assert.Nil(t, err)

assert.Equal(t, obj, *got)
}
Expand All @@ -361,8 +371,11 @@ func TestCreateOrUpdateInCluster(t *testing.T) {
func TestCreateApplications(t *testing.T) {

scheme := runtime.NewScheme()
argoprojiov1alpha1.AddToScheme(scheme)
argov1alpha1.AddToScheme(scheme)
err := argoprojiov1alpha1.AddToScheme(scheme)
assert.Nil(t, err)

err = argov1alpha1.AddToScheme(scheme)
assert.Nil(t, err)

for _, c := range []struct {
appSet argoprojiov1alpha1.ApplicationSet
Expand Down Expand Up @@ -516,7 +529,8 @@ func TestCreateApplications(t *testing.T) {
} {
initObjs := []runtime.Object{&c.appSet}
for _, a := range c.existsApps {
controllerutil.SetControllerReference(&c.appSet, &a, scheme)
err = controllerutil.SetControllerReference(&c.appSet, &a, scheme)
assert.Nil(t, err)
initObjs = append(initObjs, &a)
}

Expand All @@ -528,7 +542,8 @@ func TestCreateApplications(t *testing.T) {
Recorder: record.NewFakeRecorder(len(initObjs) + len(c.expected)),
}

r.createInCluster(context.TODO(), c.appSet, c.apps)
err = r.createInCluster(context.TODO(), c.appSet, c.apps)
assert.Nil(t, err)

for _, obj := range c.expected {
got := &argov1alpha1.Application{}
Expand All @@ -537,7 +552,8 @@ func TestCreateApplications(t *testing.T) {
Name: obj.Name,
}, got)

controllerutil.SetControllerReference(&c.appSet, &obj, r.Scheme)
err = controllerutil.SetControllerReference(&c.appSet, &obj, r.Scheme)
assert.Nil(t, err)

assert.Equal(t, obj, *got)
}
Expand All @@ -548,8 +564,10 @@ func TestCreateApplications(t *testing.T) {
func TestDeleteInCluster(t *testing.T) {

scheme := runtime.NewScheme()
argoprojiov1alpha1.AddToScheme(scheme)
argov1alpha1.AddToScheme(scheme)
err := argoprojiov1alpha1.AddToScheme(scheme)
assert.Nil(t, err)
err = argov1alpha1.AddToScheme(scheme)
assert.Nil(t, err)

for _, c := range []struct {
appSet argoprojiov1alpha1.ApplicationSet
Expand Down Expand Up @@ -649,7 +667,8 @@ func TestDeleteInCluster(t *testing.T) {
initObjs := []runtime.Object{&c.appSet}
for _, a := range c.existsApps {
temp := a
controllerutil.SetControllerReference(&c.appSet, &temp, scheme)
err = controllerutil.SetControllerReference(&c.appSet, &temp, scheme)
assert.Nil(t, err)
initObjs = append(initObjs, &temp)
}

Expand All @@ -661,7 +680,8 @@ func TestDeleteInCluster(t *testing.T) {
Recorder: record.NewFakeRecorder(len(initObjs) + len(c.expected)),
}

r.deleteInCluster(context.TODO(), c.appSet, c.apps)
err = r.deleteInCluster(context.TODO(), c.appSet, c.apps)
assert.Nil(t, err)

for _, obj := range c.expected {
got := &argov1alpha1.Application{}
Expand All @@ -670,7 +690,8 @@ func TestDeleteInCluster(t *testing.T) {
Name: obj.Name,
}, got)

controllerutil.SetControllerReference(&c.appSet, &obj, r.Scheme)
err = controllerutil.SetControllerReference(&c.appSet, &obj, r.Scheme)
assert.Nil(t, err)

assert.Equal(t, obj, *got)
}
Expand All @@ -689,8 +710,10 @@ func TestDeleteInCluster(t *testing.T) {

func TestGetMinRequeueAfter(t *testing.T) {
scheme := runtime.NewScheme()
argoprojiov1alpha1.AddToScheme(scheme)
argov1alpha1.AddToScheme(scheme)
err := argoprojiov1alpha1.AddToScheme(scheme)
assert.Nil(t, err)
err = argov1alpha1.AddToScheme(scheme)
assert.Nil(t, err)

client := fake.NewFakeClientWithScheme(scheme)

Expand Down
2 changes: 1 addition & 1 deletion pkg/generators/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (g *ClusterGenerator) GenerateParams(
return nil, err
}

if err := g.Client.List(context.Background(), clusterSecretList, client.MatchingLabelsSelector{secretSelector}); err != nil {
if err := g.Client.List(context.Background(), clusterSecretList, client.MatchingLabelsSelector{Selector: secretSelector}); err != nil {
return nil, err
}
log.Debug("clusters matching labels", "count", len(clusterSecretList.Items))
Expand Down
3 changes: 2 additions & 1 deletion pkg/generators/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ func (g *GitGenerator) filter(Directories []argoprojiov1alpha1.GitDirectoryGener
return res
}

func (g *GitGenerator) generateParams(requestedApps []string, appSetGenerator *argoprojiov1alpha1.ApplicationSetGenerator) []map[string]string {
func (g *GitGenerator) generateParams(requestedApps []string, _ *argoprojiov1alpha1.ApplicationSetGenerator) []map[string]string {
// TODO: At some point, the appicationSetGenerator param should be used

res := make([]map[string]string, len(requestedApps))
for i, a := range requestedApps {
Expand Down
35 changes: 17 additions & 18 deletions pkg/generators/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,28 @@ package generators
import (
"context"
"fmt"
"testing"

argoprojiov1alpha1 "github.com/argoproj-labs/applicationset/api/v1alpha1"
"github.com/argoproj/argo-cd/reposerver/apiclient"
"github.com/argoproj/gitops-engine/pkg/utils/io"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"testing"
)

type clientSet struct {
RepoServerServiceClient apiclient.RepoServerServiceClient
}
// type clientSet struct {
// RepoServerServiceClient apiclient.RepoServerServiceClient
// }

func (c *clientSet) NewRepoServerClient() (io.Closer, apiclient.RepoServerServiceClient, error) {
return io.NewCloser(func() error { return nil }), c.RepoServerServiceClient, nil
}
// func (c *clientSet) NewRepoServerClient() (io.Closer, apiclient.RepoServerServiceClient, error) {
// return io.NewCloser(func() error { return nil }), c.RepoServerServiceClient, nil
// }

type argoCDServiceMock struct {
mock.Mock
mock *mock.Mock
}

func (a argoCDServiceMock) GetApps(ctx context.Context, repoURL string, revision string) ([]string, error) {
args := a.Called(ctx, repoURL, revision)
args := a.mock.Called(ctx, repoURL, revision)

return args.Get(0).([]string), args.Error(1)
}
Expand All @@ -42,7 +41,7 @@ func TestGitGenerateParams(t *testing.T) {
}{
{
name: "happy flow - created apps",
directories: []argoprojiov1alpha1.GitDirectoryGeneratorItem{{"*"}},
directories: []argoprojiov1alpha1.GitDirectoryGeneratorItem{{Path: "*"}},
repoApps: []string{
"app1",
"app2",
Expand All @@ -57,7 +56,7 @@ func TestGitGenerateParams(t *testing.T) {
},
{
name: "It filters application according to the paths",
directories: []argoprojiov1alpha1.GitDirectoryGeneratorItem{{"p1/*"}, {"p1/*/*"}},
directories: []argoprojiov1alpha1.GitDirectoryGeneratorItem{{Path: "p1/*"}, {Path: "p1/*/*"}},
repoApps: []string{
"app1",
"p1/app2",
Expand All @@ -73,15 +72,15 @@ func TestGitGenerateParams(t *testing.T) {
},
{
name: "handles empty response from repo server",
directories: []argoprojiov1alpha1.GitDirectoryGeneratorItem{{"*"}},
directories: []argoprojiov1alpha1.GitDirectoryGeneratorItem{{Path: "*"}},
repoApps: []string{},
repoError: nil,
expected: []map[string]string{},
expectedError: nil,
},
{
name: "handles error from repo server",
directories: []argoprojiov1alpha1.GitDirectoryGeneratorItem{{"*"}},
directories: []argoprojiov1alpha1.GitDirectoryGeneratorItem{{Path: "*"}},
repoApps: []string{},
repoError: fmt.Errorf("error"),
expected: []map[string]string{},
Expand All @@ -92,8 +91,8 @@ func TestGitGenerateParams(t *testing.T) {
for _, c := range cases {
cc := c
t.Run(cc.name, func(t *testing.T) {
argoCDServiceMock := argoCDServiceMock{}
argoCDServiceMock.On("GetApps", mock.Anything, mock.Anything, mock.Anything).Return(c.repoApps, c.repoError)
argoCDServiceMock := argoCDServiceMock{mock: &mock.Mock{}}
argoCDServiceMock.mock.On("GetApps", mock.Anything, mock.Anything, mock.Anything).Return(c.repoApps, c.repoError)

var gitGenerator = NewGitGenerator(argoCDServiceMock)
applicationSetInfo := argoprojiov1alpha1.ApplicationSet{
Expand All @@ -120,7 +119,7 @@ func TestGitGenerateParams(t *testing.T) {
assert.Equal(t, c.expected, got)
}

argoCDServiceMock.AssertExpectations(t)
argoCDServiceMock.mock.AssertExpectations(t)
})
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/generators/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package generators

import (
"errors"
argoprojiov1alpha1 "github.com/argoproj-labs/applicationset/api/v1alpha1"
"time"

argoprojiov1alpha1 "github.com/argoproj-labs/applicationset/api/v1alpha1"
)

// Generator defines the interface implemented by all ApplicationSet generators.
Expand Down
3 changes: 2 additions & 1 deletion pkg/generators/list.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package generators

import (
"time"

argoprojiov1alpha1 "github.com/argoproj-labs/applicationset/api/v1alpha1"
"github.com/argoproj-labs/applicationset/pkg/utils"
"time"
)

var _ Generator = (*ListGenerator)(nil)
Expand Down
3 changes: 2 additions & 1 deletion pkg/services/repo_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package services

import (
"context"

"github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"
"github.com/argoproj/argo-cd/reposerver/apiclient"
"github.com/argoproj/argo-cd/util/db"
Expand Down Expand Up @@ -60,7 +61,7 @@ func (a *argoCDService) GetApps(ctx context.Context, repoURL string, revision st

res := []string{}

for name, _ := range apps.Apps {
for name := range apps.Apps {
res = append(res, name)
}

Expand Down