Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
Signed-off-by: Evsyukov Denis <denis.evsyukov@flant.com>
  • Loading branch information
juev committed Sep 12, 2024
1 parent 4ed2d87 commit badf772
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 35 deletions.
10 changes: 9 additions & 1 deletion pkg/config/linters_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@ import (
"fmt"
)

var defaultLintersSettings = LintersSettings{}
var defaultLintersSettings = LintersSettings{
OpenAPI: OpenAPISettings{
EnumFileExcludes: nil,
HAAbsoluteKeysExcludes: map[string]string{
"modules/150-user-authn/openapi/config-values.yaml": "properties.publishAPI.properties.https",
},
KeyBannedNames: []string{"x-examples", "examples", "example"},
},
}

type LintersSettings struct {
OpenAPI OpenAPISettings
Expand Down
19 changes: 10 additions & 9 deletions pkg/linters/openapi/library.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"reflect"
"strings"

"github.com/deckhouse/d8-lint/pkg/config"
"github.com/deckhouse/d8-lint/pkg/linters/openapi/validators"

"github.com/hashicorp/go-multierror"
Expand Down Expand Up @@ -74,7 +75,7 @@ func GetOpenAPIYAMLFiles(rootPath string) ([]string, error) {
}

// RunOpenAPIValidator runs validator, get channel with file paths and returns channel with results
func RunOpenAPIValidator(fileC chan fileValidation) chan fileValidation {
func RunOpenAPIValidator(fileC chan fileValidation, cfg *config.OpenAPISettings) chan fileValidation {
resultC := make(chan fileValidation, 1)

go func() {
Expand All @@ -87,7 +88,7 @@ func RunOpenAPIValidator(fileC chan fileValidation) chan fileValidation {
continue
}

runFileParser(vfile.filePath, yamlStruct, parseResultC)
runFileParser(vfile.filePath, yamlStruct, cfg, parseResultC)

var result *multierror.Error

Expand Down Expand Up @@ -176,15 +177,15 @@ func isDeckhouseCRD(data map[any]any) bool {
return false
}

func (fp fileParser) parseForWrongKeys(m map[any]any) {
keysValidator := validators.NewKeyNameValidator()
func (fp fileParser) parseForWrongKeys(m map[any]any, cfg *config.OpenAPISettings) {
keysValidator := validators.NewKeyNameValidator(cfg)
err := keysValidator.Run(fp.fileName, "allfile", m)
if err != nil {
fp.resultC <- err
}
}

func runFileParser(fileName string, data map[any]any, resultC chan error) {
func runFileParser(fileName string, data map[any]any, cfg *config.OpenAPISettings, resultC chan error) {
// exclude external CRDs
if isCRD(data) && !isDeckhouseCRD(data) {
close(resultC)
Expand All @@ -194,14 +195,14 @@ func runFileParser(fileName string, data map[any]any, resultC chan error) {
parser := fileParser{
fileName: fileName,
keyValidators: map[string]validator{
"enum": validators.NewEnumValidator(),
"highAvailability": validators.NewHAValidator(),
"https": validators.NewHAValidator(),
"enum": validators.NewEnumValidator(cfg),
"highAvailability": validators.NewHAValidator(cfg),
"https": validators.NewHAValidator(cfg),
},
resultC: resultC,
}
if isDeckhouseCRD(data) {
parser.parseForWrongKeys(data)
parser.parseForWrongKeys(data, cfg)
}
go parser.startParsing(data, resultC)
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/linters/openapi/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ func New(cfg *config.OpenAPISettings) *OpenAPI {
}
}

func (*OpenAPI) Run(_ context.Context, m *module.Module) (errors.LintRuleErrorsList, error) {
func (o *OpenAPI) Run(_ context.Context, m *module.Module) (errors.LintRuleErrorsList, error) {
apiFiles, err := GetOpenAPIYAMLFiles(m.Path)
if err != nil {
return errors.LintRuleErrorsList{}, err
}

filesC := make(chan fileValidation, len(apiFiles))
resultC := RunOpenAPIValidator(filesC)
resultC := RunOpenAPIValidator(filesC, o.cfg)

for _, apiFile := range apiFiles {
filesC <- fileValidation{
Expand All @@ -52,7 +52,7 @@ func (*OpenAPI) Run(_ context.Context, m *module.Module) (errors.LintRuleErrorsL
}
}

return errors.LintRuleErrorsList{}, nil
return result, nil
}

func (o *OpenAPI) Name() string {
Expand Down
9 changes: 6 additions & 3 deletions pkg/linters/openapi/validators/enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"unicode"

"github.com/hashicorp/go-multierror"

"github.com/deckhouse/d8-lint/pkg/config"
)

var (
Expand All @@ -15,12 +17,13 @@ var (

type EnumValidator struct {
key string
excludes map[string]struct{}
excludes map[string][]string
}

func NewEnumValidator() EnumValidator {
func NewEnumValidator(cfg *config.OpenAPISettings) EnumValidator {
return EnumValidator{
key: "enum",
key: "enum",
excludes: cfg.EnumFileExcludes,
}
}

Expand Down
19 changes: 8 additions & 11 deletions pkg/linters/openapi/validators/ha_and_https.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,21 @@ import (
"fmt"
"reflect"

"github.com/deckhouse/d8-lint/pkg/config"
"github.com/deckhouse/d8-lint/pkg/logger"
)

var (
absoluteKeysExcludes = map[string]string{
"modules/150-user-authn/openapi/config-values.yaml": "properties.publishAPI.properties.https",
"global-hooks/openapi/config-values.yaml": "properties.modules.properties.https",
}
)

type HAValidator struct {
absoluteKeysExcludes map[string]string
}

func NewHAValidator() HAValidator {
return HAValidator{}
func NewHAValidator(cfg *config.OpenAPISettings) HAValidator {
return HAValidator{
absoluteKeysExcludes: cfg.HAAbsoluteKeysExcludes,
}
}

func (HAValidator) Run(file, absoluteKey string, value any) error {
func (ha HAValidator) Run(file, absoluteKey string, value any) error {
m := make(map[any]any)
rv := reflect.ValueOf(value)
if rv.Kind() != reflect.Map {
Expand All @@ -35,7 +32,7 @@ func (HAValidator) Run(file, absoluteKey string, value any) error {

for key := range m {
if key == "default" {
if absoluteKeysExcludes[file] == absoluteKey {
if ha.absoluteKeysExcludes[file] == absoluteKey {
continue
}
return fmt.Errorf("%s is invalid: must have no default value", absoluteKey)
Expand Down
16 changes: 8 additions & 8 deletions pkg/linters/openapi/validators/keys_name_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ import (
"fmt"
"reflect"

"github.com/deckhouse/d8-lint/pkg/config"
"github.com/deckhouse/d8-lint/pkg/logger"
)

var (
bannedNames = []string{"x-examples", "examples", "example"}
)

type KeyNameValidator struct {
bannedNames []string
}

func NewKeyNameValidator() KeyNameValidator {
return KeyNameValidator{}
func NewKeyNameValidator(cfg *config.OpenAPISettings) KeyNameValidator {
return KeyNameValidator{
bannedNames: cfg.KeyBannedNames,
}
}

func checkMapForBannedKey(m map[any]any, banned []string) error {
Expand Down Expand Up @@ -52,7 +52,7 @@ func checkMapForBannedKey(m map[any]any, banned []string) error {
return nil
}

func (KeyNameValidator) Run(file, _ string, value any) error {
func (kn KeyNameValidator) Run(file, _ string, value any) error {
m := make(map[any]any)
rv := reflect.ValueOf(value)
if rv.Kind() != reflect.Map {
Expand All @@ -64,7 +64,7 @@ func (KeyNameValidator) Run(file, _ string, value any) error {
m[key.Interface()] = v.Interface()
}

err := checkMapForBannedKey(m, bannedNames)
err := checkMapForBannedKey(m, kn.bannedNames)
if err != nil {
return fmt.Errorf("%s file validation error: wrong property: %w", file, err)
}
Expand Down

0 comments on commit badf772

Please sign in to comment.