Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove afero #613

Merged
merged 2 commits into from
May 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@
name = "github.com/docker/distribution"
revision = "edc3ab29cdff8694dd6feb85cfeb4b5f1b38ed9c"

[[constraint]]
name = "github.com/spf13/afero"
version = "1.1.0"

[[constraint]]
name = "github.com/fsnotify/fsnotify"
version = "1.4.7"
Expand Down
2 changes: 1 addition & 1 deletion pkg/skaffold/build/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (l *LocalBuilder) Build(ctx context.Context, out io.Writer, tagger tag.Tagg
func (l *LocalBuilder) buildDocker(ctx context.Context, out io.Writer, a *v1alpha2.Artifact) (string, error) {
initialTag := util.RandomID()
// Add a sanity check to check if the dockerfile exists before running the build
if _, err := util.Fs.Stat(filepath.Join(a.Workspace, a.DockerArtifact.DockerfilePath)); err != nil {
if _, err := os.Stat(filepath.Join(a.Workspace, a.DockerArtifact.DockerfilePath)); err != nil {
if os.IsNotExist(err) {
return "", fmt.Errorf("Could not find dockerfile: %s", a.DockerArtifact.DockerfilePath)
}
Expand Down
18 changes: 10 additions & 8 deletions pkg/skaffold/deploy/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"bytes"
"context"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand All @@ -33,7 +34,6 @@ import (
"github.com/docker/distribution/reference"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/afero"
"gopkg.in/yaml.v2"
)

Expand Down Expand Up @@ -69,14 +69,17 @@ spec:
// KubectlDeployer deploys workflows using kubectl CLI.
type KubectlDeployer struct {
*v1alpha2.DeployConfig

workingDir string
kubeContext string
}

// NewKubectlDeployer returns a new KubectlDeployer for a DeployConfig filled
// with the needed configuration for `kubectl apply`
func NewKubectlDeployer(cfg *v1alpha2.DeployConfig, kubeContext string) *KubectlDeployer {
func NewKubectlDeployer(workingDir string, cfg *v1alpha2.DeployConfig, kubeContext string) *KubectlDeployer {
return &KubectlDeployer{
DeployConfig: cfg,
workingDir: workingDir,
kubeContext: kubeContext,
}
}
Expand Down Expand Up @@ -121,9 +124,8 @@ func (k *KubectlDeployer) Cleanup(ctx context.Context, out io.Writer) error {
return nil
}

// Not implemented
func (k *KubectlDeployer) Dependencies() ([]string, error) {
return manifestFiles(k.KubectlDeploy.Manifests)
return k.manifestFiles(k.KubectlDeploy.Manifests)
}

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

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

// readManifests reads the manifests to deploy/delete.
func (k *KubectlDeployer) readManifests() (manifestList, error) {
files, err := manifestFiles(k.KubectlDeploy.Manifests)
files, err := k.manifestFiles(k.KubectlDeploy.Manifests)
if err != nil {
return nil, errors.Wrap(err, "expanding user manifest list")
}
var manifests manifestList

for _, manifest := range files {
buf, err := afero.ReadFile(util.Fs, manifest)
buf, err := ioutil.ReadFile(manifest)
if err != nil {
return nil, errors.Wrap(err, "reading manifest")
}
Expand Down
23 changes: 12 additions & 11 deletions pkg/skaffold/deploy/kubectl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"bytes"
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"

Expand All @@ -28,7 +30,6 @@ import (
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
"github.com/GoogleContainerTools/skaffold/testutil"
"github.com/pkg/errors"
"github.com/spf13/afero"
)

const testKubeContext = "kubecontext"
Expand Down Expand Up @@ -134,11 +135,11 @@ func TestKubectlDeploy(t *testing.T) {
},
}

defer func(fs afero.Fs) { util.Fs = fs }(util.Fs)
util.Fs = afero.NewMemMapFs()
tmp, cleanup := testutil.TempDir(t)
defer cleanup()

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

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

k := NewKubectlDeployer(test.cfg, testKubeContext)
k := NewKubectlDeployer(tmp, test.cfg, testKubeContext)
err := k.Deploy(context.Background(), &bytes.Buffer{}, test.builds)

testutil.CheckError(t, test.shouldErr, err)
Expand Down Expand Up @@ -187,11 +188,11 @@ func TestKubectlCleanup(t *testing.T) {
},
}

defer func(fs afero.Fs) { util.Fs = fs }(util.Fs)
util.Fs = afero.NewMemMapFs()
tmp, cleanup := testutil.TempDir(t)
defer cleanup()

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

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

k := NewKubectlDeployer(test.cfg, testKubeContext)
k := NewKubectlDeployer(tmp, test.cfg, testKubeContext)
err := k.Cleanup(context.Background(), &bytes.Buffer{})

testutil.CheckError(t, test.shouldErr, err)
Expand Down
7 changes: 6 additions & 1 deletion pkg/skaffold/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,12 @@ func getBuilder(cfg *v1alpha2.BuildConfig, kubeContext string) (build.Builder, e
func getDeployer(cfg *v1alpha2.DeployConfig, kubeContext string) (deploy.Deployer, error) {
switch {
case cfg.KubectlDeploy != nil:
return deploy.NewKubectlDeployer(cfg, kubeContext), nil
// TODO(dgageot): this should be the folder containing skaffold.yaml. Should also be moved elsewhere.
cwd, err := os.Getwd()
if err != nil {
return nil, errors.Wrap(err, "finding current directory")
}
return deploy.NewKubectlDeployer(cwd, cfg, kubeContext), nil

case cfg.HelmDeploy != nil:
return deploy.NewHelmDeployer(cfg, kubeContext), nil
Expand Down
18 changes: 9 additions & 9 deletions pkg/skaffold/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,13 @@ import (
"io/ioutil"
"net/http"
"os"
"path/filepath"
"sort"
"strings"

"github.com/pkg/errors"
"github.com/spf13/afero"
)

var Fs = afero.NewOsFs()

func RandomID() string {
b := make([]byte, 16)
_, err := rand.Read(b)
Expand Down Expand Up @@ -66,25 +64,27 @@ func StrSliceContains(sl []string, s string) bool {

// ExpandPathsGlob expands paths according to filepath.Glob patterns
// Returns a list of unique files that match the glob patterns passed in.
func ExpandPathsGlob(paths []string) ([]string, error) {
func ExpandPathsGlob(workingDir string, paths []string) ([]string, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this code just wrong before without the workingDir?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. It was using afero that implicitly sets a root dir

expandedPaths := make(map[string]bool)
for _, p := range paths {
if _, err := Fs.Stat(p); err == nil {
path := filepath.Join(workingDir, p)

if _, err := os.Stat(path); err == nil {
// This is a file reference, so just add it
expandedPaths[p] = true
expandedPaths[path] = true
continue
}

files, err := afero.Glob(Fs, p)
files, err := filepath.Glob(path)
if err != nil {
return nil, errors.Wrap(err, "glob")
}
if files == nil {
return nil, fmt.Errorf("File pattern must match at least one file %s", p)
return nil, fmt.Errorf("File pattern must match at least one file %s", path)
}

for _, f := range files {
err := afero.Walk(Fs, f, func(path string, info os.FileInfo, err error) error {
err := filepath.Walk(f, func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
expandedPaths[path] = true
}
Expand Down
25 changes: 14 additions & 11 deletions pkg/skaffold/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ limitations under the License.
package util

import (
"io/ioutil"
"os"
"path/filepath"
"testing"

"github.com/GoogleContainerTools/skaffold/testutil"
"github.com/spf13/afero"
)

func TestSupportedKubernetesFormats(t *testing.T) {
Expand Down Expand Up @@ -62,13 +64,13 @@ func TestSupportedKubernetesFormats(t *testing.T) {
}

func TestExpandPathsGlob(t *testing.T) {
defer func(fs afero.Fs) { Fs = fs }(Fs)
Fs = afero.NewMemMapFs()
tmp, cleanup := testutil.TempDir(t)
defer cleanup()

Fs.MkdirAll("dir/sub_dir", 0700)
Fs.MkdirAll("dir_b/sub_dir_b", 0700)
afero.WriteFile(Fs, "dir_b/sub_dir_b/file", []byte(""), 0650)
afero.WriteFile(Fs, "dir/sub_dir/file", []byte(""), 0650)
os.MkdirAll(filepath.Join(tmp, "dir", "sub_dir"), 0700)
os.MkdirAll(filepath.Join(tmp, "dir_b", "sub_dir_b"), 0700)
ioutil.WriteFile(filepath.Join(tmp, "dir_b", "sub_dir_b", "file"), []byte(""), 0650)
ioutil.WriteFile(filepath.Join(tmp, "dir", "sub_dir", "file"), []byte(""), 0650)

var tests = []struct {
description string
Expand All @@ -79,17 +81,17 @@ func TestExpandPathsGlob(t *testing.T) {
{
description: "match exact filename",
in: []string{"dir/sub_dir/file"},
out: []string{"dir/sub_dir/file"},
out: []string{filepath.Join(tmp, "dir", "sub_dir", "file")},
},
{
description: "match leaf directory glob",
in: []string{"dir/sub_dir/*"},
out: []string{"dir/sub_dir/file"},
out: []string{filepath.Join(tmp, "dir", "sub_dir", "file")},
},
{
description: "match top level glob",
in: []string{"dir*"},
out: []string{"dir/sub_dir/file", "dir_b/sub_dir_b/file"},
out: []string{filepath.Join(tmp, "dir", "sub_dir", "file"), filepath.Join(tmp, "dir_b", "sub_dir_b", "file")},
},
{
description: "error unmatched glob",
Expand All @@ -100,7 +102,8 @@ func TestExpandPathsGlob(t *testing.T) {

for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
actual, err := ExpandPathsGlob(tt.in)
actual, err := ExpandPathsGlob(tmp, tt.in)

testutil.CheckErrorAndDeepEqual(t, tt.shouldErr, err, tt.out, actual)
})
}
Expand Down
Loading