Skip to content
Open
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
1 change: 1 addition & 0 deletions internal/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ func mapModuleRules(linterSettings *pkg.LintersSettings, configSettings *config.
rules.LicenseRule.SetLevel(globalRules.LicenseRule.Impact, fallbackImpact)
rules.RequarementsRule.SetLevel(globalRules.RequarementsRule.Impact, fallbackImpact)
rules.PackageYAMLRule.SetLevel(globalRules.PackageYAMLRule.Impact, fallbackImpact)
rules.ModulePackageConsistencyRule.SetLevel(globalRules.ModulePackageConsistencyRule.Impact, fallbackImpact)
rules.LegacyReleaseFileRule.SetLevel(globalRules.LegacyReleaseFileRule.Impact, fallbackImpact)
}

Expand Down
17 changes: 9 additions & 8 deletions pkg/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,15 @@ type ModuleLinterConfig struct {
ExcludeRules ModuleExcludeRules
}
type ModuleLinterRules struct {
DefinitionFileRule RuleConfig
OSSRule RuleConfig
ConversionRule RuleConfig
HelmignoreRule RuleConfig
LicenseRule RuleConfig
RequarementsRule RuleConfig
PackageYAMLRule RuleConfig
LegacyReleaseFileRule RuleConfig
DefinitionFileRule RuleConfig
OSSRule RuleConfig
ConversionRule RuleConfig
HelmignoreRule RuleConfig
LicenseRule RuleConfig
RequarementsRule RuleConfig
PackageYAMLRule RuleConfig
ModulePackageConsistencyRule RuleConfig
LegacyReleaseFileRule RuleConfig
}
type OSSRuleSettings struct {
Disable bool
Expand Down
17 changes: 9 additions & 8 deletions pkg/config/global/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,15 @@ type ModuleLinterConfig struct {
}

type ModuleLinterRules struct {
DefinitionFileRule RuleConfig `mapstructure:"definition-file"`
OSSRule RuleConfig `mapstructure:"oss"`
ConversionRule RuleConfig `mapstructure:"conversion"`
HelmignoreRule RuleConfig `mapstructure:"helmignore"`
LicenseRule RuleConfig `mapstructure:"license"`
RequarementsRule RuleConfig `mapstructure:"requarements"`
PackageYAMLRule RuleConfig `mapstructure:"package-yaml"`
LegacyReleaseFileRule RuleConfig `mapstructure:"legacy-release-file"`
DefinitionFileRule RuleConfig `mapstructure:"definition-file"`
OSSRule RuleConfig `mapstructure:"oss"`
ConversionRule RuleConfig `mapstructure:"conversion"`
HelmignoreRule RuleConfig `mapstructure:"helmignore"`
LicenseRule RuleConfig `mapstructure:"license"`
RequarementsRule RuleConfig `mapstructure:"requarements"`
PackageYAMLRule RuleConfig `mapstructure:"package-yaml"`
ModulePackageConsistencyRule RuleConfig `mapstructure:"module-package-consistency"`
LegacyReleaseFileRule RuleConfig `mapstructure:"legacy-release-file"`
}

type TemplatesLinterConfig struct {
Expand Down
1 change: 1 addition & 0 deletions pkg/linters/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func (l *Module) Run(m *module.Module) {
CheckFiles(m, errorList.WithMaxLevel(l.cfg.Rules.LicenseRule.GetLevel()))
rules.NewRequirementsRule().CheckRequirements(m.GetPath(), errorList.WithMaxLevel(l.cfg.Rules.RequarementsRule.GetLevel()))
rules.NewPackageYAMLRule().CheckPackageYAML(m.GetPath(), errorList.WithMaxLevel(l.cfg.Rules.PackageYAMLRule.GetLevel()))
rules.NewModulePackageConsistencyRule().CheckModulePackageConsistency(m.GetPath(), errorList.WithMaxLevel(l.cfg.Rules.ModulePackageConsistencyRule.GetLevel()))
rules.NewLegacyReleaseFileRule().CheckLegacyReleaseFile(m.GetPath(), errorList.WithMaxLevel(l.cfg.Rules.LegacyReleaseFileRule.GetLevel()))
}

Expand Down
259 changes: 259 additions & 0 deletions pkg/linters/module/rules/module_package_consistency.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,259 @@
/*
Copyright 2026 Flant JSC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package rules

import (
"strings"

"github.com/deckhouse/dmt/pkg"
"github.com/deckhouse/dmt/pkg/errors"
)

const ModulePackageConsistencyRuleName = "module-package-consistency"

// NewModulePackageConsistencyRule creates a rule for cross-validating module.yaml against package.yaml.
func NewModulePackageConsistencyRule() *ModulePackageConsistencyRule {
return &ModulePackageConsistencyRule{
RuleMeta: pkg.RuleMeta{
Name: ModulePackageConsistencyRuleName,
},
}
}

// ModulePackageConsistencyRule checks that module.yaml and package.yaml do not diverge
// when both files exist in the module directory.
type ModulePackageConsistencyRule struct {
pkg.RuleMeta
}

// CheckModulePackageConsistency compares overlapping fields between module.yaml and package.yaml.
// Skips modules that have only one of the two files — without both there is nothing to cross-validate.
func (r *ModulePackageConsistencyRule) CheckModulePackageConsistency(modulePath string, errorList *errors.LintRuleErrorsList) {
errorList = errorList.WithRule(r.GetName())

Comment thread
ldmonster marked this conversation as resolved.
module, err := getDeckhouseModule(modulePath, errorList.WithFilePath(ModuleConfigFilename))
if err != nil {
return
}

// load package.yaml separately so its parse errors are reported under its own file path
packageYAML, err := getModulePackage(modulePath, errorList.WithFilePath(PackageConfigFilename))
if err != nil {
return
}

if module == nil || packageYAML == nil {
return
}

compareNames(module, packageYAML, errorList)
compareDeckhouse(module, packageYAML, errorList)
compareKubernetes(module, packageYAML, errorList)
compareModules(module, packageYAML, errorList)
}

// compareNames ensures module.yaml name matches package.yaml name.
func compareNames(module *DeckhouseModule, packageYAML *ModulePackage, errorList *errors.LintRuleErrorsList) {
if module.Name == "" {
return
}

if module.Name != packageYAML.Name {
errorList.WithFilePath(ModuleConfigFilename).Errorf("module.yaml name %q does not match package.yaml name %q", module.Name, packageYAML.Name)
}
}

// compareDeckhouse ensures requirements.deckhouse in module.yaml matches requirements.deckhouse.constraint in package.yaml.
func compareDeckhouse(module *DeckhouseModule, packageYAML *ModulePackage, errorList *errors.LintRuleErrorsList) {
if module.Requirements == nil || module.Requirements.Deckhouse == "" {
return
}

moduleConstraint := strings.TrimSpace(module.Requirements.Deckhouse)

if packageYAML.Requirements == nil {
errorList.WithFilePath(ModuleConfigFilename).Errorf("module.yaml requirements.deckhouse is %q but package.yaml has no requirements section", moduleConstraint)
return
}

pkgConstraint := strings.TrimSpace(packageYAML.Requirements.Deckhouse.Constraint)

if pkgConstraint == "" {
errorList.WithFilePath(ModuleConfigFilename).Errorf("module.yaml requirements.deckhouse is %q but package.yaml requirements.deckhouse.constraint is empty", moduleConstraint)
return
}

if moduleConstraint != pkgConstraint {
errorList.WithFilePath(ModuleConfigFilename).Errorf("module.yaml requirements.deckhouse %q does not match package.yaml requirements.deckhouse.constraint %q", moduleConstraint, pkgConstraint)
}
}

// compareKubernetes ensures requirements.kubernetes in module.yaml matches requirements.kubernetes.constraint in package.yaml.
func compareKubernetes(module *DeckhouseModule, packageYAML *ModulePackage, errorList *errors.LintRuleErrorsList) {
if module.Requirements == nil || module.Requirements.Kubernetes == "" {
return
}

moduleConstraint := strings.TrimSpace(module.Requirements.Kubernetes)

if packageYAML.Requirements == nil {
errorList.WithFilePath(ModuleConfigFilename).Errorf("module.yaml requirements.kubernetes is %q but package.yaml has no requirements section", moduleConstraint)
return
}

pkgConstraint := strings.TrimSpace(packageYAML.Requirements.Kubernetes.Constraint)

if pkgConstraint == "" {
errorList.WithFilePath(ModuleConfigFilename).Errorf("module.yaml requirements.kubernetes is %q but package.yaml requirements.kubernetes.constraint is empty", moduleConstraint)
return
}

if moduleConstraint != pkgConstraint {
errorList.WithFilePath(ModuleConfigFilename).Errorf("module.yaml requirements.kubernetes %q does not match package.yaml requirements.kubernetes.constraint %q", moduleConstraint, pkgConstraint)
}
}

// compareModules cross-validates module dependency lists between module.yaml and package.yaml.
// In module.yaml dependencies are a flat map (optional ones carry the "!optional" suffix),
// while package.yaml splits them into mandatory and conditional groups.
func compareModules(module *DeckhouseModule, packageYAML *ModulePackage, errorList *errors.LintRuleErrorsList) {
if module.Requirements == nil || len(module.Requirements.ParentModules) == 0 {
if packageYAML.Requirements != nil && (len(packageYAML.Requirements.Modules.Mandatory) > 0 || len(packageYAML.Requirements.Modules.Conditional) > 0) {
checkPackageModulesNotInModule(module, packageYAML, errorList.WithFilePath(PackageConfigFilename))
}

return
}

if packageYAML.Requirements == nil {
for name := range module.Requirements.ParentModules {
errorList.WithFilePath(ModuleConfigFilename).Errorf("module.yaml module %q has requirement but package.yaml has no requirements section", name)
}

return
}

moduleMandatory := make(map[string]string)
moduleConditional := make(map[string]string)

for name, constraint := range module.Requirements.ParentModules {
constraint = strings.TrimSpace(constraint)

if strings.Contains(constraint, "!optional") {
moduleConditional[name] = strings.TrimSpace(strings.ReplaceAll(constraint, "!optional", ""))
} else {
moduleMandatory[name] = constraint
}
Comment thread
ldmonster marked this conversation as resolved.
}

pkgMandatory := make(map[string]string)
for _, m := range packageYAML.Requirements.Modules.Mandatory {
pkgMandatory[m.Name] = strings.TrimSpace(m.Constraint)
}

pkgConditional := make(map[string]string)
for _, m := range packageYAML.Requirements.Modules.Conditional {
pkgConditional[m.Name] = strings.TrimSpace(m.Constraint)
}

// module.yaml mandatory -> package.yaml mandatory
for name, constraint := range moduleMandatory {
pkgConstraint, exists := pkgMandatory[name]
if !exists {
if _, isConditional := pkgConditional[name]; isConditional {
errorList.WithFilePath(ModuleConfigFilename).Errorf("module.yaml module %q is mandatory but package.yaml lists it as conditional", name)
} else {
errorList.WithFilePath(ModuleConfigFilename).Errorf("module.yaml module %q is mandatory but not found in package.yaml requirements.modules.mandatory", name)
}

continue
}

if constraint != pkgConstraint {
errorList.WithFilePath(ModuleConfigFilename).Errorf("module.yaml module %q constraint %q does not match package.yaml constraint %q", name, constraint, pkgConstraint)
}
}

// module.yaml conditional -> package.yaml conditional
for name, constraint := range moduleConditional {
pkgConstraint, exists := pkgConditional[name]
if !exists {
if _, isMandatory := pkgMandatory[name]; isMandatory {
errorList.WithFilePath(ModuleConfigFilename).Errorf("module.yaml module %q is optional but package.yaml lists it as mandatory", name)
} else {
errorList.WithFilePath(ModuleConfigFilename).Errorf("module.yaml module %q is optional but not found in package.yaml requirements.modules.conditional", name)
}

continue
}

if constraint != pkgConstraint {
errorList.WithFilePath(ModuleConfigFilename).Errorf("module.yaml module %q constraint %q does not match package.yaml constraint %q", name, constraint, pkgConstraint)
}
}

// package.yaml mandatory -> module.yaml
for name := range pkgMandatory {
if _, exists := moduleMandatory[name]; exists {
continue
}

if _, exists := moduleConditional[name]; exists {
continue
}

errorList.WithFilePath(PackageConfigFilename).Errorf("package.yaml module %q is mandatory but not found in module.yaml requirements.modules", name)
}

// package.yaml conditional -> module.yaml
for name := range pkgConditional {
if _, exists := moduleConditional[name]; exists {
continue
}

if _, exists := moduleMandatory[name]; exists {
continue
}

errorList.WithFilePath(PackageConfigFilename).Errorf("package.yaml module %q is conditional but not found in module.yaml requirements.modules", name)
}
}

// checkPackageModulesNotInModule reports package.yaml modules that are missing from module.yaml.
func checkPackageModulesNotInModule(module *DeckhouseModule, packageYAML *ModulePackage, errorList *errors.LintRuleErrorsList) {
for _, m := range packageYAML.Requirements.Modules.Mandatory {
if module.Requirements == nil || module.Requirements.ParentModules == nil {
errorList.Errorf("package.yaml module %q is mandatory but module.yaml has no requirements.modules", m.Name)
continue
}

if _, exists := module.Requirements.ParentModules[m.Name]; !exists {
errorList.Errorf("package.yaml module %q is mandatory but not found in module.yaml requirements.modules", m.Name)
}
}

for _, m := range packageYAML.Requirements.Modules.Conditional {
if module.Requirements == nil || module.Requirements.ParentModules == nil {
errorList.Errorf("package.yaml module %q is conditional but module.yaml has no requirements.modules", m.Name)
continue
}

if _, exists := module.Requirements.ParentModules[m.Name]; !exists {
errorList.Errorf("package.yaml module %q is conditional but not found in module.yaml requirements.modules", m.Name)
}
}
}
Loading
Loading