Skip to content

Commit 996a14f

Browse files
Revert "Do validation in parallel"
This reverts commit 158fee7.
1 parent 158fee7 commit 996a14f

File tree

3 files changed

+47
-95
lines changed

3 files changed

+47
-95
lines changed

cmd/kubeapply/subcmd/validate.go

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ var validateCmd = &cobra.Command{
2323
type validateFlags struct {
2424
// Expand before validating.
2525
expand bool
26-
27-
// Expand before validating.
28-
quiet bool
2926
}
3027

3128
var validateFlagValues validateFlags
@@ -103,16 +100,12 @@ func execValidation(ctx context.Context, clusterConfig *config.ClusterConfig) er
103100
}
104101

105102
numInvalidResources := 0
106-
numValidResources := 0
107-
numSkippedResources := 0
108103

109104
for _, result := range results {
110105
switch result.Status {
111106
case validation.StatusValid:
112-
numValidResources++
113-
log.Debugf("Resource %s in file %s OK", result.PrettyName(), result.Filename)
107+
log.Infof("Resource %s in file %s OK", result.PrettyName(), result.Filename)
114108
case validation.StatusSkipped:
115-
numSkippedResources++
116109
log.Debugf("Resource %s in file %s was skipped", result.PrettyName(), result.Filename)
117110
case validation.StatusError:
118111
numInvalidResources++
@@ -133,19 +126,11 @@ func execValidation(ctx context.Context, clusterConfig *config.ClusterConfig) er
133126

134127
if numInvalidResources > 0 {
135128
return fmt.Errorf(
136-
"Validation failed for cluster %s; %d resources/files invalid, %d valid, %d skipped",
137-
clusterConfig.DescriptiveName(),
129+
"Validation failed for %d resources",
138130
numInvalidResources,
139-
numValidResources,
140-
numSkippedResources,
141131
)
142132
}
143133

144-
log.Infof(
145-
"Validation of cluster %s passed; %d resources valid, %d skipped",
146-
clusterConfig.DescriptiveName(),
147-
numValidResources,
148-
numSkippedResources,
149-
)
134+
log.Infof("Validation of cluster %s passed", clusterConfig.DescriptiveName())
150135
return nil
151136
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ require (
3939
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
4040
gopkg.in/go-playground/assert.v1 v1.2.1 // indirect
4141
gopkg.in/src-d/go-git.v4 v4.13.1
42-
gopkg.in/validator.v2 v2.0.0-20180514200540-135c24b11c19 // indirect
42+
gopkg.in/validator.v2 v2.0.0-20180514200540-135c24b11c19
4343
gopkg.in/yaml.v2 v2.3.0
4444
gopkg.in/yaml.v3 v3.0.0-20200601152816-913338de1bd2 // indirect
4545
gopkg.in/zorkian/go-datadog-api.v2 v2.28.0

pkg/validation/validation.go

Lines changed: 43 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,13 @@ package validation
33
import (
44
"context"
55
"fmt"
6-
"sync"
6+
"os"
7+
"path/filepath"
8+
"strings"
79

8-
"github.com/yannh/kubeconform/pkg/resource"
910
"github.com/yannh/kubeconform/pkg/validator"
1011
)
1112

12-
const (
13-
numWorkers = 6
14-
)
15-
1613
// KubeValidator is a struct that validates the kube configs associated with a cluster.
1714
type KubeValidator struct {
1815
validatorObj validator.Validator
@@ -77,88 +74,58 @@ func (k *KubeValidator) RunSchemaValidation(
7774
ctx context.Context,
7875
path string,
7976
) ([]ValidationResult, error) {
80-
kResults := []validator.Result{}
81-
resourcesChan, errChan := resource.FromFiles(ctx, []string{path}, nil)
82-
mut := sync.Mutex{}
83-
wg := sync.WaitGroup{}
84-
85-
// Based on implementation in
86-
// https://github.com/yannh/kubeconform/blob/master/cmd/kubeconform/main.go.
87-
for i := 0; i < numWorkers; i++ {
88-
wg.Add(1)
89-
go func() {
90-
for res := range resourcesChan {
91-
mut.Lock()
92-
kResults = append(kResults, k.validatorObj.ValidateResource(res))
93-
mut.Unlock()
77+
results := []ValidationResult{}
78+
79+
err := filepath.Walk(
80+
path,
81+
func(subPath string, info os.FileInfo, err error) error {
82+
if err != nil {
83+
return err
9484
}
95-
wg.Done()
96-
}()
97-
}
9885

99-
wg.Add(1)
100-
go func() {
101-
// Process errors while discovering resources
102-
for err := range errChan {
103-
if err == nil {
104-
continue
86+
if info.IsDir() || !strings.HasSuffix(subPath, ".yaml") {
87+
return nil
10588
}
10689

107-
var kResult validator.Result
90+
file, err := os.Open(subPath)
91+
if err != nil {
92+
return err
93+
}
10894

109-
if err, ok := err.(resource.DiscoveryError); ok {
110-
kResult = validator.Result{
111-
Resource: resource.Resource{Path: err.Path},
112-
Err: err.Err,
113-
Status: validator.Error,
95+
for _, kResult := range k.validatorObj.ValidateWithContext(ctx, subPath, file) {
96+
if kResult.Status == validator.Empty {
97+
// Skip over empty results
98+
continue
11499
}
115-
} else {
116-
kResult = validator.Result{
117-
Resource: resource.Resource{},
118-
Err: err,
119-
Status: validator.Error,
100+
101+
result := ValidationResult{
102+
Filename: kResult.Resource.Path,
103+
Status: kubeconformStatusToStatus(kResult.Status),
120104
}
121-
}
122-
mut.Lock()
123-
kResults = append(kResults, kResult)
124-
mut.Unlock()
125-
}
126-
wg.Done()
127-
}()
128-
wg.Wait()
129105

130-
results := []ValidationResult{}
106+
if kResult.Err != nil {
107+
result.Message = kResult.Err.Error()
108+
}
131109

132-
for _, kResult := range kResults {
133-
if kResult.Status == validator.Empty {
134-
// Skip over empty results
135-
continue
136-
}
137-
138-
result := ValidationResult{
139-
Filename: kResult.Resource.Path,
140-
Status: kStatusToStatus(kResult.Status),
141-
}
142-
143-
if kResult.Err != nil {
144-
result.Message = kResult.Err.Error()
145-
}
146-
147-
sig, err := kResult.Resource.Signature()
148-
if err == nil && sig != nil {
149-
result.Kind = sig.Kind
150-
result.Name = sig.Name
151-
result.Namespace = sig.Namespace
152-
result.Version = sig.Version
153-
}
154-
155-
results = append(results, result)
156-
}
110+
sig, err := kResult.Resource.Signature()
111+
if err == nil && sig != nil {
112+
result.Kind = sig.Kind
113+
result.Name = sig.Name
114+
result.Namespace = sig.Namespace
115+
result.Version = sig.Version
116+
}
117+
118+
results = append(results, result)
119+
}
120+
121+
return nil
122+
},
123+
)
157124

158-
return results, nil
125+
return results, err
159126
}
160127

161-
func kStatusToStatus(kStatus validator.Status) Status {
128+
func kubeconformStatusToStatus(kStatus validator.Status) Status {
162129
switch kStatus {
163130
case validator.Valid:
164131
return StatusValid

0 commit comments

Comments
 (0)