Skip to content

Commit d016e32

Browse files
committed
add global and per-collector redactors
add redact type, and begin wiring global redactors use per-collector redactors add a test of the 'data' collector and redaction handle literal string replacements remove redundant types and redact calls add proper redactor type, foundations of global redactors accept global redactors from the CLI, include sample redaction spec
1 parent f06cd9e commit d016e32

34 files changed

+1245
-224
lines changed

cmd/troubleshoot/cli/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ from a server that can be used to assist when troubleshooting a server.`,
4343
cmd.Flags().String("collectors", "", "name of the collectors to use")
4444
cmd.Flags().String("image", "", "the full name of the collector image to use")
4545
cmd.Flags().String("pullpolicy", "", "the pull policy of the collector image")
46+
cmd.Flags().String("redactors", "", "name of the additional redactors to use")
4647
cmd.Flags().Bool("redact", true, "enable/disable default redactions")
4748
cmd.Flags().Bool("collect-without-permissions", false, "always run troubleshoot collectors even if some require permissions that troubleshoot does not have")
4849

cmd/troubleshoot/cli/run.go

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,23 @@ func runTroubleshoot(v *viper.Viper, arg string) error {
5656

5757
collector := obj.(*troubleshootv1beta1.Collector)
5858

59+
var additionalRedactors *troubleshootv1beta1.Redactor
60+
if v.GetString("redactors") != "" {
61+
redactorContent, err := loadSpec(v, v.GetString("redactors"))
62+
if err != nil {
63+
return errors.Wrap(err, "failed to load redactor spec")
64+
}
65+
obj, _, err := decode([]byte(redactorContent), nil, nil)
66+
if err != nil {
67+
return errors.Wrapf(err, "failed to parse redactors %s", v.GetString("redactors"))
68+
}
69+
var ok bool
70+
additionalRedactors, ok = obj.(*troubleshootv1beta1.Redactor)
71+
if !ok {
72+
return fmt.Errorf("%s is not a troubleshootv1beta1 redactor type", v.GetString("redactors"))
73+
}
74+
}
75+
5976
s := spin.New()
6077
finishedCh := make(chan bool, 1)
6178
progressChan := make(chan interface{}, 0) // non-zero buffer can result in missed messages
@@ -87,7 +104,7 @@ func runTroubleshoot(v *viper.Viper, arg string) error {
87104
close(finishedCh)
88105
}()
89106

90-
archivePath, err := runCollectors(v, *collector, progressChan)
107+
archivePath, err := runCollectors(v, *collector, additionalRedactors, progressChan)
91108
if err != nil {
92109
return errors.Wrap(err, "run collectors")
93110
}
@@ -193,7 +210,7 @@ func canTryInsecure(v *viper.Viper) bool {
193210
return true
194211
}
195212

196-
func runCollectors(v *viper.Viper, collector troubleshootv1beta1.Collector, progressChan chan interface{}) (string, error) {
213+
func runCollectors(v *viper.Viper, collector troubleshootv1beta1.Collector, additionalRedactors *troubleshootv1beta1.Redactor, progressChan chan interface{}) (string, error) {
197214
bundlePath, err := ioutil.TempDir("", "troubleshoot")
198215
if err != nil {
199216
return "", errors.Wrap(err, "create temp dir")
@@ -241,6 +258,11 @@ func runCollectors(v *viper.Viper, collector troubleshootv1beta1.Collector, prog
241258
return "", errors.New("insufficient permissions to run all collectors")
242259
}
243260

261+
globalRedactors := []*troubleshootv1beta1.Redact{}
262+
if additionalRedactors != nil {
263+
globalRedactors = additionalRedactors.Spec.Redactors
264+
}
265+
244266
// Run preflights collectors synchronously
245267
for _, collector := range collectors {
246268
if len(collector.RBACErrors) > 0 {
@@ -253,7 +275,7 @@ func runCollectors(v *viper.Viper, collector troubleshootv1beta1.Collector, prog
253275

254276
progressChan <- collector.GetDisplayName()
255277

256-
result, err := collector.RunCollectorSync()
278+
result, err := collector.RunCollectorSync(globalRedactors)
257279
if err != nil {
258280
progressChan <- fmt.Errorf("failed to run collector %q: %v", collector.GetDisplayName(), err)
259281
continue

pkg/apis/troubleshoot/v1beta1/collector_shared.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import (
99
)
1010

1111
type CollectorMeta struct {
12-
CollectorName string `json:"collectorName,omitempty" yaml:"collectorName,omitempty"`
12+
CollectorName string `json:"collectorName,omitempty" yaml:"collectorName,omitempty"`
13+
Redactors []*Redact `json:"redactors,omitempty" yaml:"redactors,omitempty"`
1314
// +optional
1415
Exclude multitype.BoolOrString `json:"exclude,omitempty" yaml:"exclude,omitempty"`
1516
}

pkg/apis/troubleshoot/v1beta1/collector_types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ type AfterCollection struct {
3434
type CollectorSpec struct {
3535
Collectors []*Collect `json:"collectors,omitempty" yaml:"collectors,omitempty"`
3636
AfterCollection []*AfterCollection `json:"afterCollection,omitempty" yaml:"afterCollection,omitempty"`
37+
GlobalRedactors []*Redact `json:"globalRedactors,omitempty" yaml:"globalRedactors,omitempty"`
3738
}
3839

3940
// CollectorStatus defines the observed state of Collector
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package v1beta1
2+
3+
type Redact struct {
4+
Name string `json:"name,omitempty" yaml:"name,omitempty"`
5+
File string `json:"file,omitempty" yaml:"file,omitempty"`
6+
Files []string `json:"files,omitempty" yaml:"files,omitempty"`
7+
Values []string `json:"values,omitempty" yaml:"values,omitempty"`
8+
Regex []string `json:"regex,omitempty" yaml:"regex,omitempty"`
9+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
Copyright 2019 Replicated, Inc..
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1beta1
18+
19+
import (
20+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
21+
)
22+
23+
// RedactorSpec defines the desired state of Redactor
24+
type RedactorSpec struct {
25+
Redactors []*Redact `json:"redacts,omitempty"`
26+
}
27+
28+
// RedactorStatus defines the observed state of Redactor
29+
type RedactorStatus struct {
30+
}
31+
32+
// +genclient
33+
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
34+
35+
// Redactor is the Schema for the redaction API
36+
// +k8s:openapi-gen=true
37+
type Redactor struct {
38+
metav1.TypeMeta `json:",inline"`
39+
metav1.ObjectMeta `json:"metadata,omitempty"`
40+
41+
Spec RedactorSpec `json:"spec,omitempty"`
42+
Status RedactorStatus `json:"status,omitempty"`
43+
}
44+
45+
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
46+
47+
// RedactorList contains a list of Redactor
48+
type RedactorList struct {
49+
metav1.TypeMeta `json:",inline"`
50+
metav1.ListMeta `json:"metadata,omitempty"`
51+
Items []Redactor `json:"items"`
52+
}
53+
54+
func init() {
55+
SchemeBuilder.Register(&Redactor{}, &RedactorList{})
56+
}

0 commit comments

Comments
 (0)