Skip to content

Commit

Permalink
chore: add new skip-unreachable-dirs to not error on init command whe…
Browse files Browse the repository at this point in the history
…n a dir can not be read (#9163)

* chore: add new skip-unreachable-dirs to not error on init command when a dir can not be accessed

* docs: update docs with new skip-unreachable-dirs flag

* test: integration test for skip-unreachable-dirs flag
  • Loading branch information
renzodavid9 authored Nov 7, 2023
1 parent 85de8d7 commit ca26550
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 7 deletions.
3 changes: 3 additions & 0 deletions cmd/skaffold/app/cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ var (
cliKubernetesManifests []string
skipBuild bool
skipDeploy bool
skipUnreachableDirs bool
force bool
analyze bool
enableJibInit bool
Expand Down Expand Up @@ -70,6 +71,7 @@ func NewCmdInit() *cobra.Command {
{Value: &enableBuildpacksInit, Name: "XXenableBuildpacksInit", DefValue: true, Usage: "", Hidden: true, IsEnum: true},
{Value: &buildpacksBuilder, Name: "XXdefaultBuildpacksBuilder", DefValue: "gcr.io/buildpacks/builder:v1", Usage: "", Hidden: true},
{Value: &enableManifestGeneration, Name: "generate-manifests", DefValue: false, Usage: "Allows skaffold to try and generate basic kubernetes resources to get your project started", IsEnum: true},
{Value: &skipUnreachableDirs, Name: "skip-unreachable-dirs", DefValue: false, Usage: "Instead of erroring, it will skip the directories that cannot be accessed due to permissions", IsEnum: true},
}).
NoArgs(doInit)
}
Expand All @@ -83,6 +85,7 @@ func doInit(ctx context.Context, out io.Writer) error {
CliKubernetesManifests: cliKubernetesManifests,
SkipBuild: skipBuild,
SkipDeploy: skipDeploy,
SkipUnreachableDirs: skipUnreachableDirs,
Force: force,
Analyze: analyze,
EnableJibInit: enableJibInit,
Expand Down
2 changes: 2 additions & 0 deletions docs-v2/content/en/docs/references/cli/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -1073,6 +1073,7 @@ Options:
-m, --module=[]: Filter Skaffold configs to only the provided named modules
--remote-cache-dir='': Specify the location of the remote cache (default $HOME/.skaffold/remote-cache)
--skip-build=false: Skip generating build artifacts in Skaffold config
--skip-unreachable-dirs=false: Instead of erroring, it will skip the directories that cannot be accessed due to permissions
--sync-remote-cache='always': Controls how Skaffold manages the remote config cache (see `remote-cache-dir`). One of `always` (default), `missing`, or `never`. `always` syncs remote repositories to latest on access. `missing` only clones remote repositories if they do not exist locally. `never` means the user takes responsibility for updating remote repositories.
Usage:
Expand All @@ -1096,6 +1097,7 @@ Env vars:
* `SKAFFOLD_MODULE` (same as `--module`)
* `SKAFFOLD_REMOTE_CACHE_DIR` (same as `--remote-cache-dir`)
* `SKAFFOLD_SKIP_BUILD` (same as `--skip-build`)
* `SKAFFOLD_SKIP_UNREACHABLE_DIRS` (same as `--skip-unreachable-dirs`)
* `SKAFFOLD_SYNC_REMOTE_CACHE` (same as `--sync-remote-cache`)

### skaffold options
Expand Down
44 changes: 44 additions & 0 deletions integration/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package integration

import (
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -211,3 +212,46 @@ func exitCode(err error) int {

return 1
}

func TestInitWithDirWithoutReadPerms(t *testing.T) {
MarkIntegrationTest(t, CanRunWithoutGcp)
tests := []struct {
description string
shouldFail bool
flags []string
dir string
dirToCreate string
}{
{
description: "without --skip-unreachable-dirs flag, should fail",
shouldFail: true,
flags: []string{"--analyze"},
dir: "testdata/getting-started/",
dirToCreate: "dir1",
},
{
description: "with --skip-unreachable-dirs flag, shouldn't fail",
shouldFail: false,
flags: []string{"--analyze", "--skip-unreachable-dirs"},
dir: "testdata/getting-started",
dirToCreate: "dir2",
},
}

for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
tmpDir := testutil.NewTempDir(t.T)
copyFiles(tmpDir.Root(), test.dir)

dirWithoutReadPerms := filepath.Join(tmpDir.Root(), test.dirToCreate)
os.MkdirAll(dirWithoutReadPerms, 0377)
defer os.Remove(dirWithoutReadPerms)

output, err := skaffold.Init(test.flags...).InDir(tmpDir.Root()).RunWithCombinedOutput(t.T)
t.CheckError(test.shouldFail, err)
if test.shouldFail {
t.CheckDeepEqual(fmt.Sprintf("open %v: permission denied\n", test.dirToCreate), string(output))
}
})
}
}
21 changes: 14 additions & 7 deletions pkg/skaffold/initializer/analyze/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package analyze

import (
"context"
"errors"
"os"
"path/filepath"
"sort"
Expand All @@ -41,12 +42,13 @@ type analyzer interface {
}

type ProjectAnalysis struct {
configAnalyzer *skaffoldConfigAnalyzer
kubeAnalyzer *kubeAnalyzer
kustomizeAnalyzer *kustomizeAnalyzer
helmAnalyzer *helmAnalyzer
builderAnalyzer *builderAnalyzer
maxFileSize int64
configAnalyzer *skaffoldConfigAnalyzer
kubeAnalyzer *kubeAnalyzer
kustomizeAnalyzer *kustomizeAnalyzer
helmAnalyzer *helmAnalyzer
builderAnalyzer *builderAnalyzer
maxFileSize int64
skipUnreachableDirs bool
}

type HelmChartInfo struct {
Expand Down Expand Up @@ -114,7 +116,8 @@ func NewAnalyzer(c config.Config) *ProjectAnalysis {
analyzeMode: c.Analyze,
targetConfig: c.Opts.ConfigurationFile,
},
maxFileSize: c.MaxFileSize,
maxFileSize: c.MaxFileSize,
skipUnreachableDirs: c.SkipUnreachableDirs,
}
}

Expand All @@ -127,6 +130,10 @@ func (a *ProjectAnalysis) Analyze(dir string) error {
}

dirents, err := godirwalk.ReadDirents(dir, nil)
if errors.Is(err, os.ErrPermission) && a.skipUnreachableDirs {
log.Entry(context.TODO()).Debugf("skipping directory %s due to permissions error", dir)
return nil
}
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions pkg/skaffold/initializer/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Config struct {
CliKubernetesManifests []string
SkipBuild bool
SkipDeploy bool
SkipUnreachableDirs bool
Force bool
Analyze bool
EnableJibInit bool // TODO: Remove this parameter
Expand Down

0 comments on commit ca26550

Please sign in to comment.