Skip to content

Commit

Permalink
replace logrus and gorilla+negorni with zerolog and gin (#114)
Browse files Browse the repository at this point in the history
* replace logrus and gorilla+negorni with zerolog and gin

* fix swagger spec

* use info when LOG_LEVEL not set

* update base image in Dockerfile

* fix base image

* remove EXPOSE from Dockerfile

* use default logger in watcher

* rename watcher logger field

* add requestId field to logger
  • Loading branch information
nilsgstrabo authored Mar 14, 2024
1 parent 5b27dca commit 32bc503
Show file tree
Hide file tree
Showing 18 changed files with 530 additions and 402 deletions.
25 changes: 11 additions & 14 deletions .github/workflows/pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
name: Build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Build docker image
env:
REF: ${{ github. sha }}
Expand All @@ -21,25 +21,22 @@ jobs:
- uses: actions/checkout@v4
with:
fetch-depth: 2
- uses: actions/setup-go@v4
- uses: actions/setup-go@v5
with:
go-version: '1.21'
- name: Install dependencies
run: go mod download
- name: Install GolangCI Lint
run: curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.55.2

go-version-file: 'go.mod'
- name: golangci-lint
run: golangci-lint run --timeout=30m --max-same-issues=0 --out-format=github-actions
uses: golangci/golangci-lint-action@v4
with:
version: v1.55.2

test:
name: Unit Test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: '1.21'
go-version-file: 'go.mod'
- name: Install dependencies
run: go mod download
- name: Run Tests
Expand All @@ -50,9 +47,9 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v4
- uses: actions/setup-go@v5
with:
go-version: '1.21'
go-version-file: 'go.mod'
- name: Install dependencies
run: go mod download
- name: Install Swagger
Expand Down
15 changes: 15 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
run:
timeout: 30m

linters:
enable:
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
- unused
- zerologlint

issues:
max-same-issues: 0
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
"RADIXOPERATOR_CLUSTER_TYPE": "development",
"RADIX_DNS_ZONE":"dev.radix.equinor.com",
"RADIX_CONTAINER_REGISTRY":"radixdev.azurecr.io",
"RADIX_ENVIRONMENT":"qa",
"RADIX_ENVIRONMENT":"dev",
"RADIX_APP":"radix-job-demo",
"RADIX_JOB_SCHEDULERS_PER_ENVIRONMENT_HISTORY_LIMIT": "2",
"RADIX_DEPLOYMENT": "qa-qcd3b-cg6u3hnk",
"RADIX_DEPLOYMENT": "dev-f6dbi-w5quewh2",
"RADIX_COMPONENT": "compute",
"RADIX_PORTS": "8081",
"RADIXOPERATOR_APP_ENV_LIMITS_DEFAULT_CPU": "50m",
Expand Down
3 changes: 1 addition & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.21-alpine3.18 as builder
FROM golang:1.21-alpine3.19 as builder
ENV GO111MODULE=on

RUN addgroup -S -g 1000 job-scheduler
Expand All @@ -25,6 +25,5 @@ COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=builder /etc/passwd /etc/passwd
COPY --from=builder /usr/local/bin/radix-job-scheduler /usr/local/bin/radix-job-scheduler

EXPOSE 8080
USER 1000
ENTRYPOINT ["/usr/local/bin/radix-job-scheduler"]
15 changes: 15 additions & 0 deletions api/controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package api

import (
"github.com/gin-gonic/gin"
)

type Route struct {
Path string
Method string
Handler gin.HandlerFunc
}

type Controller interface {
GetRoutes() []Route
}
12 changes: 5 additions & 7 deletions api/controllers/controller_base.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
package controllers

import (
"net/http"

apiErrors "github.com/equinor/radix-job-scheduler/api/errors"
models "github.com/equinor/radix-job-scheduler/models/common"
"github.com/equinor/radix-job-scheduler/utils"
log "github.com/sirupsen/logrus"
"github.com/gin-gonic/gin"
)

type ControllerBase struct {
}

func (controller *ControllerBase) HandleError(w http.ResponseWriter, err error) {
var status *models.Status
func (controller *ControllerBase) HandleError(c *gin.Context, err error) {
_ = c.Error(err)

var status *models.Status
switch t := err.(type) {
case apiErrors.APIStatus:
status = t.Status()
default:
status = apiErrors.NewFromError(err).Status()
}

log.Errorf("failed: %v", err)
utils.StatusResponse(w, status)
utils.StatusResponse(c.Writer, status)
}
5 changes: 3 additions & 2 deletions api/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

radixUtils "github.com/equinor/radix-common/utils"
"github.com/equinor/radix-common/utils/numbers"
"github.com/equinor/radix-job-scheduler/api"
"github.com/equinor/radix-job-scheduler/models"
modelsv1 "github.com/equinor/radix-job-scheduler/models/v1"
"github.com/equinor/radix-job-scheduler/router"
Expand All @@ -35,10 +36,10 @@ import (
)

type ControllerTestUtils struct {
controllers []models.Controller
controllers []api.Controller
}

func New(controllers ...models.Controller) ControllerTestUtils {
func New(controllers ...api.Controller) ControllerTestUtils {
return ControllerTestUtils{
controllers: controllers,
}
Expand Down
24 changes: 15 additions & 9 deletions api/v1/batches/batch_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/equinor/radix-job-scheduler/models/common"
modelsv1 "github.com/equinor/radix-job-scheduler/models/v1"
"github.com/equinor/radix-operator/pkg/apis/kube"
log "github.com/sirupsen/logrus"
"github.com/rs/zerolog/log"
corev1 "k8s.io/api/core/v1"
)

Expand Down Expand Up @@ -51,15 +51,16 @@ func New(kube *kube.Kube, env *models.Env) BatchHandler {

// GetBatches Get status of all batches
func (handler *batchHandler) GetBatches(ctx context.Context) ([]modelsv1.BatchStatus, error) {
log.Debugf("Get batches for the namespace: %s", handler.common.Env.RadixDeploymentNamespace)
logger := log.Ctx(ctx)
logger.Debug().Msgf("Get batches for the namespace: %s", handler.common.Env.RadixDeploymentNamespace)

radixBatches, err := handler.common.HandlerApiV2.GetRadixBatches(ctx)
if err != nil {
return nil, err
}
radixBatchStatuses := make([]modelsv1.BatchStatus, 0, len(radixBatches))
if len(radixBatches) == 0 {
log.Debugf("No batches found for namespace %s", handler.common.Env.RadixDeploymentNamespace)
logger.Debug().Msgf("No batches found for namespace %s", handler.common.Env.RadixDeploymentNamespace)
return radixBatchStatuses, nil
}

Expand All @@ -73,7 +74,7 @@ func (handler *batchHandler) GetBatches(ctx context.Context) ([]modelsv1.BatchSt
setBatchJobEventMessages(radixBatchStatus, batchJobPodsMap, eventMessageForPods)
radixBatchStatuses = append(radixBatchStatuses, *radixBatchStatus)
}
log.Debugf("Found %v batches for namespace %s", len(radixBatchStatuses), handler.common.Env.RadixDeploymentNamespace)
logger.Debug().Msgf("Found %v batches for namespace %s", len(radixBatchStatuses), handler.common.Env.RadixDeploymentNamespace)
return radixBatchStatuses, nil
}

Expand All @@ -84,7 +85,8 @@ func (handler *batchHandler) GetBatchJob(ctx context.Context, batchName string,

// GetBatch Get status of a batch
func (handler *batchHandler) GetBatch(ctx context.Context, batchName string) (*modelsv1.BatchStatus, error) {
log.Debugf("get batches for namespace: %s", handler.common.Env.RadixDeploymentNamespace)
logger := log.Ctx(ctx)
logger.Debug().Msgf("get batches for namespace: %s", handler.common.Env.RadixDeploymentNamespace)
radixBatch, err := handler.common.HandlerApiV2.GetRadixBatch(ctx, batchName)
if err != nil {
return nil, err
Expand All @@ -101,7 +103,8 @@ func (handler *batchHandler) GetBatch(ctx context.Context, batchName string) (*m

// CreateBatch Create a batch with parameters
func (handler *batchHandler) CreateBatch(ctx context.Context, batchScheduleDescription *common.BatchScheduleDescription) (*modelsv1.BatchStatus, error) {
log.Debugf("create batch for namespace: %s", handler.common.Env.RadixDeploymentNamespace)
logger := log.Ctx(ctx)
logger.Debug().Msgf("create batch for namespace: %s", handler.common.Env.RadixDeploymentNamespace)
radixBatch, err := handler.common.HandlerApiV2.CreateRadixBatch(ctx, batchScheduleDescription)
if err != nil {
return nil, err
Expand All @@ -120,7 +123,8 @@ func (handler *batchHandler) CopyBatch(ctx context.Context, batchName string, de

// DeleteBatch Delete a batch
func (handler *batchHandler) DeleteBatch(ctx context.Context, batchName string) error {
log.Debugf("delete batch %s for namespace: %s", batchName, handler.common.Env.RadixDeploymentNamespace)
logger := log.Ctx(ctx)
logger.Debug().Msgf("delete batch %s for namespace: %s", batchName, handler.common.Env.RadixDeploymentNamespace)
err := handler.common.HandlerApiV2.DeleteRadixBatch(ctx, batchName)
if err != nil {
return err
Expand All @@ -130,13 +134,15 @@ func (handler *batchHandler) DeleteBatch(ctx context.Context, batchName string)

// StopBatch Stop a batch
func (handler *batchHandler) StopBatch(ctx context.Context, batchName string) error {
log.Debugf("delete batch %s for namespace: %s", batchName, handler.common.Env.RadixDeploymentNamespace)
logger := log.Ctx(ctx)
logger.Debug().Msgf("delete batch %s for namespace: %s", batchName, handler.common.Env.RadixDeploymentNamespace)
return handler.common.HandlerApiV2.StopRadixBatch(ctx, batchName)
}

// StopBatchJob Stop a batch job
func (handler *batchHandler) StopBatchJob(ctx context.Context, batchName string, jobName string) error {
log.Debugf("delete the job %s in the batch %s for namespace: %s", jobName, batchName, handler.common.Env.RadixDeploymentNamespace)
logger := log.Ctx(ctx)
logger.Debug().Msgf("delete the job %s in the batch %s for namespace: %s", jobName, batchName, handler.common.Env.RadixDeploymentNamespace)
return apiv1.StopJob(ctx, handler.common.HandlerApiV2, jobName)
}

Expand Down
Loading

0 comments on commit 32bc503

Please sign in to comment.