|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io/ioutil" |
| 6 | + "net/http" |
| 7 | + "os" |
| 8 | + "os/signal" |
| 9 | + "strings" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/pkg/errors" |
| 13 | + "github.com/replicatedhq/troubleshoot/cmd/util" |
| 14 | + troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2" |
| 15 | + "github.com/replicatedhq/troubleshoot/pkg/client/troubleshootclientset/scheme" |
| 16 | + troubleshootclientsetscheme "github.com/replicatedhq/troubleshoot/pkg/client/troubleshootclientset/scheme" |
| 17 | + "github.com/replicatedhq/troubleshoot/pkg/collect" |
| 18 | + "github.com/replicatedhq/troubleshoot/pkg/docrewrite" |
| 19 | + "github.com/replicatedhq/troubleshoot/pkg/k8sutil" |
| 20 | + "github.com/replicatedhq/troubleshoot/pkg/specs" |
| 21 | + "github.com/replicatedhq/troubleshoot/pkg/supportbundle" |
| 22 | + "github.com/spf13/viper" |
| 23 | + "k8s.io/apimachinery/pkg/labels" |
| 24 | +) |
| 25 | + |
| 26 | +const ( |
| 27 | + defaultTimeout = 30 * time.Second |
| 28 | +) |
| 29 | + |
| 30 | +func runCollect(v *viper.Viper, arg string) error { |
| 31 | + go func() { |
| 32 | + signalChan := make(chan os.Signal, 1) |
| 33 | + signal.Notify(signalChan, os.Interrupt) |
| 34 | + <-signalChan |
| 35 | + os.Exit(0) |
| 36 | + }() |
| 37 | + |
| 38 | + var collectorContent []byte |
| 39 | + var err error |
| 40 | + if strings.HasPrefix(arg, "secret/") { |
| 41 | + // format secret/namespace-name/secret-name |
| 42 | + pathParts := strings.Split(arg, "/") |
| 43 | + if len(pathParts) != 3 { |
| 44 | + return errors.Errorf("path %s must have 3 components", arg) |
| 45 | + } |
| 46 | + |
| 47 | + spec, err := specs.LoadFromSecret(pathParts[1], pathParts[2], "collect-spec") |
| 48 | + if err != nil { |
| 49 | + return errors.Wrap(err, "failed to get spec from secret") |
| 50 | + } |
| 51 | + |
| 52 | + collectorContent = spec |
| 53 | + } else if _, err = os.Stat(arg); err == nil { |
| 54 | + b, err := ioutil.ReadFile(arg) |
| 55 | + if err != nil { |
| 56 | + return err |
| 57 | + } |
| 58 | + |
| 59 | + collectorContent = b |
| 60 | + } else { |
| 61 | + if !util.IsURL(arg) { |
| 62 | + return fmt.Errorf("%s is not a URL and was not found (err %s)", arg, err) |
| 63 | + } |
| 64 | + |
| 65 | + req, err := http.NewRequest("GET", arg, nil) |
| 66 | + if err != nil { |
| 67 | + return err |
| 68 | + } |
| 69 | + req.Header.Set("User-Agent", "Replicated_Collect/v1beta2") |
| 70 | + resp, err := http.DefaultClient.Do(req) |
| 71 | + if err != nil { |
| 72 | + return err |
| 73 | + } |
| 74 | + defer resp.Body.Close() |
| 75 | + |
| 76 | + body, err := ioutil.ReadAll(resp.Body) |
| 77 | + if err != nil { |
| 78 | + return err |
| 79 | + } |
| 80 | + |
| 81 | + collectorContent = body |
| 82 | + } |
| 83 | + |
| 84 | + collectorContent, err = docrewrite.ConvertToV1Beta2(collectorContent) |
| 85 | + if err != nil { |
| 86 | + return errors.Wrap(err, "failed to convert to v1beta2") |
| 87 | + } |
| 88 | + |
| 89 | + multidocs := strings.Split(string(collectorContent), "\n---\n") |
| 90 | + |
| 91 | + troubleshootclientsetscheme.AddToScheme(scheme.Scheme) |
| 92 | + decode := scheme.Codecs.UniversalDeserializer().Decode |
| 93 | + |
| 94 | + additionalRedactors := &troubleshootv1beta2.Redactor{} |
| 95 | + for idx, redactor := range v.GetStringSlice("redactors") { |
| 96 | + redactorObj, err := supportbundle.GetRedactorFromURI(redactor) |
| 97 | + if err != nil { |
| 98 | + return errors.Wrapf(err, "failed to get redactor spec %s, #%d", redactor, idx) |
| 99 | + } |
| 100 | + |
| 101 | + if redactorObj != nil { |
| 102 | + additionalRedactors.Spec.Redactors = append(additionalRedactors.Spec.Redactors, redactorObj.Spec.Redactors...) |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + for i, additionalDoc := range multidocs { |
| 107 | + if i == 0 { |
| 108 | + continue |
| 109 | + } |
| 110 | + additionalDoc, err := docrewrite.ConvertToV1Beta2([]byte(additionalDoc)) |
| 111 | + if err != nil { |
| 112 | + return errors.Wrap(err, "failed to convert to v1beta2") |
| 113 | + } |
| 114 | + obj, _, err := decode(additionalDoc, nil, nil) |
| 115 | + if err != nil { |
| 116 | + return errors.Wrapf(err, "failed to parse additional doc %d", i) |
| 117 | + } |
| 118 | + multidocRedactors, ok := obj.(*troubleshootv1beta2.Redactor) |
| 119 | + if !ok { |
| 120 | + continue |
| 121 | + } |
| 122 | + additionalRedactors.Spec.Redactors = append(additionalRedactors.Spec.Redactors, multidocRedactors.Spec.Redactors...) |
| 123 | + } |
| 124 | + |
| 125 | + // make sure we don't block any senders |
| 126 | + progressCh := make(chan interface{}) |
| 127 | + defer close(progressCh) |
| 128 | + go func() { |
| 129 | + for range progressCh { |
| 130 | + } |
| 131 | + }() |
| 132 | + |
| 133 | + restConfig, err := k8sutil.GetRESTConfig() |
| 134 | + if err != nil { |
| 135 | + return errors.Wrap(err, "failed to convert kube flags to rest config") |
| 136 | + } |
| 137 | + |
| 138 | + labelSelector, err := labels.Parse(v.GetString("selector")) |
| 139 | + if err != nil { |
| 140 | + return errors.Wrap(err, "unable to parse selector") |
| 141 | + } |
| 142 | + |
| 143 | + namespace := v.GetString("namespace") |
| 144 | + if namespace == "" { |
| 145 | + namespace = "default" |
| 146 | + } |
| 147 | + |
| 148 | + timeout := v.GetDuration("request-timeout") |
| 149 | + if timeout == 0 { |
| 150 | + timeout = defaultTimeout |
| 151 | + } |
| 152 | + |
| 153 | + createOpts := collect.CollectorRunOpts{ |
| 154 | + CollectWithoutPermissions: v.GetBool("collect-without-permissions"), |
| 155 | + KubernetesRestConfig: restConfig, |
| 156 | + Image: v.GetString("collector-image"), |
| 157 | + PullPolicy: v.GetString("collector-pullpolicy"), |
| 158 | + LabelSelector: labelSelector.String(), |
| 159 | + Namespace: namespace, |
| 160 | + Timeout: timeout, |
| 161 | + ProgressChan: progressCh, |
| 162 | + } |
| 163 | + |
| 164 | + // we only support HostCollector or RemoteCollector kinds. |
| 165 | + hostCollector, err := collect.ParseHostCollectorFromDoc([]byte(multidocs[0])) |
| 166 | + if err == nil { |
| 167 | + results, err := collect.CollectHost(hostCollector, additionalRedactors, createOpts) |
| 168 | + if err != nil { |
| 169 | + return errors.Wrap(err, "failed to collect from host") |
| 170 | + } |
| 171 | + return showHostStdoutResults(v.GetString("format"), hostCollector.Name, results) |
| 172 | + } |
| 173 | + |
| 174 | + remoteCollector, err := collect.ParseRemoteCollectorFromDoc([]byte(multidocs[0])) |
| 175 | + if err == nil { |
| 176 | + results, err := collect.CollectRemote(remoteCollector, additionalRedactors, createOpts) |
| 177 | + if err != nil { |
| 178 | + return errors.Wrap(err, "failed to collect from remote host(s)") |
| 179 | + } |
| 180 | + return showRemoteStdoutResults(v.GetString("format"), remoteCollector.Name, results) |
| 181 | + } |
| 182 | + |
| 183 | + return errors.New("failed to parse hostCollector or remoteCollector") |
| 184 | +} |
0 commit comments