Coder Social home page Coder Social logo

longkai / kubernetes-yaml-formatter Goto Github PK

View Code? Open in Web Editor NEW
38.0 5.0 2.0 734 KB

Kubernetes Style YAML Formatter for Visual Studio Code

License: MIT License

TypeScript 100.00%
cicd devops formatter-plugin kubernetes vscode-extension ansible github-actions yaml

kubernetes-yaml-formatter's Introduction

Kubernetes Style YAML Formatter for Visual Studio Code

A Visual Studio Code extension that provides format support for YAML language.

You find the right format extension if you come from DevOps world, e.g., Kubernetes, Ansible Playbooks or CI workflow like Github Actions and so on.

We all love Emojis, and yes, it has builtin support!

Features

Format YAML

As a Kubernetes developer, aka YAML engineer, you deal with a lot of yaml files everyday. Take the k8s yaml for example:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 2
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80

As you see, the sequence style(containers and ports) in the k8s style yaml has no indent. Each time you play with kubectl, there is no indent. Not to mention some tool like kustomize requires you do that.

Unluckily, the builtin yaml format has its own option philosophy. It always does indent and resists to accept such a customization setting.

It's useless. I have to keep the style manually every time editing yaml files, or you will end up with a non-idiomatic one.

It's tedious. So I create this extension to make life easier. Now you can control which way you prefer and everyone is happy.

Enjoy!

Extension Settings

This extension contributes the following settings:

  • kubernetes-yaml-formatter.compactSequenceIndent: Enable compact sequence indent, i.e. no indent (default true).
  • kubernetes-yaml-formatter.includeDocumentStart: Include --- at document start (default false).

It makes format yaml on save default to true, you can disable it:

"[yaml]": {
  "editor.formatOnSave": false
}

Thanks

Thanks lupengpeng for the icon design. It's great!

Thanks the following projects:

Without them there wouldn't be this extension.

Throughout the process I learnt a lot and also made some contributions back to them.

Definitely welcome your patches and contributions!

kubernetes-yaml-formatter's People

Contributors

ajhall avatar longkai avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

kubernetes-yaml-formatter's Issues

Do not convert emoji

Not sure if this is a bug or a feature request and whether it's actually an issue with the upstream, but the extension messes with the code if there are emojis in it.

For example:

- test: |
    🔥

Becomes:

- test: "\U0001F525\n"

This is particularly a problem if you have complex scripts that include emojis -- it converts the entire thing into a single-line string.

Key Stroke to Toggle

Is there a key stroke to toggle can that be in the docs? Is there a right click option to toggle in document?

Prevent inlineScript from formatting to a single line

When I am writing a multi-line inline script, the formatter will automatically take all of the lines and put them on one line with characters like \n to separate new lines. This makes editing the code later much more difficult to do. Is it possible to turn this feature off?

Here is an example
Before Formatting

        inlineScript: |
          scope=$(az resource show -g ${{ parameters.resourceGroup }} -n ${{ parameters.clusterName }} --resource-type "Microsoft.ContainerService/managedClusters" --query id -o tsv) 
          az role assignment create --role "b1ff04bb-8a4e-4dc4-8eb5-8693973ce19b" --assignee-object-id $(servicePrincipalId) --assignee-principal-type ServicePrincipal --scope $scope

After Formatting

        inlineScript: "scope=$(az resource show -g ${{ parameters.resourceGroup }} -n ${{ parameters.clusterName }} --resource-type \"Microsoft.ContainerService/managedClusters\" --query id -o tsv) \naz role assignment create --role \"b1ff04bb-8a4e-4dc4-8eb5-8693973ce19b\" --assignee-object-id $(servicePrincipalId) --assignee-principal-type ServicePrincipal --scope $scope\n"

Deleting commented code for ansible playbook

Hello.

This is example of code of Ansible playbook

- hosts: any.host
  gather_facts: no
  remote_user: root
  roles:
  - rsa-upd
  - check-users
  - ssh-authorization-keys
  - ssh-daemon-setup


# - hosts: some.host
#   gather_facts: yes
#   remote_user: root
#   roles:
#   - system-checks
#   - distrib-sync
#   - db-pak-update

If I use autoformat, commented part will be deleted.
But if it will not be list in root element, commented code is OK.

expexted behaviour: commented code is not deleted

Question: how to apply it to helm-template files?

This is great, finally sane indent for list in Kubernetes / Ansible yaml files.
Just a question - how can I make it also work if file is recognized as helm-template?

I tried to set

    "[helm-template]": {
        "editor.defaultFormatter": "kennylong.kubernetes-yaml-formatter"
    },

but VS Code still complains that "There is no formatter for 'helm' files installed.".

Extra characters added when formatting gitlab ci with yaml anchors

When formatting a yaml file with anchors, extra characters (!!merge) are added on format which invalidates the file.

This is an issue specifically with the <<: *partial anchor. Or even more specifically the << part of it.
https://docs.gitlab.com/ee/ci/yaml/yaml_optimization.html

Before save:

deploy-mr:
  <<: *mr_deployer_config
  script:
  - *set_env_params
  - npm run start
  rules:
  - if: $CI_MERGE_REQUEST_ID && $CI_PIPELINE_SOURCE == "parent_pipeline"

After save:

deploy-mr:
  !!merge <<: *mr_deployer_config
  script:
  - *set_env_params
  - npm run start
  rules:
  - if: $CI_MERGE_REQUEST_ID && $CI_PIPELINE_SOURCE == "parent_pipeline"

Yamllint conflict

Hi!

I have such diff after save YAML with this plugin:

- m1-db-tst11.domain.local   # DFINANS_TST
+ m1-db-tst11.domain.local  # DFINANS_TST

And yamllint is printing a warning:

warning  too few spaces before comment  (comments)

We are using --strict option to check our MR and it is not passing

Is number of spaces before comment configurable?

Format on save does not work with "editor.formatOnSaveMode": "modifications"

Format works when selecting >Format document.

However, with "editor.formatOnSaveMode": "modifications" in global user settings format on save does not work.
Format on save does work

  • without editor.formatOnSaveMode being set
  • with "editor.formatOnSaveMode": "file"

Project .vscode/settings:

  "[yaml]": {
    "editor.defaultFormatter": "kennylong.kubernetes-yaml-formatter",
    "editor.formatOnSave": true
  },
  "files.associations": {
    "*.yml": "yaml",
    "*.yaml": "yaml"
  },

"Include Document Start" option is broken

I'm running current version in VSCode (1.1.0).

Both checked and unchecked, the "Include Document Start" option is removing the pre-existing "---" from the beginning of all my yaml files.

I'd like for this extension to add the "---" when it's missing, but more importantly, I don't want it to be removed when it's already there.

Otherwise, this extension is helpful for standardizing all my manifests, thank you!

Incorrect settings declaration

vscode 1.74.0-insider
Kubernetes YAML Formatter v1.1.0

The registered settings show the settings without the prefix kubernetes-yaml-formatter..

In vscode Settings extension names Use Compact Sequence Indent (?) with two check-boxed settings Compact Sequence Indent and Include Document Start.

When the checkbox is enabled, json settings file gets settings without prefix kubernetes-yaml-formatter., just i.e. "includeDocumentStart": true, and vscode thinks it's okay. But the settings don't work. But if i set manually "kubernetes-yaml-formatter.includeDocumentStart": true, settings works when I format file, but vscode thinks it's unknown and unused settings, dimming them.

Misunderstanding <<:

I have this code:

restic:
  <<: *image

When I save with this extension enabled it does this:

restic:
  !!merge <<: *image

is there a way to disable this ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.