Skip to content

Commit 273f8b1

Browse files
authored
Merge pull request #409 from fluxcd/dep-ioutil
2 parents be5d10e + c4d7e46 commit 273f8b1

19 files changed

+68
-84
lines changed

controllers/bucket_controller.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"context"
2121
"crypto/sha1"
2222
"fmt"
23-
"io/ioutil"
2423
"os"
2524
"path/filepath"
2625
"strings"
@@ -184,7 +183,7 @@ func (r *BucketReconciler) reconcile(ctx context.Context, bucket sourcev1.Bucket
184183
}
185184

186185
// create tmp dir
187-
tempDir, err := ioutil.TempDir("", bucket.Name)
186+
tempDir, err := os.MkdirTemp("", bucket.Name)
188187
if err != nil {
189188
err = fmt.Errorf("tmp dir error: %w", err)
190189
return sourcev1.BucketNotReady(bucket, sourcev1.StorageOperationFailedReason, err.Error()), err
@@ -368,7 +367,7 @@ func (r *BucketReconciler) checksum(root string) (string, error) {
368367
if !info.Mode().IsRegular() {
369368
return nil
370369
}
371-
data, err := ioutil.ReadFile(path)
370+
data, err := os.ReadFile(path)
372371
if err != nil {
373372
return err
374373
}

controllers/bucket_controller_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ limitations under the License.
1717
package controllers
1818

1919
import (
20-
"io/ioutil"
2120
"os"
2221
"path/filepath"
2322
"testing"
@@ -51,7 +50,7 @@ func TestBucketReconciler_checksum(t *testing.T) {
5150
}
5251
for _, tt := range tests {
5352
t.Run(tt.name, func(t *testing.T) {
54-
root, err := ioutil.TempDir("", "bucket-checksum-")
53+
root, err := os.MkdirTemp("", "bucket-checksum-")
5554
if err != nil {
5655
t.Fatal(err)
5756
}
@@ -76,7 +75,7 @@ func mockFile(root, path, content string) error {
7675
if err := os.MkdirAll(filepath.Dir(filePath), os.ModePerm); err != nil {
7776
panic(err)
7877
}
79-
if err := ioutil.WriteFile(filePath, []byte(content), 0644); err != nil {
78+
if err := os.WriteFile(filePath, []byte(content), 0644); err != nil {
8079
panic(err)
8180
}
8281
return nil

controllers/gitrepository_controller.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package controllers
1919
import (
2020
"context"
2121
"fmt"
22-
"io/ioutil"
2322
"os"
2423
"path/filepath"
2524
"strings"
@@ -223,7 +222,7 @@ func (r *GitRepositoryReconciler) checkDependencies(repository sourcev1.GitRepos
223222

224223
func (r *GitRepositoryReconciler) reconcile(ctx context.Context, repository sourcev1.GitRepository) (sourcev1.GitRepository, error) {
225224
// create tmp dir for the Git clone
226-
tmpGit, err := ioutil.TempDir("", repository.Name)
225+
tmpGit, err := os.MkdirTemp("", repository.Name)
227226
if err != nil {
228227
err = fmt.Errorf("tmp dir error: %w", err)
229228
return sourcev1.GitRepositoryNotReady(repository, sourcev1.StorageOperationFailedReason, err.Error()), err

controllers/gitrepository_controller_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"context"
2121
"crypto/tls"
2222
"fmt"
23-
"io/ioutil"
2423
"net/http"
2524
"net/url"
2625
"os"
@@ -459,7 +458,7 @@ var _ = Describe("GitRepositoryReconciler", func() {
459458

460459
// this one is linked to a real directory, so that I can
461460
// exec `git submodule add` later
462-
tmp, err := ioutil.TempDir("", "flux-test")
461+
tmp, err := os.MkdirTemp("", "flux-test")
463462
Expect(err).NotTo(HaveOccurred())
464463
defer os.RemoveAll(tmp)
465464

@@ -697,7 +696,7 @@ var _ = Describe("GitRepositoryReconciler", func() {
697696
res, err := http.Get(got.Status.URL)
698697
Expect(err).NotTo(HaveOccurred())
699698
Expect(res.StatusCode).To(Equal(http.StatusOK))
700-
tmp, err := ioutil.TempDir("", "flux-test")
699+
tmp, err := os.MkdirTemp("", "flux-test")
701700
Expect(err).NotTo(HaveOccurred())
702701
defer os.RemoveAll(tmp)
703702
_, err = untar.Untar(res.Body, filepath.Join(tmp, "tar"))
@@ -743,7 +742,7 @@ var _ = Describe("GitRepositoryReconciler", func() {
743742
res, err = http.Get(got.Status.URL)
744743
Expect(err).NotTo(HaveOccurred())
745744
Expect(res.StatusCode).To(Equal(http.StatusOK))
746-
tmp, err = ioutil.TempDir("", "flux-test")
745+
tmp, err = os.MkdirTemp("", "flux-test")
747746
Expect(err).NotTo(HaveOccurred())
748747
defer os.RemoveAll(tmp)
749748
_, err = untar.Untar(res.Body, filepath.Join(tmp, "tar"))

controllers/helmchart_controller.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"context"
2121
"fmt"
2222
"io"
23-
"io/ioutil"
2423
"net/url"
2524
"os"
2625
"path/filepath"
@@ -331,7 +330,7 @@ func (r *HelmChartReconciler) reconcileFromHelmRepository(ctx context.Context,
331330
if err != nil {
332331
return sourcev1.HelmChartNotReady(chart, sourcev1.StorageOperationFailedReason, err.Error()), err
333332
}
334-
b, err := ioutil.ReadAll(indexFile)
333+
b, err := io.ReadAll(indexFile)
335334
if err != nil {
336335
return sourcev1.HelmChartNotReady(chart, sourcev1.ChartPullFailedReason, err.Error()), err
337336
}
@@ -376,7 +375,7 @@ func (r *HelmChartReconciler) reconcileFromHelmRepository(ctx context.Context,
376375
if err != nil {
377376
return sourcev1.HelmChartNotReady(chart, sourcev1.ChartPullFailedReason, err.Error()), err
378377
}
379-
tmpFile, err := ioutil.TempFile("", fmt.Sprintf("%s-%s-", chart.Namespace, chart.Name))
378+
tmpFile, err := os.CreateTemp("", fmt.Sprintf("%s-%s-", chart.Namespace, chart.Name))
380379
if err != nil {
381380
return sourcev1.HelmChartNotReady(chart, sourcev1.ChartPullFailedReason, err.Error()), err
382381
}
@@ -448,7 +447,7 @@ func (r *HelmChartReconciler) reconcileFromHelmRepository(ctx context.Context,
448447
}
449448

450449
// Create temporary working directory
451-
tmpDir, err := ioutil.TempDir("", fmt.Sprintf("%s-%s-", chart.Namespace, chart.Name))
450+
tmpDir, err := os.MkdirTemp("", fmt.Sprintf("%s-%s-", chart.Namespace, chart.Name))
452451
if err != nil {
453452
err = fmt.Errorf("tmp dir error: %w", err)
454453
return sourcev1.HelmChartNotReady(chart, sourcev1.StorageOperationFailedReason, err.Error()), err
@@ -491,7 +490,7 @@ func (r *HelmChartReconciler) reconcileFromHelmRepository(ctx context.Context,
491490
func (r *HelmChartReconciler) reconcileFromTarballArtifact(ctx context.Context,
492491
artifact sourcev1.Artifact, chart sourcev1.HelmChart, force bool) (sourcev1.HelmChart, error) {
493492
// Create temporary working directory
494-
tmpDir, err := ioutil.TempDir("", fmt.Sprintf("%s-%s-", chart.Namespace, chart.Name))
493+
tmpDir, err := os.MkdirTemp("", fmt.Sprintf("%s-%s-", chart.Namespace, chart.Name))
495494
if err != nil {
496495
err = fmt.Errorf("tmp dir error: %w", err)
497496
return sourcev1.HelmChartNotReady(chart, sourcev1.StorageOperationFailedReason, err.Error()), err
@@ -554,7 +553,7 @@ func (r *HelmChartReconciler) reconcileFromTarballArtifact(ctx context.Context,
554553
return sourcev1.HelmChartNotReady(chart, sourcev1.StorageOperationFailedReason, err.Error()), err
555554
}
556555

557-
valuesData, err := ioutil.ReadFile(srcPath)
556+
valuesData, err := os.ReadFile(srcPath)
558557
if err != nil {
559558
err = fmt.Errorf("failed to read from values file '%s': %w", v, err)
560559
return sourcev1.HelmChartNotReady(chart, sourcev1.StorageOperationFailedReason, err.Error()), err
@@ -659,7 +658,7 @@ func (r *HelmChartReconciler) reconcileFromTarballArtifact(ctx context.Context,
659658
if err != nil {
660659
return sourcev1.HelmChartNotReady(chart, sourcev1.StorageOperationFailedReason, err.Error()), err
661660
}
662-
b, err := ioutil.ReadAll(indexFile)
661+
b, err := io.ReadAll(indexFile)
663662
if err != nil {
664663
return sourcev1.HelmChartNotReady(chart, sourcev1.ChartPullFailedReason, err.Error()), err
665664
}

controllers/helmchart_controller_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package controllers
1919
import (
2020
"context"
2121
"fmt"
22-
"io/ioutil"
2322
"net/http"
2423
"net/url"
2524
"os"
@@ -612,7 +611,7 @@ var _ = Describe("HelmChartReconciler", func() {
612611
return nil
613612
}
614613

615-
b, err := ioutil.ReadFile(p)
614+
b, err := os.ReadFile(p)
616615
if err != nil {
617616
return err
618617
}
@@ -872,14 +871,14 @@ var _ = Describe("HelmChartReconciler", func() {
872871
helmChart, err := loader.LoadDir(chartDir)
873872
Expect(err).NotTo(HaveOccurred())
874873

875-
chartPackagePath, err := ioutil.TempDir("", fmt.Sprintf("chartpackage-%s-%s", helmChart.Name(), randStringRunes(5)))
874+
chartPackagePath, err := os.MkdirTemp("", fmt.Sprintf("chartpackage-%s-%s", helmChart.Name(), randStringRunes(5)))
876875
Expect(err).NotTo(HaveOccurred())
877876
defer os.RemoveAll(chartPackagePath)
878877

879878
pkg, err := chartutil.Save(helmChart, chartPackagePath)
880879
Expect(err).NotTo(HaveOccurred())
881880

882-
b, err := ioutil.ReadFile(pkg)
881+
b, err := os.ReadFile(pkg)
883882
Expect(err).NotTo(HaveOccurred())
884883

885884
tgz := filepath.Base(pkg)
@@ -1078,7 +1077,7 @@ var _ = Describe("HelmChartReconciler", func() {
10781077
return nil
10791078
}
10801079

1081-
b, err := ioutil.ReadFile(p)
1080+
b, err := os.ReadFile(p)
10821081
if err != nil {
10831082
return err
10841083
}

controllers/storage.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"fmt"
2424
"hash"
2525
"io"
26-
"io/ioutil"
2726
"net/url"
2827
"os"
2928
"path/filepath"
@@ -174,7 +173,7 @@ func (s *Storage) Archive(artifact *sourcev1.Artifact, dir string, filter Archiv
174173
}
175174

176175
localPath := s.LocalPath(*artifact)
177-
tf, err := ioutil.TempFile(filepath.Split(localPath))
176+
tf, err := os.CreateTemp(filepath.Split(localPath))
178177
if err != nil {
179178
return err
180179
}
@@ -272,7 +271,7 @@ func (s *Storage) Archive(artifact *sourcev1.Artifact, dir string, filter Archiv
272271
// If successful, it sets the checksum and last update time on the artifact.
273272
func (s *Storage) AtomicWriteFile(artifact *sourcev1.Artifact, reader io.Reader, mode os.FileMode) (err error) {
274273
localPath := s.LocalPath(*artifact)
275-
tf, err := ioutil.TempFile(filepath.Split(localPath))
274+
tf, err := os.CreateTemp(filepath.Split(localPath))
276275
if err != nil {
277276
return err
278277
}
@@ -311,7 +310,7 @@ func (s *Storage) AtomicWriteFile(artifact *sourcev1.Artifact, reader io.Reader,
311310
// If successful, it sets the checksum and last update time on the artifact.
312311
func (s *Storage) Copy(artifact *sourcev1.Artifact, reader io.Reader) (err error) {
313312
localPath := s.LocalPath(*artifact)
314-
tf, err := ioutil.TempFile(filepath.Split(localPath))
313+
tf, err := os.CreateTemp(filepath.Split(localPath))
315314
if err != nil {
316315
return err
317316
}
@@ -357,7 +356,7 @@ func (s *Storage) CopyFromPath(artifact *sourcev1.Artifact, path string) (err er
357356
// CopyToPath copies the contents of the given artifact to the path.
358357
func (s *Storage) CopyToPath(artifact *sourcev1.Artifact, subPath, toPath string) error {
359358
// create a tmp directory to store artifact
360-
tmp, err := ioutil.TempDir("", "flux-include")
359+
tmp, err := os.MkdirTemp("", "flux-include")
361360
if err != nil {
362361
return err
363362
}

controllers/storage_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"compress/gzip"
2222
"fmt"
2323
"io"
24-
"io/ioutil"
2524
"os"
2625
"path"
2726
"path/filepath"
@@ -34,7 +33,7 @@ import (
3433
)
3534

3635
func createStoragePath() (string, error) {
37-
return ioutil.TempDir("", "")
36+
return os.MkdirTemp("", "")
3837
}
3938

4039
func cleanupStoragePath(dir string) func() {
@@ -52,7 +51,7 @@ func TestStorageConstructor(t *testing.T) {
5251
t.Fatal("nonexistent path was allowable in storage constructor")
5352
}
5453

55-
f, err := ioutil.TempFile(dir, "")
54+
f, err := os.CreateTemp(dir, "")
5655
if err != nil {
5756
t.Fatalf("while creating temporary file: %v", err)
5857
}
@@ -124,7 +123,7 @@ func TestStorage_Archive(t *testing.T) {
124123
os.RemoveAll(dir)
125124
}
126125
}()
127-
dir, err = ioutil.TempDir("", "archive-test-files-")
126+
dir, err = os.MkdirTemp("", "archive-test-files-")
128127
if err != nil {
129128
return
130129
}
@@ -244,7 +243,7 @@ func TestStorage_Archive(t *testing.T) {
244243

245244
func TestStorageRemoveAllButCurrent(t *testing.T) {
246245
t.Run("bad directory in archive", func(t *testing.T) {
247-
dir, err := ioutil.TempDir("", "")
246+
dir, err := os.MkdirTemp("", "")
248247
if err != nil {
249248
t.Fatal(err)
250249
}

controllers/suite_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ limitations under the License.
1717
package controllers
1818

1919
import (
20-
"io/ioutil"
2120
"math/rand"
2221
"net/http"
2322
"os"
@@ -97,7 +96,7 @@ var _ = BeforeSuite(func(done Done) {
9796

9897
Expect(loadExampleKeys()).To(Succeed())
9998

100-
tmpStoragePath, err := ioutil.TempDir("", "source-controller-storage-")
99+
tmpStoragePath, err := os.MkdirTemp("", "source-controller-storage-")
101100
Expect(err).NotTo(HaveOccurred(), "failed to create tmp storage dir")
102101

103102
storage, err = NewStorage(tmpStoragePath, "localhost:5050", time.Second*30)
@@ -167,15 +166,15 @@ func init() {
167166
}
168167

169168
func loadExampleKeys() (err error) {
170-
examplePublicKey, err = ioutil.ReadFile("testdata/certs/server.pem")
169+
examplePublicKey, err = os.ReadFile("testdata/certs/server.pem")
171170
if err != nil {
172171
return err
173172
}
174-
examplePrivateKey, err = ioutil.ReadFile("testdata/certs/server-key.pem")
173+
examplePrivateKey, err = os.ReadFile("testdata/certs/server-key.pem")
175174
if err != nil {
176175
return err
177176
}
178-
exampleCA, err = ioutil.ReadFile("testdata/certs/ca.pem")
177+
exampleCA, err = os.ReadFile("testdata/certs/ca.pem")
179178
return err
180179
}
181180

internal/fs/fs.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"errors"
99
"fmt"
1010
"io"
11-
"io/ioutil"
1211
"os"
1312
"path/filepath"
1413
"runtime"
@@ -92,7 +91,7 @@ func CopyDir(src, dst string) error {
9291
return fmt.Errorf("cannot mkdir %s: %w", dst, err)
9392
}
9493

95-
entries, err := ioutil.ReadDir(src)
94+
entries, err := os.ReadDir(src)
9695
if err != nil {
9796
return fmt.Errorf("cannot read directory %s: %w", dst, err)
9897
}

0 commit comments

Comments
 (0)