-
Notifications
You must be signed in to change notification settings - Fork 547
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Madhu Rajanna <[email protected]>
- Loading branch information
Showing
8 changed files
with
67 additions
and
36 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
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
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
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,32 @@ | ||
package util | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os/exec" | ||
"time" | ||
|
||
"k8s.io/klog" | ||
) | ||
|
||
// Timeout for Command execution | ||
// exporting Timeout to make it configurable | ||
var Timeout int | ||
|
||
// ExecCommand is a wrapper over exec.Command with timeout | ||
func ExecCommand(command string, args ...string) ([]byte, error) { | ||
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(Timeout)*time.Second) | ||
defer cancel() | ||
klog.V(4).Infof("exec %s %s", command, args) | ||
|
||
cmd := exec.Command(command, args...) // #nosec | ||
out, err := cmd.CombinedOutput() | ||
if ctx.Err() == context.DeadlineExceeded { | ||
return nil, fmt.Errorf("command timed out") | ||
} | ||
|
||
if err != nil { | ||
return nil, fmt.Errorf("command exited with non-zero code: %v", err) | ||
} | ||
return out, err | ||
} |