Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 72 additions & 71 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
version: "2"
linters:
disable-all: true
default: none
enable:
- containedctx
- dogsled
Expand All @@ -8,86 +9,86 @@ linters:
- errcheck
- errname
- errorlint
- gci
- gocognit
- goconst
- gocritic
# - godot
- gofmt
- gofumpt
- goprintffuncname
- gosec
- gosimple
- govet
- ineffassign
- intrange
- lll
# TODO FIX THE FOLLOWING
# - misspell
# - nakedret
# - paralleltest
- misspell
- modernize
- nakedret
- nilerr
- predeclared
- revive
- sqlclosecheck
- staticcheck
# - stylecheck
- typecheck
- unconvert
- unparam
- unused
# - whitespace

linters-settings:
gocritic:
enabled-all: true
disabled-checks:
- commentFormatting
godot:
scope: all
gofumpt:
module-path: github.com/castai/cluster-controller
extra-rules: true
goconst:
min-len: 2
min-occurrences: 5
golint:
min-confidence: 0
gomnd:
settings:
mnd:
# don't include the "operation" and "assign"
checks: [argument,case,condition,return]
govet:
# shadow is marked as experimental feature, skip it for now.
check-shadowing: false
settings:
printf:
funcs:
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Infof
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Warnf
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Errorf
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Fatalf
lll:
line-length: 200
maligned:
suggest-new: true
misspell:
locale: US
revive:
rules:
- name: redefines-builtin-id
disabled: true

# Allow code like:
# Items: binpacking.Items{
# {
# },
# }
- name: nested-structs
disabled: true
gci:
sections:
- standard
- default
- prefix(github.com/castai/cluster-controller)
issues:
exclude-dirs:
- mock
- usestdlibvars
- usetesting
- wastedassign
- whitespace
settings:
goconst:
min-len: 2
min-occurrences: 5
gocritic:
disabled-checks:
- commentFormatting
godot:
scope: all
govet:
settings:
printf:
funcs:
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Infof
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Warnf
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Errorf
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Fatalf
lll:
line-length: 200
misspell:
locale: US
revive:
rules:
- name: redefines-builtin-id
disabled: true
- name: nested-structs
disabled: true
exclusions:
generated: lax
presets:
- comments
- common-false-positives
- legacy
- std-error-handling
paths:
- mock
- third_party$
- builtin$
- examples$
formatters:
enable:
- gci
- gofmt
- gofumpt
settings:
gci:
sections:
- standard
- default
- prefix(github.com/castai/cluster-controller)
gofumpt:
module-path: github.com/castai/cluster-controller
extra-rules: true
exclusions:
generated: lax
paths:
- mock
- third_party$
- builtin$
- examples$
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ TOOLS_DIR=bin
ROOT_DIR=$(abspath .)
TOOLS_GOBIN_DIR := $(abspath $(TOOLS_DIR))

GOLANGCI_LINT_VER := v1.64.8
GOLANGCI_LINT_VER := v2.7.2
GOLANGCI_LINT_BIN := golangci-lint
GOLANGCI_LINT := $(TOOLS_GOBIN_DIR)/$(GOLANGCI_LINT_BIN)-$(GOLANGCI_LINT_VER)

Expand All @@ -20,7 +20,7 @@ endif


$(GOLANGCI_LINT):
GOBIN=$(TOOLS_GOBIN_DIR) $(GO_INSTALL) github.com/golangci/golangci-lint/cmd/golangci-lint $(GOLANGCI_LINT_BIN) $(GOLANGCI_LINT_VER)
GOBIN=$(TOOLS_GOBIN_DIR) $(GO_INSTALL) github.com/golangci/golangci-lint/v2/cmd/golangci-lint $(GOLANGCI_LINT_BIN) $(GOLANGCI_LINT_VER)

## build: Build the binary for the specified architecture and create a Docker image. Usually this means ARCH=amd64 should be set if running on an ARM machine. Use `go build .` for simple local build.
build:
Expand Down
11 changes: 6 additions & 5 deletions cmd/controller/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,14 +295,14 @@ func runWithLeaderElection(
// This method is always called(even if it was not a leader):
// - when controller shuts dow (for example because of SIGTERM)
// - we actually lost leader
// So we need to check what whas reason of acutally stopping.
// So we need to check what whas reason of actually stopping.
if err := ctx.Err(); err != nil {
log.Infof("main context done, stopping controller: %v", err)
return
}
log.Infof("leader lost: %s", id)
// We don't need to exit here.
// Leader "on started leading" receive a context that gets cancelled when you're no longer the leader.
// Leader "on started leading" receive a context that gets canceled when you're no longer the leader.
},
OnNewLeader: func(identity string) {
// We're notified when new leader elected.
Expand Down Expand Up @@ -338,13 +338,14 @@ func (e *logContextError) Unwrap() error {
return e.err
}

func runningOnGKE(clientset *kubernetes.Clientset, cfg config.Config) (isGKE bool, err error) {
func runningOnGKE(clientset *kubernetes.Clientset, cfg config.Config) (bool, error) {
// When running locally, there is no node.
if cfg.SelfPod.Node == "" {
return false, nil
}

err = waitext.Retry(context.Background(), waitext.DefaultExponentialBackoff(), 3, func(ctx context.Context) (bool, error) {
var isGKE bool
err := waitext.Retry(context.Background(), waitext.DefaultExponentialBackoff(), 3, func(ctx context.Context) (bool, error) {
node, err := clientset.CoreV1().Nodes().Get(ctx, cfg.SelfPod.Node, metav1.GetOptions{})
if err != nil && !apierrors.IsNotFound(err) {
return true, fmt.Errorf("getting node: %w", err)
Expand All @@ -361,7 +362,7 @@ func runningOnGKE(clientset *kubernetes.Clientset, cfg config.Config) (isGKE boo
}, func(err error) {
})

return
return isGKE, err
}

func saveMetadata(clusterID string, cfg config.Config, log *logrus.Entry) error {
Expand Down
8 changes: 3 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"os"
"slices"

"github.com/spf13/cobra"

Expand All @@ -21,11 +22,8 @@ func Execute(ctx context.Context) {
cmd := rootCmd.Commands()

for _, a := range cmd {
for _, b := range os.Args[1:] {
if a.Name() == b {
cmdFound = true
break
}
if slices.Contains(os.Args[1:], a.Name()) {
cmdFound = true
}
}
if !cmdFound {
Expand Down
2 changes: 1 addition & 1 deletion health/healthz.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (h *HealthzProvider) Check(_ *http.Request) (err error) {

if h.initStartedAt != nil {
if time.Since(*h.initStartedAt) > h.cfg.StartTimeLimit {
return fmt.Errorf("there was no sucessful poll action since start of application %s", h.cfg.StartTimeLimit)
return fmt.Errorf("there was no successful poll action since start of application %s", h.cfg.StartTimeLimit)
}
return nil
}
Expand Down
30 changes: 15 additions & 15 deletions internal/actions/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,23 @@ func NewDefaultActionHandlers(
helmClient helm.Client,
) ActionHandlers {
return ActionHandlers{
reflect.TypeOf(&castai.ActionDeleteNode{}): NewDeleteNodeHandler(log, clientset),
reflect.TypeOf(&castai.ActionDrainNode{}): NewDrainNodeHandler(log, clientset, castNamespace),
reflect.TypeOf(&castai.ActionPatchNode{}): NewPatchNodeHandler(log, clientset),
reflect.TypeOf(&castai.ActionCreateEvent{}): NewCreateEventHandler(log, clientset),
reflect.TypeOf(&castai.ActionChartUpsert{}): NewChartUpsertHandler(log, helmClient),
reflect.TypeOf(&castai.ActionChartUninstall{}): NewChartUninstallHandler(log, helmClient),
reflect.TypeOf(&castai.ActionChartRollback{}): NewChartRollbackHandler(log, helmClient, k8sVersion),
reflect.TypeOf(&castai.ActionDisconnectCluster{}): NewDisconnectClusterHandler(log, clientset),
reflect.TypeOf(&castai.ActionCheckNodeDeleted{}): NewCheckNodeDeletedHandler(log, clientset),
reflect.TypeOf(&castai.ActionCheckNodeStatus{}): NewCheckNodeStatusHandler(log, clientset),
reflect.TypeOf(&castai.ActionEvictPod{}): NewEvictPodHandler(log, clientset),
reflect.TypeOf(&castai.ActionPatch{}): NewPatchHandler(log, dynamicClient),
reflect.TypeOf(&castai.ActionCreate{}): NewCreateHandler(log, dynamicClient),
reflect.TypeOf(&castai.ActionDelete{}): NewDeleteHandler(log, dynamicClient),
reflect.TypeFor[*castai.ActionDeleteNode](): NewDeleteNodeHandler(log, clientset),
reflect.TypeFor[*castai.ActionDrainNode](): NewDrainNodeHandler(log, clientset, castNamespace),
reflect.TypeFor[*castai.ActionPatchNode](): NewPatchNodeHandler(log, clientset),
reflect.TypeFor[*castai.ActionCreateEvent](): NewCreateEventHandler(log, clientset),
reflect.TypeFor[*castai.ActionChartUpsert](): NewChartUpsertHandler(log, helmClient),
reflect.TypeFor[*castai.ActionChartUninstall](): NewChartUninstallHandler(log, helmClient),
reflect.TypeFor[*castai.ActionChartRollback](): NewChartRollbackHandler(log, helmClient, k8sVersion),
reflect.TypeFor[*castai.ActionDisconnectCluster](): NewDisconnectClusterHandler(log, clientset),
reflect.TypeFor[*castai.ActionCheckNodeDeleted](): NewCheckNodeDeletedHandler(log, clientset),
reflect.TypeFor[*castai.ActionCheckNodeStatus](): NewCheckNodeStatusHandler(log, clientset),
reflect.TypeFor[*castai.ActionEvictPod](): NewEvictPodHandler(log, clientset),
reflect.TypeFor[*castai.ActionPatch](): NewPatchHandler(log, dynamicClient),
reflect.TypeFor[*castai.ActionCreate](): NewCreateHandler(log, dynamicClient),
reflect.TypeFor[*castai.ActionDelete](): NewDeleteHandler(log, dynamicClient),
}
}

func (h ActionHandlers) Close() error {
return h[reflect.TypeOf(&castai.ActionCreateEvent{})].(*CreateEventHandler).Close()
return h[reflect.TypeFor[*castai.ActionCreateEvent]()].(*CreateEventHandler).Close()
}
1 change: 0 additions & 1 deletion internal/actions/check_node_deleted_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,6 @@ func TestCheckNodeDeletedHandler_Handle(t *testing.T) {
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
clientSet := fake.NewClientset(tt.args.tuneFakeObjects...)
Expand Down
12 changes: 3 additions & 9 deletions internal/actions/check_node_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import (
"errors"
"fmt"
"reflect"
"slices"
"time"

"github.com/samber/lo"
"github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/typed/core/v1"
v1 "k8s.io/client-go/kubernetes/typed/core/v1"

"github.com/castai/cluster-controller/internal/castai"
"github.com/castai/cluster-controller/internal/waitext"
Expand Down Expand Up @@ -63,7 +64,6 @@ func (h *CheckNodeStatusHandler) Handle(ctx context.Context, action *castai.Clus
case castai.ActionCheckNodeStatus_DELETED:
log.Info("checking node deleted")
return h.checkNodeDeleted(ctx, log, req)

}

return fmt.Errorf("unknown status to check provided node=%s status=%s", req.NodeName, req.NodeStatus)
Expand Down Expand Up @@ -176,13 +176,7 @@ func (h *CheckNodeStatusHandler) isNodeReady(node *corev1.Node, castNodeID, prov
}

func containsUninitializedNodeTaint(taints []corev1.Taint) bool {
for _, taint := range taints {
// Some providers like AKS provider adds this taint even if node contains ready condition.
if taint == taintCloudProviderUninitialized {
return true
}
}
return false
return slices.Contains(taints, taintCloudProviderUninitialized)
}

var taintCloudProviderUninitialized = corev1.Taint{
Expand Down
2 changes: 0 additions & 2 deletions internal/actions/check_node_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ func TestCheckNodeStatusHandler_Handle_Deleted(t *testing.T) {
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
clientSet := fake.NewClientset(tt.fields.tuneFakeObjects...)
Expand Down Expand Up @@ -434,7 +433,6 @@ func TestCheckNodeStatusHandler_Handle_Ready(t *testing.T) {
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
clientSet := fake.NewClientset()
Expand Down
Loading
Loading