Skip to content

Commit

Permalink
run command
Browse files Browse the repository at this point in the history
  • Loading branch information
marccampbell committed Jul 19, 2019
1 parent 785c3e9 commit ae0ff14
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 53 deletions.
2 changes: 2 additions & 0 deletions config/crds/troubleshoot.replicated.com_collectors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,8 @@ spec:
type: array
image:
type: string
imagePullPolicy:
type: string
name:
type: string
namespace:
Expand Down
2 changes: 2 additions & 0 deletions config/crds/troubleshoot.replicated.com_preflights.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,8 @@ spec:
type: array
image:
type: string
imagePullPolicy:
type: string
name:
type: string
namespace:
Expand Down
4 changes: 2 additions & 2 deletions config/samples/troubleshoot_v1beta1_collector.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ spec:
- run:
name: ping-google
namespace: default
image: ubuntu:latest
image: flungo/netutils
command: ["ping"]
args: ["www.google.com"]
timeout: 5s
# timeout: 5s
13 changes: 7 additions & 6 deletions pkg/apis/troubleshoot/v1beta1/collector_shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ type Logs struct {
}

type Run struct {
Name string `json:"name" yaml:"name"`
Namespace string `json:"namespace" yaml:"namespace"`
Image string `json:"image" yaml:"image"`
Command []string `json:"command,omitempty" yaml:"command,omitempty"`
Args []string `json:"args,omitempty" yaml:"args,omitempty"`
Timeout string `json:"timeout,omitempty" yaml:"timeout,omitempty"`
Name string `json:"name" yaml:"name"`
Namespace string `json:"namespace" yaml:"namespace"`
Image string `json:"image" yaml:"image"`
Command []string `json:"command,omitempty" yaml:"command,omitempty"`
Args []string `json:"args,omitempty" yaml:"args,omitempty"`
Timeout string `json:"timeout,omitempty" yaml:"timeout,omitempty"`
ImagePullPolicy string `json:"imagePullPolicy,omitempty" yaml:"imagePullPolicy,omitempty"`
}

type Collect struct {
Expand Down
172 changes: 127 additions & 45 deletions pkg/collect/run.go
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
}

0 comments on commit ae0ff14

Please sign in to comment.