mirror of
https://github.com/yannh/kubeconform.git
synced 2026-04-19 18:29:52 +00:00
Add resource name to logging
This commit is contained in:
parent
07883b8bb4
commit
52437c00d1
8 changed files with 41 additions and 32 deletions
|
|
@ -9,6 +9,7 @@ import (
|
|||
type result struct {
|
||||
Filename string `json:"filename"`
|
||||
Kind string `json:"kind"`
|
||||
Name string `json:"name"`
|
||||
Version string `json:"version"`
|
||||
Status string `json:"status"`
|
||||
Msg string `json:"msg"`
|
||||
|
|
@ -37,7 +38,7 @@ func JSON(w io.Writer, withSummary bool, verbose bool) Output {
|
|||
}
|
||||
|
||||
// JSON.Write will only write when JSON.Flush has been called
|
||||
func (o *jsono) Write(filename, kind, version string, err error, skipped bool) error {
|
||||
func (o *jsono) Write(filename, kind, name, version string, err error, skipped bool) error {
|
||||
msg, st := "", ""
|
||||
|
||||
s := status(err, skipped)
|
||||
|
|
@ -60,7 +61,7 @@ func (o *jsono) Write(filename, kind, version string, err error, skipped bool) e
|
|||
}
|
||||
|
||||
if o.verbose || (s != VALID && s != SKIPPED) {
|
||||
o.results = append(o.results, result{Filename: filename, Kind: kind, Version: version, Status: st, Msg: msg})
|
||||
o.results = append(o.results, result{Filename: filename, Kind: kind, Name: name, Version: version, Status: st, Msg: msg})
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
|
|||
|
|
@ -7,9 +7,9 @@ import (
|
|||
|
||||
func TestJSONWrite(t *testing.T) {
|
||||
type result struct {
|
||||
fileName, kind, version string
|
||||
err error
|
||||
skipped bool
|
||||
fileName, kind, name, version string
|
||||
err error
|
||||
skipped bool
|
||||
}
|
||||
|
||||
for _, testCase := range []struct {
|
||||
|
|
@ -28,6 +28,7 @@ func TestJSONWrite(t *testing.T) {
|
|||
{
|
||||
"deployment.yml",
|
||||
"Deployment",
|
||||
"my-app",
|
||||
"apps/v1",
|
||||
nil,
|
||||
false,
|
||||
|
|
@ -46,6 +47,7 @@ func TestJSONWrite(t *testing.T) {
|
|||
{
|
||||
"deployment.yml",
|
||||
"Deployment",
|
||||
"my-app",
|
||||
"apps/v1",
|
||||
nil,
|
||||
false,
|
||||
|
|
@ -70,6 +72,7 @@ func TestJSONWrite(t *testing.T) {
|
|||
{
|
||||
"deployment.yml",
|
||||
"Deployment",
|
||||
"my-app",
|
||||
"apps/v1",
|
||||
nil,
|
||||
false,
|
||||
|
|
@ -80,6 +83,7 @@ func TestJSONWrite(t *testing.T) {
|
|||
{
|
||||
"filename": "deployment.yml",
|
||||
"kind": "Deployment",
|
||||
"name": "my-app",
|
||||
"version": "apps/v1",
|
||||
"status": "VALID",
|
||||
"msg": ""
|
||||
|
|
@ -99,7 +103,7 @@ func TestJSONWrite(t *testing.T) {
|
|||
o := JSON(w, testCase.withSummary, testCase.verbose)
|
||||
|
||||
for _, res := range testCase.res {
|
||||
o.Write(res.fileName, res.kind, res.version, res.err, res.skipped)
|
||||
o.Write(res.fileName, res.kind, res.name, res.version, res.err, res.skipped)
|
||||
}
|
||||
o.Flush()
|
||||
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ const (
|
|||
)
|
||||
|
||||
type Output interface {
|
||||
Write(filename, kind, version string, err error, skipped bool) error
|
||||
Write(filename, kind, name, version string, err error, skipped bool) error
|
||||
Flush() error
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -29,7 +29,7 @@ func Text(w io.Writer, withSummary, verbose bool) Output {
|
|||
}
|
||||
}
|
||||
|
||||
func (o *text) Write(filename, kind, version string, reserr error, skipped bool) error {
|
||||
func (o *text) Write(filename, kind, name, version string, reserr error, skipped bool) error {
|
||||
o.Lock()
|
||||
defer o.Unlock()
|
||||
|
||||
|
|
@ -39,18 +39,18 @@ func (o *text) Write(filename, kind, version string, reserr error, skipped bool)
|
|||
switch status(reserr, skipped) {
|
||||
case VALID:
|
||||
if o.verbose {
|
||||
_, err = fmt.Fprintf(o.w, "%s - %s is valid\n", filename, kind)
|
||||
_, err = fmt.Fprintf(o.w, "%s - %s %s is valid\n", filename, kind, name)
|
||||
}
|
||||
o.nValid++
|
||||
case INVALID:
|
||||
_, err = fmt.Fprintf(o.w, "%s - %s is invalid: %s\n", filename, kind, reserr)
|
||||
_, err = fmt.Fprintf(o.w, "%s - %s %s is invalid: %s\n", filename, kind, name, reserr)
|
||||
o.nInvalid++
|
||||
case ERROR:
|
||||
_, err = fmt.Fprintf(o.w, "%s - %s failed validation: %s\n", filename, kind, reserr)
|
||||
_, err = fmt.Fprintf(o.w, "%s - %s %s failed validation: %s\n", filename, kind, name, reserr)
|
||||
o.nErrors++
|
||||
case SKIPPED:
|
||||
if o.verbose {
|
||||
_, err = fmt.Fprintf(o.w, "%s - %s skipped\n", filename, kind)
|
||||
_, err = fmt.Fprintf(o.w, "%s - %s %s skipped\n", filename, name, kind)
|
||||
}
|
||||
o.nSkipped++
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,9 +7,9 @@ import (
|
|||
|
||||
func TestTextWrite(t *testing.T) {
|
||||
type result struct {
|
||||
fileName, kind, version string
|
||||
err error
|
||||
skipped bool
|
||||
fileName, kind, name, version string
|
||||
err error
|
||||
skipped bool
|
||||
}
|
||||
|
||||
for _, testCase := range []struct {
|
||||
|
|
@ -28,6 +28,7 @@ func TestTextWrite(t *testing.T) {
|
|||
{
|
||||
"deployment.yml",
|
||||
"Deployment",
|
||||
"my-app",
|
||||
"apps/v1",
|
||||
nil,
|
||||
false,
|
||||
|
|
@ -43,6 +44,7 @@ func TestTextWrite(t *testing.T) {
|
|||
{
|
||||
"deployment.yml",
|
||||
"Deployment",
|
||||
"my-app",
|
||||
"apps/v1",
|
||||
nil,
|
||||
false,
|
||||
|
|
@ -58,12 +60,13 @@ func TestTextWrite(t *testing.T) {
|
|||
{
|
||||
"deployment.yml",
|
||||
"Deployment",
|
||||
"my-app",
|
||||
"apps/v1",
|
||||
nil,
|
||||
false,
|
||||
},
|
||||
},
|
||||
`deployment.yml - Deployment is valid
|
||||
`deployment.yml - Deployment my-app is valid
|
||||
Summary: 1 resource found in 1 file - Valid: 1, Invalid: 0, Errors: 0 Skipped: 0
|
||||
`,
|
||||
},
|
||||
|
|
@ -72,7 +75,7 @@ Summary: 1 resource found in 1 file - Valid: 1, Invalid: 0, Errors: 0 Skipped: 0
|
|||
o := Text(w, testCase.withSummary, testCase.verbose)
|
||||
|
||||
for _, res := range testCase.res {
|
||||
o.Write(res.fileName, res.kind, res.version, res.err, res.skipped)
|
||||
o.Write(res.fileName, res.kind, res.name, res.version, res.err, res.skipped)
|
||||
}
|
||||
o.Flush()
|
||||
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ import (
|
|||
)
|
||||
|
||||
type Signature struct {
|
||||
Kind, Version, Namespace string
|
||||
Kind, Version, Namespace, Name string
|
||||
}
|
||||
|
||||
// SignatureFromBytes returns key identifying elements from a []byte representing the resource
|
||||
|
|
@ -14,10 +14,11 @@ func SignatureFromBytes(res []byte) (Signature, error) {
|
|||
APIVersion string `yaml:"apiVersion"`
|
||||
Kind string `yaml:"kind"`
|
||||
Metadata struct {
|
||||
Name string `yaml:"Name"`
|
||||
Namespace string `yaml:"Namespace"`
|
||||
} `yaml:"Metadata"`
|
||||
}{}
|
||||
err := yaml.Unmarshal(res, &resource)
|
||||
|
||||
return Signature{Kind: resource.Kind, Version: resource.APIVersion, Namespace: resource.Metadata.Namespace}, err
|
||||
return Signature{Kind: resource.Kind, Version: resource.APIVersion, Namespace: resource.Metadata.Namespace, Name: resource.Metadata.Name}, err
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue