-
Notifications
You must be signed in to change notification settings - Fork 92
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Copy collector enhanced #237
Changes from 1 commit
f77f09f
07bc145
829f311
dba0209
5bfdffd
d5ab4e3
03c3f07
85976fc
425344a
9750a79
6cdc808
eec86f5
3a8126e
2c10570
ffc8154
d59e311
257e246
340e0d4
ca6e3e4
af004be
0225123
880f435
58c6229
f73aff0
b4a6a86
6c222eb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ import ( | |
"context" | ||
"fmt" | ||
"path/filepath" | ||
"strings" | ||
|
||
troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1" | ||
corev1 "k8s.io/api/core/v1" | ||
|
@@ -14,6 +15,7 @@ import ( | |
) | ||
|
||
func Copy(c *Collector, copyCollector *troubleshootv1beta1.Copy) (map[string][]byte, error) { | ||
|
||
client, err := kubernetes.NewForConfig(c.ClientConfig) | ||
if err != nil { | ||
return nil, err | ||
|
@@ -56,12 +58,13 @@ func Copy(c *Collector, copyCollector *troubleshootv1beta1.Copy) (map[string][]b | |
} | ||
|
||
func copyFiles(c *Collector, client *kubernetes.Clientset, pod corev1.Pod, copyCollector *troubleshootv1beta1.Copy) (map[string][]byte, map[string]string) { | ||
files := make(map[string][]byte) | ||
container := pod.Spec.Containers[0].Name | ||
if copyCollector.ContainerName != "" { | ||
container = copyCollector.ContainerName | ||
} | ||
|
||
command := []string{"cat", copyCollector.ContainerPath} | ||
command := []string{"find", copyCollector.ContainerPath, "-type", "f"} | ||
|
||
req := client.CoreV1().RESTClient().Post().Resource("pods").Name(pod.Name).Namespace(pod.Namespace).SubResource("exec") | ||
scheme := runtime.NewScheme() | ||
|
@@ -96,6 +99,9 @@ func copyFiles(c *Collector, client *kubernetes.Clientset, pod corev1.Pod, copyC | |
Stderr: &stderr, | ||
Tty: false, | ||
}) | ||
|
||
filepaths := strings.Split(output.String(), "\n") | ||
|
||
if err != nil { | ||
errors := map[string]string{ | ||
filepath.Join(copyCollector.ContainerPath, "error"): err.Error(), | ||
|
@@ -109,9 +115,59 @@ func copyFiles(c *Collector, client *kubernetes.Clientset, pod corev1.Pod, copyC | |
return nil, errors | ||
} | ||
|
||
return map[string][]byte{ | ||
copyCollector.ContainerPath: output.Bytes(), | ||
}, nil | ||
for _, file := range filepaths { | ||
if file != "" { | ||
command := []string{"cat", file} | ||
|
||
req := client.CoreV1().RESTClient().Post().Resource("pods").Name(pod.Name).Namespace(pod.Namespace).SubResource("exec") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is unnecessarily verbose.
Is cleaner and how we use the clientset There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @CodeLingoBot capture unnecessary use of client-go restClient There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rule captured at https://dash.codelingo.io/repos/replicatedhq/troubleshoot/rules/1393 CodeLingoBot HelpWhen responding to PR-level comments, I understand the following commands:
When responding to comments on specific lines of code, I understand the following commands:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi Marc! I didn't have much contact with the client set yet, so I left that line as it was. The way you suggest returns another type of variable and requires another type of variable too instead of pod.Name. I didn't investigate it further because I am sure I will have to deal with the clientset in later PR's, and I didn't wanted to delay the copy collector. |
||
scheme := runtime.NewScheme() | ||
if err := corev1.AddToScheme(scheme); err != nil { | ||
return nil, map[string]string{ | ||
filepath.Join(copyCollector.ContainerPath, "error"): err.Error(), | ||
} | ||
} | ||
parameterCodec := runtime.NewParameterCodec(scheme) | ||
req.VersionedParams(&corev1.PodExecOptions{ | ||
Command: command, | ||
Container: container, | ||
Stdin: true, | ||
Stdout: false, | ||
Stderr: true, | ||
TTY: false, | ||
}, parameterCodec) | ||
|
||
exec, err := remotecommand.NewSPDYExecutor(c.ClientConfig, "POST", req.URL()) | ||
if err != nil { | ||
return nil, map[string]string{ | ||
filepath.Join(copyCollector.ContainerPath, "error"): err.Error(), | ||
} | ||
} | ||
|
||
output := new(bytes.Buffer) | ||
var stderr bytes.Buffer | ||
err = exec.Stream(remotecommand.StreamOptions{ | ||
Stdin: nil, | ||
Stdout: output, | ||
Stderr: &stderr, | ||
Tty: false, | ||
}) | ||
files[file] = output.Bytes() | ||
|
||
if err != nil { | ||
errors := map[string]string{ | ||
filepath.Join(copyCollector.ContainerPath, "error"): err.Error(), | ||
} | ||
if s := output.String(); len(s) > 0 { | ||
errors[filepath.Join(copyCollector.ContainerPath, "stdout")] = s | ||
} | ||
if s := stderr.String(); len(s) > 0 { | ||
errors[filepath.Join(copyCollector.ContainerPath, "stderr")] = s | ||
} | ||
return nil, errors | ||
} | ||
} | ||
} | ||
return files, nil | ||
} | ||
|
||
func getCopyErrosFileName(copyCollector *troubleshootv1beta1.Copy) string { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would be better written has
The code is less conditionally indented and more readable this way
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@CodeLingoBot capture continue in conditional in loop
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rule captured at https://dash.codelingo.io/repos/replicatedhq/troubleshoot/rules/1392
CodeLingoBot Help
When responding to PR-level comments, I understand the following commands:
capture
to capture the context for a fresh rule.review
to trigger a fresh review.When responding to comments on specific lines of code, I understand the following commands:
capture
to capture the context for a fresh rule.review
to trigger a fresh review.ignore
to ignore the parent review comment.unignore
to stop ignoring a parent review comment.