-
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
1 parent
785c3e9
commit ae0ff14
Showing
5 changed files
with
140 additions
and
53 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -427,6 +427,8 @@ spec: | |
type: array | ||
image: | ||
type: string | ||
imagePullPolicy: | ||
type: string | ||
name: | ||
type: string | ||
namespace: | ||
|
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 |
---|---|---|
|
@@ -649,6 +649,8 @@ spec: | |
type: array | ||
image: | ||
type: string | ||
imagePullPolicy: | ||
type: string | ||
name: | ||
type: string | ||
namespace: | ||
|
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 |
---|---|---|
@@ -1,60 +1,142 @@ | ||
package collect | ||
|
||
import ( | ||
// "bytes" | ||
// "encoding/json" | ||
// "fmt" | ||
// "io" | ||
// "strings" | ||
// "time" | ||
"encoding/json" | ||
"fmt" | ||
"time" | ||
|
||
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/client-go/kubernetes" | ||
// "sigs.k8s.io/controller-runtime/pkg/client/config" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes" | ||
"sigs.k8s.io/controller-runtime/pkg/client/config" | ||
) | ||
|
||
type RunOutput struct { | ||
PodLogs map[string][]byte `json:"run/,omitempty"` | ||
} | ||
|
||
func Run(runCollector *troubleshootv1beta1.Run, 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, logsCollector.Namespace, logsCollector.Selector) | ||
// if err != nil { | ||
// return err | ||
// } | ||
|
||
// logsOutput := LogsOutput{ | ||
// PodLogs: make(map[string][]byte), | ||
// } | ||
// for _, pod := range pods { | ||
// podLogs, err := getPodLogs(client, pod, logsCollector.Limits) | ||
// if err != nil { | ||
// return err | ||
// } | ||
|
||
// for k, v := range podLogs { | ||
// logsOutput.PodLogs[k] = v | ||
// } | ||
// } | ||
|
||
// b, err := json.MarshalIndent(logsOutput, "", " ") | ||
// if err != nil { | ||
// return err | ||
// } | ||
|
||
// fmt.Printf("%s\n", b) | ||
cfg, err := config.GetConfig() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
client, err := kubernetes.NewForConfig(cfg) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
pod, err := runPod(client, runCollector) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
runOutput := &RunOutput{ | ||
PodLogs: make(map[string][]byte), | ||
} | ||
|
||
now := time.Now() | ||
then := now.Add(time.Duration(20 * time.Second)) | ||
|
||
if runCollector.Timeout != "" { | ||
parsedDuration, err := time.ParseDuration(runCollector.Timeout) | ||
if err != nil { | ||
fmt.Printf("unable to parse time duration %s\n", runCollector.Timeout) | ||
} else { | ||
then = now.Add(parsedDuration) | ||
} | ||
} | ||
|
||
for { | ||
if time.Now().After(then) { | ||
break | ||
} | ||
|
||
time.Sleep(time.Second) | ||
} | ||
|
||
limits := troubleshootv1beta1.LogLimits{ | ||
MaxLines: 10000, | ||
} | ||
podLogs, err := getPodLogs(client, *pod, &limits) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for k, v := range podLogs { | ||
runOutput.PodLogs[k] = v | ||
} | ||
|
||
if err := client.CoreV1().Pods(pod.Namespace).Delete(pod.Name, &metav1.DeleteOptions{}); err != nil { | ||
return err | ||
} | ||
|
||
if redact { | ||
runOutput, err = runOutput.Redact() | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
b, err := json.MarshalIndent(runOutput, "", " ") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Printf("%s\n", b) | ||
|
||
return nil | ||
} | ||
|
||
func runPod(client *kubernetes.Clientset, runCollector *troubleshootv1beta1.Run) (*corev1.Pod, error) { | ||
podLabels := make(map[string]string) | ||
podLabels["troubleshoot-role"] = "run-collector" | ||
|
||
pullPolicy := corev1.PullIfNotPresent | ||
if runCollector.ImagePullPolicy != "" { | ||
pullPolicy = corev1.PullPolicy(runCollector.ImagePullPolicy) | ||
} | ||
|
||
pod := corev1.Pod{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: runCollector.Name, | ||
Namespace: runCollector.Namespace, | ||
Labels: podLabels, | ||
}, | ||
TypeMeta: metav1.TypeMeta{ | ||
APIVersion: "v1", | ||
Kind: "Pod", | ||
}, | ||
Spec: corev1.PodSpec{ | ||
RestartPolicy: corev1.RestartPolicyNever, | ||
Containers: []corev1.Container{ | ||
{ | ||
Image: runCollector.Image, | ||
ImagePullPolicy: pullPolicy, | ||
Name: "collector", | ||
Command: runCollector.Command, | ||
Args: runCollector.Args, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
created, err := client.CoreV1().Pods(runCollector.Namespace).Create(&pod) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return created, nil | ||
} | ||
|
||
func (r *RunOutput) Redact() (*RunOutput, error) { | ||
podLogs, err := redactMap(r.PodLogs) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &RunOutput{ | ||
PodLogs: podLogs, | ||
}, nil | ||
} |