Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
小 白蛋
Datree
Commits
cfdae4d0
Commit
cfdae4d0
authored
4 years ago
by
myishay
Browse files
Options
Download
Email Patches
Plain Diff
wip: invalid files
parent
f8f19070
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
bl/evaluation/evaluator.go
+10
-3
bl/evaluation/evaluator.go
bl/evaluation/printer.go
+9
-0
bl/evaluation/printer.go
bl/validation/k8sValidator.go
+33
-30
bl/validation/k8sValidator.go
cmd/test/main.go
+6
-5
cmd/test/main.go
pkg/cliClient/evaluation.go
+1
-1
pkg/cliClient/evaluation.go
pkg/printer/printer.go
+11
-3
pkg/printer/printer.go
with
70 additions
and
42 deletions
+70
-42
bl/evaluation/evaluator.go
+
10
-
3
View file @
cfdae4d0
package
evaluation
import
(
"github.com/datreeio/datree/bl/validation"
"github.com/datreeio/datree/pkg/cliClient"
"github.com/datreeio/datree/pkg/extractor"
)
...
...
@@ -52,10 +53,14 @@ func (e *Evaluator) CreateEvaluation(cliId string, cliVersion string) (int, erro
return
evaluationId
,
err
}
func
(
e
*
Evaluator
)
Evaluate
(
validFilesPathsChan
chan
string
,
invalidFiles
Paths
[]
*
string
,
evaluationId
int
)
(
*
EvaluationResults
,
[]
*
Error
,
error
)
{
func
(
e
*
Evaluator
)
Evaluate
(
validFilesPathsChan
chan
string
,
invalidFiles
[]
validation
.
InvalidFile
,
evaluationId
int
)
(
*
EvaluationResults
,
[]
*
Error
,
error
)
{
filesConfigurations
,
errors
:=
e
.
extractFilesConfigurations
(
validFilesPathsChan
)
if
len
(
invalidFilesPaths
)
>
0
{
if
len
(
invalidFiles
)
>
0
{
var
invalidFilesPaths
[]
*
string
for
_
,
file
:=
range
invalidFiles
{
invalidFilesPaths
=
append
(
invalidFilesPaths
,
&
file
.
Path
)
}
stopEvaluation
:=
len
(
validFilesPathsChan
)
==
0
// NOTICE: validFilesPathsChan surely closed and empty
err
:=
e
.
cliClient
.
UpdateEvaluationValidation
(
&
cliClient
.
UpdateEvaluationValidationRequest
{
EvaluationId
:
evaluationId
,
...
...
@@ -63,7 +68,9 @@ func (e *Evaluator) Evaluate(validFilesPathsChan chan string, invalidFilesPaths
StopEvaluation
:
stopEvaluation
,
})
return
nil
,
errors
,
err
if
stopEvaluation
{
return
nil
,
errors
,
err
}
}
if
len
(
filesConfigurations
)
>
0
{
...
...
This diff is collapsed.
Click to expand it.
bl/evaluation/printer.go
+
9
-
0
View file @
cfdae4d0
...
...
@@ -15,6 +15,10 @@ type Printer interface {
PrintSummaryTable
(
summary
printer
.
Summary
)
}
// func PrintAllResults(results *EvaluationResults /*, invalidFiles*/) {
// // TODO: foreach invalid file
// }
// url := "https://app.datree.io/login?cliId=" + cliId
func
PrintResults
(
results
*
EvaluationResults
,
loginURL
string
,
outputFormat
string
,
printer
Printer
)
error
{
switch
{
...
...
@@ -91,6 +95,11 @@ func parseToPrinterWarnings(results *EvaluationResults, pwd string) ([]printer.W
warnings
=
append
(
warnings
,
printer
.
Warning
{
Title
:
fmt
.
Sprintf
(
">> File: %s
\n
"
,
relativePath
),
Details
:
warningDetails
,
ValidationInfo
:
printer
.
ValidationInfo
{
IsValid
:
true
,
ErrMsgStr
:
""
,
K8sVersion
:
"1.18.0"
,
},
})
}
...
...
This diff is collapsed.
Click to expand it.
bl/validation/k8sValidator.go
+
33
-
30
View file @
cfdae4d0
...
...
@@ -24,55 +24,58 @@ func New(k8sVersion string) *K8sValidator {
}
}
func
(
val
*
K8sValidator
)
ValidateResources
(
paths
[]
string
)
(
chan
string
,
[]
*
string
,
chan
error
)
{
type
InvalidFile
struct
{
Path
string
ValidationErrors
[]
error
}
func
(
val
*
K8sValidator
)
ValidateResources
(
paths
[]
string
)
(
chan
string
,
[]
InvalidFile
,
chan
error
)
{
pathsChan
:=
files
.
ToAbsolutePaths
(
paths
)
var
invalidFiles
Paths
=
[]
*
string
{}
var
invalidFiles
[]
InvalidFile
errorChan
:=
make
(
chan
error
)
validFilesPathChan
:=
make
(
chan
string
)
go
func
()
{
for
path
:=
range
pathsChan
{
isValid
,
err
:=
val
.
validateResource
(
path
)
if
isValid
{
validFilesPathChan
<-
path
}
else
{
invalidFilesPaths
=
append
(
invalidFilesPaths
,
&
path
)
}
if
err
!=
nil
{
errorChan
<-
err
}
for
path
:=
range
pathsChan
{
isValid
,
validationErrors
,
err
:=
val
.
validateResource
(
path
)
if
isValid
{
validFilesPathChan
<-
path
}
else
{
invalidFiles
=
append
(
invalidFiles
,
InvalidFile
{
Path
:
path
,
ValidationErrors
:
validationErrors
,
})
}
close
(
validFilesPathChan
)
close
(
errorChan
)
}()
if
err
!=
nil
{
errorChan
<-
err
}
}
close
(
validFilesPathChan
)
close
(
errorChan
)
return
validFilesPathChan
,
invalidFiles
Paths
,
errorChan
return
validFilesPathChan
,
invalidFiles
,
errorChan
}
func
(
val
*
K8sValidator
)
validateResource
(
filepath
string
)
(
bool
,
error
)
{
func
(
val
*
K8sValidator
)
validateResource
(
filepath
string
)
(
bool
,
[]
error
,
error
)
{
f
,
err
:=
os
.
Open
(
filepath
)
if
err
!=
nil
{
return
false
,
fmt
.
Errorf
(
"failed opening %s: %s"
,
filepath
,
err
)
return
false
,
[]
error
{},
fmt
.
Errorf
(
"failed opening %s: %s"
,
filepath
,
err
)
}
results
:=
val
.
validationClient
.
Validate
(
filepath
,
f
)
isValid
:=
false
for
i
,
res
:=
range
results
{
if
res
.
Status
==
kubeconformValidator
.
Valid
{
isValid
=
true
}
isValid
:=
true
var
validationErrors
[]
error
for
_
,
res
:=
range
results
{
// A file might contain multiple resources
// File starts with ---, the parser assumes a first empty resource
if
res
.
Status
==
kubeconformValidator
.
Invalid
{
fmt
.
Errorf
(
"resource %d in file %s is not valid: %s"
,
i
,
filepath
,
res
.
Err
)
}
if
res
.
Status
==
kubeconformValidator
.
Error
{
fmt
.
Errorf
(
"error while processing resource %d in file %s: %s"
,
i
,
filepath
,
res
.
Err
)
if
res
.
Status
==
kubeconformValidator
.
Invalid
||
res
.
Status
==
kubeconformValidator
.
Error
{
isValid
=
false
validationErrors
=
append
(
validationErrors
,
res
.
Err
)
}
}
return
isValid
,
nil
return
isValid
,
validationErrors
,
nil
}
func
newKubconformValidator
(
k8sVersion
string
)
ValidationClient
{
...
...
This diff is collapsed.
Click to expand it.
cmd/test/main.go
+
6
-
5
View file @
cfdae4d0
...
...
@@ -7,13 +7,14 @@ import (
"github.com/briandowns/spinner"
"github.com/datreeio/datree/bl/evaluation"
"github.com/datreeio/datree/bl/messager"
"github.com/datreeio/datree/bl/validation"
"github.com/datreeio/datree/pkg/localConfig"
"github.com/datreeio/datree/pkg/printer"
"github.com/spf13/cobra"
)
type
Evaluator
interface
{
Evaluate
(
validFilesPathsChan
chan
string
,
invalidFilesPaths
[]
*
string
,
evaluationId
int
)
(
*
evaluation
.
EvaluationResults
,
[]
*
evaluation
.
Error
,
error
)
Evaluate
(
validFilesPathsChan
chan
string
,
invalidFilesPaths
[]
validation
.
InvalidFile
,
evaluationId
int
)
(
*
evaluation
.
EvaluationResults
,
[]
*
evaluation
.
Error
,
error
)
CreateEvaluation
(
cliId
string
,
cliVersion
string
)
(
int
,
error
)
}
...
...
@@ -22,7 +23,7 @@ type Messager interface {
}
type
K8sValidator
interface
{
ValidateResources
(
paths
[]
string
)
(
chan
string
,
[]
*
string
,
chan
error
)
ValidateResources
(
paths
[]
string
)
(
chan
string
,
[]
validation
.
InvalidFile
,
chan
error
)
}
type
TestCommandFlags
struct
{
...
...
@@ -73,10 +74,10 @@ func test(ctx *TestCommandContext, paths []string, flags TestCommandFlags) error
messages
:=
make
(
chan
*
messager
.
VersionMessage
,
1
)
go
ctx
.
Messager
.
LoadVersionMessages
(
messages
,
ctx
.
CliVersion
)
validFilesPaths
,
invalidFiles
Paths
,
errorsChan
:=
ctx
.
K8sValidator
.
ValidateResources
(
paths
)
validFilesPaths
,
invalidFiles
,
errorsChan
:=
ctx
.
K8sValidator
.
ValidateResources
(
paths
)
go
func
()
{
for
err
:=
range
errorsChan
{
fmt
.
Print
(
err
)
fmt
.
Print
ln
(
err
)
}
}()
...
...
@@ -86,7 +87,7 @@ func test(ctx *TestCommandContext, paths []string, flags TestCommandFlags) error
return
err
}
results
,
errors
,
err
:=
ctx
.
Evaluator
.
Evaluate
(
validFilesPaths
,
invalidFiles
Paths
,
evaluationId
)
results
,
errors
,
err
:=
ctx
.
Evaluator
.
Evaluate
(
validFilesPaths
,
invalidFiles
,
evaluationId
)
spinner
.
Stop
()
...
...
This diff is collapsed.
Click to expand it.
pkg/cliClient/evaluation.go
+
1
-
1
View file @
cfdae4d0
...
...
@@ -87,7 +87,7 @@ func (c *CliClient) RequestEvaluation(request *EvaluationRequest) (*EvaluationRe
type
UpdateEvaluationValidationRequest
struct
{
EvaluationId
int
`json:"evaluationId"`
InvalidFiles
[]
*
string
`json:"failedFiles"`
StopEvaluation
bool
StopEvaluation
bool
`json:"stopEvaluation"`
}
func
(
c
*
CliClient
)
UpdateEvaluationValidation
(
request
*
UpdateEvaluationValidationRequest
)
error
{
...
...
This diff is collapsed.
Click to expand it.
pkg/printer/printer.go
+
11
-
3
View file @
cfdae4d0
...
...
@@ -25,15 +25,23 @@ type WarningInfo struct {
Suggestion
string
}
type
ValidationInfo
struct
{
IsValid
bool
ErrMsgStr
string
K8sVersion
string
}
type
Warning
struct
{
Title
string
Details
[]
WarningInfo
Title
string
Details
[]
WarningInfo
ValidationInfo
ValidationInfo
}
func
(
p
*
Printer
)
PrintWarnings
(
warnings
[]
Warning
)
{
for
_
,
warning
:=
range
warnings
{
p
.
printInColor
(
warning
.
Title
,
p
.
theme
.
Colors
.
Yellow
)
fmt
.
Println
()
p
.
printInColor
(
"[V] Kubernetes schema validation
\n
"
,
p
.
theme
.
Colors
.
Green
)
p
.
printInColor
(
"[X] Policy check
\n
"
,
p
.
theme
.
Colors
.
White
)
fmt
.
Println
()
for
_
,
d
:=
range
warning
.
Details
{
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment