Skip to content

Commit fb2f028

Browse files
committed
add methods to get and clear redactions
1 parent d37ace6 commit fb2f028

File tree

6 files changed

+35
-13
lines changed

6 files changed

+35
-13
lines changed

pkg/apis/troubleshoot/v1beta1/collector_types.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ import (
2121
)
2222

2323
type ResultRequest struct {
24-
URI string `json:"uri" yaml:"uri"`
25-
Method string `json:"method" yaml:"method"`
24+
URI string `json:"uri" yaml:"uri"`
25+
Method string `json:"method" yaml:"method"`
26+
RedactURI string `json:"redactUri" yaml:"redactUri"` // the URI to POST redaction reports to
2627
}
2728

2829
type AfterCollection struct {

pkg/redact/literal.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func (r literalRedactor) Redact(input io.Reader) io.Reader {
5353
}
5454

5555
if clean != line {
56-
go addRedaction(Redaction{
56+
addRedaction(Redaction{
5757
RedactorName: r.redactName,
5858
CharactersRemoved: len(line) - len(clean),
5959
Line: lineNum,

pkg/redact/multi_line.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func (r *MultiLineRedactor) Redact(input io.Reader) io.Reader {
7070

7171
// if clean is not equal to line2, a redaction was performed
7272
if clean != line2 {
73-
go addRedaction(Redaction{
73+
addRedaction(Redaction{
7474
RedactorName: r.redactName,
7575
CharactersRemoved: len(line2) - len(clean),
7676
Line: lineNum,

pkg/redact/redact.go

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const (
2020

2121
var allRedactions RedactionList
2222
var redactionListMut sync.Mutex
23+
var pendingRedactions sync.WaitGroup
2324

2425
func init() {
2526
allRedactions = RedactionList{
@@ -45,13 +46,6 @@ type Redaction struct {
4546
File string
4647
}
4748

48-
func addRedaction(redaction Redaction) {
49-
redactionListMut.Lock()
50-
defer redactionListMut.Unlock()
51-
allRedactions.ByRedactor[redaction.RedactorName] = append(allRedactions.ByRedactor[redaction.RedactorName], redaction)
52-
allRedactions.ByFile[redaction.File] = append(allRedactions.ByFile[redaction.File], redaction)
53-
}
54-
5549
func Redact(input []byte, path string, additionalRedactors []*troubleshootv1beta1.Redact) ([]byte, error) {
5650
redactors, err := getRedactors(path)
5751
if err != nil {
@@ -77,6 +71,22 @@ func Redact(input []byte, path string, additionalRedactors []*troubleshootv1beta
7771
return redacted, nil
7872
}
7973

74+
func GetRedactionList() RedactionList {
75+
pendingRedactions.Wait()
76+
redactionListMut.Lock()
77+
defer redactionListMut.Unlock()
78+
return allRedactions
79+
}
80+
81+
func ResetRedactionList() {
82+
redactionListMut.Lock()
83+
defer redactionListMut.Unlock()
84+
allRedactions = RedactionList{
85+
ByRedactor: map[string][]Redaction{},
86+
ByFile: map[string][]Redaction{},
87+
}
88+
}
89+
8090
func buildAdditionalRedactors(path string, redacts []*troubleshootv1beta1.Redact) ([]Redactor, error) {
8191
additionalRedactors := []Redactor{}
8292
for i, redact := range redacts {
@@ -281,6 +291,17 @@ func readLine(r *bufio.Reader) (string, error) {
281291
return string(completeLine), nil
282292
}
283293

294+
func addRedaction(redaction Redaction) {
295+
pendingRedactions.Add(1)
296+
go func(redaction Redaction) {
297+
redactionListMut.Lock()
298+
defer redactionListMut.Unlock()
299+
defer pendingRedactions.Done()
300+
allRedactions.ByRedactor[redaction.RedactorName] = append(allRedactions.ByRedactor[redaction.RedactorName], redaction)
301+
allRedactions.ByFile[redaction.File] = append(allRedactions.ByFile[redaction.File], redaction)
302+
}(redaction)
303+
}
304+
284305
func redactorName(redactorNum, withinRedactorNum int, redactorName, redactorType, redactorLiteral string) string {
285306
if redactorName != "" {
286307
return fmt.Sprintf("%s-%d", redactorName, withinRedactorNum)

pkg/redact/single_line.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func (r *SingleLineRedactor) Redact(input io.Reader) io.Reader {
6262

6363
// if clean is not equal to line, a redaction was performed
6464
if clean != line {
65-
go addRedaction(Redaction{
65+
addRedaction(Redaction{
6666
RedactorName: r.redactName,
6767
CharactersRemoved: len(line) - len(clean),
6868
Line: lineNum,

pkg/redact/yaml.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func (r *YamlRedactor) Redact(input io.Reader) io.Reader {
6464
buf := bytes.NewBuffer(newBytes)
6565
buf.WriteTo(writer)
6666

67-
go addRedaction(Redaction{
67+
addRedaction(Redaction{
6868
RedactorName: r.redactName,
6969
CharactersRemoved: len(doc) - len(newBytes),
7070
Line: 0, // line 0 because we have no way to tell what line was impacted

0 commit comments

Comments
 (0)