Skip to content

Commit

Permalink
feat(): add linter (#501)
Browse files Browse the repository at this point in the history
  • Loading branch information
VaibhavPage authored Feb 21, 2020
1 parent efa2790 commit 22de2c4
Show file tree
Hide file tree
Showing 56 changed files with 217 additions and 176 deletions.
64 changes: 64 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
run:
deadline: 2m
skip-dirs:
- pkg/client
- vendor
- "gateways/server/common/naivewatcher"
skip-files:
- "pkg/apis/sensor/v1alpha1/openapi_generated.go"
- "pkg/apis/gateway/v1alpha1/openapi_generated.go"
- "pkg/apis/eventsources/v1alpha1/openapi_generated.go"
- "pkg/apis/common/openapi_generated.go"
- "pkg/apis/common/deepcopy_generated.go"
- "pkg/apis/sensor/v1alpha1/zz_generated.deepcopy.go"
- "pkg/apis/gateway/v1alpha1/zz_generated.deepcopy.go"
- "pkg/apis/eventsources/v1alpha1/zz_generated.deepcopy.go"
linters:
enable:
- bodyclose
- deadcode
- depguard
- dogsled
- dupl
- errcheck
- goconst
- gocritic
- gocyclo
- gofmt
- goimports
- golint
- goprintffuncname
- gosec
- gosimple
- govet
- ineffassign
- interfacer
- misspell
- nakedret
- rowserrcheck
- scopelint
- staticcheck
- structcheck
- stylecheck
- typecheck
- unconvert
- unparam
- unused
- varcheck
- whitespace
# output configuration options
output:
# colored-line-number|line-number|json|tab|checkstyle|code-climate, default is "colored-line-number"
format: colored-line-number

# print lines of code with issue, default is true
print-issued-lines: true

# print linter name in the end of issue text, default is true
print-linter-name: true

# make issues output unique by line, default is true
uniq-by-line: true

service:
golangci-lint-version: 1.21.x
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -384,3 +384,8 @@ kind-e2e:

.PHONY: build-e2e-images
build-e2e-images: sensor-controller-image gateway-controller-image gateway-client-image webhook-image

.PHONY: lint
lint:
golangci-lint run

3 changes: 2 additions & 1 deletion common/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ limitations under the License.
package common

import (
"github.com/sirupsen/logrus"
"os"

"github.com/sirupsen/logrus"
)

// Logger constants
Expand Down
5 changes: 3 additions & 2 deletions common/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ limitations under the License.
package common

import (
"github.com/sirupsen/logrus"
"github.com/smartystreets/goconvey/convey"
"os"
"testing"

"github.com/sirupsen/logrus"
"github.com/smartystreets/goconvey/convey"
)

func TestNewArgoEventsLogger(t *testing.T) {
Expand Down
4 changes: 1 addition & 3 deletions common/recurrence.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ func ParseExclusionDates(vals []string) ([]time.Time, error) {
if err != nil {
return nil, err
}
for _, d := range dates {
exclusionDates = append(exclusionDates, d)
}
exclusionDates = append(exclusionDates, dates...)
}
}
return exclusionDates, nil
Expand Down
3 changes: 2 additions & 1 deletion common/recurrence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ limitations under the License.
package common

import (
"github.com/smartystreets/goconvey/convey"
"testing"
"time"

"github.com/smartystreets/goconvey/convey"
)

func TestParseExclusionDate(t *testing.T) {
Expand Down
12 changes: 9 additions & 3 deletions common/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,25 @@ func ServerResourceForGroupVersionKind(disco discovery.DiscoveryInterface, gvk s
// SendSuccessResponse sends http success response
func SendSuccessResponse(writer http.ResponseWriter, response string) {
writer.WriteHeader(http.StatusOK)
writer.Write([]byte(response))
if _, err := writer.Write([]byte(response)); err != nil {
fmt.Printf("failed to write the response. err: %+v\n", err)
}
}

// SendErrorResponse sends http error response
func SendErrorResponse(writer http.ResponseWriter, response string) {
writer.WriteHeader(http.StatusBadRequest)
writer.Write([]byte(response))
if _, err := writer.Write([]byte(response)); err != nil {
fmt.Printf("failed to write the response. err: %+v\n", err)
}
}

// SendInternalErrorResponse sends http internal error response
func SendInternalErrorResponse(writer http.ResponseWriter, response string) {
writer.WriteHeader(http.StatusInternalServerError)
writer.Write([]byte(response))
if _, err := writer.Write([]byte(response)); err != nil {
fmt.Printf("failed to write the response. err: %+v\n", err)
}
}

// Hasher hashes a string
Expand Down
2 changes: 1 addition & 1 deletion common/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ limitations under the License.
package common

import (
"github.com/smartystreets/goconvey/convey"
"net/http"
"testing"

"github.com/smartystreets/goconvey/convey"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
Expand Down
7 changes: 2 additions & 5 deletions controllers/gateway/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package gateway

import (
"context"
"fmt"

"github.com/argoproj/argo-events/common"
Expand All @@ -32,7 +31,7 @@ import (
)

// watches configuration for gateway controller
func (c *Controller) watchControllerConfigMap(ctx context.Context) (cache.Controller, error) {
func (c *Controller) watchControllerConfigMap() cache.Controller {
c.logger.Infoln("watching gateway-controller config map updates")
source := c.newControllerConfigMapWatch()
_, controller := cache.NewInformer(
Expand All @@ -59,9 +58,7 @@ func (c *Controller) watchControllerConfigMap(ctx context.Context) (cache.Contro
}
},
})

go controller.Run(ctx.Done())
return controller, nil
return controller
}

// creates a new config map watcher
Expand Down
15 changes: 5 additions & 10 deletions controllers/gateway/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
clientset "github.com/argoproj/argo-events/pkg/client/gateway/clientset/versioned"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/util/wait"
informersv1 "k8s.io/client-go/informers/core/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
Expand Down Expand Up @@ -65,10 +64,8 @@ type Controller struct {
// gatewayClient is the Argo-Events gateway resource client
gatewayClient clientset.Interface
// gateway-controller informer and queue
podInformer informersv1.PodInformer
svcInformer informersv1.ServiceInformer
informer cache.SharedIndexInformer
queue workqueue.RateLimitingInterface
informer cache.SharedIndexInformer
queue workqueue.RateLimitingInterface
}

// NewGatewayController creates a new controller
Expand Down Expand Up @@ -158,12 +155,10 @@ func (c *Controller) Run(ctx context.Context, threads int) {
common.LabelVersion: base.GetVersion().Version,
}).Infoln("starting controller")

_, err := c.watchControllerConfigMap(ctx)
if err != nil {
c.logger.WithError(err).Errorln("failed to register watch for controller config map")
return
}
configMapCtrl := c.watchControllerConfigMap()
go configMapCtrl.Run(ctx.Done())

var err error
c.informer, err = c.newGatewayInformer()
if err != nil {
panic(err)
Expand Down
7 changes: 2 additions & 5 deletions controllers/sensor/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package sensor

import (
"context"
"fmt"

"github.com/argoproj/argo-events/common"
Expand All @@ -33,7 +32,7 @@ import (
)

// watchControllerConfigMap watches updates to sensor controller configmap
func (controller *Controller) watchControllerConfigMap(ctx context.Context) (cache.Controller, error) {
func (controller *Controller) watchControllerConfigMap() cache.Controller {
log.Info("watching controller config map updates")
source := controller.newControllerConfigMapWatch()
_, ctrl := cache.NewInformer(
Expand All @@ -60,9 +59,7 @@ func (controller *Controller) watchControllerConfigMap(ctx context.Context) (cac
}
},
})

go ctrl.Run(ctx.Done())
return ctrl, nil
return ctrl
}

// newControllerConfigMapWatch returns a configmap watcher
Expand Down
8 changes: 3 additions & 5 deletions controllers/sensor/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,10 @@ func (controller *Controller) Run(ctx context.Context, threads int) {
common.LabelVersion: base.GetVersion().Version,
}).Infoln("starting the controller...")

_, err := controller.watchControllerConfigMap(ctx)
if err != nil {
controller.logger.WithError(err).Error("failed to register watch for controller config map")
return
}
configMapCtrl := controller.watchControllerConfigMap()
go configMapCtrl.Run(ctx.Done())

var err error
controller.informer, err = controller.newSensorInformer()
if err != nil {
controller.logger.WithError(err).Errorln("failed to create a new sensor controller")
Expand Down
35 changes: 16 additions & 19 deletions controllers/sensor/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (ctx *sensorContext) operate() error {
// Validation failure prevents any sort processing of the sensor object
if err := ValidateSensor(ctx.sensor); err != nil {
ctx.logger.WithError(err).Errorln("failed to validate sensor")
ctx.markSensorPhase(v1alpha1.NodePhaseError, false, err.Error())
ctx.markSensorPhase(v1alpha1.NodePhaseError, err.Error())
return err
}

Expand All @@ -79,10 +79,10 @@ func (ctx *sensorContext) operate() error {

if err := ctx.createSensorResources(); err != nil {
ctx.logger.WithError(err).Errorln("failed to create resources for the sensor")
ctx.markSensorPhase(v1alpha1.NodePhaseError, false, err.Error())
ctx.markSensorPhase(v1alpha1.NodePhaseError, err.Error())
return nil
}
ctx.markSensorPhase(v1alpha1.NodePhaseActive, false, "sensor is active")
ctx.markSensorPhase(v1alpha1.NodePhaseActive, "sensor is active")
ctx.logger.Infoln("successfully created resources for the sensor. sensor is in active state")

case v1alpha1.NodePhaseActive:
Expand All @@ -100,7 +100,7 @@ func (ctx *sensorContext) operate() error {
ctx.logger.WithError(err).Errorln("failed to update the sensor resources")
return err
}
ctx.markSensorPhase(v1alpha1.NodePhaseActive, false, "sensor is active")
ctx.markSensorPhase(v1alpha1.NodePhaseActive, "sensor is active")
ctx.logger.Infoln("successfully processed the update")
}

Expand Down Expand Up @@ -176,7 +176,7 @@ func (ctx *sensorContext) updateSensorState() {
}

// mark the overall sensor phase
func (ctx *sensorContext) markSensorPhase(phase v1alpha1.NodePhase, markComplete bool, message ...string) {
func (ctx *sensorContext) markSensorPhase(phase v1alpha1.NodePhase, message ...string) {
justCompleted := ctx.sensor.Status.Phase != phase
if justCompleted {
ctx.logger.WithFields(
Expand Down Expand Up @@ -215,22 +215,19 @@ func (ctx *sensorContext) markSensorPhase(phase v1alpha1.NodePhase, markComplete
ctx.sensor.Status.Message = message[0]
}

switch phase {
case v1alpha1.NodePhaseError:
if markComplete && justCompleted {
ctx.logger.Infoln("marking sensor state as complete")
ctx.sensor.Status.CompletedAt = metav1.Time{Time: time.Now().UTC()}
if phase == v1alpha1.NodePhaseError && justCompleted {
ctx.logger.Infoln("marking sensor state as complete")
ctx.sensor.Status.CompletedAt = metav1.Time{Time: time.Now().UTC()}

if ctx.sensor.ObjectMeta.Labels == nil {
ctx.sensor.ObjectMeta.Labels = make(map[string]string)
}
if ctx.sensor.ObjectMeta.Annotations == nil {
ctx.sensor.ObjectMeta.Annotations = make(map[string]string)
}

ctx.sensor.ObjectMeta.Labels[LabelComplete] = "true"
ctx.sensor.ObjectMeta.Annotations[LabelComplete] = string(phase)
if ctx.sensor.ObjectMeta.Labels == nil {
ctx.sensor.ObjectMeta.Labels = make(map[string]string)
}
if ctx.sensor.ObjectMeta.Annotations == nil {
ctx.sensor.ObjectMeta.Annotations = make(map[string]string)
}

ctx.sensor.ObjectMeta.Labels[LabelComplete] = "true"
ctx.sensor.ObjectMeta.Annotations[LabelComplete] = string(phase)
}
ctx.updated = true
}
Expand Down
2 changes: 1 addition & 1 deletion controllers/sensor/operator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func TestMarkSensorPhase(t *testing.T) {
sensor, err := controller.sensorClient.ArgoprojV1alpha1().Sensors(sensorObj.Namespace).Create(sensorObj)
assert.Nil(t, err)
ctx.sensor = sensor.DeepCopy()
ctx.markSensorPhase(v1alpha1.NodePhaseActive, false, "sensor is active")
ctx.markSensorPhase(v1alpha1.NodePhaseActive, "sensor is active")
assert.Equal(t, v1alpha1.NodePhaseActive, ctx.sensor.Status.Phase)
assert.Equal(t, "sensor is active", ctx.sensor.Status.Message)
}
Expand Down
1 change: 0 additions & 1 deletion controllers/sensor/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ func (ctx *sensorContext) updateService() (*corev1.Service, error) {
if err := ctx.controller.k8sClient.CoreV1().Services(currentMetadata.Namespace).Delete(currentMetadata.Name, &metav1.DeleteOptions{}); err != nil {
return nil, err
}

}
return ctx.controller.k8sClient.CoreV1().Services(currentMetadata.Namespace).Get(currentMetadata.Name, metav1.GetOptions{})
}
2 changes: 1 addition & 1 deletion controllers/sensor/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestValidateSensor(t *testing.T) {
content, err := ioutil.ReadFile(fmt.Sprintf("%s/%s", dir, file.Name()))
assert.Nil(t, err)
var sensor *v1alpha1.Sensor
err = yaml.Unmarshal([]byte(content), &sensor)
err = yaml.Unmarshal(content, &sensor)
assert.Nil(t, err)
err = ValidateSensor(sensor)
assert.Nil(t, err)
Expand Down
Loading

0 comments on commit 22de2c4

Please sign in to comment.