github.com/oam-dev/kubevela@v1.9.11/docs/examples/app-with-policy/shared-resource-policy/shared-resource.md (about) 1 ## How to share resources across applications 2 3 Sometimes, you may want different applications to share the same resource. 4 For example, you might have various applications that needs the same namespace to exist. 5 In this case, you can use the `shared-resource` policy to declare which resources should be shared. 6 7 ### Usage 8 9 ```yaml 10 apiVersion: core.oam.dev/v1beta1 11 kind: Application 12 metadata: 13 name: app1 14 spec: 15 components: 16 - name: ns1 17 type: k8s-objects 18 properties: 19 objects: 20 - apiVersion: v1 21 kind: Namespace 22 metadata: 23 name: example 24 - name: cm1 25 type: k8s-objects 26 properties: 27 objects: 28 - apiVersion: v1 29 kind: ConfigMap 30 metadata: 31 name: cm1 32 namespace: example 33 data: 34 key: value1 35 policies: 36 - name: shared-resource 37 type: shared-resource 38 properties: 39 rules: 40 - selector: 41 resourceTypes: ["Namespace"] 42 ``` 43 44 ```yaml 45 apiVersion: core.oam.dev/v1beta1 46 kind: Application 47 metadata: 48 name: app2 49 spec: 50 components: 51 - name: ns2 52 type: k8s-objects 53 properties: 54 objects: 55 - apiVersion: v1 56 kind: Namespace 57 metadata: 58 name: example 59 - name: cm2 60 type: k8s-objects 61 properties: 62 objects: 63 - apiVersion: v1 64 kind: ConfigMap 65 metadata: 66 name: cm2 67 namespace: example 68 data: 69 key: value2 70 policies: 71 - name: shared-resource 72 type: shared-resource 73 properties: 74 rules: 75 - selector: 76 resourceTypes: ["Namespace"] 77 ``` 78 79 The above two applications will dispatch the same namespace "example". 80 They will create two different ConfigMap inside namespace "example" respectively. 81 82 Both application use the `shared-resource` policy and declared the namespace resource as shared. 83 In this way, there will be no conflict for creating the same namespace. 84 If the `shared-resource` policy is not used, the second application will report error after it finds that the namespace "example" is managed by the first application. 85 86 The namespace will only be recycled when both applications are removed. 87 88 ### Working Detail 89 90 One of the problem for sharing resource is that what will happen if different application holds different configuration for the shared resource. 91 In the `shared-resource` policy, all sharers will be recorded by time order. The first sharer will be able to write the resource while other sharers can only read it. After the first sharer is deleted, it will give the control of the resource to the next sharer. If no sharer is handling it, the resource will be finally removed.