github.com/argoproj/argo-cd/v3@v3.2.1/docs/operator-manual/app-any-namespace.md (about)

     1  # Applications in any namespace
     2  
     3  !!! warning
     4      Please read this documentation carefully before you enable this feature. Misconfiguration could lead to potential security issues.
     5  
     6  ## Introduction
     7  
     8  As of version 2.5, Argo CD supports managing `Application` resources in namespaces other than the control plane's namespace (which is usually `argocd`), but this feature has to be explicitly enabled and configured appropriately.
     9  
    10  Argo CD administrators can define a certain set of namespaces where `Application` resources may be created, updated and reconciled in. However, applications in these additional namespaces will only be allowed to use certain `AppProjects`, as configured by the Argo CD administrators. This allows ordinary Argo CD users (e.g. application teams) to use patterns like declarative management of `Application` resources, implementing app-of-apps and others without the risk of a privilege escalation through usage of other `AppProjects` that would exceed the permissions granted to the application teams.
    11  
    12  Some manual steps will need to be performed by the Argo CD administrator in order to enable this feature.
    13  
    14  One additional advantage of adopting applications in any namespace is to allow end-users to configure notifications for their Argo CD application in the namespace where Argo CD application is running in. See notifications [namespace based configuration](notifications/index.md#namespace-based-configuration) page for more information.
    15  
    16  ## Prerequisites
    17  
    18  ### Cluster-scoped Argo CD installation
    19  
    20  This feature can only be enabled and used when your Argo CD is installed as a cluster-wide instance, so it has permissions to list and manipulate resources on a cluster scope. It will not work with an Argo CD installed in namespace-scoped mode.
    21  
    22  ### Switch resource tracking method
    23  
    24  Also, while technically not necessary, it is strongly suggested that you switch the application tracking method from the default `label` setting to either `annotation` or `annotation+label`. The reasoning for this is, that application names will be a composite of the namespace's name and the name of the `Application`, and this can easily exceed the 63 characters length limit imposed on label values. Annotations have a notably greater length limit.
    25  
    26  To enable annotation based resource tracking, refer to the documentation about [resource tracking methods](../../user-guide/resource_tracking/)
    27  
    28  ## Implementation details
    29  
    30  ### Overview
    31  
    32  In order for an application to be managed and reconciled outside the Argo CD's control plane namespace, two prerequisites must match:
    33  
    34  1. The `Application`'s namespace must be explicitly enabled using the `--application-namespaces` parameter for the `argocd-application-controller` and `argocd-server` workloads. This parameter controls the list of namespaces that Argo CD will be allowed to source `Application` resources from globally. Any namespace not configured here cannot be used from any `AppProject`.
    35  1. The `AppProject` referenced by the `.spec.project` field of the `Application` must have the namespace listed in its `.spec.sourceNamespaces` field. This setting will determine whether an `Application` may use a certain `AppProject`. If an `Application` specifies an `AppProject` that is not allowed, Argo CD refuses to process this `Application`. As stated above, any namespace configured in the `.spec.sourceNamespaces` field must also be enabled globally.
    36  
    37  `Applications` in different namespaces can be created and managed just like any other `Application` in the `argocd` namespace previously, either declaratively or through the Argo CD API (e.g. using the CLI, the web UI, the REST API, etc).
    38  
    39  ### Reconfigure Argo CD to allow certain namespaces
    40  
    41  #### Change workload startup parameters
    42  
    43  In order to enable this feature, the Argo CD administrator must reconfigure the `argocd-server` and `argocd-application-controller` workloads to add the `--application-namespaces` parameter to the container's startup command.
    44  
    45  The `--application-namespaces` parameter takes a comma-separated list of namespaces where `Applications` are to be allowed in. Each entry of the list supports:
    46  
    47  - shell-style wildcards such as `*`, so for example the entry `app-team-*` would match `app-team-one` and `app-team-two`. To enable all namespaces on the cluster where Argo CD is running on, you can just specify `*`, i.e. `--application-namespaces=*`.
    48  - regex, requires wrapping the string in ```/```, example to allow all namespaces except a particular one: ```/^((?!not-allowed).)*$/```.
    49    
    50  The startup parameters for both, the `argocd-server` and the `argocd-application-controller` can also be conveniently set up and kept in sync by specifying the `application.namespaces` settings in the `argocd-cmd-params-cm` ConfigMap _instead_ of changing the manifests for the respective workloads. For example:
    51  
    52  ```yaml
    53  data:
    54    application.namespaces: app-team-one, app-team-two
    55  ```
    56  
    57  would allow the `app-team-one` and `app-team-two` namespaces for managing `Application` resources. After a change to the `argocd-cmd-params-cm` namespace, the appropriate workloads need to be restarted:
    58  
    59  ```bash
    60  kubectl rollout restart -n argocd deployment argocd-server
    61  kubectl rollout restart -n argocd statefulset argocd-application-controller
    62  ```
    63  
    64  #### Adapt Kubernetes RBAC
    65  
    66  We decided to not extend the Kubernetes RBAC for the `argocd-server` workload by default for the time being. If you want `Applications` in other namespaces to be managed by the Argo CD API (i.e. the CLI and UI), you need to extend the Kubernetes permissions for the `argocd-server` ServiceAccount.
    67  
    68  We supply a `ClusterRole` and `ClusterRoleBinding` suitable for this purpose in the `examples/k8s-rbac/argocd-server-applications` directory. For a default Argo CD installation (i.e. installed to the `argocd` namespace), you can just apply them as-is:
    69  
    70  ```shell
    71  kubectl apply -k examples/k8s-rbac/argocd-server-applications/
    72  ```
    73  
    74  `argocd-notifications-controller-rbac-clusterrole.yaml` and `argocd-notifications-controller-rbac-clusterrolebinding.yaml` are used to support notifications controller to notify apps in all namespaces.
    75  
    76  !!! note
    77      At some later point in time, we may make this cluster role part of the default installation manifests.
    78  
    79  ### Allowing additional namespaces in an AppProject
    80  
    81  Any user with Kubernetes access to the Argo CD control plane's namespace (`argocd`), especially those with permissions to create or update `Applications` in a declarative way, is to be considered an Argo CD admin.
    82  
    83  This prevented unprivileged Argo CD users from declaratively creating or managing `Applications` in the past. Those users were constrained to using the API instead, subject to Argo CD RBAC which ensures only `Applications` in allowed `AppProjects` were created.
    84  
    85  For an `Application` to be created outside the `argocd` namespace, the `AppProject` referred to in the `Application`'s `.spec.project` field must include the `Application`'s namespace in its `.spec.sourceNamespaces` field.
    86  
    87  For example, consider the two following (incomplete) `AppProject` specs:
    88  
    89  ```yaml
    90  kind: AppProject
    91  apiVersion: argoproj.io/v1alpha1
    92  metadata:
    93    name: project-one
    94    namespace: argocd
    95  spec:
    96    sourceNamespaces:
    97    - namespace-one
    98  ```
    99  
   100  and
   101  
   102  ```yaml
   103  kind: AppProject
   104  apiVersion: argoproj.io/v1alpha1
   105  metadata:
   106    name: project-two
   107    namespace: argocd
   108  spec:
   109    sourceNamespaces:
   110    - namespace-two
   111  ```
   112  
   113  In order for an Application to set `.spec.project` to `project-one`, it would have to be created in either namespace `namespace-one` or `argocd`. Likewise, in order for an Application to set `.spec.project` to `project-two`, it would have to be created in either namespace `namespace-two` or `argocd`.
   114  
   115  If an Application in `namespace-two` would set their `.spec.project` to `project-one` or an Application in `namespace-one` would set their `.spec.project` to `project-two`, Argo CD would consider this as a permission violation and refuse to reconcile the Application.
   116  
   117  Also, the Argo CD API will enforce these constraints, regardless of the Argo CD RBAC permissions.
   118  
   119  The `.spec.sourceNamespaces` field of the `AppProject` is a list that can contain an arbitrary amount of namespaces, and each entry supports shell-style wildcard, so that you can allow namespaces with patterns like `team-one-*`.
   120  
   121  !!! warning
   122      Do not add user controlled namespaces in the `.spec.sourceNamespaces` field of any privileged AppProject like the `default` project. Always make sure that the AppProject follows the principle of granting least required privileges. Never grant access to the `argocd` namespace within the AppProject.
   123  
   124  !!! note
   125      For backwards compatibility, Applications in the Argo CD control plane's namespace (`argocd`) are allowed to set their `.spec.project` field to reference any AppProject, regardless of the restrictions placed by the AppProject's `.spec.sourceNamespaces` field.
   126    
   127  !!! note
   128      Currently it's not possible to have a applicationset in one namespace and have the application
   129      be generated in another. See [#11104](https://github.com/argoproj/argo-cd/issues/11104) for more info.
   130  
   131  ### Application names
   132  
   133  For the CLI and UI, applications are now referred to and displayed as in the format `<namespace>/<name>`.
   134  
   135  For backwards compatibility, if the namespace of the Application is the control plane's namespace (i.e. `argocd`), the `<namespace>` can be omitted from the application name when referring to it. For example, the application names `argocd/someapp` and `someapp` are semantically the same and refer to the same application in the CLI and the UI.
   136  
   137  ### Application RBAC
   138  
   139  The RBAC syntax for Application objects has been changed from `<project>/<application>` to `<project>/<namespace>/<application>` to accommodate the need to restrict access based on the source namespace of the Application to be managed.
   140  
   141  For backwards compatibility, Applications in the `argocd` namespace can still be referred to as `<project>/<application>` in the RBAC policy rules.
   142  
   143  Wildcards do not make any distinction between project and application namespaces yet. For example, the following RBAC rule would match any application belonging to project `foo`, regardless of the namespace it is created in:
   144  
   145  ```
   146  p, somerole, applications, get, foo/*, allow
   147  ```
   148  
   149  If you want to restrict access to be granted only to `Applications` in project `foo` within namespace `bar`, the rule would need to be adapted as follows:
   150  
   151  ```
   152  p, somerole, applications, get, foo/bar/*, allow
   153  ```
   154    
   155  ## Managing applications in other namespaces
   156  
   157  ### Declaratively
   158  
   159  For declarative management of Applications, just create the Application from a YAML or JSON manifest in the desired namespace. Make sure that the `.spec.project` field refers to an AppProject that allows this namespace. For example, the following (incomplete) Application manifest creates an Application in the namespace `some-namespace`:
   160  
   161  ```yaml
   162  kind: Application
   163  apiVersion: argoproj.io/v1alpha1
   164  metadata:
   165    name: some-app
   166    namespace: some-namespace
   167  spec:
   168    project: some-project
   169    # ...
   170  ```
   171  
   172  The project `some-project` will then need to specify `some-namespace` in the list of allowed source namespaces, e.g.
   173  
   174  ```yaml
   175  kind: AppProject
   176  apiVersion: argoproj.io/v1alpha1
   177  metadata:
   178      name: some-project
   179      namespace: argocd
   180  spec:
   181      sourceNamespaces:
   182      - some-namespace
   183  ```
   184  
   185  ### Using the CLI
   186  
   187  You can use all existing Argo CD CLI commands for managing applications in other namespaces, exactly as you would use the CLI to manage applications in the control plane's namespace.
   188  
   189  For example, to retrieve the `Application` named `foo` in the namespace `bar`, you can use the following CLI command:
   190  
   191  ```shell
   192  argocd app get foo/bar
   193  ```
   194  
   195  Likewise, to manage this application, keep referring to it as `foo/bar`:
   196  
   197  ```bash
   198  # Create an application
   199  argocd app create foo/bar ...
   200  # Sync the application
   201  argocd app sync foo/bar
   202  # Delete the application
   203  argocd app delete foo/bar
   204  # Retrieve application's manifest
   205  argocd app manifests foo/bar
   206  ```
   207  
   208  As stated previously, for applications in the Argo CD's control plane namespace, you can omit the namespace from the application name.
   209  
   210  ### Using the UI
   211  
   212  Similar to the CLI, you can refer to the application in the UI as `foo/bar`.
   213  
   214  For example, to create an application named `bar` in the namespace `foo` in the web UI, set the application name in the creation dialogue's _Application Name_ field to `foo/bar`. If the namespace is omitted, the control plane's namespace will be used.
   215  
   216  ### Using the REST API
   217  
   218  If you are using the REST API, the namespace for `Application` cannot be specified as the application name, and resources need to be specified using the optional `appNamespace` query parameter. For example, to work with the `Application` resource named `foo` in the namespace `bar`, the request would look like follows:
   219  
   220  ```bash
   221  GET /api/v1/applications/foo?appNamespace=bar
   222  ```
   223  
   224  For other operations such as `POST` and `PUT`, the `appNamespace` parameter must be part of the request's payload.
   225  
   226  For `Application` resources in the control plane namespace, this parameter can be omitted.