Skip to content

Commit

Permalink
Moor redact
Browse files Browse the repository at this point in the history
  • Loading branch information
marccampbell committed Jul 18, 2019
1 parent 7416987 commit 785c3e9
Show file tree
Hide file tree
Showing 16 changed files with 299 additions and 97 deletions.
23 changes: 23 additions & 0 deletions config/crds/troubleshoot.replicated.com_collectors.yaml
Expand Up @@ -415,6 +415,29 @@ spec:
required:
- selector
type: object
run:
properties:
args:
items:
type: string
type: array
command:
items:
type: string
type: array
image:
type: string
name:
type: string
namespace:
type: string
timeout:
type: string
required:
- name
- namespace
- image
type: object
secret:
properties:
includeValue:
Expand Down
23 changes: 23 additions & 0 deletions config/crds/troubleshoot.replicated.com_preflights.yaml
Expand Up @@ -637,6 +637,29 @@ spec:
required:
- selector
type: object
run:
properties:
args:
items:
type: string
type: array
command:
items:
type: string
type: array
image:
type: string
name:
type: string
namespace:
type: string
timeout:
type: string
required:
- name
- namespace
- image
type: object
secret:
properties:
includeValue:
Expand Down
30 changes: 30 additions & 0 deletions config/crds/zz_generated.deepcopy.go
Expand Up @@ -348,6 +348,11 @@ func (in *Collect) DeepCopyInto(out *Collect) {
*out = new(Logs)
(*in).DeepCopyInto(*out)
}
if in.Run != nil {
in, out := &in.Run, &out.Run
*out = new(Run)
(*in).DeepCopyInto(*out)
}
}

// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Collect.
Expand Down Expand Up @@ -934,6 +939,31 @@ func (in *PreflightStatus) DeepCopy() *PreflightStatus {
return out
}

// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *Run) DeepCopyInto(out *Run) {
*out = *in
if in.Command != nil {
in, out := &in.Command, &out.Command
*out = make([]string, len(*in))
copy(*out, *in)
}
if in.Args != nil {
in, out := &in.Args, &out.Args
*out = make([]string, len(*in))
copy(*out, *in)
}
}

// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Run.
func (in *Run) DeepCopy() *Run {
if in == nil {
return nil
}
out := new(Run)
in.DeepCopyInto(out)
return out
}

// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *Secret) DeepCopyInto(out *Secret) {
*out = *in
Expand Down
19 changes: 13 additions & 6 deletions config/samples/troubleshoot_v1beta1_collector.yaml
Expand Up @@ -9,10 +9,17 @@ spec:
# name: illmannered-cricket-mysql
# namespace: default
# key: mysql-password
- logs:
selector:
- name=nginx-ingress-microk8s
# - logs:
# selector:
# - name=nginx-ingress-microk8s
# namespace: default
# limits:
# maxAge: 30d
# maxLines: 10000
- run:
name: ping-google
namespace: default
limits:
maxAge: 30d
maxLines: 10000
image: ubuntu:latest
command: ["ping"]
args: ["www.google.com"]
timeout: 5s
10 changes: 10 additions & 0 deletions pkg/apis/troubleshoot/v1beta1/collector_shared.go
Expand Up @@ -24,9 +24,19 @@ type Logs struct {
Limits *LogLimits `json:"limits,omitempty" yaml:"omitempty"`
}

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"`
}

type Collect struct {
ClusterInfo *ClusterInfo `json:"clusterInfo,omitempty" yaml:"clusterInfo,omitempty"`
ClusterResources *ClusterResources `json:"clusterResources,omitempty" yaml:"clusterResources,omitempty"`
Secret *Secret `json:"secret,omitempty" yaml:"secret,omitempty"`
Logs *Logs `json:"logs,omitempty" yaml:"logs,omitempty"`
Run *Run `json:"run,omitempty" yaml:"run,omitempty"`
}
30 changes: 30 additions & 0 deletions pkg/apis/troubleshoot/v1beta1/zz_generated.deepcopy.go
Expand Up @@ -364,6 +364,11 @@ func (in *Collect) DeepCopyInto(out *Collect) {
*out = new(Logs)
(*in).DeepCopyInto(*out)
}
if in.Run != nil {
in, out := &in.Run, &out.Run
*out = new(Run)
(*in).DeepCopyInto(*out)
}
}

// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Collect.
Expand Down Expand Up @@ -950,6 +955,31 @@ func (in *PreflightStatus) DeepCopy() *PreflightStatus {
return out
}

// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *Run) DeepCopyInto(out *Run) {
*out = *in
if in.Command != nil {
in, out := &in.Command, &out.Command
*out = make([]string, len(*in))
copy(*out, *in)
}
if in.Args != nil {
in, out := &in.Args, &out.Args
*out = make([]string, len(*in))
copy(*out, *in)
}
}

// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Run.
func (in *Run) DeepCopy() *Run {
if in == nil {
return nil
}
out := new(Run)
in.DeepCopyInto(out)
return out
}

// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *Secret) DeepCopyInto(out *Secret) {
*out = *in
Expand Down
12 changes: 0 additions & 12 deletions pkg/collect/cluster_resources.go
Expand Up @@ -270,15 +270,3 @@ func (c *ClusterResourcesOutput) Redact() (*ClusterResourcesOutput, error) {
CustomResourceDefinitions: crds,
}, nil
}

func redactMap(input map[string][]byte) (map[string][]byte, error) {
result := make(map[string][]byte)
for k, v := range input {
redacted, err := redact.Redact(v)
if err != nil {
return nil, err
}
result[k] = redacted
}
return result, nil
}
7 changes: 5 additions & 2 deletions pkg/collect/collector.go
Expand Up @@ -25,10 +25,13 @@ func (c *Collector) RunCollectorSync() error {
return ClusterResources(c.Redact)
}
if collect.Secret != nil {
return Secret(collect.Secret)
return Secret(collect.Secret, c.Redact)
}
if collect.Logs != nil {
return Logs(collect.Logs)
return Logs(collect.Logs, c.Redact)
}
if collect.Run != nil {
return Run(collect.Run, c.Redact)
}

return errors.New("no spec found to run")
Expand Down
22 changes: 20 additions & 2 deletions pkg/collect/logs.go
Expand Up @@ -19,7 +19,7 @@ type LogsOutput struct {
PodLogs map[string][]byte `json:"logs/,omitempty"`
}

func Logs(logsCollector *troubleshootv1beta1.Logs) error {
func Logs(logsCollector *troubleshootv1beta1.Logs, redact bool) error {
cfg, err := config.GetConfig()
if err != nil {
return err
Expand All @@ -35,7 +35,7 @@ func Logs(logsCollector *troubleshootv1beta1.Logs) error {
return err
}

logsOutput := LogsOutput{
logsOutput := &LogsOutput{
PodLogs: make(map[string][]byte),
}
for _, pod := range pods {
Expand All @@ -49,6 +49,13 @@ func Logs(logsCollector *troubleshootv1beta1.Logs) error {
}
}

if redact {
logsOutput, err = logsOutput.Redact()
if err != nil {
return err
}
}

b, err := json.MarshalIndent(logsOutput, "", " ")
if err != nil {
return err
Expand Down Expand Up @@ -114,3 +121,14 @@ func getPodLogs(client *kubernetes.Clientset, pod corev1.Pod, limits *troublesho
fmt.Sprintf("%s/%s.txt", pod.Namespace, pod.Name): buf.Bytes(),
}, nil
}

func (l *LogsOutput) Redact() (*LogsOutput, error) {
podLogs, err := redactMap(l.PodLogs)
if err != nil {
return nil, err
}

return &LogsOutput{
PodLogs: podLogs,
}, nil
}
17 changes: 17 additions & 0 deletions pkg/collect/redact.go
@@ -0,0 +1,17 @@
package collect

import (
"github.com/replicatedhq/troubleshoot/pkg/redact"
)

func redactMap(input map[string][]byte) (map[string][]byte, error) {
result := make(map[string][]byte)
for k, v := range input {
redacted, err := redact.Redact(v)
if err != nil {
return nil, err
}
result[k] = redacted
}
return result, nil
}
60 changes: 60 additions & 0 deletions pkg/collect/run.go
@@ -0,0 +1,60 @@
package collect

import (
// "bytes"
// "encoding/json"
// "fmt"
// "io"
// "strings"
// "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"
)

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)

return nil
}
23 changes: 3 additions & 20 deletions pkg/collect/runner.go
Expand Up @@ -30,7 +30,7 @@ func CreateCollector(client client.Client, scheme *runtime.Scheme, ownerRef meta
}

func createCollectorSpecConfigMap(client client.Client, scheme *runtime.Scheme, ownerRef metav1.Object, jobName string, jobNamespace string, collect *troubleshootv1beta1.Collect) (*corev1.ConfigMap, error) {
name := fmt.Sprintf("%s-%s", jobName, idForCollector(collect))
name := fmt.Sprintf("%s-%s", jobName, DeterministicIDForCollector(collect))
namespacedName := types.NamespacedName{
Name: name,
Namespace: jobNamespace,
Expand Down Expand Up @@ -76,7 +76,7 @@ func createCollectorSpecConfigMap(client client.Client, scheme *runtime.Scheme,
}

func createCollectorPod(client client.Client, scheme *runtime.Scheme, ownerRef metav1.Object, jobName string, jobNamespace string, jobType string, collect *troubleshootv1beta1.Collect, configMap *corev1.ConfigMap, image string, pullPolicy string) (*corev1.Pod, error) {
name := fmt.Sprintf("%s-%s", jobName, idForCollector(collect))
name := fmt.Sprintf("%s-%s", jobName, DeterministicIDForCollector(collect))

namespacedName := types.NamespacedName{
Name: name,
Expand Down Expand Up @@ -120,7 +120,7 @@ func createCollectorPod(client client.Client, scheme *runtime.Scheme, ownerRef m
{
Image: imageName,
ImagePullPolicy: imagePullPolicy,
Name: idForCollector(collect),
Name: DeterministicIDForCollector(collect),
Command: []string{"collector"},
Args: []string{
"run",
Expand Down Expand Up @@ -162,20 +162,3 @@ func createCollectorPod(client client.Client, scheme *runtime.Scheme, ownerRef m

return &pod, nil
}

func idForCollector(collector *troubleshootv1beta1.Collect) string {
if collector.ClusterInfo != nil {
return "cluster-info"
}
if collector.ClusterResources != nil {
return "cluster-resources"
}
if collector.Secret != nil {
return fmt.Sprintf("secret-%s%s", collector.Secret.Namespace, collector.Secret.Name)
}
if collector.Logs != nil {
randomString := "abcdef" // TODO
return fmt.Sprintf("logs-%s%s", collector.Logs.Namespace, randomString)
}
return ""
}

0 comments on commit 785c3e9

Please sign in to comment.