github.com/oam-dev/kubevela@v1.9.11/docs/examples/app-with-policy/apply-once-policy/apply-once.md (about) 1 # How to use ApplyOnce policy 2 3 By default, the KubeVela operator will prevent configuration drift for applied resources by reconciling them routinely. 4 This is useful if you want to keep your application always have the desired configuration in avoid of some unintentional 5 changes by external modifiers. 6 7 However, sometimes, you might want to use KubeVela Application to do the dispatch job and recycle job but want to leave 8 resources mutable after workflow is finished such as `Horizontal Pod Autoscaler`, etc. In this case, you can use the 9 following ApplyOnce policy. 10 11 ```shell 12 $ cat <<EOF | kubectl apply -f - 13 apiVersion: core.oam.dev/v1beta1 14 kind: Application 15 metadata: 16 name: apply-once-app-1 17 spec: 18 components: 19 - name: hello-world 20 type: webservice 21 properties: 22 image: crccheck/hello-world 23 traits: 24 - type: scaler 25 properties: 26 replicas: 1 27 policies: 28 - name: apply-once 29 type: apply-once 30 properties: 31 enable: true 32 EOF 33 ``` 34 35 In the `apply-once-app-1` case, if you change the replicas of the `hello-world` deployment after Application 36 enters `running` state, it would be brought back. On the contrary, if you set the `apply-once` policy to be disabled (by 37 default), any changes to the replicas of `hello-world` application will be brought back in the next reconcile loop. 38 39 ```shell 40 $ cat <<EOF | kubectl apply -f - 41 apiVersion: core.oam.dev/v1beta1 42 kind: Application 43 metadata: 44 name: apply-once-app-2 45 spec: 46 components: 47 - name: hello-world 48 type: webservice 49 properties: 50 image: crccheck/hello-world 51 traits: 52 - type: scaler 53 properties: 54 replicas: 1 55 - name: hello-cosmos 56 type: webservice 57 properties: 58 image: crccheck/hello-world 59 traits: 60 - type: scaler 61 properties: 62 replicas: 1 63 policies: 64 - name: apply-once 65 type: apply-once 66 properties: 67 enable: true 68 rules: 69 - selector: 70 componentNames: [ "hello-cosmos" ] 71 resourceTypes: [ "Deployment" ] 72 strategy: 73 path: [ "spec.replicas", "spec.template.spec.containers[0].resources" ] 74 EOF 75 ``` 76 77 In the `apply-once-app-2` case, any changes to the replicas or containers[0].resources of `hello-cosmos` deployment will 78 not be brought back in the next reconcile loop. And any changes of `hello-world` component will be brought back in the 79 next reconcile loop. 80 81 ```shell 82 $ cat <<EOF | kubectl apply -f - 83 apiVersion: core.oam.dev/v1beta1 84 kind: Application 85 metadata: 86 name: apply-once-app-3 87 spec: 88 components: 89 - name: hello-world 90 type: webservice 91 properties: 92 image: crccheck/hello-world 93 port: 8080 94 traits: 95 - type: scaler 96 properties: 97 replicas: 1 98 - name: hello-cosmos 99 type: webservice 100 properties: 101 image: crccheck/hello-world 102 port: 8080 103 traits: 104 - type: scaler 105 properties: 106 replicas: 1 107 policies: 108 - name: apply-once 109 type: apply-once 110 properties: 111 enable: true 112 rules: 113 - selector: 114 componentNames: [ "hello-cosmos" ] 115 resourceTypes: [ "Deployment" ] 116 strategy: 117 path: [ "*" ] 118 EOF 119 ``` 120 121 In the `apply-once-app-3` case, any changes of `hello-cosmos` deployment will not be brought back and any changes 122 of `hello-cosmos` service will be brought back in the next reconcile loop. In the same time, any changes 123 of `hello-world` component will be brought back in the next reconcile loop. 124 125 ```shell 126 $ cat <<EOF | kubectl apply -f - 127 apiVersion: core.oam.dev/v1beta1 128 kind: Application 129 metadata: 130 name: apply-once-app-4 131 spec: 132 components: 133 - name: hello-world 134 type: webservice 135 properties: 136 image: crccheck/hello-world 137 port: 8080 138 traits: 139 - type: scaler 140 properties: 141 replicas: 1 142 - name: hello-cosmos 143 type: webservice 144 properties: 145 image: crccheck/hello-world 146 port: 8080 147 traits: 148 - type: scaler 149 properties: 150 replicas: 1 151 policies: 152 - name: apply-once 153 type: apply-once 154 properties: 155 enable: true 156 rules: 157 - selector: 158 componentNames: [ "hello-cosmos" ] 159 resourceTypes: [ "Deployment" ] 160 strategy: 161 affect: onStateKeep 162 path: [ "spec.replicas"] 163 EOF 164 ``` 165 166 By default, KubeVela executes the apply-once policy in two phases: application update and cycle state maintenance, 167 allowing configuration drift depending on the policy configuration. 168 169 If you have special requirements, you can set the affect to determine the phase of policy execution . 170 affect supported configurations: onUpdate/onStateKeep/always (default) 171 172 When affect=always, or not set, the policy is executed in two phase. 173 174 When affect=onStateKeep, the policy is executed only during the stateKeep phase. In the case of `apply-once-app-4`, any 175 changes to the deployed copy of `hello-cosmos` will not be brought back to the next state keeping loop, but will be 176 brought back to the next application update. 177 178 When affect=onUpdate, the policy is only executed when the application is updated. In the case of ` 179 apply-once-app-4`, if affect=onUpdate is set, any changes to the deployed copy of `hello-cosmos` will not be brought 180 back in the next application update, but will be brought back in the next state keeping loop.