From dcb5604e6e2dbb7191fb24461763b801805666c4 Mon Sep 17 00:00:00 2001
From: TzlilSwimmer123 <51244810+TzlilSwimmer123@users.noreply.github.com>
Date: Thu, 17 Feb 2022 18:29:21 +0200
Subject: [PATCH] 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: Tzlil Swimmer <tzlilswimmer@Tzlils-MacBook-Pro.local>
Co-authored-by: Alex Fedin <alex@datree.io>
---
 bl/evaluation/printer.go           | 32 +++++++++++++++++++
 internal/fixtures/kube/values.yaml |  5 +++
 pkg/printer/printer.go             | 15 +++++++++
 pkg/printer/printer_test.go        | 50 +++++++++++++++++++++++-------
 pkg/printer/theme.go               |  5 +++
 5 files changed, 95 insertions(+), 12 deletions(-)
 create mode 100644 internal/fixtures/kube/values.yaml

diff --git a/bl/evaluation/printer.go b/bl/evaluation/printer.go
index 7f149aa..3e627d8 100644
--- a/bl/evaluation/printer.go
+++ b/bl/evaluation/printer.go
@@ -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 (
diff --git a/internal/fixtures/kube/values.yaml b/internal/fixtures/kube/values.yaml
new file mode 100644
index 0000000..0fbf00f
--- /dev/null
+++ b/internal/fixtures/kube/values.yaml
@@ -0,0 +1,5 @@
+testService:
+  name: test-service
+  image:
+    tag: staging
+  certificateArn: arn:aws:acm
diff --git a/pkg/printer/printer.go b/pkg/printer/printer.go
index ef33313..2b2fef4 100644
--- a/pkg/printer/printer.go
+++ b/pkg/printer/printer.go
@@ -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
 	}
diff --git a/pkg/printer/printer_test.go b/pkg/printer/printer_test.go
index c5e37e3..eb88467 100644
--- a/pkg/printer/printer_test.go
+++ b/pkg/printer/printer_test.go
@@ -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))
diff --git a/pkg/printer/theme.go b/pkg/printer/theme.go
index 46d2c01..9369908 100644
--- a/pkg/printer/theme.go
+++ b/pkg/printer/theme.go
@@ -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
-- 
GitLab