github.com/oam-dev/kubevela@v1.9.11/references/docgen/def-doc/trait/service-binding.eg.md (about)

     1  1. Prepare a Kubernetes Secret
     2  
     3  The secret can be manually created, or generated by other component or external system.
     4  
     5  For example, we have a secret `db-conn-example` whose data is as below:
     6  
     7  ```yaml
     8  endpoint: https://xxx.com
     9  password: 123
    10  username: myname
    11  ```
    12  
    13  2. Bind the Secret into your component by `service-binding` trait
    14  
    15  For example, we have a webservice component who needs to consume a database. The database connection string should be set
    16  to Pod environments: `endpoint`, `username` and `DB_PASSWORD`.
    17  
    18  We can set the properties for envMappings as below. For each environment, `secret` represents the secret name, and `key`
    19  represents the key of the secret.
    20  
    21  Here is the complete properties for the trait.
    22  
    23  ```yaml
    24  traits:
    25  - type: service-binding
    26    properties:
    27      envMappings:
    28        DB_PASSWORD:
    29          secret: db-conn-example
    30          key: password            
    31        endpoint:
    32          secret: db-conn-example
    33          key: endpoint
    34        username:
    35          secret: db-conn-example
    36          key: username
    37  ```
    38  
    39  In particular, if the environment name, like `endpoint`, is same to the `key` of the secret, we can omit the `key`.
    40  So we can simplify the properties as below.
    41  
    42  ```yaml
    43  traits:
    44  - type: service-binding
    45    properties:
    46      envMappings:
    47        DB_PASSWORD:
    48          secret: db-conn-example
    49          key: password            
    50        endpoint:
    51          secret: db-conn-example
    52        username:
    53          secret: db-conn-example
    54  ```
    55  
    56  We can finally prepare an Application for the business component `binding-test-comp` to consume the secret, which is a
    57  representative of a database cloud resource.
    58  
    59  ```yaml
    60  apiVersion: core.oam.dev/v1beta1
    61  kind: Application
    62  metadata:
    63    name: webapp
    64  spec:
    65    components:
    66      - name: binding-test-comp
    67        type: webservice
    68        properties:
    69          image: zzxwill/flask-web-application:v0.3.1-crossplane
    70          ports: 80
    71        traits:
    72          - type: service-binding
    73            properties:
    74              envMappings:
    75                # environments refer to db-conn secret
    76                DB_PASSWORD:
    77                  secret: db-conn-example
    78                  key: password            
    79                endpoint:
    80                  secret: db-conn-example
    81                username:
    82                  secret: db-conn-example
    83  ```
    84  
    85  Deploy this YAML and the Secret `db-conn-example` will be binding into environment of workload.