delete io/ioutil package. (#5955)

Signed-off-by: yanggang <gang.yang@daocloud.io>
This commit is contained in:
杨刚 (成都)
2023-03-16 09:25:58 +08:00
committed by GitHub
parent 1d8ca4f2ef
commit ad9c6e8dee
31 changed files with 92 additions and 83 deletions
+12 -2
View File
@@ -18,9 +18,9 @@ package controller
import (
"bytes"
"compress/gzip"
"context"
"fmt"
"io/ioutil"
"os"
"sync"
"time"
@@ -573,8 +573,18 @@ func (b *backupReconciler) validateAndGetSnapshotLocations(backup *velerov1api.B
// runBackup runs and uploads a validated backup. Any error returned from this function
// causes the backup to be Failed; if no error is returned, the backup's status's Errors
// field is checked to see if the backup was a partial failure.
func (b *backupReconciler) runBackup(backup *pkgbackup.Request) error {
b.logger.WithField(Backup, kubeutil.NamespaceAndName(backup)).Info("Setting up backup log")
logFile, err := os.CreateTemp("", "")
if err != nil {
return errors.Wrap(err, "error creating temp file for backup log")
}
gzippedLogFile := gzip.NewWriter(logFile)
// Assuming we successfully uploaded the log file, this will have already been closed below. It is safe to call
// close multiple times. If we get an error closing this, there's not really anything we can do about it.
defer gzippedLogFile.Close()
defer closeAndRemoveFile(logFile, b.logger.WithField(Backup, kubeutil.NamespaceAndName(backup)))
// Log the backup to both a backup log file and to stdout. This will help see what happened if the upload of the
// backup log failed for whatever reason.
@@ -586,7 +596,7 @@ func (b *backupReconciler) runBackup(backup *pkgbackup.Request) error {
defer backupLog.Dispose(b.logger.WithField(Backup, kubeutil.NamespaceAndName(backup)))
backupLog.Info("Setting up backup temp file")
backupFile, err := ioutil.TempFile("", "")
backupFile, err := os.CreateTemp("", "")
if err != nil {
return errors.Wrap(err, "error creating temp file for backup")
}
@@ -19,11 +19,11 @@ package controller
import (
"bytes"
"fmt"
"io"
"sort"
"time"
"context"
"io/ioutil"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
@@ -320,7 +320,7 @@ func TestBackupDeletionControllerReconcile(t *testing.T) {
td.controller.newPluginManager = func(logrus.FieldLogger) clientmgmt.Manager { return pluginManager }
td.backupStore.On("GetBackupVolumeSnapshots", input.Spec.BackupName).Return(snapshots, nil)
td.backupStore.On("GetBackupContents", input.Spec.BackupName).Return(ioutil.NopCloser(bytes.NewReader([]byte("hello world"))), nil)
td.backupStore.On("GetBackupContents", input.Spec.BackupName).Return(io.NopCloser(bytes.NewReader([]byte("hello world"))), nil)
td.backupStore.On("DeleteBackup", input.Spec.BackupName).Return(nil)
td.backupStore.On("DeleteRestore", "restore-1").Return(nil)
td.backupStore.On("DeleteRestore", "restore-2").Return(nil)
@@ -441,7 +441,7 @@ func TestBackupDeletionControllerReconcile(t *testing.T) {
td.controller.newPluginManager = func(logrus.FieldLogger) clientmgmt.Manager { return pluginManager }
td.backupStore.On("GetBackupVolumeSnapshots", dbr.Spec.BackupName).Return(snapshots, nil)
td.backupStore.On("GetBackupContents", dbr.Spec.BackupName).Return(ioutil.NopCloser(bytes.NewReader([]byte("hello world"))), nil)
td.backupStore.On("GetBackupContents", dbr.Spec.BackupName).Return(io.NopCloser(bytes.NewReader([]byte("hello world"))), nil)
td.backupStore.On("DeleteBackup", dbr.Spec.BackupName).Return(nil)
td.backupStore.On("DeleteRestore", "restore-1").Return(nil)
td.backupStore.On("DeleteRestore", "restore-2").Return(nil)
@@ -19,7 +19,6 @@ package controller
import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"time"
@@ -308,7 +307,7 @@ func (c *PodVolumeRestoreReconciler) processRestore(ctx context.Context, req *ve
// Write a done file with name=<restore-uid> into the just-created .velero dir
// within the volume. The velero init container on the pod is waiting
// for this file to exist in each restored volume before completing.
if err := ioutil.WriteFile(filepath.Join(volumePath, ".velero", string(restoreUID)), nil, 0644); err != nil { //nolint:gosec
if err := os.WriteFile(filepath.Join(volumePath, ".velero", string(restoreUID)), nil, 0644); err != nil { //nolint:gosec
return errors.Wrap(err, "error writing done file")
}
+1 -2
View File
@@ -23,7 +23,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"sort"
"time"
@@ -611,7 +610,7 @@ func downloadToTempFile(backupName string, backupStore persistence.BackupStore,
}
defer readCloser.Close()
file, err := ioutil.TempFile("", backupName)
file, err := os.CreateTemp("", backupName)
if err != nil {
return nil, errors.Wrap(err, "error creating Backup temp file")
}
+2 -2
View File
@@ -19,7 +19,7 @@ package controller
import (
"bytes"
"context"
"io/ioutil"
"io"
"testing"
"time"
@@ -444,7 +444,7 @@ func TestRestoreReconcile(t *testing.T) {
errors.Velero = append(errors.Velero, "error uploading log file to object storage: "+test.putRestoreLogErr.Error())
}
if test.expectedRestorerCall != nil {
backupStore.On("GetBackupContents", test.backup.Name).Return(ioutil.NopCloser(bytes.NewReader([]byte("hello world"))), nil)
backupStore.On("GetBackupContents", test.backup.Name).Return(io.NopCloser(bytes.NewReader([]byte("hello world"))), nil)
restorer.On("RestoreWithResolvers", mock.Anything, mock.Anything, mock.Anything, mock.Anything,
mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(warnings, errors)