Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added some more linters #1218

Merged
merged 2 commits into from
Sep 21, 2023
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
78 changes: 68 additions & 10 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,88 @@ run:
linters:
disable-all: true
enable:
- deadcode
- asasalint
- asciicheck
- bidichk
- decorder
- dupl
- durationcheck
- errcheck
- errname
- errorlint
- exhaustive
- exportloopref
- ginkgolinter
- gocheckcompilerdirectives
- gofmt
- goimports
- gosimple
- govet
- grouper
- importas
- ineffassign
- makezero
- misspell
- unused
- varcheck
- staticcheck
- errcheck
# - goimports
- dupl
- nakedret
- promlinter
- revive
- staticcheck
- stylecheck
# - gofmt
# - golint
# - structcheck
- tagalign
- typecheck
- unconvert
- unused
- usestdlibvars
- whitespace

# next linters to be enabled:
# - prealloc
# - nilnil
# - errchkjson
# - gocritic

# linters to be enabled in the distant future:
# - cyclop
# - dupl
# - funlen
# - interfacebloat
# - predeclared
# - stylecheck
# - wrapcheck

linters-settings:
gofmt:
simplify: true
dupl:
threshold: 400

exhaustive:
check:
- switch
- map
ignore-enum-types: "ResourceName|Atom"
default-signifies-exhaustive: true

importas:
no-unaliased: true
alias:
# Kubernetes
- pkg: k8s\.io/api/(\w+)/(v[\w\d]+)
alias: $1$2
- pkg: k8s\.io/apimachinery/pkg/api/(\w+)/(v[\w\d]+)
alias: $1$2
- pkg: k8s.io/apimachinery/pkg/api/errors
alias: kerrors
- pkg: k8s.io/apimachinery/pkg/apis/meta/internalversion
alias: metainternalversion

tagalign:
order:
- json
- yaml
- xml
- form

issues:
# Maximum issues count per one linter. Set to 0 to disable. Default is 50.
max-issues-per-linter: 0
Expand Down
4 changes: 2 additions & 2 deletions cmd/vcluster/cmd/certs.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/pkg/errors"
"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/klog/v2"
Expand Down Expand Up @@ -178,7 +178,7 @@ func ExecuteCerts(ctx context.Context, options *CertsCmd) error {

// finally create the secret
_, err = kubeClient.CoreV1().Secrets(options.Namespace).Create(ctx, secret, metav1.CreateOptions{})
if err != nil && !k8serrors.IsAlreadyExists(err) {
if err != nil && !kerrors.IsAlreadyExists(err) {
return errors.Wrap(err, "create certs secret")
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/vcluster/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ func WriteKubeConfigToSecret(ctx context.Context, currentNamespace string, curre
// write the extra secret
err = kubeconfig.WriteKubeConfig(ctx, currentNamespaceClient, options.KubeConfigSecret, secretNamespace, config)
if err != nil {
return fmt.Errorf("creating %s secret in the %s ns failed: %v", options.KubeConfigSecret, secretNamespace, err)
return fmt.Errorf("creating %s secret in the %s ns failed: %w", options.KubeConfigSecret, secretNamespace, err)
}
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/vcluster/context/enable_controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"strings"

"k8s.io/apimachinery/pkg/api/errors"
kerrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/discovery"
"k8s.io/klog/v2"
Expand Down Expand Up @@ -161,7 +161,7 @@ func disableMissingAPIs(discoveryClient discovery.DiscoveryInterface, controller
enabledControllers := controllers.Clone()
for groupVersion, resourceList := range possibleMissing {
resources, err := discoveryClient.ServerResourcesForGroupVersion(groupVersion)
if err != nil && !errors.IsNotFound(err) {
if err != nil && !kerrors.IsNotFound(err) {
return nil, err
}
for _, resourcePlural := range resourceList {
Expand Down
24 changes: 12 additions & 12 deletions cmd/vclusterctl/cmd/app/create/types.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package create

// CreateOptions holds the create cmd options
type CreateOptions struct {
// Options holds the create cmd options
type Options struct {
KubeConfigContextName string
ChartVersion string
ChartName string
Expand Down Expand Up @@ -33,20 +33,20 @@ type Values struct {

type Init struct {
Manifests string `json:"manifests" mapstructure:"manifests"`
Helm []HelmChart `json:"helm" mapstructure:"helm"`
Helm []HelmChart `json:"helm" mapstructure:"helm"`
}

type HelmChart struct {
Bundle string `mapstructure:"bundle,omitempty" json:"bundle,omitempty"`
Name string `mapstructure:"name" json:"name,omitempty"`
Repo string `mapstructure:"repo" json:"repo,omitempty"`
Version string `mapstructure:"version" json:"version,omitempty"`
Namespace string `mapstructure:"namespace" json:"namespace,omitempty"`
Values string `mapstructure:"values" json:"values,omitempty"`
Release HelmRelease `mapstructure:"release" json:"release,omitempty"`
Bundle string `json:"bundle,omitempty" mapstructure:"bundle,omitempty"`
Name string `json:"name,omitempty" mapstructure:"name"`
Repo string `json:"repo,omitempty" mapstructure:"repo"`
Version string `json:"version,omitempty" mapstructure:"version"`
Namespace string `json:"namespace,omitempty" mapstructure:"namespace"`
Values string `json:"values,omitempty" mapstructure:"values"`
Release HelmRelease `json:"release,omitempty" mapstructure:"release"`
}

type HelmRelease struct {
Name string `mapstructure:"name" json:"name,omitempty"`
Namespace string `mapstructure:"namespace" json:"namespace,omitempty"`
Name string `json:"name,omitempty" mapstructure:"name"`
Namespace string `json:"namespace,omitempty" mapstructure:"namespace"`
}
10 changes: 6 additions & 4 deletions cmd/vclusterctl/cmd/app/localkubernetes/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func ExposeLocal(ctx context.Context, vClusterName, vClusterNamespace string, ra
return minikubeProxy(ctx, vClusterName, vClusterNamespace, rawConfig, vRawConfig, service, localPort, timeout, log)
case ClusterTypeK3D:
return k3dProxy(ctx, vClusterName, vClusterNamespace, rawConfig, vRawConfig, service, localPort, timeout, log)
default:
}

return "", nil
Expand All @@ -64,6 +65,7 @@ func CleanupLocal(vClusterName, vClusterNamespace string, rawConfig *clientcmdap
return cleanupProxy(vClusterName, vClusterNamespace, rawConfig, log)
case ClusterTypeK3D:
return cleanupProxy(vClusterName, vClusterNamespace, rawConfig, log)
default:
}

return nil
Expand Down Expand Up @@ -135,7 +137,7 @@ func minikubeProxy(ctx context.Context, vClusterName, vClusterNamespace string,
return true, nil
})
if waitErr != nil {
return "", fmt.Errorf("test connection: %v %v", waitErr, err)
return "", fmt.Errorf("test connection: %w %w", waitErr, err)
}

// now it's safe to modify the vRawConfig struct that was passed in as a pointer
Expand Down Expand Up @@ -218,7 +220,7 @@ func directConnection(ctx context.Context, vRawConfig *clientcmdapi.Config, serv
return true, nil
})
if waitErr != nil {
return "", fmt.Errorf("test connection: %v %v", waitErr, err)
return "", fmt.Errorf("test connection: %w %w", waitErr, err)
}

return server, nil
Expand Down Expand Up @@ -261,7 +263,7 @@ func createProxyContainer(ctx context.Context, vClusterName, vClusterNamespace s
return true, nil
})
if waitErr != nil {
return "", fmt.Errorf("test connection: %v %v", waitErr, err)
return "", fmt.Errorf("test connection: %w %w", waitErr, err)
}

return server, nil
Expand Down Expand Up @@ -329,7 +331,7 @@ func CreateBackgroundProxyContainer(ctx context.Context, vClusterName, vClusterN
return true, nil
})
if waitErr != nil {
return "", fmt.Errorf("test connection: %v %v", waitErr, err)
return "", fmt.Errorf("test connection: %w %w", waitErr, err)
}
return server, nil
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/vclusterctl/cmd/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"time"

"github.com/pkg/errors"
"github.com/samber/lo"
"github.com/spf13/cobra"
authenticationv1 "k8s.io/api/authentication/v1"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -341,7 +342,7 @@ func (cmd *ConnectCmd) getVClusterKubeConfig(ctx context.Context, vclusterName s
return true, nil
})
if waitErr != nil {
return nil, fmt.Errorf("finding vcluster pod: %v - %v", waitErr, err)
return nil, fmt.Errorf("finding vcluster pod: %w - %w", waitErr, err)
}
}

Expand Down Expand Up @@ -611,7 +612,7 @@ func (cmd *ConnectCmd) executeCommand(ctx context.Context, vKubeConfig api.Confi

return errors.Wrap(err, "error port-forwarding")
case err := <-commandErrChan:
if exitError, ok := err.(*exec.ExitError); ok {
if exitError, ok := lo.ErrorsAs[*exec.ExitError](err); ok {
cmd.Log.Errorf("Error executing command: %v", err)
os.Exit(exitError.ExitCode())
}
Expand Down
24 changes: 12 additions & 12 deletions cmd/vclusterctl/cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const LoftChartRepo = "https://charts.loft.sh"
// CreateCmd holds the login cmd flags
type CreateCmd struct {
*flags.GlobalFlags
create.CreateOptions
create.Options

log log.Logger

Expand Down Expand Up @@ -91,7 +91,7 @@ vcluster create test --namespace test
RunE: func(cobraCmd *cobra.Command, args []string) error {
// Check for newer version
upgrade.PrintNewerVersionWarning()
validateDeprecated(&cmd.CreateOptions, cmd.log)
validateDeprecated(&cmd.Options, cmd.log)
return cmd.Run(cobraCmd.Context(), args)
},
}
Expand Down Expand Up @@ -119,7 +119,7 @@ vcluster create test --namespace test
return cobraCmd
}

func validateDeprecated(createOptions *create.CreateOptions, log log.Logger) {
func validateDeprecated(createOptions *create.Options, log log.Logger) {
if createOptions.ReleaseValues != "" {
log.Warn("Flag --release-values is deprecated, please use --extra-values instead. This flag will be removed in future!")
}
Expand Down Expand Up @@ -247,12 +247,12 @@ func (cmd *CreateCmd) Run(ctx context.Context, args []string) error {
}

return connectCmd.Connect(ctx, args[0], nil)
}

if cmd.localCluster {
cmd.log.Donef("Successfully created virtual cluster %s in namespace %s. \n- Use 'vcluster connect %s --namespace %s' to access the virtual cluster", args[0], cmd.Namespace, args[0], cmd.Namespace)
} else {
if cmd.localCluster {
cmd.log.Donef("Successfully created virtual cluster %s in namespace %s. \n- Use 'vcluster connect %s --namespace %s' to access the virtual cluster", args[0], cmd.Namespace, args[0], cmd.Namespace)
} else {
cmd.log.Donef("Successfully created virtual cluster %s in namespace %s. \n- Use 'vcluster connect %s --namespace %s' to access the virtual cluster\n- Use `vcluster connect %s --namespace %s -- kubectl get ns` to run a command directly within the vcluster", args[0], cmd.Namespace, args[0], cmd.Namespace, args[0], cmd.Namespace)
}
cmd.log.Donef("Successfully created virtual cluster %s in namespace %s. \n- Use 'vcluster connect %s --namespace %s' to access the virtual cluster\n- Use `vcluster connect %s --namespace %s -- kubectl get ns` to run a command directly within the vcluster", args[0], cmd.Namespace, args[0], cmd.Namespace, args[0], cmd.Namespace)
}

return nil
Expand All @@ -270,7 +270,7 @@ func (cmd *CreateCmd) deployChart(ctx context.Context, vClusterName, chartValues
// check if there is a vcluster directory already
workDir, err := os.Getwd()
if err != nil {
return fmt.Errorf("unable to get current work directory: %v", err)
return fmt.Errorf("unable to get current work directory: %w", err)
}
if _, err := os.Stat(filepath.Join(workDir, cmd.ChartName)); err == nil {
return fmt.Errorf("aborting vcluster creation. Current working directory contains a file or a directory with the name equal to the vcluster chart name - \"%s\". Please execute vcluster create command from a directory that doesn't contain a file or directory named \"%s\"", cmd.ChartName, cmd.ChartName)
Expand Down Expand Up @@ -400,7 +400,7 @@ func (cmd *CreateCmd) prepare(ctx context.Context, vClusterName string) error {
// load the raw config
rawConfig, err := kubeClientConfig.RawConfig()
if err != nil {
return fmt.Errorf("there is an error loading your current kube config (%v), please make sure you have access to a kubernetes cluster and the command `kubectl get namespaces` is working", err)
return fmt.Errorf("there is an error loading your current kube config (%w), please make sure you have access to a kubernetes cluster and the command `kubectl get namespaces` is working", err)
}
if cmd.Context != "" {
rawConfig.CurrentContext = cmd.Context
Expand All @@ -427,7 +427,7 @@ func (cmd *CreateCmd) prepare(ctx context.Context, vClusterName string) error {
})
rawConfig, err = kubeClientConfig.RawConfig()
if err != nil {
return fmt.Errorf("there is an error loading your current kube config (%v), please make sure you have access to a kubernetes cluster and the command `kubectl get namespaces` is working", err)
return fmt.Errorf("there is an error loading your current kube config (%w), please make sure you have access to a kubernetes cluster and the command `kubectl get namespaces` is working", err)
}
err = switchContext(&rawConfig, cmd.Context)
if err != nil {
Expand All @@ -442,7 +442,7 @@ func (cmd *CreateCmd) prepare(ctx context.Context, vClusterName string) error {
// load the rest config
kubeConfig, err := kubeClientConfig.ClientConfig()
if err != nil {
return fmt.Errorf("there is an error loading your current kube config (%v), please make sure you have access to a kubernetes cluster and the command `kubectl get namespaces` is working", err)
return fmt.Errorf("there is an error loading your current kube config (%w), please make sure you have access to a kubernetes cluster and the command `kubectl get namespaces` is working", err)
}

client, err := kubernetes.NewForConfig(kubeConfig)
Expand Down
2 changes: 1 addition & 1 deletion cmd/vclusterctl/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func (cmd *DeleteCmd) prepare(ctx context.Context, vClusterName string) error {
// load the raw config
rawConfig, err := vCluster.ClientFactory.RawConfig()
if err != nil {
return fmt.Errorf("there is an error loading your current kube config (%v), please make sure you have access to a kubernetes cluster and the command `kubectl get namespaces` is working", err)
return fmt.Errorf("there is an error loading your current kube config (%w), please make sure you have access to a kubernetes cluster and the command `kubectl get namespaces` is working", err)
}
err = deleteContext(&rawConfig, find.VClusterContextName(vCluster.Name, vCluster.Namespace, vCluster.Context), vCluster.Context)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/vclusterctl/cmd/disconnect.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ vcluster disconnect
}

// Run executes the functionality
func (cmd *DisconnectCmd) Run(cobraCmd *cobra.Command, args []string) error {
func (cmd *DisconnectCmd) Run(*cobra.Command, []string) error {
rawConfig, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(clientcmd.NewDefaultClientConfigLoadingRules(), &clientcmd.ConfigOverrides{
CurrentContext: cmd.Context,
}).RawConfig()
Expand Down
4 changes: 2 additions & 2 deletions cmd/vclusterctl/cmd/get/service_cidr.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func getServiceCIDR(globalFlags *flags.GlobalFlags) *cobra.Command {
#######################################################
Prints Service CIDR of the cluster

Ex:
Ex:
vcluster get service-cidr
10.96.0.0/12
#######################################################
Expand All @@ -52,7 +52,7 @@ func (cmd *serviceCIDRCmd) Run(cobraCmd *cobra.Command) error {
// load the rest config
kubeConfig, err := kubeClientConfig.ClientConfig()
if err != nil {
return fmt.Errorf("there is an error loading your current kube config (%v), please make sure you have access to a kubernetes cluster and the command `kubectl get namespaces` is working", err)
return fmt.Errorf("there is an error loading your current kube config (%w), please make sure you have access to a kubernetes cluster and the command `kubectl get namespaces` is working", err)
}

client, err := kubernetes.NewForConfig(kubeConfig)
Expand Down
2 changes: 1 addition & 1 deletion cmd/vclusterctl/cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ vcluster list --namespace test
}

// Run executes the functionality
func (cmd *ListCmd) Run(cobraCmd *cobra.Command, args []string) error {
func (cmd *ListCmd) Run(cobraCmd *cobra.Command, _ []string) error {
rawConfig, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(clientcmd.NewDefaultClientConfigLoadingRules(), &clientcmd.ConfigOverrides{}).RawConfig()
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion cmd/vclusterctl/cmd/login/notpro.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ import (
"github.com/spf13/cobra"
)

func NewLoginCmd(globalFlags *flags.GlobalFlags) (*cobra.Command, error) {
func NewLoginCmd(*flags.GlobalFlags) (*cobra.Command, error) {
return nil, nil
}
Loading