Skip to content

Commit

Permalink
fix: golangci-lint error
Browse files Browse the repository at this point in the history
Signed-off-by: PoAn Yang <poan.yang@suse.com>
  • Loading branch information
FrankYang0529 authored and innobead committed Apr 25, 2024
1 parent 2c27d58 commit 2306e97
Show file tree
Hide file tree
Showing 73 changed files with 900 additions and 600 deletions.
2 changes: 1 addition & 1 deletion api/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -2156,7 +2156,7 @@ func toInstanceManagerResource(im *longhorn.InstanceManager) *InstanceManager {
DataEngine: string(im.Spec.DataEngine),
InstanceEngines: im.Status.InstanceEngines,
InstanceReplicas: im.Status.InstanceReplicas,
Instances: im.Status.Instances,
Instances: im.Status.Instances, // nolint: staticcheck
}
}

Expand Down
13 changes: 8 additions & 5 deletions api/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@ const (
writeWait = 10 * time.Second
)

var upgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool { return true },
}
var (
upgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool { return true },
}
randFunc *rand.Rand
)

func init() {
rand.Seed(time.Now().UnixNano())
randFunc = rand.New(rand.NewSource(time.Now().UnixNano()))
}

func NewStreamHandlerFunc(streamType string, watcher *controller.Watcher, listFunc func(ctx *api.ApiContext) (*client.GenericCollection, error)) func(w http.ResponseWriter, r *http.Request) error {
Expand All @@ -38,7 +41,7 @@ func NewStreamHandlerFunc(streamType string, watcher *controller.Watcher, listFu
return err
}
fields := logrus.Fields{
"id": strconv.Itoa(rand.Int()),
"id": strconv.Itoa(randFunc.Int()),
"type": streamType,
}
logrus.WithFields(fields).Debug("websocket: open")
Expand Down
29 changes: 0 additions & 29 deletions app/recurring_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,17 +478,6 @@ func (job *Job) eventCreate(eventType, eventReason, message string) error {
return nil
}

func (job *Job) deleteSnapshots(names []string, volume *longhornclient.Volume, volumeAPI longhornclient.VolumeOperations) error {
for _, name := range names {
_, err := volumeAPI.ActionSnapshotDelete(volume, &longhornclient.SnapshotInput{Name: name})
if err != nil {
return err
}
job.logger.WithField("volume", volume.Name).Infof("Deleted snapshot %v", name)
}
return nil
}

func (job *Job) purgeSnapshots(volume *longhornclient.Volume, volumeAPI longhornclient.VolumeOperations) error {
// Trigger snapshot purge of the volume
if _, err := volumeAPI.ActionSnapshotPurge(volume); err != nil {
Expand Down Expand Up @@ -806,24 +795,6 @@ func (job *Job) GetSettingAsBool(name types.SettingName) (bool, error) {
return value, nil
}

// waitForVolumeState timeout in second
func (job *Job) waitForVolumeState(state string, timeout int) (*longhornclient.Volume, error) {
volumeAPI := job.api.Volume
volumeName := job.volumeName

for i := 0; i < timeout; i++ {
volume, err := volumeAPI.ById(volumeName)
if err == nil {
if volume.State == state {
return volume, nil
}
}
time.Sleep(1 * time.Second)
}

return nil, fmt.Errorf("timeout waiting for volume %v to be in state %v", volumeName, state)
}

func filterSnapshotCRs(snapshotCRs []longhornclient.SnapshotCR, predicate func(snapshot longhornclient.SnapshotCR) bool) []longhornclient.SnapshotCR {
filtered := []longhornclient.SnapshotCR{}
for _, snapshotCR := range snapshotCRs {
Expand Down
5 changes: 4 additions & 1 deletion app/system_rollout.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func systemRollout(c *cli.Context) error {
doneCh := make(chan struct{})
informerFactories.Start(doneCh)

ctrl := controller.NewSystemRolloutController(
ctrl, err := controller.NewSystemRolloutController(
systemRestoreName,
logger,
currentNodeID,
Expand All @@ -104,5 +104,8 @@ func systemRollout(c *cli.Context) error {
kubeClient,
extensionsClient,
)
if err != nil {
return errors.Wrap(err, "failed to create system rollout controller")
}
return ctrl.Run()
}
5 changes: 4 additions & 1 deletion app/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func uninstall(c *cli.Context) error {
logger := logrus.StandardLogger()

doneCh := make(chan struct{})
ctrl := controller.NewUninstallController(
ctrl, err := controller.NewUninstallController(
logger,
namespace,
c.Bool(FlagForce),
Expand All @@ -92,6 +92,9 @@ func uninstall(c *cli.Context) error {
kubeClient,
extensionsClient,
)
if err != nil {
return errors.Wrap(err, "failed to create uninstall controller")
}

informerFactories.Start(doneCh)

Expand Down
3 changes: 1 addition & 2 deletions client/schemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import (

type Schemas struct {
Collection
Data []Schema `json:"data,omitempty"`
schemasByName map[string]*Schema
Data []Schema `json:"data,omitempty"`
}

func (s *Schema) CheckField(name string) (Field, bool) {
Expand Down
31 changes: 20 additions & 11 deletions controller/backing_image_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func NewBackingImageController(
ds *datastore.DataStore,
scheme *runtime.Scheme,
kubeClient clientset.Interface,
namespace string, controllerID, serviceAccount, backingImageManagerImage string) *BackingImageController {
namespace string, controllerID, serviceAccount, backingImageManagerImage string) (*BackingImageController, error) {

eventBroadcaster := record.NewBroadcaster()
eventBroadcaster.StartLogging(logrus.Infof)
Expand All @@ -73,35 +73,44 @@ func NewBackingImageController(
ds: ds,
}

ds.BackingImageInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
var err error
if _, err = ds.BackingImageInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: bic.enqueueBackingImage,
UpdateFunc: func(old, cur interface{}) { bic.enqueueBackingImage(cur) },
DeleteFunc: bic.enqueueBackingImage,
})
}); err != nil {
logrus.WithError(err).Fatal("Failed to register event handler for backing image")
}
bic.cacheSyncs = append(bic.cacheSyncs, ds.BackingImageInformer.HasSynced)

ds.BackingImageManagerInformer.AddEventHandlerWithResyncPeriod(cache.ResourceEventHandlerFuncs{
if _, err = ds.BackingImageManagerInformer.AddEventHandlerWithResyncPeriod(cache.ResourceEventHandlerFuncs{
AddFunc: bic.enqueueBackingImageForBackingImageManager,
UpdateFunc: func(old, cur interface{}) { bic.enqueueBackingImageForBackingImageManager(cur) },
DeleteFunc: bic.enqueueBackingImageForBackingImageManager,
}, 0)
}, 0); err != nil {
return nil, err
}
bic.cacheSyncs = append(bic.cacheSyncs, ds.BackingImageManagerInformer.HasSynced)

ds.BackingImageDataSourceInformer.AddEventHandlerWithResyncPeriod(cache.ResourceEventHandlerFuncs{
if _, err = ds.BackingImageDataSourceInformer.AddEventHandlerWithResyncPeriod(cache.ResourceEventHandlerFuncs{
AddFunc: bic.enqueueBackingImageForBackingImageDataSource,
UpdateFunc: func(old, cur interface{}) { bic.enqueueBackingImageForBackingImageDataSource(cur) },
DeleteFunc: bic.enqueueBackingImageForBackingImageDataSource,
}, 0)
}, 0); err != nil {
return nil, err
}
bic.cacheSyncs = append(bic.cacheSyncs, ds.BackingImageDataSourceInformer.HasSynced)

ds.ReplicaInformer.AddEventHandlerWithResyncPeriod(cache.ResourceEventHandlerFuncs{
if _, err = ds.ReplicaInformer.AddEventHandlerWithResyncPeriod(cache.ResourceEventHandlerFuncs{
AddFunc: bic.enqueueBackingImageForReplica,
UpdateFunc: func(old, cur interface{}) { bic.enqueueBackingImageForReplica(cur) },
DeleteFunc: bic.enqueueBackingImageForReplica,
}, 0)
}, 0); err != nil {
return nil, err
}
bic.cacheSyncs = append(bic.cacheSyncs, ds.ReplicaInformer.HasSynced)

return bic
return bic, nil
}

func (bic *BackingImageController) Run(workers int, stopCh <-chan struct{}) {
Expand Down Expand Up @@ -561,7 +570,7 @@ func (bic *BackingImageController) handleBackingImageManagers(bi *longhorn.Backi
if bim.DeletionTimestamp == nil && bim.Spec.Image == bic.bimImageName {
if uuidInManager, exists := bim.Spec.BackingImages[bi.Name]; !exists || uuidInManager != bi.Status.UUID {
bim.Spec.BackingImages[bi.Name] = bi.Status.UUID
if bim, err = bic.ds.UpdateBackingImageManager(bim); err != nil {
if _, err = bic.ds.UpdateBackingImageManager(bim); err != nil {
return err
}
}
Expand Down
35 changes: 23 additions & 12 deletions controller/backing_image_data_source_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func NewBackingImageDataSourceController(
kubeClient clientset.Interface,
namespace, controllerID, serviceAccount, imageManagerImage string,
proxyConnCounter util.Counter,
) *BackingImageDataSourceController {
) (*BackingImageDataSourceController, error) {

eventBroadcaster := record.NewBroadcaster()
eventBroadcaster.StartLogging(logrus.Infof)
Expand All @@ -110,42 +110,53 @@ func NewBackingImageDataSourceController(
proxyConnCounter: proxyConnCounter,
}

ds.BackingImageDataSourceInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
var err error
if _, err = ds.BackingImageDataSourceInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: c.enqueueBackingImageDataSource,
UpdateFunc: func(old, cur interface{}) { c.enqueueBackingImageDataSource(cur) },
DeleteFunc: c.enqueueBackingImageDataSource,
})
}); err != nil {
return nil, err
}
c.cacheSyncs = append(c.cacheSyncs, ds.BackingImageDataSourceInformer.HasSynced)

ds.BackingImageInformer.AddEventHandlerWithResyncPeriod(cache.ResourceEventHandlerFuncs{
if _, err = ds.BackingImageInformer.AddEventHandlerWithResyncPeriod(cache.ResourceEventHandlerFuncs{
AddFunc: c.enqueueForBackingImage,
UpdateFunc: func(old, cur interface{}) { c.enqueueForBackingImage(cur) },
DeleteFunc: c.enqueueForBackingImage,
}, 0)
}, 0); err != nil {
return nil, err
}
c.cacheSyncs = append(c.cacheSyncs, ds.BackingImageInformer.HasSynced)

ds.VolumeInformer.AddEventHandlerWithResyncPeriod(cache.ResourceEventHandlerFuncs{
if _, err = ds.VolumeInformer.AddEventHandlerWithResyncPeriod(cache.ResourceEventHandlerFuncs{
UpdateFunc: func(old, cur interface{}) { c.enqueueForVolume(cur) },
}, 0)
}, 0); err != nil {
return nil, err
}
c.cacheSyncs = append(c.cacheSyncs, ds.VolumeInformer.HasSynced)

ds.NodeInformer.AddEventHandlerWithResyncPeriod(cache.ResourceEventHandlerFuncs{
if _, err = ds.NodeInformer.AddEventHandlerWithResyncPeriod(cache.ResourceEventHandlerFuncs{
UpdateFunc: func(oldObj, cur interface{}) { c.enqueueForLonghornNode(cur) },
DeleteFunc: c.enqueueForLonghornNode,
}, 0)
}, 0); err != nil {
return nil, err
}
c.cacheSyncs = append(c.cacheSyncs, ds.NodeInformer.HasSynced)

ds.PodInformer.AddEventHandlerWithResyncPeriod(cache.FilteringResourceEventHandler{
if _, err = ds.PodInformer.AddEventHandlerWithResyncPeriod(cache.FilteringResourceEventHandler{
FilterFunc: isBackingImageDataSourcePod,
Handler: cache.ResourceEventHandlerFuncs{
AddFunc: c.enqueueForBackingImageDataSourcePod,
UpdateFunc: func(old, cur interface{}) { c.enqueueForBackingImageDataSourcePod(cur) },
DeleteFunc: c.enqueueForBackingImageDataSourcePod,
},
}, 0)
}, 0); err != nil {
return nil, err
}
c.cacheSyncs = append(c.cacheSyncs, ds.PodInformer.HasSynced)

return c
return c, nil
}

func (c *BackingImageDataSourceController) Run(workers int, stopCh <-chan struct{}) {
Expand Down
29 changes: 19 additions & 10 deletions controller/backing_image_manager_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func NewBackingImageManagerController(
ds *datastore.DataStore,
scheme *runtime.Scheme,
kubeClient clientset.Interface,
namespace, controllerID, serviceAccount, backingImageManagerImage string) *BackingImageManagerController {
namespace, controllerID, serviceAccount, backingImageManagerImage string) (*BackingImageManagerController, error) {

eventBroadcaster := record.NewBroadcaster()
eventBroadcaster.StartLogging(logrus.Infof)
Expand All @@ -129,37 +129,46 @@ func NewBackingImageManagerController(
versionUpdater: updateBackingImageManagerVersion,
}

ds.BackingImageManagerInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
var err error
if _, err = ds.BackingImageManagerInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: c.enqueueBackingImageManager,
UpdateFunc: func(old, cur interface{}) { c.enqueueBackingImageManager(cur) },
DeleteFunc: c.enqueueBackingImageManager,
})
}); err != nil {
return nil, err
}
c.cacheSyncs = append(c.cacheSyncs, ds.BackingImageManagerInformer.HasSynced)

ds.BackingImageInformer.AddEventHandlerWithResyncPeriod(cache.ResourceEventHandlerFuncs{
if _, err = ds.BackingImageInformer.AddEventHandlerWithResyncPeriod(cache.ResourceEventHandlerFuncs{
AddFunc: c.enqueueForBackingImage,
UpdateFunc: func(old, cur interface{}) { c.enqueueForBackingImage(cur) },
DeleteFunc: c.enqueueForBackingImage,
}, 0)
}, 0); err != nil {
return nil, err
}
c.cacheSyncs = append(c.cacheSyncs, ds.BackingImageInformer.HasSynced)

ds.NodeInformer.AddEventHandlerWithResyncPeriod(cache.ResourceEventHandlerFuncs{
if _, err = ds.NodeInformer.AddEventHandlerWithResyncPeriod(cache.ResourceEventHandlerFuncs{
UpdateFunc: func(oldObj, cur interface{}) { c.enqueueForLonghornNode(cur) },
DeleteFunc: c.enqueueForLonghornNode,
}, 0)
}, 0); err != nil {
return nil, err
}
c.cacheSyncs = append(c.cacheSyncs, ds.NodeInformer.HasSynced)

ds.PodInformer.AddEventHandlerWithResyncPeriod(cache.FilteringResourceEventHandler{
if _, err = ds.PodInformer.AddEventHandlerWithResyncPeriod(cache.FilteringResourceEventHandler{
FilterFunc: isBackingImageManagerPod,
Handler: cache.ResourceEventHandlerFuncs{
AddFunc: c.enqueueForBackingImageManagerPod,
UpdateFunc: func(old, cur interface{}) { c.enqueueForBackingImageManagerPod(cur) },
DeleteFunc: c.enqueueForBackingImageManagerPod,
},
}, 0)
}, 0); err != nil {
return nil, err
}
c.cacheSyncs = append(c.cacheSyncs, ds.PodInformer.HasSynced)

return c
return c, nil
}

func (c *BackingImageManagerController) Run(workers int, stopCh <-chan struct{}) {
Expand Down
11 changes: 7 additions & 4 deletions controller/backup_backing_image_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func NewBackupBackingImageController(
controllerID string,
namespace string,
proxyConnCounter util.Counter,
) *BackupBackingImageController {
) (*BackupBackingImageController, error) {
eventBroadcaster := record.NewBroadcaster()
eventBroadcaster.StartLogging(logrus.Infof)
// TODO: remove the wrapper when every clients have moved to use the clientset.
Expand All @@ -88,14 +88,17 @@ func NewBackupBackingImageController(
proxyConnCounter: proxyConnCounter,
}

ds.BackupBackingImageInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{ //nolint:errcheck
var err error
if _, err = ds.BackupBackingImageInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{ //nolint:errcheck
AddFunc: bc.enqueueBackupBackingImage,
UpdateFunc: func(old, cur interface{}) { bc.enqueueBackupBackingImage(cur) },
DeleteFunc: bc.enqueueBackupBackingImage,
})
}); err != nil {
return nil, err
}
bc.cacheSyncs = append(bc.cacheSyncs, ds.BackupBackingImageInformer.HasSynced)

return bc
return bc, nil
}

func (bc *BackupBackingImageController) enqueueBackupBackingImage(obj interface{}) {
Expand Down
Loading

0 comments on commit 2306e97

Please sign in to comment.