Skip to content

Commit

Permalink
fix: enhance linting rules and apply necessary corrections (#436)
Browse files Browse the repository at this point in the history
  • Loading branch information
gacevicljubisa authored Dec 5, 2024
1 parent bf56c7d commit 1bf2d6d
Show file tree
Hide file tree
Showing 49 changed files with 114 additions and 127 deletions.
12 changes: 11 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@ run:
timeout: 10m
linters:
enable:
- misspell
- copyloopvar
- errcheck
- errname
- errorlint
- goconst
- gofmt
- gofumpt
- govet
- misspell
- nilerr
- staticcheck
- unconvert
- unused
2 changes: 1 addition & 1 deletion cmd/beekeeper/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ func (c *command) setK8S() (err error) {
k8s.WithKubeconfigPath(kubeconfigPath),
}

if c.k8sClient, err = k8s.NewClient(options...); err != nil && err != k8s.ErrKubeconfigNotSet {
if c.k8sClient, err = k8s.NewClient(options...); err != nil && !errors.Is(err, k8s.ErrKubeconfigNotSet) {
return fmt.Errorf("creating Kubernetes client: %w", err)
}
}
Expand Down
6 changes: 6 additions & 0 deletions mocks/k8s/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import (
rest "k8s.io/client-go/rest"
)

const (
CreateBad string = "create_bad"
UpdateBad string = "update_bad"
DeleteBad string = "delete_bad"
)

// compile simulation whether ClientsetMock implements interface
var _ v1.AppsV1Interface = (*AppV1)(nil)

Expand Down
6 changes: 3 additions & 3 deletions mocks/k8s/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (*ConfigMap) Apply(ctx context.Context, configMap *cofnigcorev1.ConfigMapAp

// Create implements v1.ConfigMapInterface
func (c *ConfigMap) Create(ctx context.Context, configMap *v1.ConfigMap, opts metav1.CreateOptions) (*v1.ConfigMap, error) {
if configMap.ObjectMeta.Name == "create_bad" {
if configMap.ObjectMeta.Name == CreateBad {
return nil, fmt.Errorf("mock error: cannot create config map")
} else {
return nil, fmt.Errorf("mock error: unknown")
Expand All @@ -39,7 +39,7 @@ func (c *ConfigMap) Create(ctx context.Context, configMap *v1.ConfigMap, opts me

// Delete implements v1.ConfigMapInterface
func (c *ConfigMap) Delete(ctx context.Context, name string, opts metav1.DeleteOptions) error {
if name == "delete_bad" {
if name == DeleteBad {
return fmt.Errorf("mock error: cannot delete config map")
} else {
return errors.NewNotFound(schema.GroupResource{}, name)
Expand Down Expand Up @@ -68,7 +68,7 @@ func (*ConfigMap) Patch(ctx context.Context, name string, pt types.PatchType, da

// Update implements v1.ConfigMapInterface
func (c *ConfigMap) Update(ctx context.Context, configMap *v1.ConfigMap, opts metav1.UpdateOptions) (*v1.ConfigMap, error) {
if configMap.ObjectMeta.Name == "update_bad" {
if configMap.ObjectMeta.Name == UpdateBad {
return nil, errors.NewBadRequest("mock error: cannot update config map")
} else {
return nil, errors.NewNotFound(schema.GroupResource{}, configMap.ObjectMeta.Name)
Expand Down
6 changes: 3 additions & 3 deletions mocks/k8s/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (*Ingress) ApplyStatus(ctx context.Context, ingress *networkingv1.IngressAp

// Create implements v1.IngressInterface
func (*Ingress) Create(ctx context.Context, ingress *netv1.Ingress, opts metav1.CreateOptions) (*netv1.Ingress, error) {
if ingress.ObjectMeta.Name == "create_bad" {
if ingress.ObjectMeta.Name == CreateBad {
return nil, fmt.Errorf("mock error: cannot create ingress")
} else {
return nil, fmt.Errorf("mock error: unknown")
Expand All @@ -44,7 +44,7 @@ func (*Ingress) Create(ctx context.Context, ingress *netv1.Ingress, opts metav1.

// Delete implements v1.IngressInterface
func (*Ingress) Delete(ctx context.Context, name string, opts metav1.DeleteOptions) error {
if name == "delete_bad" {
if name == DeleteBad {
return fmt.Errorf("mock error: cannot delete ingress")
} else {
return errors.NewNotFound(schema.GroupResource{}, name)
Expand Down Expand Up @@ -73,7 +73,7 @@ func (*Ingress) Patch(ctx context.Context, name string, pt types.PatchType, data

// Update implements v1.IngressInterface
func (*Ingress) Update(ctx context.Context, ingress *netv1.Ingress, opts metav1.UpdateOptions) (*netv1.Ingress, error) {
if ingress.ObjectMeta.Name == "update_bad" {
if ingress.ObjectMeta.Name == UpdateBad {
return nil, errors.NewBadRequest("mock error: cannot update ingress")
} else {
return nil, errors.NewNotFound(schema.GroupResource{}, ingress.ObjectMeta.Name)
Expand Down
6 changes: 3 additions & 3 deletions mocks/k8s/persistentvolumeclaim.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (*Pvc) ApplyStatus(ctx context.Context, persistentVolumeClaim *configcorev1

// Create implements v1.PersistentVolumeClaimInterface
func (*Pvc) Create(ctx context.Context, persistentVolumeClaim *v1.PersistentVolumeClaim, opts metav1.CreateOptions) (*v1.PersistentVolumeClaim, error) {
if persistentVolumeClaim.ObjectMeta.Name == "create_bad" {
if persistentVolumeClaim.ObjectMeta.Name == CreateBad {
return nil, fmt.Errorf("mock error: cannot create pvc")
} else {
return nil, fmt.Errorf("mock error: unknown")
Expand All @@ -44,7 +44,7 @@ func (*Pvc) Create(ctx context.Context, persistentVolumeClaim *v1.PersistentVolu

// Delete implements v1.PersistentVolumeClaimInterface
func (*Pvc) Delete(ctx context.Context, name string, opts metav1.DeleteOptions) error {
if name == "delete_bad" {
if name == DeleteBad {
return fmt.Errorf("mock error: cannot delete pvc")
} else {
return errors.NewNotFound(schema.GroupResource{}, name)
Expand Down Expand Up @@ -73,7 +73,7 @@ func (*Pvc) Patch(ctx context.Context, name string, pt types.PatchType, data []b

// Update implements v1.PersistentVolumeClaimInterface
func (*Pvc) Update(ctx context.Context, persistentVolumeClaim *v1.PersistentVolumeClaim, opts metav1.UpdateOptions) (*v1.PersistentVolumeClaim, error) {
if persistentVolumeClaim.ObjectMeta.Name == "update_bad" {
if persistentVolumeClaim.ObjectMeta.Name == UpdateBad {
return nil, errors.NewBadRequest("mock error: cannot update pvc")
} else {
return nil, errors.NewNotFound(schema.GroupResource{}, persistentVolumeClaim.ObjectMeta.Name)
Expand Down
6 changes: 3 additions & 3 deletions mocks/k8s/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (*Pod) Watch(ctx context.Context, opts metav1.ListOptions) (watch.Interface

// Create implements v1.PodInterface
func (*Pod) Create(ctx context.Context, pod *v1.Pod, opts metav1.CreateOptions) (*v1.Pod, error) {
if pod.ObjectMeta.Name == "create_bad" {
if pod.ObjectMeta.Name == CreateBad {
return nil, fmt.Errorf("mock error: cannot create pod")
} else {
return nil, fmt.Errorf("mock error: unknown")
Expand All @@ -112,7 +112,7 @@ func (*Pod) Create(ctx context.Context, pod *v1.Pod, opts metav1.CreateOptions)

// Delete implements v1.PodInterface
func (*Pod) Delete(ctx context.Context, name string, opts metav1.DeleteOptions) error {
if name == "delete_bad" {
if name == DeleteBad {
return fmt.Errorf("mock error: cannot delete pod")
} else {
return errors.NewNotFound(schema.GroupResource{}, name)
Expand All @@ -121,7 +121,7 @@ func (*Pod) Delete(ctx context.Context, name string, opts metav1.DeleteOptions)

// Update implements v1.PodInterface
func (*Pod) Update(ctx context.Context, pod *v1.Pod, opts metav1.UpdateOptions) (*v1.Pod, error) {
if pod.ObjectMeta.Name == "update_bad" {
if pod.ObjectMeta.Name == UpdateBad {
return nil, errors.NewBadRequest("mock error: cannot update pod")
} else {
return nil, errors.NewNotFound(schema.GroupResource{}, pod.ObjectMeta.Name)
Expand Down
6 changes: 3 additions & 3 deletions mocks/k8s/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (*Secret) Apply(ctx context.Context, secret *configcorev1.SecretApplyConfig

// Create implements v1.SecretInterface
func (*Secret) Create(ctx context.Context, secret *v1.Secret, opts metav1.CreateOptions) (*v1.Secret, error) {
if secret.ObjectMeta.Name == "create_bad" {
if secret.ObjectMeta.Name == CreateBad {
return nil, fmt.Errorf("mock error: cannot create secret")
} else {
return nil, fmt.Errorf("mock error: unknown")
Expand All @@ -39,7 +39,7 @@ func (*Secret) Create(ctx context.Context, secret *v1.Secret, opts metav1.Create

// Delete implements v1.SecretInterface
func (*Secret) Delete(ctx context.Context, name string, opts metav1.DeleteOptions) error {
if name == "delete_bad" {
if name == DeleteBad {
return fmt.Errorf("mock error: cannot delete secret")
} else {
return errors.NewNotFound(schema.GroupResource{}, name)
Expand Down Expand Up @@ -68,7 +68,7 @@ func (*Secret) Patch(ctx context.Context, name string, pt types.PatchType, data

// Update implements v1.SecretInterface
func (*Secret) Update(ctx context.Context, secret *v1.Secret, opts metav1.UpdateOptions) (*v1.Secret, error) {
if secret.ObjectMeta.Name == "update_bad" {
if secret.ObjectMeta.Name == UpdateBad {
return nil, errors.NewBadRequest("mock error: cannot update secret")
} else {
return nil, errors.NewNotFound(schema.GroupResource{}, secret.ObjectMeta.Name)
Expand Down
10 changes: 5 additions & 5 deletions mocks/k8s/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (*Service) ApplyStatus(ctx context.Context, service *configcorev1.ServiceAp

// Create implements v1.ServiceInterface
func (*Service) Create(ctx context.Context, service *v1.Service, opts metav1.CreateOptions) (*v1.Service, error) {
if service.ObjectMeta.Name == "create_bad" {
if service.ObjectMeta.Name == CreateBad {
return nil, fmt.Errorf("mock error: cannot create service")
} else {
return nil, fmt.Errorf("mock error: unknown")
Expand All @@ -50,7 +50,7 @@ func (*Service) Create(ctx context.Context, service *v1.Service, opts metav1.Cre

// Delete implements v1.ServiceInterface
func (*Service) Delete(ctx context.Context, name string, opts metav1.DeleteOptions) error {
if name == "delete_bad" {
if name == DeleteBad {
return fmt.Errorf("mock error: cannot delete service")
} else {
return errors.NewNotFound(schema.GroupResource{}, name)
Expand All @@ -59,9 +59,9 @@ func (*Service) Delete(ctx context.Context, name string, opts metav1.DeleteOptio

// Get implements v1.ServiceInterface
func (*Service) Get(ctx context.Context, name string, opts metav1.GetOptions) (*v1.Service, error) {
if name == "create_bad" {
if name == CreateBad {
return nil, errors.NewNotFound(schema.GroupResource{}, name)
} else if name == "update_bad" {
} else if name == UpdateBad {
return &v1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Expand All @@ -83,7 +83,7 @@ func (*Service) Patch(ctx context.Context, name string, pt types.PatchType, data

// Update implements v1.ServiceInterface
func (*Service) Update(ctx context.Context, service *v1.Service, opts metav1.UpdateOptions) (*v1.Service, error) {
if service.ObjectMeta.Name == "update_bad" {
if service.ObjectMeta.Name == UpdateBad {
return nil, errors.NewBadRequest("mock error: cannot update service")
} else {
return nil, errors.NewNotFound(schema.GroupResource{}, service.ObjectMeta.Name)
Expand Down
6 changes: 3 additions & 3 deletions mocks/k8s/serviceaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (*ServiceAccount) Apply(ctx context.Context, serviceAccount *configcorev1.S

// Create implements v1.ServiceAccountInterface
func (*ServiceAccount) Create(ctx context.Context, serviceAccount *v1.ServiceAccount, opts metav1.CreateOptions) (*v1.ServiceAccount, error) {
if serviceAccount.ObjectMeta.Name == "create_bad" {
if serviceAccount.ObjectMeta.Name == CreateBad {
return nil, fmt.Errorf("mock error: cannot create service account")
} else {
return nil, fmt.Errorf("mock error: unknown")
Expand All @@ -45,7 +45,7 @@ func (*ServiceAccount) CreateToken(ctx context.Context, serviceAccountName strin

// Delete implements v1.ServiceAccountInterface
func (*ServiceAccount) Delete(ctx context.Context, name string, opts metav1.DeleteOptions) error {
if name == "delete_bad" {
if name == DeleteBad {
return fmt.Errorf("mock error: cannot delete service account")
} else {
return errors.NewNotFound(schema.GroupResource{}, name)
Expand Down Expand Up @@ -74,7 +74,7 @@ func (*ServiceAccount) Patch(ctx context.Context, name string, pt types.PatchTyp

// Update implements v1.ServiceAccountInterface
func (*ServiceAccount) Update(ctx context.Context, serviceAccount *v1.ServiceAccount, opts metav1.UpdateOptions) (*v1.ServiceAccount, error) {
if serviceAccount.ObjectMeta.Name == "update_bad" {
if serviceAccount.ObjectMeta.Name == UpdateBad {
return nil, errors.NewBadRequest("mock error: cannot update service account")
} else {
return nil, errors.NewNotFound(schema.GroupResource{}, serviceAccount.ObjectMeta.Name)
Expand Down
6 changes: 3 additions & 3 deletions mocks/k8s/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (*StatefulSet) ApplyStatus(ctx context.Context, statefulSet *configappsv1.S

// Create implements v1.StatefulSetInterface
func (*StatefulSet) Create(ctx context.Context, statefulSet *v1.StatefulSet, opts metav1.CreateOptions) (*v1.StatefulSet, error) {
if statefulSet.ObjectMeta.Name == "create_bad" {
if statefulSet.ObjectMeta.Name == CreateBad {
return nil, fmt.Errorf("mock error: cannot create statefulset")
} else {
return nil, fmt.Errorf("mock error: unknown")
Expand All @@ -55,7 +55,7 @@ func (*StatefulSet) Create(ctx context.Context, statefulSet *v1.StatefulSet, opt

// Delete implements v1.StatefulSetInterface
func (*StatefulSet) Delete(ctx context.Context, name string, opts metav1.DeleteOptions) error {
if name == "delete_bad" {
if name == DeleteBad {
return fmt.Errorf("mock error: cannot delete statefulset")
} else {
return errors.NewNotFound(schema.GroupResource{}, name)
Expand Down Expand Up @@ -96,7 +96,7 @@ func (*StatefulSet) Patch(ctx context.Context, name string, pt types.PatchType,

// Update implements v1.StatefulSetInterface
func (*StatefulSet) Update(ctx context.Context, statefulSet *v1.StatefulSet, opts metav1.UpdateOptions) (*v1.StatefulSet, error) {
if statefulSet.ObjectMeta.Name == "update_bad" {
if statefulSet.ObjectMeta.Name == UpdateBad {
return nil, errors.NewBadRequest("mock error: cannot update statefulset")
} else {
return nil, errors.NewNotFound(schema.GroupResource{}, statefulSet.ObjectMeta.Name)
Expand Down
3 changes: 2 additions & 1 deletion pkg/bee/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -283,7 +284,7 @@ func responseErrorHandler(r *http.Response) (err error) {

var e messageResponse
if strings.Contains(r.Header.Get("Content-Type"), "application/json") {
if err = json.NewDecoder(r.Body).Decode(&e); err != nil && err != io.EOF {
if err = json.NewDecoder(r.Body).Decode(&e); err != nil && !errors.Is(err, io.EOF) {
return err
}
}
Expand Down
9 changes: 5 additions & 4 deletions pkg/bee/api/pinning.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"context"
"fmt"
"net/http"

"github.com/ethersphere/bee/v2/pkg/swarm"
Expand Down Expand Up @@ -41,9 +42,9 @@ func (ps *PinningService) GetPinnedRootHash(ctx context.Context, ref swarm.Addre
}{}
err := ps.client.requestJSON(ctx, http.MethodGet, pinsPath(ref.String()), nil, &res)
if err != nil {
return swarm.ZeroAddress, nil
return swarm.ZeroAddress, fmt.Errorf("get pinned root hash: %w", err)
}
return res.Reference, err
return res.Reference, nil
}

// GetPins returns all references of pinned root hashes.
Expand All @@ -53,7 +54,7 @@ func (ps *PinningService) GetPins(ctx context.Context) ([]swarm.Address, error)
}{}
err := ps.client.requestJSON(ctx, http.MethodGet, pinsBasePath, nil, &res)
if err != nil {
return nil, nil
return nil, fmt.Errorf("get pins: %w", err)
}
return res.References, err
return res.References, nil
}
1 change: 0 additions & 1 deletion pkg/bee/chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ func (c *Chunk) ClosestNodeFromMap(nodes map[string]swarm.Address, skipNodes ...

next:
for i, a := range addresses {

for _, skip := range skipNodes {
if a.Equal(skip) {
continue next
Expand Down
6 changes: 0 additions & 6 deletions pkg/beekeeper/beekeeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,6 @@ func RunConcurrently(ctx context.Context, cluster orchestration.Cluster, action

waitDeleted := false
for j, u := range s {
j, u := j, u

if u.Actions.DeleteCount > 0 {
waitDeleted = true
}
Expand Down Expand Up @@ -265,7 +263,6 @@ func updateNodeGroupConcurrently(ctx context.Context, ng orchestration.NodeGroup

// add nodes
for _, n := range toAdd {
n := n
updateSemaphore <- struct{}{}
updateGroup.Go(func() error {
defer func() {
Expand All @@ -290,7 +287,6 @@ func updateNodeGroupConcurrently(ctx context.Context, ng orchestration.NodeGroup

// delete nodes
for _, n := range toDelete {
n := n
updateSemaphore <- struct{}{}
updateGroup.Go(func() error {
defer func() {
Expand All @@ -315,7 +311,6 @@ func updateNodeGroupConcurrently(ctx context.Context, ng orchestration.NodeGroup

// start nodes
for _, n := range toStart {
n := n
updateSemaphore <- struct{}{}
updateGroup.Go(func() error {
defer func() {
Expand All @@ -340,7 +335,6 @@ func updateNodeGroupConcurrently(ctx context.Context, ng orchestration.NodeGroup

// stop nodes
for _, n := range toStop {
n := n
updateSemaphore <- struct{}{}
updateGroup.Go(func() error {
defer func() {
Expand Down
2 changes: 1 addition & 1 deletion pkg/check/balances/balances.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (c *Check) Run(ctx context.Context, cluster orchestration.Cluster, opts int

// initial validation
if err := validateBalances(balances, c.logger); err != nil {
return fmt.Errorf("invalid initial balances: %v", err)
return fmt.Errorf("invalid initial balances: %w", err)
}

c.logger.Info("Balances are valid")
Expand Down
2 changes: 0 additions & 2 deletions pkg/check/networkavailability/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ iteration:
// upload
var chunks []swarm.Chunk
for _, n := range neighborhoods(int(storageRadius)) {

batch, err := uploadClient.GetOrCreateMutableBatch(ctx, o.PostageAmount, o.PostageDepth, "net-avail-check")
if err != nil {
c.logger.Errorf("create batch failed failed")
Expand Down Expand Up @@ -129,7 +128,6 @@ iteration:
c.logger.Infof("uploaded to %d neighborhoods, starting downloading", len(chunks))

for _, ch := range chunks {

t := time.Now()

c.metrics.DownloadAttempts.Inc()
Expand Down
1 change: 0 additions & 1 deletion pkg/check/pullsync/pullsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ func (c *Check) Run(ctx context.Context, cluster orchestration.Cluster, opts int

sortedNodes := cluster.NodeNames()
for i := 0; i < o.UploadNodeCount; i++ {

nodeName := sortedNodes[i]
client := clients[nodeName]

Expand Down
Loading

0 comments on commit 1bf2d6d

Please sign in to comment.