Skip to content

Commit

Permalink
add revive linter and fix the lint error
Browse files Browse the repository at this point in the history
Signed-off-by: duanmengkk <[email protected]>
  • Loading branch information
duanmengkk committed Sep 13, 2024
1 parent 978e257 commit 84b80c3
Show file tree
Hide file tree
Showing 139 changed files with 545 additions and 926 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ jobs:
uses: actions/setup-go@v4
with:
go-version: '1.20'
- name: Set up GOPROXY for Go
run: go env -w GOPROXY=https://goproxy.cn,direct
- name: lint
run: hack/verify-staticcheck.sh
- name: vendor
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/release_binaries.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ jobs:
- name: Install Go
uses: actions/setup-go@v3

- name: Set up GOPROXY for Go
run: go env -w GOPROXY=https://goproxy.cn,direct

# step3: make binaries
- name: Make binaries
run: make release
Expand Down
31 changes: 21 additions & 10 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
run:
timeout: 10m
modules-download-mode: vendor
skip-dirs:
- (^|/)vendor($|/)
- pkg/utils/lifted
- pkg/scheduler/lifted
linters:
disable-all: true
enable:
- whitespace
- bodyclose
- dupl
- errcheck
- gci
# linters maintained by golang.org
- gofmt
- govet
- goimports
- misspell
- unused
# linters default enabled by golangci-lint .
- errcheck
- gosimple
- typecheck
- staticcheck
- gosimple
- govet
- ineffassign
- typecheck
- unused
# other linters supported by golangci-lint.
- gci
- misspell
- bodyclose
- gocyclo
- gosec
- dupl
- revive
- whitespace
linters-settings:
goimports:
local-prefixes: github.com/kosmos.io/kosmos
Expand All @@ -31,6 +39,9 @@ linters-settings:
- Standard
- Default
- Prefix(github.com/kosmos.io/kosmos)
gocyclo:
# minimal cyclomatic complexity to report
min-complexity: 40 # The recommended value is 15
output:
sort-results: true

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ lint-fix: golangci-lint

golangci-lint:
ifeq (, $(shell which golangci-lint))
GO111MODULE=on go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.54.2
GO111MODULE=on go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.55.2
GOLANGLINT_BIN=$(shell go env GOPATH)/bin/golangci-lint
else
GOLANGLINT_BIN=$(shell which golangci-lint)
Expand Down
5 changes: 1 addition & 4 deletions cmd/clusterlink/agent/app/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ func NewAgentCommand(ctx context.Context) *cobra.Command {
if errs := opts.Validate(); len(errs) != 0 {
return errs.ToAggregate()
}
if err := run(ctx, opts); err != nil {
return err
}
return nil
return run(ctx, opts)
},
Args: func(cmd *cobra.Command, args []string) error {
for _, arg := range args {
Expand Down
5 changes: 1 addition & 4 deletions cmd/clusterlink/clusterlink-operator/app/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ func NewLinkOperatorCommand(ctx context.Context) *cobra.Command {
if errs := opts.Validate(); len(errs) != 0 {
return errs.ToAggregate()
}
if err := Run(ctx, opts); err != nil {
return err
}
return nil
return Run(ctx, opts)
},
Args: func(cmd *cobra.Command, args []string) error {
for _, arg := range args {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func Run(ctx context.Context, opts *options.ControllerManagerOptions) error {
return nil
}

func setupControllers(mgr ctrl.Manager, opts *options.ControllerManagerOptions, ctx context.Context) []ctrlcontext.CleanFunc {
func setupControllers(ctx context.Context, mgr ctrl.Manager, opts *options.ControllerManagerOptions) []ctrlcontext.CleanFunc {
controlPanelConfig, err := clientcmd.BuildConfigFromFlags("", opts.ControlPanelConfig)
if err != nil {
klog.Fatalf("build controlpanel config err: %v", err)
Expand Down
6 changes: 3 additions & 3 deletions cmd/clusterlink/controller-manager/app/controllerstarter.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (c *Controller) OnAdd(obj interface{}) {
}

// OnUpdate handles object update event and push the object to queue.
func (c *Controller) OnUpdate(oldObj, newObj interface{}) {
func (c *Controller) OnUpdate(_, newObj interface{}) {
runtimeObj, ok := newObj.(runtime.Object)
if !ok {
return
Expand All @@ -111,7 +111,7 @@ func (c *Controller) OnDelete(obj interface{}) {
c.OnAdd(obj)
}

func (c *Controller) Reconcile(key lifted.QueueKey) error {
func (c *Controller) Reconcile(_ lifted.QueueKey) error {
cluster, err := c.clusterLister.Get(c.opts.ClusterName)
if err != nil {
return err
Expand Down Expand Up @@ -180,7 +180,7 @@ func (c *Controller) removeFinalizer(cluster *clusterlinkv1alpha1.Cluster) error
}
func (c *Controller) setupControllers() {
subCtx, cancelFunc := context.WithCancel(c.ctx)
cleanFuns := setupControllers(c.mgr, c.opts, subCtx)
cleanFuns := setupControllers(subCtx, c.mgr, c.opts)
c.cancelFunc = cancelFunc
c.cleanFuncs = cleanFuns
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/clusterlink/controller-manager/app/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func startCalicoPoolController(ctx ctrlcontext.Context) (bool, ctrlcontext.Clean
func startNodeCIDRController(ctx ctrlcontext.Context) (bool, ctrlcontext.CleanFunc, error) {
mgr := ctx.Mgr

nodeCIDRCtl := nodecidr.NewNodeCIDRController(mgr.GetConfig(), ctx.Opts.ClusterName, ctx.ClusterLinkClient, ctx.Opts.RateLimiterOpts, ctx.Ctx)
nodeCIDRCtl := nodecidr.NewNodeCIDRController(ctx.Ctx, mgr.GetConfig(), ctx.Opts.ClusterName, ctx.ClusterLinkClient, ctx.Opts.RateLimiterOpts)
if err := mgr.Add(nodeCIDRCtl); err != nil {
klog.Fatalf("Failed to setup node CIDR Controller: %v", err)
return true, nil, nil
Expand Down
5 changes: 1 addition & 4 deletions cmd/clusterlink/elector/app/elector.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ func NewElectorCommand(ctx context.Context) *cobra.Command {
if errs := opts.Validate(); len(errs) != 0 {
return errs.ToAggregate()
}
if err := run(ctx, opts); err != nil {
return err
}
return nil
return run(ctx, opts)
},
Args: func(cmd *cobra.Command, args []string) error {
for _, arg := range args {
Expand Down
5 changes: 1 addition & 4 deletions cmd/clusterlink/floater/app/floater.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ func NewFloaterCommand(ctx context.Context) *cobra.Command {
if errs := opts.Validate(); len(errs) != 0 {
return errs.ToAggregate()
}
if err := Run(ctx, opts); err != nil {
return err
}
return nil
return Run(ctx, opts)
},
Args: func(cmd *cobra.Command, args []string) error {
for _, arg := range args {
Expand Down
2 changes: 1 addition & 1 deletion cmd/clusterlink/floater/app/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ func NewOptions() *Options {
}

// AddFlags adds flags of agent to the specified FlagSet
func (o *Options) AddFlags(fs *pflag.FlagSet) {
func (o *Options) AddFlags(_ *pflag.FlagSet) {
}
5 changes: 1 addition & 4 deletions cmd/clusterlink/network-manager/app/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ func NewNetworkManagerCommand(ctx context.Context) *cobra.Command {
if errs := opts.Validate(); len(errs) != 0 {
return errs.ToAggregate()
}
if err := run(ctx, opts); err != nil {
return err
}
return nil
return run(ctx, opts)
},
}

Expand Down
5 changes: 1 addition & 4 deletions cmd/clusterlink/proxy/app/clusterlink-proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ func NewClusterLinkProxyCommand(ctx context.Context) *cobra.Command {
return errs.ToAggregate()
}
*/
if err := run(ctx, opts); err != nil {
return err
}
return nil
return run(ctx, opts)
},
Args: func(cmd *cobra.Command, args []string) error {
for _, arg := range args {
Expand Down
6 changes: 1 addition & 5 deletions cmd/clusterlink/proxy/app/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,5 @@ func (o *Options) genericOptionsApplyTo(config *genericapiserver.RecommendedConf
if err := o.CoreAPI.ApplyTo(config); err != nil {
return err
}
if err := o.Admission.ApplyTo(&config.Config, config.SharedInformerFactory, config.ClientConfig, o.FeatureGate); err != nil {
return err
}

return nil
return o.Admission.ApplyTo(&config.Config, config.SharedInformerFactory, config.ClientConfig, o.FeatureGate)
}
5 changes: 1 addition & 4 deletions cmd/clustertree/cluster-manager/app/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ func NewClusterManagerCommand(ctx context.Context) (*cobra.Command, error) {
if errs := opts.Validate(); len(errs) != 0 {
return errs.ToAggregate()
}
if err := leaderElectionRun(ctx, opts); err != nil {
return err
}
return nil
return leaderElectionRun(ctx, opts)
},
}

Expand Down
12 changes: 6 additions & 6 deletions cmd/kubenest/node-agent/app/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ var (
operation string // operation for client to execute
)

func cmdCheckRun(cmd *cobra.Command, args []string) error {
func cmdCheckRun(cmd *cobra.Command, _ []string) error {
if len(params) != 1 {
log.Errorf("port list is required and port list size must not be greater than 1")
return fmt.Errorf("port list is required and port list size must not be greater than 1")
Expand Down Expand Up @@ -143,7 +143,7 @@ func init() {
ClientCmd.AddCommand(checkCmd)
}

func cmdTtyRun(cmd *cobra.Command, args []string) error {
func cmdTtyRun(cmd *cobra.Command, _ []string) error {
auth, err := getAuth(cmd)
if err != nil {
return err
Expand Down Expand Up @@ -242,7 +242,7 @@ func connectTty(wsURL string, headers http.Header) error {
}
}

func cmdCmdRun(cmd *cobra.Command, args []string) error {
func cmdCmdRun(cmd *cobra.Command, _ []string) error {
if len(operation) == 0 {
log.Errorf("operation is required")
return fmt.Errorf("operation is required")
Expand All @@ -255,7 +255,7 @@ func cmdCmdRun(cmd *cobra.Command, args []string) error {
return executeWebSocketCommand(auth)
}

func cmdUploadRun(cmd *cobra.Command, args []string) error {
func cmdUploadRun(cmd *cobra.Command, _ []string) error {
auth, err := getAuth(cmd)
if err != nil {
return err
Expand Down Expand Up @@ -316,7 +316,7 @@ func uploadFile(filePath, fileName, auth string) error {
defer wg.Done()
wsURL := fmt.Sprintf("wss://%s/upload/?file_name=%s&file_path=%s", addr, url.QueryEscape(filepath.Base(fileName)), url.QueryEscape(filePath))
fmt.Println("Uploading file:", fileName, "from", filePath, "to", addr)
err := connectAndSendFile(wsURL, headers, filePath, fileName)
err := connectAndSendFile(wsURL, headers, fileName)
if err != nil {
log.Errorf("failed to upload file: %v on %s: %v\n", err, addr, fileName)
}
Expand Down Expand Up @@ -344,7 +344,7 @@ func connectAndHandleMessages(wsURL string, headers http.Header) error {
return nil
}

func connectAndSendFile(wsURL string, headers http.Header, filePath, fileName string) error {
func connectAndSendFile(wsURL string, headers http.Header, fileName string) error {
ws, resp, err := dialer.Dial(wsURL, headers)
if err != nil {
return fmt.Errorf("WebSocket dial error: %v", err)
Expand Down
8 changes: 4 additions & 4 deletions cmd/kubenest/node-agent/app/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func init() {
time.Sleep(10 * time.Second)
}

func TestCmd(t *testing.T) {
func TestCmd(_ *testing.T) {
fmt.Println("Command test")
command := url.QueryEscape("ls -l")
ws, resp, err := dialer.Dial("wss://"+testAddr+"/cmd/?command="+command, headers)
Expand All @@ -59,7 +59,7 @@ func TestCmd(t *testing.T) {
handleMessages(ws)
}

func TestUpload(t *testing.T) {
func TestUpload(_ *testing.T) {
fmt.Println("Upload file test")
fileName := url.QueryEscape("app.go")
filePath := url.QueryEscape("/tmp/websocket")
Expand All @@ -76,7 +76,7 @@ func TestUpload(t *testing.T) {
handleMessages(ws)
}

func TestShellScript(t *testing.T) {
func TestShellScript(_ *testing.T) {
fmt.Println("Shell script test")

ws, resp, err := dialer.Dial("wss://"+testAddr+"/sh/?args=10&&args=10", headers)
Expand All @@ -91,7 +91,7 @@ func TestShellScript(t *testing.T) {
handleMessages(ws)
}

func TestPyScript(t *testing.T) {
func TestPyScript(_ *testing.T) {
fmt.Println("Python script test")
ws, resp, err := dialer.Dial("wss://"+testAddr+"/py/?args=10&&args=10", headers)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/kubenest/node-agent/app/serve/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func init() {
ServeCmd.PersistentFlags().StringVarP(&keyFile, "key", "k", "key.pem", "SSL key file")
}

func serveCmdRun(cmd *cobra.Command, args []string) error {
func serveCmdRun(_ *cobra.Command, _ []string) error {
user := viper.GetString("WEB_USER")
password := viper.GetString("WEB_PASS")
if len(user) == 0 || len(password) == 0 {
Expand Down
17 changes: 7 additions & 10 deletions cmd/kubenest/operator/app/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ func NewVirtualClusterOperatorCommand(ctx context.Context) *cobra.Command {
Use: "virtual-cluster-operator",
Long: `create virtual kubernetes control plane with VirtualCluster`,
RunE: func(cmd *cobra.Command, args []string) error {
if err := runCommand(ctx, opts); err != nil {
return err
}
return nil
return runCommand(ctx, opts)
},
}

Expand Down Expand Up @@ -89,7 +86,7 @@ func SetupConfig(opts *options.Options) (*config.Config, error) {
ko.KubeInKubeConfig.ETCDStorageClass = opts.DeprecatedOptions.KubeInKubeConfig.ETCDStorageClass
ko.KubeInKubeConfig.AdmissionPlugins = opts.DeprecatedOptions.KubeInKubeConfig.AdmissionPlugins
ko.KubeInKubeConfig.AnpMode = opts.DeprecatedOptions.KubeInKubeConfig.AnpMode
ko.KubeInKubeConfig.ApiServerReplicas = opts.DeprecatedOptions.KubeInKubeConfig.ApiServerReplicas
ko.KubeInKubeConfig.APIServerReplicas = opts.DeprecatedOptions.KubeInKubeConfig.APIServerReplicas
ko.KubeInKubeConfig.ClusterCIDR = opts.DeprecatedOptions.KubeInKubeConfig.ClusterCIDR

koc = *ko
Expand Down Expand Up @@ -125,12 +122,12 @@ func SetupConfig(opts *options.Options) (*config.Config, error) {
}

// TODO
func printKubeNestConfiguration(koc v1alpha1.KubeNestConfiguration) {
func printKubeNestConfiguration(_ v1alpha1.KubeNestConfiguration) {

}

// TODO
func fillInForDefault(c *config.Config, koc v1alpha1.KubeNestConfiguration) {
func fillInForDefault(_ *config.Config, _ v1alpha1.KubeNestConfiguration) {

}

Expand Down Expand Up @@ -201,13 +198,13 @@ func startEndPointsControllers(mgr manager.Manager) error {
return fmt.Errorf("error starting %s: %v", endpointscontroller.KonnectivitySyncControllerName, err)
}

ApiServerExternalSyncController := endpointscontroller.ApiServerExternalSyncController{
APIServerExternalSyncController := endpointscontroller.APIServerExternalSyncController{
Client: mgr.GetClient(),
EventRecorder: mgr.GetEventRecorderFor(constants.GlobalNodeControllerName),
}

if err := ApiServerExternalSyncController.SetupWithManager(mgr); err != nil {
return fmt.Errorf("error starting %s: %v", endpointscontroller.ApiServerExternalSyncControllerName, err)
if err := APIServerExternalSyncController.SetupWithManager(mgr); err != nil {
return fmt.Errorf("error starting %s: %v", endpointscontroller.APIServerExternalSyncControllerName, err)
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions cmd/kubenest/operator/app/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type KubeNestOptions struct {
ForceDestroy bool
AnpMode string
AdmissionPlugins bool
ApiServerReplicas int
APIServerReplicas int
ClusterCIDR string
ETCDStorageClass string
ETCDUnitSize string
Expand Down Expand Up @@ -65,7 +65,7 @@ func (o *Options) AddFlags(flags *pflag.FlagSet) {
flags.BoolVar(&o.DeprecatedOptions.KubeInKubeConfig.ForceDestroy, "kube-nest-force-destroy", false, "Force destroy the node.If it set true.If set to true, Kubernetes will not evict the existing nodes on the node when joining nodes to the tenant's control plane, but will instead force destroy.")
flags.StringVar(&o.DeprecatedOptions.KubeInKubeConfig.AnpMode, "kube-nest-anp-mode", "tcp", "kube-apiserver network proxy mode, must be set to tcp or uds. uds mode the replicas for apiserver should be one, and tcp for multi apiserver replicas.")
flags.BoolVar(&o.DeprecatedOptions.KubeInKubeConfig.AdmissionPlugins, "kube-nest-admission-plugins", false, "kube-apiserver network disable-admission-plugins, false for - --disable-admission-plugins=License, true for remove the --disable-admission-plugins=License flag .")
flags.IntVar(&o.DeprecatedOptions.KubeInKubeConfig.ApiServerReplicas, "kube-nest-apiserver-replicas", 1, "virtual-cluster kube-apiserver replicas. default is 2.")
flags.IntVar(&o.DeprecatedOptions.KubeInKubeConfig.APIServerReplicas, "kube-nest-apiserver-replicas", 1, "virtual-cluster kube-apiserver replicas. default is 2.")
flags.StringVar(&o.DeprecatedOptions.KubeInKubeConfig.ClusterCIDR, "cluster-cidr", "10.244.0.0/16", "Used to set the cluster-cidr of kube-controller-manager and kube-proxy (configmap)")
flags.StringVar(&o.DeprecatedOptions.KubeInKubeConfig.ETCDStorageClass, "etcd-storage-class", "openebs-hostpath", "Used to set the etcd storage class.")
flags.StringVar(&o.DeprecatedOptions.KubeInKubeConfig.ETCDUnitSize, "etcd-unit-size", "1Gi", "Used to set the etcd unit size, each node is allocated storage of etcd-unit-size.")
Expand Down
Loading

0 comments on commit 84b80c3

Please sign in to comment.