crumbhole / argocd-lovely-plugin

A plugin to make Argo CD behave like we'd like.
BSD 3-Clause "New" or "Revised" License
356 stars 22 forks source link

Help wanted with dynamic kustomize resource configuration. #429

Open AbstractVersion opened 4 months ago

AbstractVersion commented 4 months ago

I am trying to set a dynamic configuration of manifests using Argocd, kustomize, helm and the lovely plugin. For example want to dynamically change the host of an ingress resource.

This is a reference kustomization file that uses grafana helm chart:

# kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

helmCharts:
- name: grafana
  includeCRDs: true
  releaseName: grafana
  version: 7.3.0
  repo: https://grafana.github.io/helm-charts
  valuesFile: values.yaml
  # namespace: demo

I would like to dynamically configure the ingress host name through the argo application or application set. I saw on the docs that I can use LOVELY_KUSTOMIZE_PATCH env variable to set kustomization patches. My issue is that I cant understand how to define the target resource (eg. kind :Ingress, name: grafana-ingress). An example Argocd Application is the following

apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: kustomize-grafana-helm
  namespace: argocd
  finalizers:
    - resources-finalizer.argocd.argoproj.io
spec:
  destination:
    namespace: demo
    server: https://kubernetes.default.svc
  project: default
  source:
    path: resources/kustomize/grafana # <- points to the above kustomization file
    repoURL: <eg-repo>
    targetRevision: HEAD
    plugin:
      name: lovely-vault-plugin
      env:
        - name: LOVELY_KUSTOMIZE_PATCH
          value: '[{"op": "replace", "path": "/spec/rules/0/host", "value": "grafana-test.demo.com"}]'

The problem with the above application is that I don't see how can I reference the Ingress resource and naturally argocd throws an error of :

Failed to load target state: failed to generate manifest for source 1 of 1: rpc error: code = Unknown desc = Manifest generation error (cached): plugin sidecar failed. error generating manifests in cmp: rpc error: code = Unknown desc = error generating manifests: `argocd-lovely-plugin` failed exit status 1: 2024/02/24 15:30:22 replace operation does not apply: doc is missing path: /spec/rules/0/host: missing value

I might not be in the right path to do this, so any suggestions would be much appreciated.

Thanks in advance.

Joibel commented 4 months ago

LOVELY_KUSTOMIZE_PATCH is patching the kustomization.yaml file.

It can do what you want, but you need to write the correct patch. Start by writing what you need into a kustomization.yaml without the patching happening. This will need to identify the object needed to be patched (an ingress). You can then convert this into an appropriate patch.

What you have written there is patching kustomization.yaml as though it were the ingress object.