fluxcd / image-automation-controller

GitOps Toolkit controller that patches container image tags in Git
https://fluxcd.io
Apache License 2.0
155 stars 67 forks source link

Image Automation Controller is reformatting inline blocks in a single line (Flux 2.2.0) #616

Open ruiengana opened 6 months ago

ruiengana commented 6 months ago

related to https://github.com/fluxcd/image-automation-controller/issues/528

@stefanprodan we just upgraded to Flux 2.2.0 and problem is still occurring.

All our HelmRelease files getting picked up by the Image Automation Controller are getting inline blocks reformatted to single line blocks.

When a HelmRelease manifest has a block configuration like below and is updated by image automation controller, blocks are overwritten as a single line of code making it unreadable. Code blocks should not be reformatted.

For example, this is pipe block

  config: |
    _format_version: "2.1"
    _transform: true
    services:
    - name: example
      url: https://example.org
      routes:
      - name: example
        paths:
        - /example

After image automation controller commit becomes

  config: "_format_version: \"2.1\"\n    _transform: true\n    services:\n    - name: example\n      url: https://example.org\n      routes:\n      - name: example\n        paths:\n        - /example\n"

This behaviour is unexpected, we would like blocks to stay exactly as they were before.

stefanprodan commented 6 months ago

Can you post here flux check for the cluster where you are running image-automation-controller.

ruiengana commented 6 months ago

Can you post here flux check for the cluster where you are running image-automation-controller.

$ flux check
► checking prerequisites
✔ Kubernetes 1.27.7-eks-4f4795d >=1.26.0-0
► checking version in cluster
✔ distribution: flux-v2.2.0
✔ bootstrapped: true
► checking controllers
✔ helm-controller: deployment ready
► ghcr.io/fluxcd/helm-controller:v0.37.0
✔ image-automation-controller: deployment ready
► ghcr.io/fluxcd/image-automation-controller:v0.37.0
✔ image-reflector-controller: deployment ready
► ghcr.io/fluxcd/image-reflector-controller:v0.31.1
✔ kustomize-controller: deployment ready
► ghcr.io/fluxcd/kustomize-controller:v1.2.0
✔ notification-controller: deployment ready
► ghcr.io/fluxcd/notification-controller:v1.2.2
✔ source-controller: deployment ready
► ghcr.io/fluxcd/source-controller:v1.2.2
► checking crds
✔ alerts.notification.toolkit.fluxcd.io/v1beta3
✔ buckets.source.toolkit.fluxcd.io/v1beta2
✔ gitrepositories.source.toolkit.fluxcd.io/v1
✔ helmcharts.source.toolkit.fluxcd.io/v1beta2
✔ helmreleases.helm.toolkit.fluxcd.io/v2beta2
✔ helmrepositories.source.toolkit.fluxcd.io/v1beta2
✔ imagepolicies.image.toolkit.fluxcd.io/v1beta2
✔ imagerepositories.image.toolkit.fluxcd.io/v1beta2
✔ imageupdateautomations.image.toolkit.fluxcd.io/v1beta1
✔ kustomizations.kustomize.toolkit.fluxcd.io/v1
✔ ocirepositories.source.toolkit.fluxcd.io/v1beta2
✔ providers.notification.toolkit.fluxcd.io/v1beta3
✔ receivers.notification.toolkit.fluxcd.io/v1
✔ all checks passed
stefanprodan commented 6 months ago

Ok can you paste there the HelmRelease as it is in your Git repo, I want to see where the markers are.

AchazRyus commented 6 months ago

I found a solution working for me ! In fact it's when there is a space after a : when non needed in your deployment file. For example if you have : annotations: something: "" If you have a space after annotations:, or any other key, flux will commit and add \n everywhere

ruiengana commented 6 months ago

config: | _format_version: "2.1" _transform: true services:

Please find below a extract of the HelmRelease. Essentially we have an umbrella chart with couple subcharts, let's say subchart A and subchart B. Subchart A has a inline block config and no image automation markers. Subchart B doesn't have inline block configs but has image automation markers.

When image automation controller picks a change on subchart B, it also changes subchart A inline block config to single line.

apiVersion: helm.toolkit.fluxcd.io/v2beta2
kind: HelmRelease
spec:
  releaseName: <release>
  values:
    subchart-a:
      config: |
        _format_version: "2.1"
        _transform: true
        services:
        - name: example
          url: https://example.org
          routes:
          - name: example
            paths:
            - /example
    subchart-b:
      enabled: true
      image:
        repository: <repo> # {"$imagepolicy": "flux-system:repo:name"}
        tag: <tag> # {"$imagepolicy": "flux-system:repo:tag"}

PS: We also have inline configs in annotations, but those are always kept OK.

stefanprodan commented 6 months ago

@ruiengana do you mixed indentation in your YAML, tabs and spaces? If so, then that will trigger a full reformat.

ruiengana commented 6 months ago

@ruiengana do you mixed indentation in your YAML, tabs and spaces? If so, then that will trigger a full reformat.

We may very well have. Isn't that supported? Which one should we adopt to avoid the full reformat, tab or spaces?

stefanprodan commented 6 months ago

I've used tabs to write a test for these issues https://github.com/fluxcd/image-automation-controller/commit/05021967b0b80aea5d7519e542d73238655fc1fd

cimillas commented 6 months ago

In our case, the use of space is strictly necessary. Is there any intention to add support for yaml files with tabs and spaces mixed?

stefanprodan commented 6 months ago

Is there any intention to add support for yaml files with tabs and spaces mixed?

No, this restriction comes from Kubernetes Go YAML package, Flux does not uses it's own YAML formatter.

stefanprodan commented 6 months ago

@cimillas I suggest moving the subchart-a values to a ConfigMap, and use valuesFrom, this way you can format the HelmRelease properly.

wgorczyca commented 1 week ago

Reformat which causes merge multiple line to one line is still being triggered if there is space/whitespace at the end of any line, so make sure you don't have it to avoid this issue