github.com/argoproj/argo-cd/v2@v2.10.9/docs/operator-manual/applicationset/Generators-Cluster.md (about) 1 # Cluster Generator 2 3 In Argo CD, managed clusters [are stored within Secrets](../../declarative-setup/#clusters) in the Argo CD namespace. The ApplicationSet controller uses those same Secrets to generate parameters to identify and target available clusters. 4 5 For each cluster registered with Argo CD, the Cluster generator produces parameters based on the list of items found within the cluster secret. 6 7 It automatically provides the following parameter values to the Application template for each cluster: 8 9 - `name` 10 - `nameNormalized` *('name' but normalized to contain only lowercase alphanumeric characters, '-' or '.')* 11 - `server` 12 - `metadata.labels.<key>` *(for each label in the Secret)* 13 - `metadata.annotations.<key>` *(for each annotation in the Secret)* 14 15 !!! note 16 Use the `nameNormalized` parameter if your cluster name contains characters (such as underscores) that are not valid for Kubernetes resource names. This prevents rendering invalid Kubernetes resources with names like `my_cluster-app1`, and instead would convert them to `my-cluster-app1`. 17 18 19 Within [Argo CD cluster Secrets](../../declarative-setup/#clusters) are data fields describing the cluster: 20 ```yaml 21 kind: Secret 22 data: 23 # Within Kubernetes these fields are actually encoded in Base64; they are decoded here for convenience. 24 # (They are likewise decoded when passed as parameters by the Cluster generator) 25 config: "{'tlsClientConfig':{'insecure':false}}" 26 name: "in-cluster2" 27 server: "https://kubernetes.default.svc" 28 metadata: 29 labels: 30 argocd.argoproj.io/secret-type: cluster 31 # (...) 32 ``` 33 34 The Cluster generator will automatically identify clusters defined with Argo CD, and extract the cluster data as parameters: 35 ```yaml 36 apiVersion: argoproj.io/v1alpha1 37 kind: ApplicationSet 38 metadata: 39 name: guestbook 40 namespace: argocd 41 spec: 42 goTemplate: true 43 goTemplateOptions: ["missingkey=error"] 44 generators: 45 - clusters: {} # Automatically use all clusters defined within Argo CD 46 template: 47 metadata: 48 name: '{{.name}}-guestbook' # 'name' field of the Secret 49 spec: 50 project: "my-project" 51 source: 52 repoURL: https://github.com/argoproj/argocd-example-apps/ 53 targetRevision: HEAD 54 path: guestbook 55 destination: 56 server: '{{.server}}' # 'server' field of the secret 57 namespace: guestbook 58 ``` 59 (*The full example can be found [here](https://github.com/argoproj/argo-cd/tree/master/applicationset/examples/cluster).*) 60 61 In this example, the cluster secret's `name` and `server` fields are used to populate the `Application` resource `name` and `server` (which are then used to target that same cluster). 62 63 ### Label selector 64 65 A label selector may be used to narrow the scope of targeted clusters to only those matching a specific label: 66 ```yaml 67 kind: ApplicationSet 68 metadata: 69 name: guestbook 70 namespace: argocd 71 spec: 72 goTemplate: true 73 goTemplateOptions: ["missingkey=error"] 74 generators: 75 - clusters: 76 selector: 77 matchLabels: 78 staging: true 79 # The cluster generator also supports matchExpressions. 80 #matchExpressions: 81 # - key: staging 82 # operator: In 83 # values: 84 # - "true" 85 template: 86 # (...) 87 ``` 88 89 This would match an Argo CD cluster secret containing: 90 ```yaml 91 kind: Secret 92 data: 93 # (... fields as above ...) 94 metadata: 95 labels: 96 argocd.argoproj.io/secret-type: cluster 97 staging: "true" 98 # (...) 99 ``` 100 101 The cluster selector also supports set-based requirements, as used by [several core Kubernetes resources](https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#resources-that-support-set-based-requirements). 102 103 ### Deploying to the local cluster 104 105 In Argo CD, the 'local cluster' is the cluster upon which Argo CD (and the ApplicationSet controller) is installed. This is to distinguish it from 'remote clusters', which are those that are added to Argo CD [declaratively](../../declarative-setup/#clusters) or via the [Argo CD CLI](../../getting_started.md/#5-register-a-cluster-to-deploy-apps-to-optional). 106 107 The cluster generator will automatically target both local and non-local clusters, for every cluster that matches the cluster selector. 108 109 If you wish to target only remote clusters with your Applications (e.g. you want to exclude the local cluster), then use a cluster selector with labels, for example: 110 ```yaml 111 spec: 112 goTemplate: true 113 goTemplateOptions: ["missingkey=error"] 114 generators: 115 - clusters: 116 selector: 117 matchLabels: 118 argocd.argoproj.io/secret-type: cluster 119 # The cluster generator also supports matchExpressions. 120 #matchExpressions: 121 # - key: staging 122 # operator: In 123 # values: 124 # - "true" 125 ``` 126 127 This selector will not match the default local cluster, since the default local cluster does not have a Secret (and thus does not have the `argocd.argoproj.io/secret-type` label on that secret). Any cluster selector that selects on that label will automatically exclude the default local cluster. 128 129 However, if you do wish to target both local and non-local clusters, while also using label matching, you can create a secret for the local cluster within the Argo CD web UI: 130 131 1. Within the Argo CD web UI, select *Settings*, then *Clusters*. 132 2. Select your local cluster, usually named `in-cluster`. 133 3. Click the *Edit* button, and change the *NAME* of the cluster to another value, for example `in-cluster-local`. Any other value here is fine. 134 4. Leave all other fields unchanged. 135 5. Click *Save*. 136 137 These steps might seem counterintuitive, but the act of changing one of the default values for the local cluster causes the Argo CD Web UI to create a new secret for this cluster. In the Argo CD namespace, you should now see a Secret resource named `cluster-(cluster suffix)` with label `argocd.argoproj.io/secret-type": "cluster"`. You may also create a local [cluster secret declaratively](../../declarative-setup/#clusters), or with the CLI using `argocd cluster add "(context name)" --in-cluster`, rather than through the Web UI. 138 139 ### Pass additional key-value pairs via `values` field 140 141 You may pass additional, arbitrary string key-value pairs via the `values` field of the cluster generator. Values added via the `values` field are added as `values.(field)` 142 143 In this example, a `revision` parameter value is passed, based on matching labels on the cluster secret: 144 ```yaml 145 spec: 146 goTemplate: true 147 goTemplateOptions: ["missingkey=error"] 148 generators: 149 - clusters: 150 selector: 151 matchLabels: 152 type: 'staging' 153 # A key-value map for arbitrary parameters 154 values: 155 revision: HEAD # staging clusters use HEAD branch 156 - clusters: 157 selector: 158 matchLabels: 159 type: 'production' 160 values: 161 # production uses a different revision value, for 'stable' branch 162 revision: stable 163 template: 164 metadata: 165 name: '{{.name}}-guestbook' 166 spec: 167 project: "my-project" 168 source: 169 repoURL: https://github.com/argoproj/argocd-example-apps/ 170 # The cluster values field for each generator will be substituted here: 171 targetRevision: '{{.values.revision}}' 172 path: guestbook 173 destination: 174 server: '{{.server}}' 175 namespace: guestbook 176 ``` 177 178 In this example the `revision` value from the `generators.clusters` fields is passed into the template as `values.revision`, containing either `HEAD` or `stable` (based on which generator generated the set of parameters). 179 180 !!! note 181 The `values.` prefix is always prepended to values provided via `generators.clusters.values` field. Ensure you include this prefix in the parameter name within the `template` when using it. 182 183 In `values` we can also interpolate the following parameter values (i.e. the same values as presented in the beginning of this page) 184 185 - `name` 186 - `nameNormalized` *('name' but normalized to contain only lowercase alphanumeric characters, '-' or '.')* 187 - `server` 188 - `metadata.labels.<key>` *(for each label in the Secret)* 189 - `metadata.annotations.<key>` *(for each annotation in the Secret)* 190 191 Extending the example above, we could do something like this: 192 193 ```yaml 194 spec: 195 goTemplate: true 196 goTemplateOptions: ["missingkey=error"] 197 generators: 198 - clusters: 199 selector: 200 matchLabels: 201 type: 'staging' 202 # A key-value map for arbitrary parameters 203 values: 204 # If `my-custom-annotation` is in your cluster secret, `revision` will be substituted with it. 205 revision: '{{index .metadata.annotations "my-custom-annotation"}}' 206 clusterName: '{{.name}}' 207 - clusters: 208 selector: 209 matchLabels: 210 type: 'production' 211 values: 212 # production uses a different revision value, for 'stable' branch 213 revision: stable 214 clusterName: '{{.name}}' 215 template: 216 metadata: 217 name: '{{.name}}-guestbook' 218 spec: 219 project: "my-project" 220 source: 221 repoURL: https://github.com/argoproj/argocd-example-apps/ 222 # The cluster values field for each generator will be substituted here: 223 targetRevision: '{{.values.revision}}' 224 path: guestbook 225 destination: 226 # In this case this is equivalent to just using {{name}} 227 server: '{{.values.clusterName}}' 228 namespace: guestbook 229 ```