github.com/argoproj/argo-cd/v2@v2.10.5/docs/user-guide/kustomize.md (about)

     1  # Kustomize
     2  
     3  The following configuration options are available for Kustomize:
     4  
     5  * `namePrefix` is a prefix appended to resources for Kustomize apps
     6  * `nameSuffix` is a suffix appended to resources for Kustomize apps
     7  * `images` is a list of Kustomize image overrides
     8  * `replicas` is a list of Kustomize replica overrides
     9  * `commonLabels` is a string map of additional labels
    10  * `forceCommonLabels` is a boolean value which defines if it's allowed to override existing labels
    11  * `commonAnnotations` is a string map of additional annotations
    12  * `namespace` is a Kubernetes resources namespace
    13  * `forceCommonAnnotations` is a boolean value which defines if it's allowed to override existing annotations
    14  * `commonAnnotationsEnvsubst` is a boolean value which enables env variables substition in annotation  values
    15  * `patches` is a list of Kustomize patches that supports inline updates
    16  * `components` is a list of Kustomize components
    17  
    18  To use Kustomize with an overlay, point your path to the overlay.
    19  
    20  !!! tip
    21      If you're generating resources, you should read up how to ignore those generated resources using the [`IgnoreExtraneous` compare option](compare-options.md).
    22  
    23  ## Patches
    24  Patches are a way to kustomize resources using inline configurations in Argo CD applications.  `patches`  follow the same logic as the corresponding Kustomization.  Any patches that target existing Kustomization file will be merged.
    25  
    26  This Kustomize example sources manifests from the `/kustomize-guestbook` folder of the `argoproj/argocd-example-apps` repository, and patches the `Deployment` to use port `443` on the container.
    27  ```yaml
    28  apiVersion: kustomize.config.k8s.io/v1beta1
    29  kind: Kustomization
    30  metadata:
    31    name: kustomize-inline-example
    32  namespace: test1
    33  resources:
    34    - https://raw.githubusercontent.com/argoproj/argocd-example-apps/master/kustomize-guestbook/
    35  patches:
    36    - target:
    37        kind: Deployment
    38        name: guestbook-ui
    39      patch: |-
    40        - op: replace
    41          path: /spec/template/spec/containers/0/ports/0/containerPort
    42          value: 443
    43  ```
    44  
    45  This `Application` does the equivalent using the inline `kustomize.patches` configuration.
    46  ```yaml
    47  apiVersion: argoproj.io/v1alpha1
    48  kind: Application
    49  metadata:
    50    name: kustomize-inline-guestbook
    51    namespace: argocd
    52    finalizers:
    53      - resources-finalizer.argocd.argoproj.io
    54  spec:
    55    destination:
    56      namespace: test1
    57      server: https://kubernetes.default.svc
    58    project: default
    59    source:
    60      path: kustomize-guestbook
    61      repoURL: https://github.com/argoproj/argocd-example-apps.git
    62      targetRevision: master
    63      kustomize:
    64        patches:
    65          - target:
    66              kind: Deployment
    67              name: guestbook-ui
    68            patch: |-
    69              - op: replace
    70                path: /spec/template/spec/containers/0/ports/0/containerPort
    71                value: 443
    72  ```
    73  
    74  The inline kustomize patches work well with `ApplicationSets`, too. Instead of maintaining a patch or overlay for each cluster, patches can now be done in the `Application` template and utilize attributes from the generators. For example, with [`external-dns`](https://github.com/kubernetes-sigs/external-dns/) to set the [`txt-owner-id`](https://github.com/kubernetes-sigs/external-dns/blob/e1adc9079b12774cccac051966b2c6a3f18f7872/docs/registry/registry.md?plain=1#L6) to the cluster name.
    75  
    76  ```yaml
    77  apiVersion: argoproj.io/v1alpha1
    78  kind: ApplicationSet
    79  metadata:
    80    name: external-dns
    81  spec:
    82    goTemplate: true
    83    goTemplateOptions: ["missingkey=error"]
    84    generators:
    85    - clusters: {}
    86    template:
    87      metadata:
    88        name: 'external-dns'
    89      spec:
    90        project: default
    91        source:
    92          repoURL: https://github.com/kubernetes-sigs/external-dns/
    93          targetRevision: v0.14.0
    94          path: kustomize
    95          kustomize:
    96            patches:
    97            - target:
    98                kind: Deployment
    99                name: external-dns
   100              patch: |-
   101                - op: add
   102                  path: /spec/template/spec/containers/0/args/3
   103                  value: --txt-owner-id={{.name}}   # patch using attribute from generator
   104        destination:
   105          name: 'in-cluster'
   106          namespace: default
   107  ```
   108  
   109  ## Components
   110  Kustomize [components](https://github.com/kubernetes-sigs/kustomize/blob/master/examples/components.md) encapsulate both resources and patches together. They provide a powerful way to modularize and reuse configuration in Kubernetes applications.
   111  
   112  Outside of Argo CD, to utilize components, you must add the following to the `kustomization.yaml` that the Application references. For example:
   113  ```yaml
   114  apiVersion: kustomize.config.k8s.io/v1beta1
   115  kind: Kustomization
   116  ...
   117  components:
   118  - ../component
   119  ```
   120  
   121  With support added for components in `v2.10.0`, you can now reference a component directly in the Application:
   122  ```yaml
   123  apiVersion: argoproj.io/v1alpha1
   124  kind: Application
   125  metadata:
   126    name: application-kustomize-components
   127  spec:
   128    ...
   129    source:
   130      path: examples/application-kustomize-components/base
   131      repoURL: https://github.com/my-user/my-repo
   132      targetRevision: main
   133      
   134      # This!
   135      kustomize:
   136        components:
   137          - ../component  # relative to the kustomization.yaml (`source.path`).
   138  ```
   139  
   140  ## Private Remote Bases
   141  
   142  If you have remote bases that are either (a) HTTPS and need username/password (b) SSH and need SSH private key, then they'll inherit that from the app's repo.
   143  
   144  This will work if the remote bases uses the same credentials/private key. It will not work if they use different ones. For security reasons your app only ever knows about its own repo (not other team's or users repos), and so you won't be able to access other private repos, even if Argo CD knows about them.
   145  
   146  Read more about [private repos](private-repositories.md).
   147  
   148  ## `kustomize build` Options/Parameters
   149  
   150  To provide build options to `kustomize build` of default Kustomize version, use `kustomize.buildOptions` field of `argocd-cm` ConfigMap. Use `kustomize.buildOptions.<version>` to register version specific build options.
   151  
   152  ```yaml
   153  apiVersion: v1
   154  kind: ConfigMap
   155  metadata:
   156    name: argocd-cm
   157    namespace: argocd
   158    labels:
   159      app.kubernetes.io/name: argocd-cm
   160      app.kubernetes.io/part-of: argocd
   161  data:
   162      kustomize.buildOptions: --load-restrictor LoadRestrictionsNone
   163      kustomize.buildOptions.v4.4.0: --output /tmp
   164  ```
   165  ## Custom Kustomize versions
   166  
   167  Argo CD supports using multiple Kustomize versions simultaneously and specifies required version per application.
   168  To add additional versions make sure required versions are [bundled](../operator-manual/custom_tools.md) and then
   169  use `kustomize.path.<version>` fields of `argocd-cm` ConfigMap to register bundled additional versions.
   170  
   171  ```yaml
   172  apiVersion: v1
   173  kind: ConfigMap
   174  metadata:
   175    name: argocd-cm
   176    namespace: argocd
   177    labels:
   178      app.kubernetes.io/name: argocd-cm
   179      app.kubernetes.io/part-of: argocd
   180  data:
   181      kustomize.path.v3.5.1: /custom-tools/kustomize_3_5_1
   182      kustomize.path.v3.5.4: /custom-tools/kustomize_3_5_4
   183  ```
   184  
   185  Once a new version is configured you can reference it in an Application spec as follows:
   186  
   187  ```yaml
   188  apiVersion: argoproj.io/v1alpha1
   189  kind: Application
   190  metadata:
   191    name: guestbook
   192  spec:
   193    source:
   194      repoURL: https://github.com/argoproj/argocd-example-apps.git
   195      targetRevision: HEAD
   196      path: kustomize-guestbook
   197  
   198      kustomize:
   199        version: v3.5.4
   200  ```
   201  
   202  Additionally, the application kustomize version can be configured using the Parameters tab of the Application Details page, or using the following CLI command:
   203  
   204  ```bash
   205  argocd app set <appName> --kustomize-version v3.5.4
   206  ```
   207  
   208  
   209  ## Build Environment
   210  
   211  Kustomize apps have access to the [standard build environment](build-environment.md) which can be used in combination with a [config managment plugin](../operator-manual/config-management-plugins.md) to alter the rendered manifests.
   212  
   213  You can use these build environment variables in your Argo CD Application manifests. You can enable this by setting `.spec.source.kustomize.commonAnnotationsEnvsubst` to `true` in your Application manifest.
   214  
   215  For example, the following Application manifest will set the `app-source` annotation to the name of the Application:
   216  
   217  ```yaml
   218  apiVersion: argoproj.io/v1alpha1
   219  kind: Application
   220  metadata:
   221    name: guestbook-app
   222    namespace: argocd
   223  spec:
   224    project: default
   225    destination:
   226      namespace: demo
   227      server: https://kubernetes.default.svc
   228    source:
   229      path: kustomize-guestbook
   230      repoURL: https://github.com/argoproj/argocd-example-apps
   231      targetRevision: HEAD
   232      kustomize:
   233        commonAnnotationsEnvsubst: true
   234        commonAnnotations:
   235          app-source: ${ARGOCD_APP_NAME}
   236    syncPolicy:
   237      syncOptions:
   238        - CreateNamespace=true
   239  ```
   240  
   241  ## Kustomizing Helm charts
   242  
   243  It's possible to [render Helm charts with Kustomize](https://github.com/kubernetes-sigs/kustomize/blob/master/examples/chart.md).
   244  Doing so requires that you pass the `--enable-helm` flag to the `kustomize build` command.
   245  This flag is not part of the Kustomize options within Argo CD.
   246  If you would like to render Helm charts through Kustomize in an Argo CD application, you have two options:
   247  You can either create a [custom plugin](https://argo-cd.readthedocs.io/en/stable/user-guide/config-management-plugins/), or modify the `argocd-cm` ConfigMap to include the `--enable-helm` flag globally for all Kustomize applications:
   248  
   249  ```yaml
   250  apiVersion: v1
   251  kind: ConfigMap
   252  metadata:
   253    name: argocd-cm
   254    namespace: argocd
   255  data:
   256    kustomize.buildOptions: --enable-helm
   257  ```
   258  
   259  ## Setting the manifests' namespace
   260  
   261  The `spec.destination.namespace` field only adds a namespace when it's missing from the manifests generated by Kustomize. It also uses `kubectl` to set the namespace, which sometimes misses namespace fields in certain resources (for example, custom resources). In these cases, you might get an error like this: `ClusterRoleBinding.rbac.authorization.k8s.io "example" is invalid: subjects[0].namespace: Required value.`
   262  
   263  Using Kustomize directly to set the missing namespaces can resolve this problem. Setting `spec.source.kustomize.namespace` instructs Kustomize to set namespace fields to the given value.
   264  
   265  If `spec.destination.namespace` and `spec.source.kustomize.namespace` are both set, Argo CD will defer to the latter, the namespace value set by Kustomize.