Version: v1.0
Service Binding
Service binding trait will bind data from Kubernetes Secret to the application container’s ENV.
apiVersion: core.oam.dev/v1beta1kind: TraitDefinitionmetadata:annotations:definition.oam.dev/description: "binding cloud resource secrets to pod env"name: service-bindingspec:appliesToWorkloads:- webservice- workerschematic:cue:template: |patch: {spec: template: spec: {// +patchKey=namecontainers: [{name: context.name// +patchKey=nameenv: [for envName, v in parameter.envMappings {name: envNamevalueFrom: {secretKeyRef: {name: v.secretif v["key"] != _|_ {key: v.key}if v["key"] == _|_ {key: envName}}}},]}]}}parameter: {// +usage=The mapping of environment variables to secretenvMappings: [string]: [string]: string}
With the help of this service-binding trait, you can explicitly set parameter envMappings to mapping all environment names with secret key. Here is an example.
apiVersion: core.oam.dev/v1beta1kind: Applicationmetadata:name: webappspec:components:- name: express-servertype: webserviceproperties:image: zzxwill/flask-web-application:v0.3.1-crossplaneports: 80traits:- type: service-bindingproperties:envMappings:# environments refer to db-conn secretDB_PASSWORD:secret: db-connkey: password # 1) If the env name is different from secret key, secret key has to be set.endpoint:secret: db-conn # 2) If the env name is the same as the secret key, secret key can be omitted.username:secret: db-conn- name: sample-dbtype: alibaba-rdsproperties:name: sample-dbengine: mysqlengineVersion: "8.0"instanceClass: rds.mysql.c1.largeusername: oamtestsecretName: db-conn