github.com/oam-dev/kubevela@v1.9.11/docs/examples/app-with-policy/gc-policy/persist-resources.md (about)

     1  # How to persist resources
     2  
     3  By leveraging the garbage-collect policy, users can persist some resources, which skip the normal garbage-collect process when application is updated.
     4  
     5  ### traitTypes
     6  
     7  Take the following app as an example, in the garbage-collect policy, a rule is added which marks all the resources created by the `expose` trait to use the `onAppDelete` strategy. This will keep those services until application is deleted.
     8  ```shell
     9  $ cat <<EOF | kubectl apply -f -
    10  apiVersion: core.oam.dev/v1beta1
    11  kind: Application
    12  metadata:
    13    name: garbage-collect-app
    14  spec:
    15    components:
    16      - name: hello-world
    17        type: webservice
    18        properties:
    19          image: crccheck/hello-world
    20        traits:
    21          - type: expose
    22            properties:
    23              port: [8000]
    24    policies:
    25      - name: garbage-collect
    26        type: garbage-collect
    27        properties:
    28          rules:
    29            - selector:
    30                traitTypes:
    31                  - expose
    32              strategy: onAppDelete
    33  EOF
    34  ```
    35  
    36  You can find deployment and service are created.
    37  ```shell
    38  $ kubectl get deployment
    39  NAME          READY   UP-TO-DATE   AVAILABLE   AGE
    40  hello-world   1/1     1            1           74s
    41  $ kubectl get service   
    42  NAME          TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
    43  hello-world   ClusterIP   10.96.160.208   <none>        8000/TCP   78s
    44  ```
    45  
    46  If you upgrade the application and use a different component, you will find the old versioned deployment is deleted by the service is kept.
    47  ```shell
    48  $ cat <<EOF | kubectl apply -f -
    49  apiVersion: core.oam.dev/v1beta1
    50  kind: Application
    51  metadata:
    52    name: garbage-collect-app
    53  spec:
    54    components:
    55      - name: hello-world-new
    56        type: webservice
    57        properties:
    58          image: crccheck/hello-world
    59        traits:
    60          - type: expose
    61            properties:
    62              port: [8000]
    63    policies:
    64      - name: garbage-collect
    65        type: garbage-collect
    66        properties:
    67          rules:
    68            - selector:
    69                traitTypes:
    70                  - expose
    71              strategy: onAppDelete
    72  EOF
    73  
    74  $ kubectl get deployment
    75  NAME              READY   UP-TO-DATE   AVAILABLE   AGE
    76  hello-world-new   1/1     1            1           10s
    77  $ kubectl get service   
    78  NAME              TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
    79  hello-world       ClusterIP   10.96.160.208   <none>        8000/TCP   5m56s
    80  hello-world-new   ClusterIP   10.96.20.4      <none>        8000/TCP   13s
    81  ```
    82  
    83  ### componentTypes
    84  
    85  Users can also keep component if they are deploying job-like components. Resources dispatched by `job-like-component` type component will be kept after application is deleted.
    86  
    87  ```yaml
    88  apiVersion: core.oam.dev/v1beta1
    89  kind: Application
    90  metadata:
    91    name: garbage-collect-app
    92  spec:
    93    components:
    94      - name: hello-world-new
    95        type: job-like-component
    96    policies:
    97      - name: garbage-collect
    98        type: garbage-collect
    99        properties:
   100          rules:
   101            - selector:
   102                componentTypes:
   103                  - webservice
   104              strategy: never
   105  ```
   106  
   107  ### componentNames
   108  
   109  A more straightforward way is to specify `compNames` to match specified components.
   110  ```yaml
   111  apiVersion: core.oam.dev/v1beta1
   112  kind: Application
   113  metadata:
   114    name: create-ns-app
   115  spec:
   116    components:
   117      - name: example-addon-namespace
   118        type: k8s-objects
   119        properties:
   120          objects:
   121            - apiVersion: v1
   122              kind: Namespace
   123    policies:
   124      - name: garbage-collect
   125        type: garbage-collect
   126        properties:
   127          rules:
   128            - selector:
   129                componentNames:
   130                  - example-addon-namespace
   131              strategy: never
   132  ```
   133  
   134  ### oamTypes
   135  
   136  Users can also persist resources using `oamTypes`, where the values of `oamTypes` can be `TRAIT` and `WORKLOAD`.
   137  
   138  ```shell
   139  $ cat <<EOF | kubectl apply -f -
   140  apiVersion: core.oam.dev/v1beta1
   141  kind: Application
   142  metadata:
   143    name: garbage-collect-app
   144  spec:
   145    components:
   146      - name: hello-world
   147        type: webservice
   148        properties:
   149          image: crccheck/hello-world
   150        traits:
   151          - type: expose
   152            properties:
   153              port: [8000]
   154    policies:
   155      - name: garbage-collect
   156        type: garbage-collect
   157        properties:
   158          rules:
   159            - selector:
   160                oamTypes:
   161                  - TRAIT
   162              strategy: onAppDelete
   163  EOF
   164  ```
   165  
   166  And then, let's modify the component name.
   167  
   168  ```shell
   169  $ cat <<EOF | kubectl apply -f -
   170  apiVersion: core.oam.dev/v1beta1
   171  kind: Application
   172  metadata:
   173    name: garbage-collect-app
   174  spec:
   175    components:
   176      - name: hello-world-new
   177        type: webservice
   178        properties:
   179          image: crccheck/hello-world
   180        traits:
   181          - type: expose
   182            properties:
   183              port: [8000]
   184    policies:
   185      - name: garbage-collect
   186        type: garbage-collect
   187        properties:
   188          rules:
   189            - selector:
   190                oamTypes:
   191                  - TRAIT
   192              strategy: onAppDelete
   193  EOF
   194  ```
   195  
   196  List the service in cluster, you will find:
   197  
   198  ```shell
   199  $ kubectl get service
   200  NAME              TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)    AGE
   201  hello-world       ClusterIP   10.96.31.209   <none>        8000/TCP   31s
   202  hello-world-new   ClusterIP   10.96.17.103   <none>        8000/TCP   5s
   203  ```