-
Notifications
You must be signed in to change notification settings - Fork 94
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
9 changed files
with
303 additions
and
0 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,137 @@ | ||
package collect | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
// "time" | ||
"bytes" | ||
|
||
troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1" | ||
corev1 "k8s.io/api/core/v1" | ||
// metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/tools/remotecommand" | ||
"sigs.k8s.io/controller-runtime/pkg/client/config" | ||
) | ||
|
||
type ExecOutput struct { | ||
Results map[string][]byte `json:"exec/,omitempty"` | ||
} | ||
|
||
type execResult struct { | ||
Stdout string `json:"stdout,omitempty"` | ||
Stderr string `json:"stderr,omitempty"` | ||
Error error `json:"error,omitempty"` | ||
} | ||
|
||
func Exec(execCollector *troubleshootv1beta1.Exec, redact bool) error { | ||
cfg, err := config.GetConfig() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
client, err := kubernetes.NewForConfig(cfg) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
pods, err := listPodsInSelectors(client, execCollector.Namespace, execCollector.Selector) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
execOutput := &ExecOutput{ | ||
Results: make(map[string][]byte), | ||
} | ||
|
||
for _, pod := range pods { | ||
output, err := getExecOutputs(client, pod, execCollector, redact) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
b, err := json.MarshalIndent(output, "", " ") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
execOutput.Results[fmt.Sprintf("%s/%s/%s", pod.Namespace, pod.Name, execCollector.Name)] = b | ||
} | ||
|
||
if redact { | ||
execOutput, err = execOutput.Redact() | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
b, err := json.MarshalIndent(execOutput, "", " ") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Printf("%s\n", b) | ||
|
||
return nil | ||
} | ||
|
||
func getExecOutputs(client *kubernetes.Clientset, pod corev1.Pod, execCollector *troubleshootv1beta1.Exec, doRedact bool) (*execResult, error) { | ||
cfg, err := config.GetConfig() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
container := pod.Spec.Containers[0].Name | ||
if execCollector.ContainerName != "" { | ||
container = execCollector.ContainerName | ||
} | ||
|
||
req := client.CoreV1().RESTClient().Post().Resource("pods").Name(pod.Name).Namespace(pod.Namespace).SubResource("exec") | ||
scheme := runtime.NewScheme() | ||
if err := corev1.AddToScheme(scheme); err != nil { | ||
return nil, err | ||
} | ||
|
||
parameterCodec := runtime.NewParameterCodec(scheme) | ||
req.VersionedParams(&corev1.PodExecOptions{ | ||
Command: append(execCollector.Command, execCollector.Args...), | ||
Container: container, | ||
Stdin: true, | ||
Stdout: false, | ||
Stderr: true, | ||
TTY: false, | ||
}, parameterCodec) | ||
|
||
exec, err := remotecommand.NewSPDYExecutor(cfg, "POST", req.URL()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
stdout := new(bytes.Buffer) | ||
stderr := new(bytes.Buffer) | ||
|
||
err = exec.Stream(remotecommand.StreamOptions{ | ||
Stdin: nil, | ||
Stdout: stdout, | ||
Stderr: stderr, | ||
Tty: false, | ||
}) | ||
|
||
return &execResult{ | ||
Stdout: stdout.String(), | ||
Stderr: stderr.String(), | ||
Error: err, | ||
}, nil | ||
} | ||
|
||
func (r *ExecOutput) Redact() (*ExecOutput, error) { | ||
results, err := redactMap(r.Results) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &ExecOutput{ | ||
Results: results, | ||
}, nil | ||
} |
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