Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ dist
/dmt
# Ignore Go coverage output
coverage.out
coverage.svg
coverage.svg
# Ignore manual test modules
/test-modules/**
11 changes: 8 additions & 3 deletions cmd/dmt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,23 @@ func runLint(dir string) error {
color.NoColor = false
logger.InfoF("DMT version: %s", version)

cfg, err := config.NewDefaultRootConfig(dir)
dtoLoader := config.NewDTOLoader(dir)
userDTO, err := dtoLoader.LoadUserConfig()
logger.CheckErr(err)

globalDTO, err := dtoLoader.LoadGlobalConfig()
logger.CheckErr(err)

cfg := config.NewRootConfig(userDTO, globalDTO)

// init metrics storage, should be done before running manager
metrics.GetClient(dir)

mng := manager.NewManager(dir, cfg)
mng := manager.NewManager(dir, cfg.ToLintersSettings())
mng.Run()
mng.PrintResult()

metrics.SetDmtInfo()
metrics.SetLinterWarningsMetrics(cfg.GlobalSettings)
metrics.SetDmtRuntimeDuration()
metrics.SetDmtRuntimeDurationSeconds()

Expand Down
67 changes: 45 additions & 22 deletions internal/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"github.com/deckhouse/dmt/internal/fsutils"
"github.com/deckhouse/dmt/internal/logger"
"github.com/deckhouse/dmt/internal/metrics"
"github.com/deckhouse/dmt/internal/module"
modulePkg "github.com/deckhouse/dmt/internal/module"
"github.com/deckhouse/dmt/internal/values"
"github.com/deckhouse/dmt/pkg"
"github.com/deckhouse/dmt/pkg/config"
Expand All @@ -60,21 +60,21 @@
)

type Linter interface {
Run(m *module.Module)
Run(m *modulePkg.Module)
Name() string
}

type Manager struct {
cfg *config.RootConfig
Modules []*module.Module
cfg *config.LintersSettings
Modules []*modulePkg.Module

errors *errors.LintRuleErrorsList
}

func NewManager(dir string, rootConfig *config.RootConfig) *Manager {
func NewManager(dir string, lintersSettings *config.LintersSettings) *Manager {
managerLevel := pkg.Error
m := &Manager{
cfg: rootConfig,
cfg: lintersSettings,

errors: errors.NewLintRuleErrorsList().WithMaxLevel(&managerLevel),
}
Expand Down Expand Up @@ -107,7 +107,7 @@
// linting errors are already logged
continue
}
mdl, err := module.NewModule(paths[i], &vals, globalValues, errorList)
mdl, err := modulePkg.NewModule(paths[i], &vals, globalValues, errorList, nil)
if err != nil {
errorList.
WithFilePath(paths[i]).WithModule(moduleName).
Expand All @@ -116,8 +116,6 @@
continue
}

mdl.MergeRootConfig(m.cfg)

m.Modules = append(m.Modules, mdl)
}

Expand Down Expand Up @@ -147,15 +145,25 @@
processingCh <- struct{}{}
wg.Add(1)

go func() {
go func(module *modulePkg.Module) {
defer func() {
<-processingCh
wg.Done()
}()

logger.InfoF("Run linters for `%s` module", module.GetName())

for _, linter := range getLintersForModule(module.GetModuleConfig(), m.errors) {
moduleConfig := module.GetModuleConfig()
if moduleConfig == nil {
logger.ErrorF("Module config is nil for module `%s`", module.GetName())
return
}
if moduleConfig.LintersSettings == nil {
logger.ErrorF("LintersSettings is nil for module `%s`", module.GetName())
return
}

for _, linter := range getLintersForModule(m.cfg, errorListWithRuleImpact) {

Check failure on line 166 in internal/manager/manager.go

View workflow job for this annotation

GitHub Actions / golangci-lint

undefined: errorListWithRuleImpact (typecheck)
if flags.LinterName != "" && linter.Name() != flags.LinterName {
continue
}
Expand All @@ -164,22 +172,26 @@

linter.Run(module)
}
}()
}(module)
}

wg.Wait()
}

func getLintersForModule(cfg *config.ModuleConfig, errList *errors.LintRuleErrorsList) []Linter {
func getLintersForModule(cfg *config.LintersSettings, errList *errors.LintRuleErrorsList) []Linter {
moduleConfig := &config.ModuleConfig{
LintersSettings: cfg,
}

return []Linter{
openapi.New(cfg, errList),
no_cyrillic.New(cfg, errList),
container.New(cfg, errList),
templates.New(cfg, errList),
images.New(cfg, errList),
rbac.New(cfg, errList),
hooks.New(cfg, errList),
moduleLinter.New(cfg, errList),
openapi.New(moduleConfig, errList),
no_cyrillic.New(moduleConfig, errList),
container.New(moduleConfig, errList),
templates.New(moduleConfig, errList),
images.New(moduleConfig, errList),
rbac.New(moduleConfig, errList),
hooks.New(moduleConfig, errList),
moduleLinter.New(moduleConfig, errList),
}
}

Expand Down Expand Up @@ -231,7 +243,18 @@
}

// header
fmt.Fprint(w, emoji.Sprintf(":monkey:"))
var emojiStr string
switch err.Level {
case pkg.Warn:
emojiStr = ":warning:"
case pkg.Error:
emojiStr = ":monkey:"
case pkg.Critical:
emojiStr = ":monkey:"
default:
emojiStr = ":monkey:"
}
fmt.Fprint(w, emoji.Sprint(emojiStr))
fmt.Fprint(w, color.New(color.FgHiBlue).SprintFunc()("["))

if err.RuleID != "" {
Expand Down
30 changes: 22 additions & 8 deletions internal/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/deckhouse/dmt/internal/values"
"github.com/deckhouse/dmt/internal/werf"
"github.com/deckhouse/dmt/pkg/config"
"github.com/deckhouse/dmt/pkg/config/global"
dmtErrors "github.com/deckhouse/dmt/pkg/errors"
)

Expand Down Expand Up @@ -133,11 +134,7 @@ func (m *Module) GetModuleConfig() *config.ModuleConfig {
return m.linterConfig
}

func (m *Module) MergeRootConfig(cfg *config.RootConfig) {
m.linterConfig.LintersSettings.MergeGlobal(&cfg.GlobalSettings.Linters)
}

func NewModule(path string, vals *chartutil.Values, globalSchema *spec.Schema, errorList *dmtErrors.LintRuleErrorsList) (*Module, error) {
func NewModule(path string, vals *chartutil.Values, globalSchema *spec.Schema, errorList *dmtErrors.LintRuleErrorsList, _ *global.Linters) (*Module, error) {
module, err := newModuleFromPath(path)
if err != nil {
return nil, err
Expand Down Expand Up @@ -167,12 +164,29 @@ func NewModule(path string, vals *chartutil.Values, globalSchema *spec.Schema, e
module.werfFile = werfFile
}

cfg := &config.ModuleConfig{}
if err := config.NewLoader(cfg, path).Load(); err != nil {
dtoConfig, err := config.LoadUserConfig(path)
if err != nil {
return nil, fmt.Errorf("can not parse module config: %w", err)
}

module.linterConfig = cfg
globalDTO := &config.GlobalRootConfigDTO{
Global: config.GlobalLintersDTO{
Container: config.GlobalLinterSettingsDTO{},
Hooks: config.GlobalLinterSettingsDTO{},
Images: config.GlobalLinterSettingsDTO{},
Module: config.GlobalLinterSettingsDTO{},
NoCyrillic: config.GlobalLinterSettingsDTO{},
OpenAPI: config.GlobalLinterSettingsDTO{},
Rbac: config.GlobalLinterSettingsDTO{},
Templates: config.GlobalLinterSettingsDTO{},
},
}
domainConfig := config.NewRootConfig(dtoConfig, globalDTO)

module.linterConfig = &config.ModuleConfig{
DomainConfig: domainConfig,
LintersSettings: domainConfig.ToLintersSettings(),
}

return module, nil
}
Expand Down
58 changes: 58 additions & 0 deletions pkg/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package pkg

type LinterConfig struct {
Impact *Level
}

type RuleConfig struct {
impact *Level
}

func (rc *RuleConfig) GetLevel() *Level {
return rc.impact
}

type LintersSettings struct {
Container ContainerLinterConfig
Image ImageLinterConfig
}

type ImageLinterConfig struct {
LinterConfig
Rules ImageLinterRules
ExcludeRules ImageExcludeRules
}

type ImageLinterRules struct {
DistrolessRule RuleConfig
ImageRule RuleConfig
PatchesRule RuleConfig
WerfRule RuleConfig
}

type ImageExcludeRules struct {
}

type ContainerLinterConfig struct {
LinterConfig
ExcludeRules ContainerExcludeRules
}

type ContainerExcludeRules struct {
ControllerSecurityContext KindRuleExcludeList
DNSPolicy KindRuleExcludeList

HostNetworkPorts ContainerRuleExcludeList
Ports ContainerRuleExcludeList
ReadOnlyRootFilesystem ContainerRuleExcludeList
ImageDigest ContainerRuleExcludeList
Resources ContainerRuleExcludeList
SecurityContext ContainerRuleExcludeList
Liveness ContainerRuleExcludeList
Readiness ContainerRuleExcludeList

Description StringRuleExcludeList
}

// ContainerRuleExcludeList represents a list of container exclusions
type ContainerRuleExcludeList []ContainerRuleExclude
Loading
Loading