Skip to content

Commit

Permalink
fix: lint
Browse files Browse the repository at this point in the history
Signed-off-by: sivchari <shibuuuu5@gmail.com>
  • Loading branch information
sivchari committed Jan 29, 2024
1 parent 7414cbb commit 650a98b
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 111 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ jobs:
go-version-file: go.mod
check-latest: true
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
uses: golangci/golangci-lint-action@v3
with:
version: v1.42.1
version: latest
skip-pkg-cache: true
skip-build-cache: true
test:
runs-on: ubuntu-latest
steps:
Expand Down
46 changes: 46 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
run:
timeout: 30m

linters-settings:
govet:
enable-all: true
disable:
- shadow
- atomicalign
- fieldalignment
revive:
rules:
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#var-naming
- name: var-naming
gci:
sections:
# Standard section: captures all standard packages.
- standard
# Default section: contains all imports that could not be matched to another section type.
- default
# Custom section: groups all imports with the specified Prefix.
- prefix(github.com/ubie-oss)
skip-generated: true
custom-order: true
linters:
disable-all: true
enable:
- testifylint
- tparallel
- bodyclose
- gofumpt
- containedctx
- tenv
- govet
- ineffassign
- staticcheck
- unused
- exportloopref
- gci
- ginkgolinter
- gocritic
- makezero
- misspell
- prealloc
- revive

6 changes: 3 additions & 3 deletions controllers/constants.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package controllers

type CTX_VALUE_KEY string
type CtxValueKey string

var (
CTX_VALUE_NAME CTX_VALUE_KEY = "name"
CTX_VALUE_NAMESPACE CTX_VALUE_KEY = "namespace"
CtxValueName CtxValueKey = "name"
CtxValueNamespace CtxValueKey = "namespace"
)
21 changes: 6 additions & 15 deletions controllers/cron_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"k8s.io/apimachinery/pkg/types"
)

Expand All @@ -30,33 +31,25 @@ func TestCron(t *testing.T) {
cronhpa: nil,
patchName: "patch-1",
})
if !assert.NoError(t, err) {
t.FailNow()
}
require.NoError(t, err)
err = cron.Add(types.NamespacedName{Name: "foo", Namespace: "ns"}, "patch-2", "0 * * * *", &CronContext{
reconciler: nil,
cronhpa: nil,
patchName: "patch-2",
})
if !assert.NoError(t, err) {
t.FailNow()
}
require.NoError(t, err)
err = cron.Add(types.NamespacedName{Name: "foo", Namespace: "ns"}, "patch-3", "0 * * * *", &CronContext{
reconciler: nil,
cronhpa: nil,
patchName: "patch-3",
})
if !assert.NoError(t, err) {
t.FailNow()
}
require.NoError(t, err)
err = cron.Add(types.NamespacedName{Name: "bar", Namespace: "ns"}, "patch-4", "0 * * * *", &CronContext{
reconciler: nil,
cronhpa: nil,
patchName: "patch-4",
})
if !assert.NoError(t, err) {
t.FailNow()
}
require.NoError(t, err)
resourceEntry := cron.ListResourceEntry(types.NamespacedName{Name: "foo", Namespace: "ns"})
assert.Equal(t, ResourceEntry{
"patch-1": 1,
Expand All @@ -73,9 +66,7 @@ func TestCron(t *testing.T) {
cron.Remove(types.NamespacedName{Name: "foo", Namespace: "ns"}, "paptch-1")

cron.RemoveResourceEntry(types.NamespacedName{Name: "foo", Namespace: "ns"})
if !assert.NoError(t, err) {
t.FailNow()
}
require.NoError(t, err)
resourceEntry = cron.ListResourceEntry(types.NamespacedName{Name: "foo", Namespace: "ns"})
assert.Equal(t, ResourceEntry{}, resourceEntry)
}
4 changes: 2 additions & 2 deletions controllers/croncontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ type CronContext struct {

func (cronctx *CronContext) Run() {
ctx := context.Background()
ctx = context.WithValue(ctx, CTX_VALUE_NAME, cronctx.cronhpa.Name)
ctx = context.WithValue(ctx, CTX_VALUE_NAMESPACE, cronctx.cronhpa.Namespace)
ctx = context.WithValue(ctx, CtxValueName, cronctx.cronhpa.Name)
ctx = context.WithValue(ctx, CtxValueNamespace, cronctx.cronhpa.Namespace)
logger := log.FromContext(ctx)

if err := cronctx.run(ctx); err != nil {
Expand Down
17 changes: 10 additions & 7 deletions controllers/cronhpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"time"

"github.com/robfig/cron/v3"
cronhpav1alpha1 "github.com/ubie-oss/cron-hpa/api/v1alpha1"
autoscalingv2 "k8s.io/api/autoscaling/v2"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
Expand All @@ -33,6 +32,8 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/log"

cronhpav1alpha1 "github.com/ubie-oss/cron-hpa/api/v1alpha1"
)

type CronHorizontalPodAutoscaler cronhpav1alpha1.CronHorizontalPodAutoscaler
Expand All @@ -50,7 +51,7 @@ const (
CronHPAEventNone CronHPAEvent = ""
)

const MAX_SCHEDULE_TRY = 1000000
const MaxScheduleTry = 1000000

func (cronhpa *CronHorizontalPodAutoscaler) UpdateSchedules(ctx context.Context, reconciler *CronHorizontalPodAutoscalerReconciler) error {
logger := log.FromContext(ctx)
Expand Down Expand Up @@ -89,6 +90,7 @@ func (cronhpa *CronHorizontalPodAutoscaler) ClearSchedules(ctx context.Context,
func (cronhpa *CronHorizontalPodAutoscaler) ApplyHPAPatch(patchName string, hpa *autoscalingv2.HorizontalPodAutoscaler) error {
var scheduledPatch *cronhpav1alpha1.CronHorizontalPodAutoscalerScheduledPatch
for _, sp := range cronhpa.Spec.ScheduledPatches {
sp := sp
if sp.Name == patchName {
scheduledPatch = &sp
break
Expand Down Expand Up @@ -171,13 +173,13 @@ func (cronhpa *CronHorizontalPodAutoscaler) GetCurrentPatchName(ctx context.Cont
}
nextTime := lastCronTimestamp.Time
latestTime := lastCronTimestamp.Time
for i := 0; i <= MAX_SCHEDULE_TRY; i++ {
for i := 0; i <= MaxScheduleTry; i++ {
nextTime = schedule.Next(nextTime)
if nextTime.After(currentTime) || nextTime.IsZero() {
break
}
latestTime = nextTime
if i == MAX_SCHEDULE_TRY {
if i == MaxScheduleTry {
return "", fmt.Errorf("Cannot find the next schedule of patch %s", scheduledPatch.Name)
}
}
Expand Down Expand Up @@ -221,15 +223,16 @@ func (cronhpa *CronHorizontalPodAutoscaler) CreateOrPatchHPA(ctx context.Context
event = CronHPAEventCreated
msg = "Created HPA"
} else {
if hpa.Annotations[annotationNameSkip] == "true" {
switch {
case hpa.Annotations[annotationNameSkip] == "true":
logger.Info("Skip updating an HPA by an annotation")
event = CronHPAEventSkipped
msg = "Skipped updating HPA by an annotation"
} else if reflect.DeepEqual(hpa.Spec, newhpa.Spec) {
case reflect.DeepEqual(hpa.Spec, newhpa.Spec):
logger.Info("Skip updating an HPA with no changes")
event = CronHPAEventSkipped
msg = "Skipped updating HPA with no changes"
} else {
default:
patch := client.MergeFrom(hpa)
if err := reconciler.Patch(ctx, newhpa, patch); err != nil {
return err
Expand Down

0 comments on commit 650a98b

Please sign in to comment.