Unverified Commit dcb5604e authored by TzlilSwimmer123's avatar TzlilSwimmer123 Committed by GitHub
Browse files

feat: warning on helm files (#420)


* on messgaing warning - check if k8s failed due to helm file (Chart/chart/Values/values.ym?al)
added new color for that - Checking with Adi what she thinks

* new line

* rename new function

* change the logic - add warning extra messages

* update message text

* use array and goimports

* fix fmt test
Co-authored-by: default avatarTzlil Swimmer <tzlilswimmer@Tzlils-MacBook-Pro.local>
Co-authored-by: default avatarAlex Fedin <alex@datree.io>
parent b11c0d16
Showing with 95 additions and 12 deletions
+95 -12
......@@ -137,6 +137,7 @@ func parseInvalidK8sFilesToWarnings(invalidK8sFiles []*validation.InvalidK8sFile
ValidationErrors: invalidFile.ValidationErrors,
K8sVersion: k8sVersion,
},
ExtraMessages: GetWarningExtraMessages(invalidFile),
})
}
......@@ -200,6 +201,37 @@ func parseToPrinterWarnings(results *EvaluationResults, invalidYamlFiles []*vali
return warnings, nil
}
func GetWarningExtraMessages(invalidFile *validation.InvalidK8sFile) []printer.ExtraMessage {
var extraMessages []printer.ExtraMessage
if IsHelmFile(invalidFile.Path) {
extraMessages = append(extraMessages, printer.ExtraMessage{
Text: "Are you trying to test a raw helm file ? To run Datree with Helm - check out the helm plugin README:\nhttps://github.com/datreeio/helm-datree",
Color: "blue",
})
}
return extraMessages
}
func IsHelmFile(filePath string) bool {
cleanFilePath := strings.Replace(filePath, "\n", "", -1)
fileExtension := filepath.Ext(cleanFilePath)
if fileExtension != ".yml" && fileExtension != ".yaml" {
return false
}
helmFilesExtensions := [...]string{"Chart", "chart", "Values", "values"}
for _, extension := range helmFilesExtensions {
if strings.Contains(cleanFilePath, extension) {
return true
}
}
return false
}
type OutputTitle int
const (
......
testService:
name: test-service
image:
tag: staging
certificateArn: arn:aws:acm
......@@ -40,11 +40,18 @@ type InvalidK8sInfo struct {
ValidationErrors []error
K8sVersion string
}
type ExtraMessage struct {
Text string
Color string
}
type Warning struct {
Title string
FailedRules []FailedRule
InvalidYamlInfo InvalidYamlInfo
InvalidK8sInfo InvalidK8sInfo
ExtraMessages []ExtraMessage
}
func (p *Printer) SetTheme(theme *Theme) {
......@@ -74,7 +81,13 @@ func (p *Printer) printK8sValidationWarning(warning Warning) {
validationError := p.Theme.Colors.RedBold.Sprint(validationError.Error())
fmt.Fprintf(out, "%v %v\n", p.Theme.Emoji.Error, validationError)
}
for _, extraMessage := range warning.ExtraMessages {
p.PrintMessage(extraMessage.Text, extraMessage.Color)
}
fmt.Fprintln(out)
p.printSkippedPolicyCheck()
fmt.Fprintln(out)
}
......@@ -201,6 +214,8 @@ func (p *Printer) createNewColor(clr string) *color.Color {
return p.Theme.Colors.Yellow
case "green":
return p.Theme.Colors.Green
case "blue":
return p.Theme.Colors.Blue
default:
return p.Theme.Colors.White
}
......
......@@ -11,28 +11,34 @@ import (
func TestPrintWarnings(t *testing.T) {
printer := CreateNewPrinter()
warnings := []Warning{
Warning{
Title: "Failed with Occurrences",
FailedRules: []FailedRule{
FailedRule{
Name: "Caption",
Occurrences: 1,
Suggestion: "Suggestion",
OccurrencesDetails: []OccurrenceDetails{OccurrenceDetails{MetadataName: "yishay", Kind: "Pod"}},
},
warnings := []Warning{{
Title: "Failed with Occurrences",
FailedRules: []FailedRule{
{
Name: "Caption",
Occurrences: 1,
Suggestion: "Suggestion",
OccurrencesDetails: []OccurrenceDetails{{MetadataName: "yishay", Kind: "Pod"}},
},
},
Warning{
},
{
Title: "Failed with yaml validation",
FailedRules: []FailedRule{},
InvalidYamlInfo: InvalidYamlInfo{ValidationErrors: []error{fmt.Errorf("yaml validation error")}},
},
Warning{
{
Title: "Failed with k8s validation",
FailedRules: []FailedRule{},
InvalidK8sInfo: InvalidK8sInfo{ValidationErrors: []error{fmt.Errorf("K8S validation error")}, K8sVersion: "1.18.0"},
},
{
Title: ">> File: /datree/datree/internal/fixtures/kube/Chart.yaml\n",
FailedRules: []FailedRule{},
InvalidK8sInfo: InvalidK8sInfo{ValidationErrors: []error{fmt.Errorf("K8S validation error")}, K8sVersion: "1.18.0"},
ExtraMessages: []ExtraMessage{{Text: "Are you trying to test a raw helm file ? To run Datree with Helm - check out the helm plugin README:\nhttps://github.com/datreeio/helm-datree",
Color: "blue"}},
},
}
t.Run("Test PrintWarnings", func(t *testing.T) {
......@@ -70,6 +76,16 @@ Failed with k8s validation
[?] Policy check didn't run for this file
>> File: /datree/datree/internal/fixtures/kube/Chart.yaml
[V] YAML validation
[X] Kubernetes schema validation
❌ K8S validation error
Are you trying to test a raw helm file ? To run Datree with Helm - check out the helm plugin README:
https://github.com/datreeio/helm-datree
[?] Policy check didn't run for this file
`)
assert.Equal(t, string(expected), string(got))
......@@ -112,6 +128,16 @@ Failed with k8s validation
[?] Policy check didn't run for this file
>> File: /datree/datree/internal/fixtures/kube/Chart.yaml
[V] YAML validation
[X] Kubernetes schema validation
[X] K8S validation error
Are you trying to test a raw helm file ? To run Datree with Helm - check out the helm plugin README:
https://github.com/datreeio/helm-datree
[?] Policy check didn't run for this file
`)
assert.Equal(t, string(expected), string(got))
......
......@@ -14,6 +14,7 @@ type Theme struct {
RedBold *color.Color
White *color.Color
Error *color.Color
Blue *color.Color
}
ColorsAttributes struct {
Green color.Attribute
......@@ -36,12 +37,14 @@ func createDefaultTheme() *Theme {
RedBold *color.Color
White *color.Color
Error *color.Color
Blue *color.Color
}{
Green: color.New(color.FgGreen),
Yellow: color.New(color.FgYellow),
RedBold: color.New(color.FgHiRed, color.Bold),
Error: color.New(color.FgHiRed),
White: color.New(color.FgHiWhite),
Blue: color.New(color.FgBlue),
},
ColorsAttributes: struct {
Green color.Attribute
......@@ -70,12 +73,14 @@ func CreateSimpleTheme() *Theme {
RedBold *color.Color
White *color.Color
Error *color.Color
Blue *color.Color
}{
Green: color.New(),
Yellow: color.New(),
RedBold: color.New(),
Error: color.New(),
White: color.New(),
Blue: color.New(),
},
ColorsAttributes: struct {
Green color.Attribute
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment