opt-nc / yamlfixer

Automates the fixing of problems reported by yamllint by parsing its output
https://dev.to/adriens/series/18168
GNU General Public License v3.0
55 stars 8 forks source link

ValueError exception on un-indented kustomize resources #207

Open alvarogonzalez-packlink opened 1 year ago

alvarogonzalez-packlink commented 1 year ago

1️⃣ Description

If I try to run yamlfixer on a simple kustomization.yaml file where the resources section is unindented, I get a ValueError exception

📑 Steps to Reproduce

Steps to reproduce the behavior:

I have this kustomization.yaml:

---
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../../base

I run yamlfixer on it and it fails:

% yamlfixer -dn ./apps/elasticsearch-eck/overlays/devops/kustomization.yaml
DEBUG: yamlfixer v0.9.15
DEBUG: arguments=Namespace(backup=False, backupsuffix='.orig', debug=True, diffto='/dev/null', ext='yaml,yml,yamllint', forcecolors=False, followsymlinks=False, listfixers=False, nosyntax=False, nochange=True, recurse=0, jsonsummary=False, plainsummary=False, summary=False, tabsize=2, config_file=None, config_data=None, filenames=['./apps/elasticsearch-eck/overlays/devops/kustomization.yaml'])
DEBUG: Fixing ./apps/elasticsearch-eck/overlays/devops/kustomization.yaml ...
DEBUG: Executing linter with 'yamllint --format parsable --strict -'
DEBUG: Linter's exit code is 1
DEBUG: (5+0, 1+0) => [wrong indentation: expected at least 1 (indentation)]
DEBUG: Calling fix_wrong_indentation("", "- ../../base")
Traceback (most recent call last):
  File "/Users/alvaro/.local/bin/yamlfixer", line 8, in <module>
    sys.exit(run())
  File "/Users/alvaro/.local/pipx/venvs/yamlfixer-opt-nc/lib/python3.10/site-packages/yamlfixer/__main__.py", line 151, in run
    return yfixer.fix()
  File "/Users/alvaro/.local/pipx/venvs/yamlfixer-opt-nc/lib/python3.10/site-packages/yamlfixer/yamlfixer.py", line 167, in fix
    (status, unidiff) = filetofix.fix()
  File "/Users/alvaro/.local/pipx/venvs/yamlfixer-opt-nc/lib/python3.10/site-packages/yamlfixer/filefixer.py", line 211, in fix
    handled = ProblemFixer(self, linenumber, colnumber, problem)()
  File "/Users/alvaro/.local/pipx/venvs/yamlfixer-opt-nc/lib/python3.10/site-packages/yamlfixer/problemfixer.py", line 55, in __call__
    getattr(self, methodname)(left, right)
  File "/Users/alvaro/.local/pipx/venvs/yamlfixer-opt-nc/lib/python3.10/site-packages/yamlfixer/problemfixer.py", line 256, in fix_wrong_indentation
    expected = int(parts[3])
ValueError: invalid literal for int() with base 10: 'at'

I indent the resources section:

---
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
  - ../../base

Now it works perfectly:

% yamlfixer -dn ./apps/elasticsearch-eck/overlays/devops/kustomization.yaml
DEBUG: yamlfixer v0.9.15
DEBUG: arguments=Namespace(backup=False, backupsuffix='.orig', debug=True, diffto='/dev/null', ext='yaml,yml,yamllint', forcecolors=False, followsymlinks=False, listfixers=False, nosyntax=False, nochange=True, recurse=0, jsonsummary=False, plainsummary=False, summary=False, tabsize=2, config_file=None, config_data=None, filenames=['./apps/elasticsearch-eck/overlays/devops/kustomization.yaml'])
DEBUG: Fixing ./apps/elasticsearch-eck/overlays/devops/kustomization.yaml ...
DEBUG: Executing linter with 'yamllint --format parsable --strict -'
DEBUG: Linter's exit code is 0
DEBUG: passed linter's strict mode

2️⃣ Expected behavior

3️⃣ Scripts

N/A

ℹ️ Environment details

tamere-allo-peter commented 1 year ago

I think you've installed a recent version of yamllint, for which the lint output formatting has changed a bit. I need to work to bring yamlfixer back up-to-date wrt yamllint, unfortunately this will have to wait until I've got more time on my hands. Please could you install an earlier version of yamllint in the meantime, this should solve this problem (yamllint v1.26 should be fine) ?

alvarogonzalez-packlink commented 1 year ago

Yep, now that I check, last yamllint version working is 1.28.0. With 1.29.0 fails.

1.29 says:

./file.yaml:5:1: [error] wrong indentation: expected at least 1 (indentation)

1.28 says:

./file.yaml:5:1: [error] wrong indentation: expected 2 but found 0 (indentation)

Maybe, meanwhile, the install_requires can be changed from yamllint >= 1.27.1 to `yamllint >= 1.27.1, <1.29' or something like that?

Edit: the change: adrienverge/yamllint#491

tamere-allo-peter commented 1 year ago

Yes in the meantime that could be a solution. Anyway thanks a lot for your feedback and your help in diagnosing this problem. Have a nice day.

alvarogonzalez-packlink commented 1 year ago

Thank you too!