Skip to content

Commit 8c13026

Browse files
authored
Merge pull request #613 from dgageot/remove-afero
Remove afero
2 parents c737218 + 245eaaf commit 8c13026

29 files changed

+53
-3518
lines changed

Gopkg.lock

+1-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

-4
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@
3131
name = "github.com/docker/distribution"
3232
revision = "edc3ab29cdff8694dd6feb85cfeb4b5f1b38ed9c"
3333

34-
[[constraint]]
35-
name = "github.com/spf13/afero"
36-
version = "1.1.0"
37-
3834
[[constraint]]
3935
name = "github.com/fsnotify/fsnotify"
4036
version = "1.4.7"

pkg/skaffold/build/local.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ func (l *LocalBuilder) Build(ctx context.Context, out io.Writer, tagger tag.Tagg
132132
func (l *LocalBuilder) buildDocker(ctx context.Context, out io.Writer, a *v1alpha2.Artifact) (string, error) {
133133
initialTag := util.RandomID()
134134
// Add a sanity check to check if the dockerfile exists before running the build
135-
if _, err := util.Fs.Stat(filepath.Join(a.Workspace, a.DockerArtifact.DockerfilePath)); err != nil {
135+
if _, err := os.Stat(filepath.Join(a.Workspace, a.DockerArtifact.DockerfilePath)); err != nil {
136136
if os.IsNotExist(err) {
137137
return "", fmt.Errorf("Could not find dockerfile: %s", a.DockerArtifact.DockerfilePath)
138138
}

pkg/skaffold/deploy/kubectl.go

+10-8
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"bytes"
2121
"context"
2222
"io"
23+
"io/ioutil"
2324
"os"
2425
"os/exec"
2526
"path/filepath"
@@ -33,7 +34,6 @@ import (
3334
"github.com/docker/distribution/reference"
3435
"github.com/pkg/errors"
3536
"github.com/sirupsen/logrus"
36-
"github.com/spf13/afero"
3737
"gopkg.in/yaml.v2"
3838
)
3939

@@ -69,14 +69,17 @@ spec:
6969
// KubectlDeployer deploys workflows using kubectl CLI.
7070
type KubectlDeployer struct {
7171
*v1alpha2.DeployConfig
72+
73+
workingDir string
7274
kubeContext string
7375
}
7476

7577
// NewKubectlDeployer returns a new KubectlDeployer for a DeployConfig filled
7678
// with the needed configuration for `kubectl apply`
77-
func NewKubectlDeployer(cfg *v1alpha2.DeployConfig, kubeContext string) *KubectlDeployer {
79+
func NewKubectlDeployer(workingDir string, cfg *v1alpha2.DeployConfig, kubeContext string) *KubectlDeployer {
7880
return &KubectlDeployer{
7981
DeployConfig: cfg,
82+
workingDir: workingDir,
8083
kubeContext: kubeContext,
8184
}
8285
}
@@ -121,9 +124,8 @@ func (k *KubectlDeployer) Cleanup(ctx context.Context, out io.Writer) error {
121124
return nil
122125
}
123126

124-
// Not implemented
125127
func (k *KubectlDeployer) Dependencies() ([]string, error) {
126-
return manifestFiles(k.KubectlDeploy.Manifests)
128+
return k.manifestFiles(k.KubectlDeploy.Manifests)
127129
}
128130

129131
// readOrGenerateManifests reads the manifests to deploy/delete. If no manifest exists, try to
@@ -156,8 +158,8 @@ func (k *KubectlDeployer) kubectl(in io.Reader, out io.Writer, arg ...string) er
156158
return util.RunCmd(cmd)
157159
}
158160

159-
func manifestFiles(manifests []string) ([]string, error) {
160-
list, err := util.ExpandPathsGlob(manifests)
161+
func (k *KubectlDeployer) manifestFiles(manifests []string) ([]string, error) {
162+
list, err := util.ExpandPathsGlob(k.workingDir, manifests)
161163
if err != nil {
162164
return nil, errors.Wrap(err, "expanding kubectl manifest paths")
163165
}
@@ -179,14 +181,14 @@ func manifestFiles(manifests []string) ([]string, error) {
179181

180182
// readManifests reads the manifests to deploy/delete.
181183
func (k *KubectlDeployer) readManifests() (manifestList, error) {
182-
files, err := manifestFiles(k.KubectlDeploy.Manifests)
184+
files, err := k.manifestFiles(k.KubectlDeploy.Manifests)
183185
if err != nil {
184186
return nil, errors.Wrap(err, "expanding user manifest list")
185187
}
186188
var manifests manifestList
187189

188190
for _, manifest := range files {
189-
buf, err := afero.ReadFile(util.Fs, manifest)
191+
buf, err := ioutil.ReadFile(manifest)
190192
if err != nil {
191193
return nil, errors.Wrap(err, "reading manifest")
192194
}

pkg/skaffold/deploy/kubectl_test.go

+12-11
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"bytes"
2121
"context"
2222
"fmt"
23+
"io/ioutil"
24+
"os"
2325
"path/filepath"
2426
"testing"
2527

@@ -28,7 +30,6 @@ import (
2830
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
2931
"github.com/GoogleContainerTools/skaffold/testutil"
3032
"github.com/pkg/errors"
31-
"github.com/spf13/afero"
3233
)
3334

3435
const testKubeContext = "kubecontext"
@@ -134,11 +135,11 @@ func TestKubectlDeploy(t *testing.T) {
134135
},
135136
}
136137

137-
defer func(fs afero.Fs) { util.Fs = fs }(util.Fs)
138-
util.Fs = afero.NewMemMapFs()
138+
tmp, cleanup := testutil.TempDir(t)
139+
defer cleanup()
139140

140-
util.Fs.MkdirAll("test", 0750)
141-
afero.WriteFile(util.Fs, "test/deployment.yaml", []byte(deploymentYAML), 0644)
141+
os.MkdirAll(filepath.Join(tmp, "test"), 0750)
142+
ioutil.WriteFile(filepath.Join(tmp, "test", "deployment.yaml"), []byte(deploymentYAML), 0644)
142143

143144
for _, test := range tests {
144145
t.Run(test.description, func(t *testing.T) {
@@ -147,7 +148,7 @@ func TestKubectlDeploy(t *testing.T) {
147148
util.DefaultExecCommand = test.command
148149
}
149150

150-
k := NewKubectlDeployer(test.cfg, testKubeContext)
151+
k := NewKubectlDeployer(tmp, test.cfg, testKubeContext)
151152
err := k.Deploy(context.Background(), &bytes.Buffer{}, test.builds)
152153

153154
testutil.CheckError(t, test.shouldErr, err)
@@ -187,11 +188,11 @@ func TestKubectlCleanup(t *testing.T) {
187188
},
188189
}
189190

190-
defer func(fs afero.Fs) { util.Fs = fs }(util.Fs)
191-
util.Fs = afero.NewMemMapFs()
191+
tmp, cleanup := testutil.TempDir(t)
192+
defer cleanup()
192193

193-
util.Fs.MkdirAll("test", 0750)
194-
afero.WriteFile(util.Fs, "test/deployment.yaml", []byte(deploymentYAML), 0644)
194+
os.MkdirAll(filepath.Join(tmp, "test"), 0750)
195+
ioutil.WriteFile(filepath.Join(tmp, "test", "deployment.yaml"), []byte(deploymentYAML), 0644)
195196

196197
for _, test := range tests {
197198
t.Run(test.description, func(t *testing.T) {
@@ -200,7 +201,7 @@ func TestKubectlCleanup(t *testing.T) {
200201
util.DefaultExecCommand = test.command
201202
}
202203

203-
k := NewKubectlDeployer(test.cfg, testKubeContext)
204+
k := NewKubectlDeployer(tmp, test.cfg, testKubeContext)
204205
err := k.Cleanup(context.Background(), &bytes.Buffer{})
205206

206207
testutil.CheckError(t, test.shouldErr, err)

pkg/skaffold/runner/runner.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,12 @@ func getBuilder(cfg *v1alpha2.BuildConfig, kubeContext string) (build.Builder, e
111111
func getDeployer(cfg *v1alpha2.DeployConfig, kubeContext string) (deploy.Deployer, error) {
112112
switch {
113113
case cfg.KubectlDeploy != nil:
114-
return deploy.NewKubectlDeployer(cfg, kubeContext), nil
114+
// TODO(dgageot): this should be the folder containing skaffold.yaml. Should also be moved elsewhere.
115+
cwd, err := os.Getwd()
116+
if err != nil {
117+
return nil, errors.Wrap(err, "finding current directory")
118+
}
119+
return deploy.NewKubectlDeployer(cwd, cfg, kubeContext), nil
115120

116121
case cfg.HelmDeploy != nil:
117122
return deploy.NewHelmDeployer(cfg, kubeContext), nil

pkg/skaffold/util/util.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,13 @@ import (
2222
"io/ioutil"
2323
"net/http"
2424
"os"
25+
"path/filepath"
2526
"sort"
2627
"strings"
2728

2829
"github.com/pkg/errors"
29-
"github.com/spf13/afero"
3030
)
3131

32-
var Fs = afero.NewOsFs()
33-
3432
func RandomID() string {
3533
b := make([]byte, 16)
3634
_, err := rand.Read(b)
@@ -66,25 +64,27 @@ func StrSliceContains(sl []string, s string) bool {
6664

6765
// ExpandPathsGlob expands paths according to filepath.Glob patterns
6866
// Returns a list of unique files that match the glob patterns passed in.
69-
func ExpandPathsGlob(paths []string) ([]string, error) {
67+
func ExpandPathsGlob(workingDir string, paths []string) ([]string, error) {
7068
expandedPaths := make(map[string]bool)
7169
for _, p := range paths {
72-
if _, err := Fs.Stat(p); err == nil {
70+
path := filepath.Join(workingDir, p)
71+
72+
if _, err := os.Stat(path); err == nil {
7373
// This is a file reference, so just add it
74-
expandedPaths[p] = true
74+
expandedPaths[path] = true
7575
continue
7676
}
7777

78-
files, err := afero.Glob(Fs, p)
78+
files, err := filepath.Glob(path)
7979
if err != nil {
8080
return nil, errors.Wrap(err, "glob")
8181
}
8282
if files == nil {
83-
return nil, fmt.Errorf("File pattern must match at least one file %s", p)
83+
return nil, fmt.Errorf("File pattern must match at least one file %s", path)
8484
}
8585

8686
for _, f := range files {
87-
err := afero.Walk(Fs, f, func(path string, info os.FileInfo, err error) error {
87+
err := filepath.Walk(f, func(path string, info os.FileInfo, err error) error {
8888
if !info.IsDir() {
8989
expandedPaths[path] = true
9090
}

pkg/skaffold/util/util_test.go

+14-11
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ limitations under the License.
1717
package util
1818

1919
import (
20+
"io/ioutil"
21+
"os"
22+
"path/filepath"
2023
"testing"
2124

2225
"github.com/GoogleContainerTools/skaffold/testutil"
23-
"github.com/spf13/afero"
2426
)
2527

2628
func TestSupportedKubernetesFormats(t *testing.T) {
@@ -62,13 +64,13 @@ func TestSupportedKubernetesFormats(t *testing.T) {
6264
}
6365

6466
func TestExpandPathsGlob(t *testing.T) {
65-
defer func(fs afero.Fs) { Fs = fs }(Fs)
66-
Fs = afero.NewMemMapFs()
67+
tmp, cleanup := testutil.TempDir(t)
68+
defer cleanup()
6769

68-
Fs.MkdirAll("dir/sub_dir", 0700)
69-
Fs.MkdirAll("dir_b/sub_dir_b", 0700)
70-
afero.WriteFile(Fs, "dir_b/sub_dir_b/file", []byte(""), 0650)
71-
afero.WriteFile(Fs, "dir/sub_dir/file", []byte(""), 0650)
70+
os.MkdirAll(filepath.Join(tmp, "dir", "sub_dir"), 0700)
71+
os.MkdirAll(filepath.Join(tmp, "dir_b", "sub_dir_b"), 0700)
72+
ioutil.WriteFile(filepath.Join(tmp, "dir_b", "sub_dir_b", "file"), []byte(""), 0650)
73+
ioutil.WriteFile(filepath.Join(tmp, "dir", "sub_dir", "file"), []byte(""), 0650)
7274

7375
var tests = []struct {
7476
description string
@@ -79,17 +81,17 @@ func TestExpandPathsGlob(t *testing.T) {
7981
{
8082
description: "match exact filename",
8183
in: []string{"dir/sub_dir/file"},
82-
out: []string{"dir/sub_dir/file"},
84+
out: []string{filepath.Join(tmp, "dir", "sub_dir", "file")},
8385
},
8486
{
8587
description: "match leaf directory glob",
8688
in: []string{"dir/sub_dir/*"},
87-
out: []string{"dir/sub_dir/file"},
89+
out: []string{filepath.Join(tmp, "dir", "sub_dir", "file")},
8890
},
8991
{
9092
description: "match top level glob",
9193
in: []string{"dir*"},
92-
out: []string{"dir/sub_dir/file", "dir_b/sub_dir_b/file"},
94+
out: []string{filepath.Join(tmp, "dir", "sub_dir", "file"), filepath.Join(tmp, "dir_b", "sub_dir_b", "file")},
9395
},
9496
{
9597
description: "error unmatched glob",
@@ -100,7 +102,8 @@ func TestExpandPathsGlob(t *testing.T) {
100102

101103
for _, tt := range tests {
102104
t.Run(tt.description, func(t *testing.T) {
103-
actual, err := ExpandPathsGlob(tt.in)
105+
actual, err := ExpandPathsGlob(tmp, tt.in)
106+
104107
testutil.CheckErrorAndDeepEqual(t, tt.shouldErr, err, tt.out, actual)
105108
})
106109
}

0 commit comments

Comments
 (0)