-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
177 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright 2020 The Kubernetes Authors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package git | ||
|
||
import ( | ||
"os/exec" | ||
"time" | ||
|
||
"github.com/pkg/errors" | ||
"sigs.k8s.io/kustomize/api/filesys" | ||
"sigs.k8s.io/kustomize/api/types" | ||
) | ||
|
||
// Arbitrary, but non-infinite, timeout for running commands. | ||
const defaultDuration = 27 * time.Second | ||
|
||
// gitRunner runs the external git binary. | ||
type gitRunner struct { | ||
gitProgram string | ||
duration time.Duration | ||
dir filesys.ConfirmedDir | ||
} | ||
|
||
// newCmdRunner returns a gitRunner if it can find the binary. | ||
// It also creats a temp directory for cloning repos. | ||
func newCmdRunner() (*gitRunner, error) { | ||
gitProgram, err := exec.LookPath("git") | ||
if err != nil { | ||
return nil, errors.Wrap(err, "no 'git' program on path") | ||
} | ||
dir, err := filesys.NewTmpConfirmedDir() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &gitRunner{ | ||
gitProgram: gitProgram, | ||
duration: defaultDuration, | ||
dir: dir, | ||
}, nil | ||
} | ||
|
||
// run a command with a timeout. | ||
func (r gitRunner) run(args ...string) (err error) { | ||
ch := make(chan bool, 1) | ||
defer close(ch) | ||
//nolint: gosec | ||
cmd := exec.Command(r.gitProgram, args...) | ||
cmd.Dir = r.dir.String() | ||
timer := time.NewTimer(r.duration) | ||
defer timer.Stop() | ||
go func() { | ||
_, err = cmd.CombinedOutput() | ||
ch <- true | ||
}() | ||
select { | ||
case <-ch: | ||
if err != nil { | ||
return errors.Wrapf(err, "git cmd = '%s'", cmd.String()) | ||
} | ||
return nil | ||
case <-timer.C: | ||
return types.NewErrTimeOut(r.duration, cmd.String()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright 2019 The Kubernetes Authors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package krusty_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"sigs.k8s.io/kustomize/api/filesys" | ||
"sigs.k8s.io/kustomize/api/krusty" | ||
"sigs.k8s.io/kustomize/api/types" | ||
) | ||
|
||
func TestRemoteLoad(t *testing.T) { | ||
fSys := filesys.MakeFsOnDisk() | ||
b := krusty.MakeKustomizer(fSys, krusty.MakeDefaultOptions()) | ||
m, err := b.Run( | ||
"github.com/kubernetes-sigs/kustomize/examples/multibases/dev/?ref=v1.0.6") | ||
if types.IsErrTimeout(err) { | ||
// Don't fail on timeouts. | ||
t.SkipNow() | ||
} | ||
if !assert.NoError(t, err) { | ||
t.FailNow() | ||
} | ||
yml, err := m.AsYaml() | ||
assert.NoError(t, err) | ||
assert.Equal(t, `apiVersion: v1 | ||
kind: Pod | ||
metadata: | ||
labels: | ||
app: myapp | ||
name: dev-myapp-pod | ||
spec: | ||
containers: | ||
- image: nginx:1.7.9 | ||
name: nginx | ||
`, string(yml)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright 2020 The Kubernetes Authors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package types | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
type errTimeOut struct { | ||
duration time.Duration | ||
cmd string | ||
} | ||
|
||
func NewErrTimeOut(d time.Duration, c string) errTimeOut { | ||
return errTimeOut{duration: d, cmd: c} | ||
} | ||
|
||
func (e errTimeOut) Error() string { | ||
return fmt.Sprintf("hit %s timeout running '%s'", e.duration, e.cmd) | ||
} | ||
|
||
func IsErrTimeout(err error) bool { | ||
if err == nil { | ||
return false | ||
} | ||
_, ok := err.(errTimeOut) | ||
if ok { | ||
return true | ||
} | ||
_, ok = errors.Cause(err).(errTimeOut) | ||
return ok | ||
} |