Skip to content

Commit

Permalink
chore(lint): Update to golangci-lint v1.58.2 and fix lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
hrak committed Jun 18, 2024
1 parent 029c698 commit 94f7938
Show file tree
Hide file tree
Showing 27 changed files with 242 additions and 186 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pr-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.56.2
version: v1.58.2
args: --timeout=5m

build:
Expand Down
47 changes: 25 additions & 22 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,34 @@ issues:
max-same-issues: 0 # disable

linters-settings:

staticcheck:
go: "1.15"
checks: [ "all" ]

stylecheck:
go: "1.15"
checks: [ "all" ]

gci:
sections:
- standard
- default
- prefix(github.com/leaseweb/cloudstack-csi-driver)
goimports:
local-prefixes: github.com/apalia/cloudstack-csi-driver
local-prefixes: github.com/leaseweb/cloudstack-csi-driver

misspell:
locale: US

linters:
disable-all: true
enable:
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
- stylecheck
- goimports
- typecheck
- unused
- misspell
enable-all: true
disable:
- cyclop
- depguard
- err113
- exhaustruct
- funlen
- gochecknoglobals
- gomnd
- inamedparam
- ireturn
- lll
- mnd
- paralleltest
- tagliatelle
- testpackage
- varnamelen
- wrapcheck
- wsl
11 changes: 6 additions & 5 deletions cmd/cloudstack-csi-driver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var (
debug = flag.Bool("debug", false, "Enable debug logging")
showVersion = flag.Bool("version", false, "Show version")

// Version is set by the build process
// Version is set by the build process.
version = ""
isDevEnv = false
)
Expand All @@ -35,7 +35,8 @@ func main() {

if *showVersion {
baseName := path.Base(os.Args[0])
fmt.Println(baseName, version)
fmt.Println(baseName, version) //nolint:forbidigo

return
}

Expand All @@ -48,7 +49,7 @@ func main() {
}

func run() {
// Setup logging
// Setup logging.
var logConfig zap.Config
if isDevEnv {
logConfig = zap.NewDevelopmentConfig()
Expand All @@ -63,11 +64,11 @@ func run() {
undo := zap.ReplaceGlobals(logger)
defer undo()

// Setup cloud connector
// Setup cloud connector.
config, err := cloud.ReadConfig(*cloudstackconfig)
if err != nil {
logger.Sugar().Errorw("Cannot read CloudStack configuration", "error", err)
os.Exit(1)
os.Exit(1) //nolint:gocritic
}
logger.Sugar().Debugf("Successfully read CloudStack configuration %v", *cloudstackconfig)
csConnector := cloud.New(config)
Expand Down
9 changes: 5 additions & 4 deletions cmd/cloudstack-csi-sc-syncer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ var (
kubeconfig = flag.String("kubeconfig", path.Join(os.Getenv("HOME"), ".kube/config"), "Kubernetes configuration file. Use \"-\" to use in-cluster configuration.")
label = flag.String("label", "app.kubernetes.io/managed-by="+agent, "")
namePrefix = flag.String("namePrefix", "cloudstack-", "")
delete = flag.Bool("delete", false, "Delete")
deleteUnused = flag.Bool("delete", false, "Delete")
volumeExpansion = flag.Bool("volumeExpansion", false, "VolumeExpansion")
showVersion = flag.Bool("version", false, "Show version")

// Version is set by the build process
// Version is set by the build process.
version = ""
)

Expand All @@ -33,7 +33,8 @@ func main() {

if *showVersion {
baseName := path.Base(os.Args[0])
fmt.Println(baseName, version)
fmt.Println(baseName, version) //nolint:forbidigo

return
}

Expand All @@ -43,7 +44,7 @@ func main() {
KubeConfig: *kubeconfig,
Label: *label,
NamePrefix: *namePrefix,
Delete: *delete,
Delete: *deleteUnused,
VolumeExpansion: *volumeExpansion,
})
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion pkg/cloud/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type VM struct {
ZoneID string
}

// Specific errors
// Specific errors.
var (
ErrNotFound = errors.New("not found")
ErrTooManyResults = errors.New("too many results")
Expand All @@ -60,5 +60,6 @@ type client struct {
// New creates a new cloud connector, given its configuration.
func New(config *Config) Interface {
csClient := cloudstack.NewAsyncClient(config.APIURL, config.APIKey, config.SecretKey, config.VerifySSL)

return &client{csClient}
}
2 changes: 1 addition & 1 deletion pkg/cloud/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Config struct {
// csConfig wraps the config for the CloudStack cloud provider.
// It is taken from https://github.com/apache/cloudstack-kubernetes-provider
// in order to have the same config in cloudstack-kubernetes-provider
// and in this cloudstack-csi-driver
// and in this cloudstack-csi-driver.
type csConfig struct {
Global struct {
APIURL string `gcfg:"api-url"`
Expand Down
34 changes: 20 additions & 14 deletions pkg/cloud/fake/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,45 +35,49 @@ func New() cloud.Interface {
ID: "0d7107a3-94d2-44e7-89b8-8930881309a5",
ZoneID: zoneID,
}

return &fakeConnector{
node: node,
volumesByID: map[string]cloud.Volume{volume.ID: volume},
volumesByName: map[string]cloud.Volume{volume.Name: volume},
}
}

func (f *fakeConnector) GetVMByID(ctx context.Context, vmID string) (*cloud.VM, error) {
func (f *fakeConnector) GetVMByID(_ context.Context, vmID string) (*cloud.VM, error) {
if vmID == f.node.ID {
return f.node, nil
}

return nil, cloud.ErrNotFound
}

func (f *fakeConnector) GetNodeInfo(ctx context.Context, vmName string) (*cloud.VM, error) {
func (f *fakeConnector) GetNodeInfo(_ context.Context, _ string) (*cloud.VM, error) {
return f.node, nil
}

func (f *fakeConnector) ListZonesID(ctx context.Context) ([]string, error) {
func (f *fakeConnector) ListZonesID(_ context.Context) ([]string, error) {
return []string{zoneID}, nil
}

func (f *fakeConnector) GetVolumeByID(ctx context.Context, volumeID string) (*cloud.Volume, error) {
func (f *fakeConnector) GetVolumeByID(_ context.Context, volumeID string) (*cloud.Volume, error) {
vol, ok := f.volumesByID[volumeID]
if ok {
return &vol, nil
}

return nil, cloud.ErrNotFound
}

func (f *fakeConnector) GetVolumeByName(ctx context.Context, name string) (*cloud.Volume, error) {
func (f *fakeConnector) GetVolumeByName(_ context.Context, name string) (*cloud.Volume, error) {
vol, ok := f.volumesByName[name]
if ok {
return &vol, nil
}

return nil, cloud.ErrNotFound
}

func (f *fakeConnector) CreateVolume(ctx context.Context, diskOfferingID, zoneID, name string, sizeInGB int64) (string, error) {
func (f *fakeConnector) CreateVolume(_ context.Context, diskOfferingID, zoneID, name string, sizeInGB int64) (string, error) {
id, _ := uuid.GenerateUUID()
vol := cloud.Volume{
ID: id,
Expand All @@ -84,37 +88,39 @@ func (f *fakeConnector) CreateVolume(ctx context.Context, diskOfferingID, zoneID
}
f.volumesByID[vol.ID] = vol
f.volumesByName[vol.Name] = vol

return vol.ID, nil
}

func (f *fakeConnector) DeleteVolume(ctx context.Context, id string) error {
func (f *fakeConnector) DeleteVolume(_ context.Context, id string) error {
if vol, ok := f.volumesByID[id]; ok {
name := vol.Name
delete(f.volumesByName, name)
}
delete(f.volumesByID, id)

return nil
}

func (f *fakeConnector) AttachVolume(ctx context.Context, volumeID, vmID string) (string, error) {
func (f *fakeConnector) AttachVolume(_ context.Context, _, _ string) (string, error) {
return "1", nil
}

func (f *fakeConnector) DetachVolume(ctx context.Context, volumeID string) error {
func (f *fakeConnector) DetachVolume(_ context.Context, _ string) error {
return nil
}
func (f *fakeConnector) ExpandVolume(ctx context.Context, volumeID string, newSizeInGB int64) error {

func (f *fakeConnector) ExpandVolume(_ context.Context, volumeID string, newSizeInGB int64) error {
if vol, ok := f.volumesByID[volumeID]; ok {
newSizeInBytes := newSizeInGB * 1024 * 1024 * 1024
if newSizeInBytes > vol.Size {
vol.Size = newSizeInBytes
f.volumesByID[volumeID] = vol
f.volumesByName[vol.Name] = vol
return nil
} else {
return nil
}
} else {

return nil
}

return cloud.ErrNotFound
}
17 changes: 10 additions & 7 deletions pkg/cloud/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strings"

Expand All @@ -22,6 +21,7 @@ func (c *client) metadataInstanceID(ctx context.Context) string {
// Try a NODE_ID environment variable
if envNodeID := os.Getenv("NODE_ID"); envNodeID != "" {
slog.Debugf("Found CloudStack VM ID from environment variable NODE_ID: %s", envNodeID)

return envNodeID
}

Expand All @@ -32,11 +32,10 @@ func (c *client) metadataInstanceID(ctx context.Context) string {
ciData, err := c.readCloudInit(ctx, cloudInitInstanceFilePath)
if err != nil {
slog.Errorf("Cannot read cloud-init instance data: %v", err)
} else {
if ciData.V1.InstanceID != "" {
slog.Debugf("Found CloudStack VM ID from cloud-init: %s", ciData.V1.InstanceID)
return ciData.V1.InstanceID
}
} else if ciData.V1.InstanceID != "" {
slog.Debugf("Found CloudStack VM ID from cloud-init: %s", ciData.V1.InstanceID)

return ciData.V1.InstanceID
}
slog.Error("cloud-init instance ID is not provided")
} else if os.IsNotExist(err) {
Expand All @@ -46,6 +45,7 @@ func (c *client) metadataInstanceID(ctx context.Context) string {
}

slog.Debug("CloudStack VM ID not found in meta-data.")

return ""
}

Expand All @@ -62,20 +62,23 @@ type cloudInitV1 struct {
func (c *client) readCloudInit(ctx context.Context, instanceFilePath string) (*cloudInitInstanceData, error) {
slog := ctxzap.Extract(ctx).Sugar()

b, err := ioutil.ReadFile(instanceFilePath)
b, err := os.ReadFile(instanceFilePath)
if err != nil {
slog.Errorf("Cannot read %s", instanceFilePath)

return nil, err
}

var data cloudInitInstanceData
if err := json.Unmarshal(b, &data); err != nil {
slog.Errorf("Cannot parse JSON file %s", instanceFilePath)

return nil, err
}

if strings.ToLower(data.V1.CloudName) != cloudStackCloudName {
slog.Errorf("Cloud-Init cloud name is %s, only %s is supported", data.V1.CloudName, cloudStackCloudName)

return nil, fmt.Errorf("Cloud-Init cloud name is %s, only %s is supported", data.V1.CloudName, cloudStackCloudName)
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/cloud/vms.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func (c *client) GetVMByID(ctx context.Context, vmID string) (*VM, error) {
return nil, ErrTooManyResults
}
vm := l.VirtualMachines[0]

return &VM{
ID: vm.Id,
ZoneID: vm.Zoneid,
Expand All @@ -46,6 +47,7 @@ func (c *client) getVMByName(ctx context.Context, name string) (*VM, error) {
return nil, ErrTooManyResults
}
vm := l.VirtualMachines[0]

return &VM{
ID: vm.Id,
ZoneID: vm.Zoneid,
Expand Down
Loading

0 comments on commit 94f7938

Please sign in to comment.