Question

Format Kubernetes Yaml file into VSCode

Kubernetes YAML file, which I copied from PDF file. It should be nice, if I could format the file in order to have the correct indentation of the k8s file

What I have now:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: minimal-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
ingressClassName: nginx-example
rules:
- http:
paths:
- path: /testpath
pathType: Prefix
backend:
service:
name: test
port:
number: 80

What would I like to have

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: minimal-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  ingressClassName: nginx-example
  rules:
  - http:
      paths:
      - path: /testpath
        pathType: Prefix
        backend:
          service:
            name: test
            port:
              number: 80

Additional information: from my current vscode (1.73.1) I have already installed both the following extensions

Name: YAML
Id: redhat.vscode-yaml
Description: YAML Language Support by Red Hat, with built-in Kubernetes syntax support
Version: 1.10.1
Publisher: Red Hat
VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=redhat.vscode-yaml

and

Name: Kubernetes YAML Formatter
Id: kennylong.kubernetes-yaml-formatter
Description: A better YAML formatter for DevOps like Kubernetes, Ansible, CI/CD, etc.
Version: 1.1.0
Publisher: kennylong
VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=kennylong.kubernetes-yaml-formatter

Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
March 27, 2023

Hi there,

I don’t think that there is something out of the box that will work for more complex Kubernetes YAML files.

The closes that I’ve seen so far is the YAML extension by Red Hat:

https://marketplace.visualstudio.com/items?itemName=redhat.vscode-yaml

Once you install it, you can right-click on the YAML file’s content and select “Format Document” from the context menu.

Unfortunately, it does not seem to work for all cases.

What I personally do is to use the kubeval tool to validate my YAML files:

https://www.kubeval.com/

Hope that this helps!

Best,

Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

card icon
Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Sign up
card icon
Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We’d like to help.

Learn more
card icon
Become a contributor

You get paid; we donate to tech nonprofits.

Learn more
Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow – whether you’re running one virtual machine or ten thousand.

Learn more ->
DigitalOcean Cloud Control Panel
Get started for free

Enter your email to get $200 in credit for your first 60 days with DigitalOcean.

New accounts only. By submitting your email you agree to our Privacy Policy.

© 2023 DigitalOcean, LLC.