Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/go_modules/go.opentelemetry.io/ot…
Browse files Browse the repository at this point in the history
…el-1.22.0
  • Loading branch information
banjoh authored Feb 2, 2024
2 parents 7c329a5 + 710366c commit dfb01b7
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 7 deletions.
2 changes: 2 additions & 0 deletions config/crds/troubleshoot.sh_hostcollectors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1386,6 +1386,8 @@ spec:
type: object
outputDir:
type: string
timeout:
type: string
required:
- args
- command
Expand Down
2 changes: 2 additions & 0 deletions config/crds/troubleshoot.sh_hostpreflights.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1386,6 +1386,8 @@ spec:
type: object
outputDir:
type: string
timeout:
type: string
required:
- args
- command
Expand Down
2 changes: 2 additions & 0 deletions config/crds/troubleshoot.sh_supportbundles.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12195,6 +12195,8 @@ spec:
type: object
outputDir:
type: string
timeout:
type: string
required:
- args
- command
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ require (
github.com/c9s/goprocinfo v0.0.0-20170724085704-0010a05ce49f // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/chzyer/readline v1.5.1 // indirect
github.com/containerd/containerd v1.7.11 // indirect
github.com/containerd/containerd v1.7.13 // indirect
github.com/containerd/stargz-snapshotter/estargz v0.15.1 // indirect
github.com/containers/libtrust v0.0.0-20230121012942-c1716e8a8d01 // indirect
github.com/containers/ocicrypt v1.1.9 // indirect
Expand Down Expand Up @@ -207,7 +207,7 @@ require (
github.com/nsf/termbox-go v0.0.0-20190121233118-02980233997d // indirect
github.com/nwaples/rardecode v1.1.2 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/runc v1.1.10 // indirect
github.com/opencontainers/runc v1.1.12 // indirect
github.com/opencontainers/runtime-spec v1.1.0 // indirect
github.com/opencontainers/selinux v1.11.0 // indirect
github.com/ostreedev/ostree-go v0.0.0-20210805093236-719684c64e4f // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,8 @@ github.com/cncf/xds/go v0.0.0-20211001041855-01bcc9b48dfe/go.mod h1:eXthEFrGJvWH
github.com/cncf/xds/go v0.0.0-20211011173535-cb28da3451f1/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
github.com/containerd/cgroups/v3 v3.0.2 h1:f5WFqIVSgo5IZmtTT3qVBo6TzI1ON6sycSBKkymb9L0=
github.com/containerd/cgroups/v3 v3.0.2/go.mod h1:JUgITrzdFqp42uI2ryGA+ge0ap/nxzYgkGmIcetmErE=
github.com/containerd/containerd v1.7.11 h1:lfGKw3eU35sjV0aG2eYZTiwFEY1pCzxdzicHP3SZILw=
github.com/containerd/containerd v1.7.11/go.mod h1:5UluHxHTX2rdvYuZ5OJTC5m/KJNs0Zs9wVoJm9zf5ZE=
github.com/containerd/containerd v1.7.13 h1:wPYKIeGMN8vaggSKuV1X0wZulpMz4CrgEsZdaCyB6Is=
github.com/containerd/containerd v1.7.13/go.mod h1:zT3up6yTRfEUa6+GsITYIJNgSVL9NQ4x4h1RPzk0Wu4=
github.com/containerd/continuity v0.4.2 h1:v3y/4Yz5jwnvqPKJJ+7Wf93fyWoCB3F5EclWG023MDM=
github.com/containerd/continuity v0.4.2/go.mod h1:F6PTNCKepoxEaXLQp3wDAjygEnImnZ/7o4JzpodfroQ=
github.com/containerd/log v0.1.0 h1:TCJt7ioM2cr/tfR8GPbGf9/VRAX8D2B4PjzCpfX540I=
Expand Down Expand Up @@ -752,8 +752,8 @@ github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
github.com/opencontainers/image-spec v1.1.0-rc5 h1:Ygwkfw9bpDvs+c9E34SdgGOj41dX/cbdlwvlWt0pnFI=
github.com/opencontainers/image-spec v1.1.0-rc5/go.mod h1:X4pATf0uXsnn3g5aiGIsVnJBR4mxhKzfwmvK/B2NTm8=
github.com/opencontainers/runc v1.1.10 h1:EaL5WeO9lv9wmS6SASjszOeQdSctvpbu0DdBQBizE40=
github.com/opencontainers/runc v1.1.10/go.mod h1:+/R6+KmDlh+hOO8NkjmgkG9Qzvypzk0yXxAPYYR65+M=
github.com/opencontainers/runc v1.1.12 h1:BOIssBaW1La0/qbNZHXOOa71dZfZEQOzW7dqQf3phss=
github.com/opencontainers/runc v1.1.12/go.mod h1:S+lQwSfncpBha7XTy/5lBwWgm5+y5Ma/O44Ekby9FK8=
github.com/opencontainers/runtime-spec v1.1.0 h1:HHUyrt9mwHUjtasSbXSMvs4cyFxh+Bll4AjJ9odEGpg=
github.com/opencontainers/runtime-spec v1.1.0/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0=
github.com/opencontainers/selinux v1.11.0 h1:+5Zbo97w3Lbmb3PeqQtpmTkMwsW5nRI3YaLpt7tQ7oU=
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/troubleshoot/v1beta2/hostcollector_shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ type HostRun struct {
Env []string `json:"env,omitempty" yaml:"env,omitempty"`
InheritEnvs []string `json:"inheritEnvs,omitempty" yaml:"inheritEnvs,omitempty"`
IgnoreParentEnvs bool `json:"ignoreParentEnvs,omitempty" yaml:"ignoreParentEnvs,omitempty"`
Timeout string `json:"timeout,omitempty" yaml:"timeout,omitempty"`
}

type HostCollect struct {
Expand Down
29 changes: 28 additions & 1 deletion pkg/collect/host_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package collect

import (
"bytes"
"context"
"encoding/json"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"time"

"github.com/pkg/errors"
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
Expand Down Expand Up @@ -49,7 +51,29 @@ func (c *CollectHostRun) Collect(progressChan chan<- interface{}) (map[string][]
collectorName = "run-host"
}

cmd := exec.Command(c.attemptToConvertCmdToAbsPath(), runHostCollector.Args...)
var (
timeout time.Duration
cmd *exec.Cmd
errInvalidDuration error
)

ctx := context.Background()
cmdPath := c.attemptToConvertCmdToAbsPath()

if runHostCollector.Timeout != "" {
timeout, errInvalidDuration = time.ParseDuration(runHostCollector.Timeout)
if errInvalidDuration != nil {
return nil, errors.Wrapf(errInvalidDuration, "failed to parse timeout %q", runHostCollector.Timeout)
}
}

if timeout <= time.Duration(0) {
cmd = exec.Command(cmdPath, runHostCollector.Args...)
} else {
timeoutCtx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
cmd = exec.CommandContext(timeoutCtx, cmdPath, runHostCollector.Args...)
}

klog.V(2).Infof("Run host collector command: %q", cmd.String())
runInfo := &HostRunInfo{
Expand Down Expand Up @@ -118,6 +142,9 @@ func (c *CollectHostRun) Collect(progressChan chan<- interface{}) (map[string][]
if werr, ok := err.(*exec.ExitError); ok {
runInfo.ExitCode = strings.TrimPrefix(werr.Error(), "exit status ")
runInfo.Error = stderr.String()
} else if err == context.DeadlineExceeded {
runInfo.ExitCode = "-1"
runInfo.Error = fmt.Sprintf("command timed out after %s", timeout.String())
} else {
return nil, errors.Wrap(err, "failed to run")
}
Expand Down
74 changes: 74 additions & 0 deletions pkg/collect/host_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,77 @@ func TestCollectHostRun_Collect(t *testing.T) {
}
}
}

func TestCollectHostRunCollectWithTimeout(t *testing.T) {
tests := []struct {
name string
collector *CollectHostRun
wantError bool
}{
{
name: "no timeout",
collector: &CollectHostRun{
hostCollector: &troubleshootv1beta2.HostRun{
HostCollectorMeta: troubleshootv1beta2.HostCollectorMeta{
CollectorName: "no timeout",
},
Command: "echo",
Args: []string{"1"},
},
},
wantError: false,
},
{
name: "invalid timeout",
collector: &CollectHostRun{
hostCollector: &troubleshootv1beta2.HostRun{
HostCollectorMeta: troubleshootv1beta2.HostCollectorMeta{
CollectorName: "negative timeout",
},
Command: "echo",
Args: []string{"1"},
Timeout: "thisistheway",
},
},
wantError: true,
},
{
name: "negative timeout",
collector: &CollectHostRun{
hostCollector: &troubleshootv1beta2.HostRun{
HostCollectorMeta: troubleshootv1beta2.HostCollectorMeta{
CollectorName: "negative timeout",
},
Command: "echo",
Args: []string{"1"},
Timeout: "-10s",
},
},
wantError: false,
},
{
name: "endless command with timeout",
collector: &CollectHostRun{
hostCollector: &troubleshootv1beta2.HostRun{
HostCollectorMeta: troubleshootv1beta2.HostCollectorMeta{
CollectorName: "endless command",
},
Command: "ping",
Args: []string{"google.com"},
Timeout: "200ms",
},
},
wantError: false,
},
}

for _, test := range tests {
got, err := test.collector.Collect(nil)
if test.wantError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.NotNil(t, got)
}
}
}
3 changes: 3 additions & 0 deletions schemas/supportbundle-troubleshoot-v1beta2.json
Original file line number Diff line number Diff line change
Expand Up @@ -11839,6 +11839,9 @@
},
"outputDir": {
"type": "string"
},
"timeout": {
"type": "string"
}
}
},
Expand Down

0 comments on commit dfb01b7

Please sign in to comment.