-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from reactiveops/jg/add-reporting-support
add support for reporting
- Loading branch information
Showing
4 changed files
with
132 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package report | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/reactiveops/fairwinds/pkg/types" | ||
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log" | ||
) | ||
|
||
var log = logf.Log.WithName("Fairwinds report") | ||
|
||
// Results contains the validation check results. | ||
type Results struct { | ||
Pass bool | ||
FailMsg string | ||
Containers []types.ContainerResults | ||
InitContainers []types.ContainerResults | ||
} | ||
|
||
// Format structures the validation results to return back to k8s API. | ||
func (r *Results) Format() (bool, string) { | ||
var sb strings.Builder | ||
|
||
for _, container := range r.Containers { | ||
if len(container.Failures) == 0 { | ||
r.Pass = true | ||
} | ||
|
||
r.Pass = false | ||
s := fmt.Sprintf("\nContainer: %s\n Failure/s:\n", container.Name) | ||
sb.WriteString(s) | ||
for _, failure := range container.Failures { | ||
sb.WriteString(failure.Reason()) | ||
} | ||
} | ||
|
||
for _, container := range r.InitContainers { | ||
if len(container.Failures) == 0 && r.Pass == true { | ||
return r.Pass, r.FailMsg | ||
} | ||
|
||
r.Pass = false | ||
s := fmt.Sprintf("\nInitContainer: %s\n Failure/s:\n", container.Name) | ||
sb.WriteString(s) | ||
for _, failure := range container.Failures { | ||
sb.WriteString(failure.Reason()) | ||
} | ||
} | ||
|
||
r.FailMsg = sb.String() | ||
return r.Pass, r.FailMsg | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package types | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
// Failure contains information about the failing validation. | ||
type Failure struct { | ||
Name string | ||
Expected string | ||
Actual string | ||
} | ||
|
||
// NewFailure is a factory function for a Failure. | ||
func NewFailure(name, expected, actual string) *Failure { | ||
return &Failure{ | ||
Name: name, | ||
Expected: expected, | ||
Actual: actual, | ||
} | ||
} | ||
|
||
// Reason returns a string that describes the reason for a Failure. | ||
func (f *Failure) Reason() string { | ||
return fmt.Sprintf("- %s: Expected: %s, Actual: %s.\n", | ||
f.Name, | ||
f.Expected, | ||
f.Actual, | ||
) | ||
} | ||
|
||
// ContainerResults has the results of the validation checks for containers. | ||
type ContainerResults struct { | ||
Name string | ||
Failures []Failure | ||
} | ||
|
||
// AddFailure creates a new Failure and adds it to ContainerResults. | ||
func (c *ContainerResults) AddFailure(name, expected, actual string) { | ||
f := NewFailure(name, expected, actual) | ||
c.Failures = append(c.Failures, *f) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters