Skip to content

Conversation

roguepikachu
Copy link

@roguepikachu roguepikachu commented Oct 17, 2025

This PR implements a new optional check called sorted-keys that validates whether YAML keys in Kubernetes manifests are sorted in alphabetical order, helping improve consistency and readability.

Fixes #451

How It Works

The check uses gopkg.in/yaml.v3 to parse raw YAML bytes and preserve the original key ordering. It recursively traverses the YAML tree, extracts keys from each mapping node, compares them with their alphabetically sorted version, and reports violations with the exact path location.

  • Recursive checking at all nesting levels (configurable)
  • Handles arrays of objects (containers, volumes, tolerations, etc.)
  • Uses standard lexicographic sorting
  • Reports precise paths (e.g., spec.template.spec.containers[0])

Why Raw YAML? Go structs don't preserve field order. The check requires access to original YAML bytes via object.Metadata.Raw to accurately detect key ordering.

Usage

Since this check is disabled by default, users must explicitly enable it:

# Enable sorted-keys along with default checks
kube-linter lint my-manifests/ --include sorted-keys

# Run only sorted-keys check
kube-linter lint my-manifests/ --do-not-auto-add-defaults --include sorted-keys

Or via configuration file:

checks:
  include:
    - sorted-keys

Note

This check is disabled by default, users must explicitly enable it

Copy link

codecov bot commented Oct 17, 2025

Codecov Report

❌ Patch coverage is 65.57377% with 42 lines in your changes missing coverage. Please review.
✅ Project coverage is 26.43%. Comparing base (dbd7529) to head (c4a039f).
⚠️ Report is 186 commits behind head on main.

Files with missing lines Patch % Lines
...templates/sortedkeys/internal/params/gen-params.go 0.00% 19 Missing ⚠️
pkg/lintcontext/mocks/context.go 0.00% 13 Missing ⚠️
pkg/templates/sortedkeys/template.go 88.88% 7 Missing and 3 partials ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##             main    #1042       +/-   ##
===========================================
- Coverage   62.36%   26.43%   -35.93%     
===========================================
  Files         197      233       +36     
  Lines        4854     7304     +2450     
===========================================
- Hits         3027     1931     -1096     
- Misses       1439     5218     +3779     
+ Partials      388      155      -233     
Flag Coverage Δ
unit 26.43% <65.57%> (-35.93%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@roguepikachu
Copy link
Author

roguepikachu commented Oct 17, 2025

Hi @rhybrillou, @rukletsov , can I get a review on this Thanks! I stumbled across this while looking for hacktoberfest. If this PR is okay please add the label hacktoberfest-accepted post merge.

@roguepikachu
Copy link
Author

Hi @janisz, could you please take a look at this PR when you get a chance? It seems things have been a bit quiet on the Slack channels lately.

@janisz janisz self-requested a review October 19, 2025 07:51
@janisz janisz self-assigned this Oct 19, 2025
@janisz janisz added hacktoberfest Hacktoberfest is a month-long celebration of open source projects hacktoberfest-accepted labels Oct 19, 2025
@janisz
Copy link
Collaborator

janisz commented Oct 19, 2025

I'll take a deeper look tomorrow. So far I wonder if there's a yaml linter that we could embed and maybe get this for free and some additional features like trailing spaces, tabs/spaces etc. I only know https://github.com/adrienverge/yamllint

Another thing that we need to test is how anchors, aliases, and overrides are handled.

I agree slack is silent and I do not look at it. I was thinking about migration to GitHub issues or discussions to keep everything in a single place or maybe a channel on k8s slack/discord https://www.kubernetes.dev/blog/2025/06/16/changes-to-kubernetes-slack-2025/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

hacktoberfest Hacktoberfest is a month-long celebration of open source projects hacktoberfest-accepted new-check

Projects

None yet

Development

Successfully merging this pull request may close these issues.

check for yaml key alphabetic sort

2 participants