Skip to content

Commit 1564449

Browse files
authored
Replace os filesystem operations with pkg/filesystem (#22)
1 parent 647365b commit 1564449

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+1866
-511
lines changed

cmd/executor/cmd/root.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"github.com/GoogleContainerTools/kaniko/pkg/config"
3232
"github.com/GoogleContainerTools/kaniko/pkg/constants"
3333
"github.com/GoogleContainerTools/kaniko/pkg/executor"
34+
"github.com/GoogleContainerTools/kaniko/pkg/filesystem"
3435
"github.com/GoogleContainerTools/kaniko/pkg/logging"
3536
"github.com/GoogleContainerTools/kaniko/pkg/timing"
3637
"github.com/GoogleContainerTools/kaniko/pkg/util"
@@ -210,7 +211,7 @@ var RootCmd = &cobra.Command{
210211
}
211212
logrus.Infof("Benchmark file written at %s", benchmarkFile)
212213
} else {
213-
f, err := os.Create(benchmarkFile)
214+
f, err := filesystem.FS.Create(benchmarkFile)
214215
if err != nil {
215216
logrus.Warnf("Unable to create benchmarking file %s: %s", benchmarkFile, err)
216217
return
@@ -305,7 +306,7 @@ func checkKanikoDir(dir string) error {
305306
return err
306307
}
307308

308-
if err := os.RemoveAll(constants.DefaultKanikoPath); err != nil {
309+
if err := filesystem.FS.RemoveAll(constants.DefaultKanikoPath); err != nil {
309310
return err
310311
}
311312
// After remove DefaultKankoPath, the DOKCER_CONFIG env will point to a non-exist dir, so we should update DOCKER_CONFIG env to new dir
@@ -437,7 +438,7 @@ func resolveSourceContext() error {
437438
}
438439
if ctxSubPath != "" {
439440
opts.SrcContext = filepath.Join(opts.SrcContext, ctxSubPath)
440-
if _, err := os.Stat(opts.SrcContext); os.IsNotExist(err) {
441+
if _, err := filesystem.FS.Stat(opts.SrcContext); os.IsNotExist(err) {
441442
return err
442443
}
443444
}

cmd/warmer/cmd/root.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525

2626
"github.com/GoogleContainerTools/kaniko/pkg/cache"
2727
"github.com/GoogleContainerTools/kaniko/pkg/config"
28+
"github.com/GoogleContainerTools/kaniko/pkg/filesystem"
2829
"github.com/GoogleContainerTools/kaniko/pkg/logging"
2930
"github.com/GoogleContainerTools/kaniko/pkg/util"
3031
"github.com/containerd/containerd/platforms"
@@ -70,16 +71,15 @@ var RootCmd = &cobra.Command{
7071
return nil
7172
},
7273
Run: func(cmd *cobra.Command, args []string) {
73-
if _, err := os.Stat(opts.CacheDir); os.IsNotExist(err) {
74-
err = os.MkdirAll(opts.CacheDir, 0755)
74+
if _, err := filesystem.FS.Stat(opts.CacheDir); os.IsNotExist(err) {
75+
err = filesystem.MkdirAll(opts.CacheDir, 0o755)
7576
if err != nil {
7677
exit(errors.Wrap(err, "Failed to create cache directory"))
7778
}
7879
}
7980
if err := cache.WarmCache(opts); err != nil {
8081
exit(errors.Wrap(err, "Failed warming cache"))
8182
}
82-
8383
},
8484
}
8585

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ require (
3737
require (
3838
github.com/GoogleCloudPlatform/docker-credential-gcr/v2 v2.1.22
3939
github.com/containerd/containerd v1.7.19
40+
github.com/twpayne/go-vfs/v5 v5.0.4
4041
)
4142

4243
require github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,8 @@ github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8
483483
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
484484
github.com/toqueteos/webbrowser v1.2.0 h1:tVP/gpK69Fx+qMJKsLE7TD8LuGWPnEV71wBN9rrstGQ=
485485
github.com/toqueteos/webbrowser v1.2.0/go.mod h1:XWoZq4cyp9WeUeak7w7LXRUQf1F1ATJMir8RTqb4ayM=
486+
github.com/twpayne/go-vfs/v5 v5.0.4 h1:/ne3h+rW7f5YOyOFguz+3ztfUwzOLR0Vts3y0mMAitg=
487+
github.com/twpayne/go-vfs/v5 v5.0.4/go.mod h1:zTPFJUbgsEMFNSWnWQlLq9wh4AN83edZzx3VXbxrS1w=
486488
github.com/vbatts/tar-split v0.11.5 h1:3bHCTIheBm1qFTcgh9oPu+nNBtX+XJIupG/vacinCts=
487489
github.com/vbatts/tar-split v0.11.5/go.mod h1:yZbwRsSeGjusneWgA781EKej9HF8vme8okylkAeNKLk=
488490
github.com/xanzy/ssh-agent v0.3.3 h1:+/15pJfg/RsTxqYcX6fHqOXZwwMP+2VyYWJeWM2qQFM=

integration/benchmark_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import (
2727
"sync"
2828
"testing"
2929
"time"
30+
31+
"github.com/GoogleContainerTools/kaniko/pkg/filesystem"
3032
)
3133

3234
type result struct {
@@ -68,7 +70,7 @@ func TestSnapshotBenchmark(t *testing.T) {
6870
r := newResult(t, filepath.Join(benchmarkDir, dockerfile))
6971
timeMap.Store(num, r)
7072
wg.Done()
71-
defer os.Remove(benchmarkDir)
73+
defer filesystem.FS.Remove(benchmarkDir)
7274
}(num, &err)
7375
if err != nil {
7476
t.Errorf("could not run benchmark results for num %d due to %s", num, err)
@@ -84,12 +86,11 @@ func TestSnapshotBenchmark(t *testing.T) {
8486
t.Logf("%d,%f,%f,%f", d, v.totalBuildTime, v.walkingFiles, v.resolvingFiles)
8587
return true
8688
})
87-
8889
}
8990

9091
func newResult(t *testing.T, f string) result {
9192
var current map[string]time.Duration
92-
jsonFile, err := os.Open(f)
93+
jsonFile, err := filesystem.FS.Open(f)
9394
defer jsonFile.Close()
9495
if err != nil {
9596
t.Errorf("could not read benchmark file %s", f)
@@ -141,7 +142,7 @@ func TestSnapshotBenchmarkGcloud(t *testing.T) {
141142
r := newResult(t, filepath.Join(dir, "results"))
142143
t.Log(fmt.Sprintf("%d,%f,%f,%f, %f", num, r.totalBuildTime, r.walkingFiles, r.resolvingFiles, r.hashingFiles))
143144
wg.Done()
144-
defer os.Remove(dir)
145+
defer filesystem.FS.Remove(dir)
145146
defer os.Chdir(cwd)
146147
}(num)
147148
})
@@ -160,7 +161,7 @@ func runInGcloud(dir string, num int) (string, error) {
160161
}
161162

162163
// grab gcs and to temp dir and return
163-
tmpDir, err := os.MkdirTemp("", fmt.Sprintf("%d", num))
164+
tmpDir, err := filesystem.MkdirTemp("", fmt.Sprintf("%d", num))
164165
if err != nil {
165166
return "", err
166167
}

integration/images.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"time"
3232

3333
"cloud.google.com/go/storage"
34+
"github.com/GoogleContainerTools/kaniko/pkg/filesystem"
3435
"github.com/GoogleContainerTools/kaniko/pkg/timing"
3536
"github.com/GoogleContainerTools/kaniko/pkg/util"
3637
"github.com/GoogleContainerTools/kaniko/pkg/util/bucket"
@@ -379,7 +380,7 @@ func (d *DockerFileBuilder) buildCachedImage(config *integrationTestConfig, cach
379380

380381
benchmarkEnv := "BENCHMARK_FILE=false"
381382
if b, err := strconv.ParseBool(os.Getenv("BENCHMARK")); err == nil && b {
382-
os.Mkdir("benchmarks", 0o755)
383+
filesystem.FS.Mkdir("benchmarks", 0o755)
383384
benchmarkEnv = "BENCHMARK_FILE=/workspace/benchmarks/" + dockerfile
384385
}
385386
kanikoImage := GetVersionedKanikoImage(imageRepo, dockerfile, version)
@@ -470,7 +471,7 @@ func buildKanikoImage(
470471
shdUpload bool,
471472
) (string, error) {
472473
benchmarkEnv := "BENCHMARK_FILE=false"
473-
benchmarkDir, err := os.MkdirTemp("", "")
474+
benchmarkDir, err := filesystem.MkdirTemp("", "")
474475
if err != nil {
475476
return "", err
476477
}
@@ -480,7 +481,7 @@ func buildKanikoImage(
480481
benchmarkFile := path.Join(benchmarkDir, dockerfile)
481482
fileName := fmt.Sprintf("run_%s_%s", time.Now().Format("2006-01-02-15:04"), dockerfile)
482483
dst := path.Join("benchmarks", fileName)
483-
file, err := os.Open(benchmarkFile)
484+
file, err := filesystem.FS.Open(benchmarkFile)
484485
if err != nil {
485486
return "", err
486487
}

integration/integration_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import (
4040
"github.com/pkg/errors"
4141
"google.golang.org/api/option"
4242

43+
"github.com/GoogleContainerTools/kaniko/pkg/filesystem"
4344
"github.com/GoogleContainerTools/kaniko/pkg/timing"
4445
"github.com/GoogleContainerTools/kaniko/pkg/util"
4546
"github.com/GoogleContainerTools/kaniko/pkg/util/bucket"
@@ -104,7 +105,7 @@ func launchTests(m *testing.M) (int, error) {
104105
if err != nil {
105106
return 1, errors.Wrap(err, "failed to get bucket name from uri")
106107
}
107-
contextFile, err := os.Open(contextFilePath)
108+
contextFile, err := filesystem.FS.Open(contextFilePath)
108109
if err != nil {
109110
return 1, fmt.Errorf("failed to read file at path %v: %w", contextFilePath, err)
110111
}
@@ -113,7 +114,7 @@ func launchTests(m *testing.M) (int, error) {
113114
return 1, errors.Wrap(err, "Failed to upload build context")
114115
}
115116

116-
if err = os.Remove(contextFilePath); err != nil {
117+
if err = filesystem.FS.Remove(contextFilePath); err != nil {
117118
return 1, errors.Wrap(err, fmt.Sprintf("Failed to remove tarball at %s", contextFilePath))
118119
}
119120

@@ -1035,7 +1036,7 @@ func getLastLayerFiles(image string) ([]string, error) {
10351036

10361037
func logBenchmarks(benchmark string) error {
10371038
if b, err := strconv.ParseBool(os.Getenv("BENCHMARK")); err == nil && b {
1038-
f, err := os.Create(benchmark)
1039+
f, err := filesystem.FS.Create(benchmark)
10391040
if err != nil {
10401041
return err
10411042
}
@@ -1074,7 +1075,7 @@ func initIntegrationTestConfig() *integrationTestConfig {
10741075
if err != nil {
10751076
log.Fatalf("Error getting absolute path for service account: %s\n", c.serviceAccount)
10761077
}
1077-
if _, err := os.Stat(absPath); os.IsNotExist(err) {
1078+
if _, err := filesystem.FS.Stat(absPath); os.IsNotExist(err) {
10781079
log.Fatalf("Service account does not exist: %s\n", absPath)
10791080
}
10801081
c.serviceAccount = absPath

integration/integration_with_context_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import (
2222
"os"
2323
"path/filepath"
2424
"testing"
25+
26+
"github.com/GoogleContainerTools/kaniko/pkg/filesystem"
2527
)
2628

2729
func TestWithContext(t *testing.T) {
@@ -31,7 +33,7 @@ func TestWithContext(t *testing.T) {
3133
}
3234

3335
dir := filepath.Join(cwd, "dockerfiles-with-context")
34-
entries, err := os.ReadDir(dir)
36+
entries, err := filesystem.ReadDir(dir)
3537
if err != nil {
3638
t.Fatal(err)
3739
}
@@ -68,7 +70,6 @@ func TestWithContext(t *testing.T) {
6870

6971
expected := fmt.Sprintf(emptyContainerDiff, dockerImage, kanikoImage, dockerImage, kanikoImage)
7072
checkContainerDiffOutput(t, diff, expected)
71-
7273
})
7374
}
7475

integration/integration_with_stdin_test.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"sync"
2727
"testing"
2828

29+
"github.com/GoogleContainerTools/kaniko/pkg/filesystem"
2930
"github.com/GoogleContainerTools/kaniko/pkg/util"
3031
"github.com/GoogleContainerTools/kaniko/testutil"
3132
)
@@ -37,7 +38,7 @@ func TestBuildWithStdin(t *testing.T) {
3738
testDir := "test_dir"
3839
testDirLongPath := filepath.Join(cwd, testDir)
3940

40-
if err := os.MkdirAll(testDirLongPath, 0750); err != nil {
41+
if err := filesystem.MkdirAll(testDirLongPath, 0o750); err != nil {
4142
t.Errorf("Failed to create dir_where_to_extract: %v", err)
4243
}
4344

@@ -62,7 +63,7 @@ func TestBuildWithStdin(t *testing.T) {
6263
// Create Tar Gz File with dockerfile inside
6364
go func(wg *sync.WaitGroup) {
6465
defer wg.Done()
65-
tarFile, err := os.Create(tarPath)
66+
tarFile, err := filesystem.FS.Create(tarPath)
6667
if err != nil {
6768
t.Errorf("Failed to create %s: %v", tarPath, err)
6869
}
@@ -86,10 +87,12 @@ func TestBuildWithStdin(t *testing.T) {
8687

8788
dockerImage := GetDockerImage(config.imageRepo, dockerfile)
8889
dockerCmd := exec.Command("docker",
89-
append([]string{"build",
90+
append([]string{
91+
"build",
9092
"-t", dockerImage,
9193
"-f", dockerfile,
92-
"."})...)
94+
".",
95+
})...)
9396

9497
_, err := RunCommandWithoutTest(dockerCmd)
9598
if err != nil {
@@ -145,7 +148,7 @@ func TestBuildWithStdin(t *testing.T) {
145148
expected := fmt.Sprintf(emptyContainerDiff, dockerImage, kanikoImageStdin, dockerImage, kanikoImageStdin)
146149
checkContainerDiffOutput(t, diff, expected)
147150

148-
if err := os.RemoveAll(testDirLongPath); err != nil {
151+
if err := filesystem.FS.RemoveAll(testDirLongPath); err != nil {
149152
t.Errorf("Failed to remove %s: %v", testDirLongPath, err)
150153
}
151154
}

integration/k8s_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import (
2525
"path/filepath"
2626
"testing"
2727
"text/template"
28+
29+
"github.com/GoogleContainerTools/kaniko/pkg/filesystem"
2830
)
2931

3032
type K8sConfig struct {
@@ -41,7 +43,7 @@ func TestK8s(t *testing.T) {
4143

4244
dir := filepath.Join(cwd, "dockerfiles-with-context")
4345

44-
entries, err := os.ReadDir(dir)
46+
entries, err := filesystem.ReadDir(dir)
4547
if err != nil {
4648
t.Fatal(err)
4749
}
@@ -73,11 +75,11 @@ func TestK8s(t *testing.T) {
7375
dockerImage := GetDockerImage(config.imageRepo, name)
7476
kanikoImage := GetKanikoImage(config.imageRepo, name)
7577

76-
tmpfile, err := os.CreateTemp("", "k8s-job-*.yaml")
78+
tmpfile, err := filesystem.CreateTemp("", "k8s-job-*.yaml")
7779
if err != nil {
7880
log.Fatal(err)
7981
}
80-
defer os.Remove(tmpfile.Name()) // clean up
82+
defer filesystem.FS.Remove(tmpfile.Name()) // clean up
8183
tmpl := template.Must(template.ParseFiles("k8s-job.yaml"))
8284
job := K8sConfig{KanikoImage: kanikoImage, Context: testDir, Name: name}
8385
if err := tmpl.Execute(tmpfile, job); err != nil {
@@ -86,7 +88,7 @@ func TestK8s(t *testing.T) {
8688

8789
t.Logf("Testing K8s based Kaniko building of dockerfile %s and push to %s \n",
8890
testDir, kanikoImage)
89-
content, err := os.ReadFile(tmpfile.Name())
91+
content, err := filesystem.ReadFile(tmpfile.Name())
9092
if err != nil {
9193
log.Fatal(err)
9294
}

0 commit comments

Comments
 (0)