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

     1  # Kustomize
     2  
     3  ## Declarative
     4  
     5  You can define a Kustomize application manifest in the declarative GitOps way. Here is an example:
     6  
     7  ```yaml
     8  apiVersion: argoproj.io/v1alpha1
     9  kind: Application
    10  metadata:
    11    name: kustomize-example
    12  spec:
    13    project: default
    14    source:
    15      path: examples/helloWorld
    16      repoURL: 'https://github.com/kubernetes-sigs/kustomize'
    17      targetRevision: HEAD
    18    destination:
    19      namespace: default
    20      server: 'https://kubernetes.default.svc'
    21  ```
    22  
    23  If the `kustomization.yaml` file exists at the location pointed to by `repoURL` and `path`, Argo CD will render the manifests using Kustomize.
    24  
    25  The following configuration options are available for Kustomize:
    26  
    27  * `namePrefix` is a prefix appended to resources for Kustomize apps
    28  * `nameSuffix` is a suffix appended to resources for Kustomize apps
    29  * `images` is a list of Kustomize image overrides
    30  * `replicas` is a list of Kustomize replica overrides
    31  * `commonLabels` is a string map of additional labels
    32  * `labelWithoutSelector` is a boolean value which defines if the common label(s) should be applied to resource selectors. It also excludes common labels from templates unless `labelIncludeTemplates` is set to true.
    33  * `labelIncludeTemplates` is a boolean value which defines if the common label(s) should be applied to resource templates.
    34  * `forceCommonLabels` is a boolean value which defines if it's allowed to override existing labels
    35  * `commonAnnotations` is a string map of additional annotations
    36  * `namespace` is a Kubernetes resources namespace
    37  * `forceCommonAnnotations` is a boolean value which defines if it's allowed to override existing annotations
    38  * `commonAnnotationsEnvsubst` is a boolean value which enables env variables substitution in annotation  values
    39  * `patches` is a list of Kustomize patches that supports inline updates
    40  * `components` is a list of Kustomize components
    41  * `ignoreMissingComponents` prevents kustomize from failing when components do not exist locally by not appending them to kustomization file
    42  
    43  To use Kustomize with an overlay, point your path to the overlay.
    44  
    45  !!! tip
    46      If you're generating resources, you should read up how to ignore those generated resources using the [`IgnoreExtraneous` compare option](compare-options.md).
    47  
    48  ## Patches
    49  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.
    50  
    51  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.
    52  ```yaml
    53  apiVersion: kustomize.config.k8s.io/v1beta1
    54  kind: Kustomization
    55  metadata:
    56    name: kustomize-inline-example
    57  namespace: test1
    58  resources:
    59    - https://github.com/argoproj/argocd-example-apps//kustomize-guestbook/
    60  patches:
    61    - target:
    62        kind: Deployment
    63        name: guestbook-ui
    64      patch: |-
    65        - op: replace
    66          path: /spec/template/spec/containers/0/ports/0/containerPort
    67          value: 443
    68  ```
    69  
    70  This `Application` does the equivalent using the inline `kustomize.patches` configuration.
    71  ```yaml
    72  apiVersion: argoproj.io/v1alpha1
    73  kind: Application
    74  metadata:
    75    name: kustomize-inline-guestbook
    76    namespace: argocd
    77    finalizers:
    78      - resources-finalizer.argocd.argoproj.io
    79  spec:
    80    destination:
    81      namespace: test1
    82      server: https://kubernetes.default.svc
    83    project: default
    84    source:
    85      path: kustomize-guestbook
    86      repoURL: https://github.com/argoproj/argocd-example-apps.git
    87      targetRevision: master
    88      kustomize:
    89        patches:
    90          - target:
    91              kind: Deployment
    92              name: guestbook-ui
    93            patch: |-
    94              - op: replace
    95                path: /spec/template/spec/containers/0/ports/0/containerPort
    96                value: 443
    97  ```
    98  
    99  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.
   100  
   101  ```yaml
   102  apiVersion: argoproj.io/v1alpha1
   103  kind: ApplicationSet
   104  metadata:
   105    name: external-dns
   106  spec:
   107    goTemplate: true
   108    goTemplateOptions: ["missingkey=error"]
   109    generators:
   110    - clusters: {}
   111    template:
   112      metadata:
   113        name: 'external-dns'
   114      spec:
   115        project: default
   116        source:
   117          repoURL: https://github.com/kubernetes-sigs/external-dns/
   118          targetRevision: v0.14.0
   119          path: kustomize
   120          kustomize:
   121            patches:
   122            - target:
   123                kind: Deployment
   124                name: external-dns
   125              patch: |-
   126                - op: add
   127                  path: /spec/template/spec/containers/0/args/3
   128                  value: --txt-owner-id={{.name}}   # patch using attribute from generator
   129        destination:
   130          name: 'in-cluster'
   131          namespace: default
   132  ```
   133  
   134  ## Components
   135  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. 
   136  If Kustomize is passed a non-existing component directory, it will error out. Missing component directories can be ignored (meaning, not passed to Kustomize) using `ignoreMissingComponents`. This can be particularly helpful to implement a [default/override pattern].
   137  
   138  Outside of Argo CD, to utilize components, you must add the following to the `kustomization.yaml` that the Application references. For example:
   139  ```yaml
   140  apiVersion: kustomize.config.k8s.io/v1beta1
   141  kind: Kustomization
   142  ...
   143  components:
   144  - ../component
   145  ```
   146  
   147  With support added for components in `v2.10.0`, you can now reference a component directly in the Application:
   148  ```yaml
   149  apiVersion: argoproj.io/v1alpha1
   150  kind: Application
   151  metadata:
   152    name: application-kustomize-components
   153  spec:
   154    ...
   155    source:
   156      path: examples/application-kustomize-components/base
   157      repoURL: https://github.com/my-user/my-repo
   158      targetRevision: main
   159      
   160      # This!
   161      kustomize:
   162        components:
   163          - ../component  # relative to the kustomization.yaml (`source.path`).
   164        ignoreMissingComponents: true
   165  ```
   166  
   167  ## Private Remote Bases
   168  
   169  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.
   170  
   171  This will work if the remote bases use 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.
   172  
   173  Read more about [private repos](private-repositories.md).
   174  
   175  ## `kustomize build` Options/Parameters
   176  
   177  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.
   178  
   179  ```yaml
   180  apiVersion: v1
   181  kind: ConfigMap
   182  metadata:
   183    name: argocd-cm
   184    namespace: argocd
   185    labels:
   186      app.kubernetes.io/name: argocd-cm
   187      app.kubernetes.io/part-of: argocd
   188  data:
   189      kustomize.buildOptions: --load-restrictor LoadRestrictionsNone
   190      kustomize.buildOptions.v4.4.0: --output /tmp
   191  ```
   192  
   193  After modifying `kustomize.buildOptions`, you may need to restart ArgoCD for the changes to take effect.
   194  
   195  ## Custom Kustomize versions
   196  
   197  Argo CD supports using multiple Kustomize versions simultaneously and specifies required version per application.
   198  To add additional versions make sure required versions are [bundled](../operator-manual/custom_tools.md) and then
   199  use `kustomize.path.<version>` fields of `argocd-cm` ConfigMap to register bundled additional versions.
   200  
   201  ```yaml
   202  apiVersion: v1
   203  kind: ConfigMap
   204  metadata:
   205    name: argocd-cm
   206    namespace: argocd
   207    labels:
   208      app.kubernetes.io/name: argocd-cm
   209      app.kubernetes.io/part-of: argocd
   210  data:
   211      kustomize.path.v3.5.1: /custom-tools/kustomize_3_5_1
   212      kustomize.path.v3.5.4: /custom-tools/kustomize_3_5_4
   213  ```
   214  
   215  Once a new version is configured you can reference it in an Application spec as follows:
   216  
   217  ```yaml
   218  apiVersion: argoproj.io/v1alpha1
   219  kind: Application
   220  metadata:
   221    name: guestbook
   222  spec:
   223    source:
   224      repoURL: https://github.com/argoproj/argocd-example-apps.git
   225      targetRevision: HEAD
   226      path: kustomize-guestbook
   227  
   228      kustomize:
   229        version: v3.5.4
   230  ```
   231  
   232  Additionally, the application kustomize version can be configured using the Parameters tab of the Application Details page, or using the following CLI command:
   233  
   234  ```bash
   235  argocd app set <appName> --kustomize-version v3.5.4
   236  ```
   237  
   238  
   239  ## Build Environment
   240  
   241  Kustomize apps have access to the [standard build environment](build-environment.md) which can be used in combination with a [config management plugin](../operator-manual/config-management-plugins.md) to alter the rendered manifests.
   242  
   243  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.
   244  
   245  For example, the following Application manifest will set the `app-source` annotation to the name of the Application:
   246  
   247  ```yaml
   248  apiVersion: argoproj.io/v1alpha1
   249  kind: Application
   250  metadata:
   251    name: guestbook-app
   252    namespace: argocd
   253  spec:
   254    project: default
   255    destination:
   256      namespace: demo
   257      server: https://kubernetes.default.svc
   258    source:
   259      path: kustomize-guestbook
   260      repoURL: https://github.com/argoproj/argocd-example-apps
   261      targetRevision: HEAD
   262      kustomize:
   263        commonAnnotationsEnvsubst: true
   264        commonAnnotations:
   265          app-source: ${ARGOCD_APP_NAME}
   266    syncPolicy:
   267      syncOptions:
   268        - CreateNamespace=true
   269  ```
   270  
   271  ## Kustomizing Helm charts
   272  
   273  It's possible to [render Helm charts with Kustomize](https://github.com/kubernetes-sigs/kustomize/blob/master/examples/chart.md).
   274  Doing so requires that you pass the `--enable-helm` flag to the `kustomize build` command.
   275  This flag is not part of the Kustomize options within Argo CD.
   276  If you would like to render Helm charts through Kustomize in an Argo CD application, you have two options:
   277  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:
   278  
   279  ```yaml
   280  apiVersion: v1
   281  kind: ConfigMap
   282  metadata:
   283    name: argocd-cm
   284    namespace: argocd
   285  data:
   286    kustomize.buildOptions: --enable-helm
   287  ```
   288  
   289  ## Setting the manifests' namespace
   290  
   291  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.`
   292  
   293  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.
   294  
   295  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.