Skip to content

Commit 62a9651

Browse files
committed
Fix too many open files
Factored validate file into own func, the defer was not being called on each iteration. This results in the tool keeping every KEP yaml open during execution.
1 parent 82b70b4 commit 62a9651

File tree

1 file changed

+37
-24
lines changed

1 file changed

+37
-24
lines changed

pkg/repo/validate.go

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -84,41 +84,22 @@ func (r *Repo) Validate() (
8484
return warnings, valErrMap, errors.New("must find more than zero KEPs")
8585
}
8686

87-
kepHandler, prrHandler := r.KEPHandler, r.PRRHandler
8887
prrDir := r.PRRApprovalPath
8988
logrus.Infof("PRR directory: %s", prrDir)
9089

9190
for _, filename := range files {
92-
kepFile, err := os.Open(filename)
93-
if err != nil {
94-
return warnings, valErrMap, errors.Wrapf(err, "could not open file %s", filename)
95-
}
96-
97-
defer kepFile.Close()
98-
99-
logrus.Infof("parsing %s", filename)
100-
kep, kepParseErr := kepHandler.Parse(kepFile)
101-
if kepParseErr != nil {
102-
return warnings, valErrMap, errors.Wrap(kepParseErr, "parsing KEP file")
103-
}
104-
kep.Filename = filename
105-
106-
// TODO: This shouldn't be required once we push the errors into the
107-
// parser struct
108-
if kep.Error != nil {
109-
return warnings, valErrMap, errors.Wrapf(kep.Error, "%v has an error", filename)
110-
}
111-
112-
err = kepval.ValidatePRR(kep, prrHandler, prrDir)
113-
if err != nil {
91+
if err := validateFile(r, prrDir, filename); err != nil {
92+
fvErr := &fatalValidationError{}
93+
if errors.As(err, fvErr) {
94+
return warnings, valErrMap, err
95+
}
11496
valErrMap[filename] = append(valErrMap[filename], err)
11597
}
11698
}
11799

118100
if len(valErrMap) > 0 {
119101
for filename, errs := range valErrMap {
120102
logrus.Infof("the following PRR validation errors occurred in %s:", filename)
121-
122103
for _, e := range errs {
123104
logrus.Infof("%v", e)
124105
}
@@ -127,3 +108,35 @@ func (r *Repo) Validate() (
127108

128109
return warnings, valErrMap, nil
129110
}
111+
112+
// fatalValidationError will short-circuit KEP parsing and return early.
113+
type fatalValidationError struct{ Err error }
114+
115+
func (e fatalValidationError) Error() string { return e.Err.Error() }
116+
func (e fatalValidationError) Unwrap() error { return e.Err }
117+
118+
// validateFile runs a validation and returns an error if validation fails.
119+
// fatalValidationError will be returned if further parsing should be stopped.
120+
func validateFile(r *Repo, prrDir, filename string) error {
121+
kepFile, err := os.Open(filename)
122+
if err != nil {
123+
return &fatalValidationError{Err: errors.Wrapf(err, "could not open file %s", filename)}
124+
}
125+
defer kepFile.Close()
126+
127+
logrus.Infof("parsing %s", filename)
128+
kepHandler, prrHandler := r.KEPHandler, r.PRRHandler
129+
kep, kepParseErr := kepHandler.Parse(kepFile)
130+
if kepParseErr != nil {
131+
return errors.Wrap(kepParseErr, "parsing KEP file")
132+
}
133+
kep.Filename = filename
134+
135+
// TODO: This shouldn't be required once we push the errors into the
136+
// parser struct
137+
if kep.Error != nil {
138+
return &fatalValidationError{Err: errors.Wrapf(kep.Error, "%v has an error", filename)}
139+
}
140+
141+
return kepval.ValidatePRR(kep, prrHandler, prrDir)
142+
}

0 commit comments

Comments
 (0)