Skip to content

Commit ebaf78c

Browse files
committed
Update addon template documentation to better describe variable usage
- Add detailed explanation of built-in vs customized variables - Provide comprehensive examples of using AddOnDeploymentConfig variables - Include step-by-step guide for defining and using customized variables - Add variable naming conventions and validation rules - Explain variable precedence order - Include practical examples for different environments (dev/prod) - Add common use cases for variables Fixes #495 Signed-off-by: zhujian <[email protected]>
1 parent b64dbe5 commit ebaf78c

File tree

1 file changed

+227
-5
lines changed
  • content/en/docs/developer-guides

1 file changed

+227
-5
lines changed

content/en/docs/developer-guides/addon.md

Lines changed: 227 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,13 +1203,235 @@ Users can use variables in the `addonTemplate.agentSpec.workload.manifests` fiel
12031203
is similar to go template syntax but not identical, only String value is supported. And there are two types of
12041204
variables:
12051205

1206-
1. built-in variables;
1207-
* constant parameters(can not be overridden by user's variables):
1208-
* `CLUSTER_NAME`: name of the managed cluster(e.g cluster1)
1209-
* default parameters(can be overridden by user's variables)
1206+
1. **Built-in variables**:
1207+
* **Constant parameters** (can not be overridden by user's variables):
1208+
* `CLUSTER_NAME`: name of the managed cluster (e.g., cluster1)
1209+
* **Default parameters** (can be overridden by user's variables):
12101210
* `HUB_KUBECONFIG`: path of the kubeconfig to access the hub cluster, default value is
12111211
`/managed/hub-kubeconfig/kubeconfig`
1212-
2. Customize variables; Variables defines in `addonDeploymentConfig.customizedVariables` can be used.
1212+
1213+
2. **Customized variables**: Variables defined in `addonDeploymentConfig.customizedVariables` can be used.
1214+
1215+
#### Using customized variables from AddOnDeploymentConfig
1216+
1217+
To use customized variables in your addon template, you need to:
1218+
1219+
1. **Define the variables in an AddOnDeploymentConfig**:
1220+
1221+
```yaml
1222+
apiVersion: addon.open-cluster-management.io/v1alpha1
1223+
kind: AddOnDeploymentConfig
1224+
metadata:
1225+
name: hello-template-deploy-config
1226+
namespace: open-cluster-management
1227+
spec:
1228+
customizedVariables:
1229+
- name: LOG_LEVEL
1230+
value: "2"
1231+
- name: REPLICA_COUNT
1232+
value: "3"
1233+
- name: IMAGE_TAG
1234+
value: "v1.2.3"
1235+
- name: CUSTOM_ENV_VAR
1236+
value: "production"
1237+
```
1238+
1239+
2. **Reference the AddOnDeploymentConfig in your ClusterManagementAddOn**:
1240+
1241+
```yaml
1242+
apiVersion: addon.open-cluster-management.io/v1alpha1
1243+
kind: ClusterManagementAddOn
1244+
metadata:
1245+
name: hello-template
1246+
annotations:
1247+
addon.open-cluster-management.io/lifecycle: "addon-manager"
1248+
spec:
1249+
addOnMeta:
1250+
description: hello-template is an addon built with addon template
1251+
displayName: hello-template
1252+
supportedConfigs:
1253+
- group: addon.open-cluster-management.io
1254+
resource: addontemplates
1255+
defaultConfig:
1256+
name: hello-template
1257+
- group: addon.open-cluster-management.io
1258+
resource: addondeploymentconfigs
1259+
defaultConfig:
1260+
name: hello-template-deploy-config
1261+
namespace: open-cluster-management
1262+
```
1263+
1264+
3. **Use the variables in your AddOnTemplate**:
1265+
1266+
```yaml
1267+
apiVersion: addon.open-cluster-management.io/v1alpha1
1268+
kind: AddOnTemplate
1269+
metadata:
1270+
name: hello-template
1271+
spec:
1272+
addonName: hello-template
1273+
agentSpec:
1274+
workload:
1275+
manifests:
1276+
- kind: Deployment
1277+
apiVersion: apps/v1
1278+
metadata:
1279+
name: hello-template-agent
1280+
namespace: open-cluster-management-agent-addon
1281+
labels:
1282+
app: hello-template-agent
1283+
version: "{{IMAGE_TAG}}" # Using customized variable
1284+
spec:
1285+
replicas: {{REPLICA_COUNT}} # Using customized variable
1286+
selector:
1287+
matchLabels:
1288+
app: hello-template-agent
1289+
template:
1290+
metadata:
1291+
labels:
1292+
app: hello-template-agent
1293+
spec:
1294+
serviceAccountName: hello-template-agent-sa
1295+
containers:
1296+
- name: helloworld-agent
1297+
image: quay.io/open-cluster-management/addon-examples:{{IMAGE_TAG}} # Using customized variable
1298+
imagePullPolicy: IfNotPresent
1299+
env:
1300+
- name: CUSTOM_ENV
1301+
value: "{{CUSTOM_ENV_VAR}}" # Using customized variable
1302+
args:
1303+
- "/helloworld"
1304+
- "agent"
1305+
- "--cluster-name={{CLUSTER_NAME}}" # Using built-in variable
1306+
- "--addon-namespace=open-cluster-management-agent-addon"
1307+
- "--addon-name=hello-template"
1308+
- "--hub-kubeconfig={{HUB_KUBECONFIG}}" # Using built-in variable
1309+
- "--v={{LOG_LEVEL}}" # Using customized variable
1310+
```
1311+
1312+
#### Per-cluster customization
1313+
1314+
You can also override the default AddOnDeploymentConfig for specific clusters by referencing a different config in the ManagedClusterAddOn:
1315+
1316+
```yaml
1317+
apiVersion: addon.open-cluster-management.io/v1alpha1
1318+
kind: ManagedClusterAddOn
1319+
metadata:
1320+
name: hello-template
1321+
namespace: cluster1
1322+
spec:
1323+
installNamespace: open-cluster-management-agent-addon
1324+
configs:
1325+
- group: addon.open-cluster-management.io
1326+
resource: addondeploymentconfigs
1327+
name: cluster1-specific-config # Override with cluster-specific config
1328+
namespace: cluster1
1329+
```
1330+
1331+
This allows you to have different variable values for different clusters while using the same addon template.
1332+
1333+
#### Variable naming and validation
1334+
1335+
When defining customized variables in AddOnDeploymentConfig, please note:
1336+
1337+
* **Variable names** must follow the pattern `^[a-zA-Z_][_a-zA-Z0-9]*$` (start with letter or underscore, followed by letters, numbers, or underscores)
1338+
* **Variable names** are case-sensitive and have a maximum length of 255 characters
1339+
* **Variable values** have a maximum length of 1024 characters
1340+
* **Variable names** should be descriptive and follow naming conventions (e.g., `LOG_LEVEL`, `IMAGE_TAG`, `REPLICA_COUNT`)
1341+
1342+
#### Variable precedence
1343+
1344+
Variables are resolved in the following order (later values override earlier ones):
1345+
1346+
1. Built-in default parameters (e.g., `HUB_KUBECONFIG`)
1347+
2. Variables from the default AddOnDeploymentConfig (referenced in ClusterManagementAddOn)
1348+
3. Variables from cluster-specific AddOnDeploymentConfig (referenced in ManagedClusterAddOn)
1349+
4. Built-in constant parameters (e.g., `CLUSTER_NAME`) - these cannot be overridden
1350+
1351+
#### Common use cases for variables
1352+
1353+
* **Image configuration**: Use variables like `IMAGE_TAG`, `IMAGE_REGISTRY` to customize container images
1354+
* **Resource configuration**: Use variables like `REPLICA_COUNT`, `CPU_LIMIT`, `MEMORY_LIMIT` for resource settings
1355+
* **Environment-specific settings**: Use variables like `LOG_LEVEL`, `DEBUG_MODE`, `ENVIRONMENT` for different environments
1356+
* **Feature flags**: Use variables like `ENABLE_FEATURE_X`, `USE_TLS` to enable/disable features
1357+
1358+
#### Complete example: Using variables for different environments
1359+
1360+
Here's a complete example showing how to use variables to deploy the same addon with different configurations for development and production environments:
1361+
1362+
**Development AddOnDeploymentConfig:**
1363+
1364+
```yaml
1365+
apiVersion: addon.open-cluster-management.io/v1alpha1
1366+
kind: AddOnDeploymentConfig
1367+
metadata:
1368+
name: hello-template-dev-config
1369+
namespace: open-cluster-management
1370+
spec:
1371+
customizedVariables:
1372+
- name: LOG_LEVEL
1373+
value: "4" # Debug level
1374+
- name: REPLICA_COUNT
1375+
value: "1"
1376+
- name: IMAGE_TAG
1377+
value: "latest"
1378+
- name: ENVIRONMENT
1379+
value: "development"
1380+
```
1381+
1382+
**Production AddOnDeploymentConfig:**
1383+
1384+
```yaml
1385+
apiVersion: addon.open-cluster-management.io/v1alpha1
1386+
kind: AddOnDeploymentConfig
1387+
metadata:
1388+
name: hello-template-prod-config
1389+
namespace: open-cluster-management
1390+
spec:
1391+
customizedVariables:
1392+
- name: LOG_LEVEL
1393+
value: "1" # Error level only
1394+
- name: REPLICA_COUNT
1395+
value: "3"
1396+
- name: IMAGE_TAG
1397+
value: "v1.2.3"
1398+
- name: ENVIRONMENT
1399+
value: "production"
1400+
```
1401+
1402+
**ManagedClusterAddOn for development cluster:**
1403+
```yaml
1404+
apiVersion: addon.open-cluster-management.io/v1alpha1
1405+
kind: ManagedClusterAddOn
1406+
metadata:
1407+
name: hello-template
1408+
namespace: dev-cluster
1409+
spec:
1410+
installNamespace: open-cluster-management-agent-addon
1411+
configs:
1412+
- group: addon.open-cluster-management.io
1413+
resource: addondeploymentconfigs
1414+
name: hello-template-dev-config
1415+
namespace: open-cluster-management
1416+
```
1417+
1418+
**ManagedClusterAddOn for production cluster:**
1419+
```yaml
1420+
apiVersion: addon.open-cluster-management.io/v1alpha1
1421+
kind: ManagedClusterAddOn
1422+
metadata:
1423+
name: hello-template
1424+
namespace: prod-cluster
1425+
spec:
1426+
installNamespace: open-cluster-management-agent-addon
1427+
configs:
1428+
- group: addon.open-cluster-management.io
1429+
resource: addondeploymentconfigs
1430+
name: hello-template-prod-config
1431+
namespace: open-cluster-management
1432+
```
1433+
1434+
With this setup, the same AddOnTemplate will be rendered differently for each environment, with appropriate log levels, replica counts, and image tags.
12131435

12141436
### Using kubeconfig/certificates in the addon agent Deployment
12151437

0 commit comments

Comments
 (0)