Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for in-line generic patches to Flux Kustomization API #364

Merged
merged 2 commits into from
Jun 14, 2021

Conversation

glebiller
Copy link
Contributor

@glebiller glebiller commented Jun 10, 2021

The patchesJson6902 or patchesStrategicMerge fields in a Kustomize resource only allow patching single named resources. In order to patch multiple resources with a single patch, the generic patches field need to be used.

This PR implement the generic patches at the kustomize.toolkit.fluxcd.io/v1beta1/Kustomization level to unlock that feature.

One use case, would be for instance to override all path of all Kustomization at once without having to patch then one by one:

apiVersion: kustomize.toolkit.fluxcd.io/v1beta1
kind: Kustomization
metadata:
  name: podinfo
  namespace: flux-system
spec:
  # ...omitted for brevity
  patches:
    - patch: |-
        - op: replace
          path: /spec/path
          value: ./deploy/lab/
      target:
        group: kustomize.toolkit.fluxcd.io
        version: v1beta1
        kind: Kustomization

The field options from the original Kustomization is not implemented

  options:
    allowNameChange: true
    allowKindChange: true

This PR can only be merge if pkg PR #109 is accepted.

Allow patching multiple resources instead of a single existing one as
StrategicMerge & JSON6902 are forced to target existing named resources.

Signed-off-by: Guillaume Le Biller <glebiller@Traveldoo.com>
@glebiller glebiller marked this pull request as draft June 11, 2021 07:05
@glebiller
Copy link
Contributor Author

Converted the PR to draft while waiting for pkg PR #109

To add [Kustomize `patches` entries](https://kubectl.docs.kubernetes.io/references/kustomize/kustomization/patches/)
to the configuration, and patch resources using either a [strategic merge](https://kubectl.docs.kubernetes.io/references/kustomize/glossary#patchstrategicmerge)
patch or a [JSON](https://kubectl.docs.kubernetes.io/references/kustomize/glossary#patchjson6902) patch,
`spec.patches` items must contain a `target` selector and a `patch` document.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

items must contain a target but in the blow example there is no target specified

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the catch :)

In the original Kustomize resource, it's possible to apply a strategic merge patch (not JSON6902) without specifying the target.
I decided to force the target to be present in the Flux implementation for simplicity. Maybe that is something could be added in the future.

@stefanprodan
Copy link
Member

One use case, would be for instance to override all path of all Kustomization at once without having to patch then one by one.

Having more than one Kustomization that points at the same path is not something you should ever do, as the controller will delete and recreate everything. Why are you doing this?

@stefanprodan
Copy link
Member

@Kissy please update apis/kustomize to v0.2.0 in both go.mod files and then run make tidy.

@glebiller
Copy link
Contributor Author

The use case I have in mind is:
each micro-services have their own git repository following that layout:

|- src & build files
|- deploy
    |- base
        |- Kustomization.yaml
        |- xxx.yaml
    |- test
        |- Kustomization.yaml
        |- patches.yaml
    |- prod 
        |- Kustmoization.yaml
        |- patches.yaml

Each git repository can be handled by a PR & permissions per teams with fine grain settings.
This layout follow the podinfo repository layout globally.

On the other side, the Infra team is managing a single repository with:

|- applications
    |- Kustomization.yaml
    |- app-1.yaml (1)
    |- app-2.yaml (2)
    |- ...
|- clusters
    |- test
        |- applications.yaml (3)
    |- prod
        |- applications.yaml (4)

(1) Would be a kustomize.toolkit.fluxcd.io/v1beta1/Kustomization source-ref to git repo of app-1
(2) Would be a kustomize.toolkit.fluxcd.io/v1beta1/Kustomization source-ref to git repo of app-2
(3) Would be creating a kustomize.toolkit.fluxcd.io/v1beta1/Kustomization with a patch targeting all kustomize.toolkit.fluxcd.io/v1beta1/Kustomization and overriding path to ./deploy/test
(4) Would be creating a kustomize.toolkit.fluxcd.io/v1beta1/Kustomization with a patch targeting all kustomize.toolkit.fluxcd.io/v1beta1/Kustomization and overriding path to ./deploy/prod

The Infra repo is clean and adding a new app / removing an app is done by adding or removing file in the applications folder.

@hiddeco
Copy link
Member

hiddeco commented Jun 11, 2021

I think this patch could be almost identically applied to the helm-controller which also has support for JSON6902 and strategic merge patches at the moment.

@stefanprodan
Copy link
Member

@hiddeco I'm not sure it would work in all cases, as the patch can be on disk. While KC has the full source locally, HC doesn't. I don't expect people to place patches inside the chart, but for Git could work...

@stefanprodan
Copy link
Member

each micro-services have their own git repository

Thanks @Kissy now I understand your use-case, the path is the same but the source is different so no conflicts 👌

@stefanprodan
Copy link
Member

@Kissy apis/kustomize v0.2.0 contains the Patch field you've added to fluxcd/pkg.

Signed-off-by: Guillaume Le Biller <glebiller@Traveldoo.com>
@glebiller glebiller marked this pull request as ready for review June 11, 2021 14:47
@glebiller
Copy link
Contributor Author

Successfully ran a test with an external cluster with:

    - patch: |-
        - op: add
          path: /metadata/labels/app.kubernetes.io~1instance
          value: podinfo-abcxzy
      target:
        labelSelector: app=podinfo

@stefanprodan stefanprodan added area/kustomize Kustomize related issues and pull requests enhancement New feature or request labels Jun 14, 2021
@stefanprodan stefanprodan changed the title Implement generic patches in Kustomization Add support for in-line generic patches to Flux Kustomization API Jun 14, 2021
Copy link
Member

@stefanprodan stefanprodan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Thanks @Kissy 🏅

@stefanprodan stefanprodan merged commit 6672928 into fluxcd:main Jun 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/kustomize Kustomize related issues and pull requests enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants