-
Notifications
You must be signed in to change notification settings - Fork 3
[dmt] fix add mount points validation rule #401
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
abb6a8b
add mount points validation rule
diyliv 2913fc0
add mount points exclude and docs
diyliv 944f8e5
fix mount points early return and exclude normalization
diyliv 57afb3f
warn missing mount points dir
diyliv 0ba8af9
document exclude paths in readme
diyliv e745fc0
add container mount points reverse rule
diyliv 191b6f7
fix lint issues in mount points
diyliv e8ad992
fix gofmt struct alignment
diyliv ad2315f
Merge remote-tracking branch 'origin/main' into fix/mount-points-vali…
diyliv 75ec085
resolve merge conflict in module.go: DirectoryRuleExcludeList vs Moun…
diyliv 6f2e391
replace .dmt.yaml with dmtlint.yaml in mount-points docs
diyliv 4430dd3
Merge branch 'main' into fix/mount-points-validation-rule
ldmonster d20eb6c
Merge branch 'main' into fix/mount-points-validation-rule
ldmonster c28536b
Merge branch 'main' into fix/mount-points-validation-rule
ldmonster aa5216a
fix copilot remarks
diyliv 3fe2e24
fix wsl lint
diyliv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| /* | ||
| 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 ( | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| "sync" | ||
|
|
||
| "gopkg.in/yaml.v3" | ||
| corev1 "k8s.io/api/core/v1" | ||
|
|
||
| "github.com/deckhouse/dmt/internal/storage" | ||
| "github.com/deckhouse/dmt/pkg" | ||
| "github.com/deckhouse/dmt/pkg/errors" | ||
| ) | ||
|
|
||
| const ( | ||
| MountPointsRuleName = "mount-points" | ||
| ) | ||
|
|
||
| type mountPointsFile struct { | ||
| Dirs []string `yaml:"dirs"` | ||
| } | ||
|
|
||
| func NewMountPointsRule(excludeRules []pkg.StringRuleExclude, modulePath string) *MountPointsRule { | ||
| return &MountPointsRule{ | ||
| RuleMeta: pkg.RuleMeta{ | ||
| Name: MountPointsRuleName, | ||
| }, | ||
| StringRule: pkg.StringRule{ | ||
| ExcludeRules: excludeRules, | ||
| }, | ||
| mountPointsDirs: collectMountPointsDirs(modulePath), | ||
| } | ||
| } | ||
|
|
||
| type MountPointsRule struct { | ||
| pkg.RuleMeta | ||
| pkg.StringRule | ||
| mountPointsDirs map[string]bool | ||
| } | ||
|
|
||
| // CheckMountPaths verifies that every volumeMount.mountPath in pod controllers | ||
| // is declared in at least one mount-points.yaml file in the module. | ||
| // | ||
| // Direction: templates → mount-points.yaml (reverse of the existing templates rule). | ||
| func (r *MountPointsRule) CheckMountPaths(object storage.StoreObject, containers []corev1.Container, errorList *errors.LintRuleErrorsList) { | ||
| errorList = errorList.WithRule(r.GetName()).WithFilePath(object.ShortPath()) | ||
|
|
||
| if len(r.mountPointsDirs) == 0 { | ||
| return | ||
| } | ||
|
|
||
| switch object.Unstructured.GetKind() { | ||
| case "Deployment", "DaemonSet", "StatefulSet": | ||
| default: | ||
| return | ||
| } | ||
|
|
||
| for _, container := range containers { | ||
| for _, vm := range container.VolumeMounts { | ||
| normalizedPath := strings.TrimRight(vm.MountPath, "/") | ||
| if !r.Enabled(normalizedPath) { | ||
| continue | ||
| } | ||
|
|
||
| if !r.mountPointsDirs[normalizedPath] { | ||
| errorList.WithObjectID(object.Identity()). | ||
| Warnf("Container %q mountPath %q is not declared in any mount-points.yaml", container.Name, vm.MountPath) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| var mpDirsCache sync.Map // map[string]map[string]bool | ||
|
|
||
| // collectMountPointsDirs walks the module directory and collects all dirs | ||
| // from mount-points.yaml files into a set keyed by normalized path. | ||
| // Results are cached per module path. | ||
| func collectMountPointsDirs(modulePath string) map[string]bool { | ||
| if cached, ok := mpDirsCache.Load(modulePath); ok { | ||
| return cached.(map[string]bool) | ||
| } | ||
|
|
||
| dirs := make(map[string]bool) | ||
|
|
||
| searchDir := filepath.Join(modulePath, "images") | ||
| if _, err := os.Stat(searchDir); os.IsNotExist(err) { | ||
| mpDirsCache.Store(modulePath, dirs) | ||
| return dirs | ||
| } | ||
|
|
||
| _ = filepath.Walk(modulePath, func(path string, info os.FileInfo, err error) error { | ||
| if err != nil { | ||
| return nil | ||
| } | ||
|
|
||
| if info.IsDir() { | ||
| return nil | ||
| } | ||
|
|
||
| if filepath.Base(path) != "mount-points.yaml" { | ||
| return nil | ||
| } | ||
|
|
||
| data, err := os.ReadFile(path) | ||
| if err != nil { | ||
| return nil | ||
| } | ||
|
|
||
| var mpf mountPointsFile | ||
| if err := yaml.Unmarshal(data, &mpf); err != nil { | ||
| return nil | ||
| } | ||
|
|
||
| for _, dir := range mpf.Dirs { | ||
| dirs[strings.TrimRight(dir, "/")] = true | ||
| } | ||
|
|
||
| return nil | ||
| }) | ||
|
|
||
| mpDirsCache.Store(modulePath, dirs) | ||
|
|
||
| return dirs | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.