|
| 1 | +# Resource Life Cycle |
| 2 | + |
| 3 | +## Resource |
| 4 | + |
| 5 | +A resource represents the current state and desired state of real-world entities. A resource definition look like |
| 6 | + |
| 7 | +``` |
| 8 | +type Resource struct { |
| 9 | +Resource metadata. |
| 10 | + URN string |
| 11 | + Name string |
| 12 | + Kind string |
| 13 | + Project string |
| 14 | + Labels map[string]string |
| 15 | + Version int |
| 16 | + CreatedAt time.Time |
| 17 | + UpdatedAt time.Time |
| 18 | +
|
| 19 | +Resource spec & current-state. |
| 20 | + Spec Spec |
| 21 | + State State |
| 22 | +} |
| 23 | +
|
| 24 | +type Spec struct { |
| 25 | + Configs map[string]interface{} |
| 26 | + Dependencies map[string]string |
| 27 | +} |
| 28 | +
|
| 29 | +type State struct { |
| 30 | + ModuleData json.RawMessage |
| 31 | + Status string |
| 32 | + Output Output |
| 33 | +} |
| 34 | +
|
| 35 | +type Output map[string]interface{} |
| 36 | +``` |
| 37 | + |
| 38 | +The `Resource` definition is self explanatory. It has the `Spec` field which holds the `Configs` and `Dependencies` of a resource. The `State` field has three parts, `Status` holds the current status of a resource, `Output` holds the outcome of the latest action performed while `Data` holds the transactory information which might be used to perform actions on the reosurce. |
| 39 | + |
| 40 | +For instance, a [firehose](https://github.com/odpf/firehose) resource looks like: |
| 41 | + |
| 42 | +``` |
| 43 | +{ |
| 44 | + "name": "foo", |
| 45 | + "parent": "bar", |
| 46 | + "kind": "firehose", |
| 47 | + "spec": { |
| 48 | + "configs": { |
| 49 | + "log_level": "INFO" |
| 50 | + }, |
| 51 | + "dependencies": { |
| 52 | + "deployment_cluster": "urn:odpf:entropy:kubernetes:godata" |
| 53 | + } |
| 54 | + }, |
| 55 | + "state": { |
| 56 | + "status": "STATUS_PENDING" |
| 57 | + } |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +## Resource Lifecycle - The Plan & Sync Approach |
| 62 | + |
| 63 | +We use a Plan and Sync approach for the resource lifecycle in Entropy. For illustration, we will take you through each of the steps in the lifecycle for a firehose resource. |
| 64 | + |
| 65 | +### 1. Create a resource |
| 66 | + |
| 67 | +``` |
| 68 | +POST /api/v1beta1/resources |
| 69 | +
|
| 70 | +{ |
| 71 | + "name": "foo", |
| 72 | + "parent": "bar", |
| 73 | + "kind": "firehose", |
| 74 | + "configs": { |
| 75 | + "log_level": "INFO" |
| 76 | + }, |
| 77 | + "dependencies": { |
| 78 | + "deployment_cluster": "urn:odpf:entropy:kubernetes:godata" |
| 79 | + } |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +### 2. Plan phase |
| 84 | + |
| 85 | +Plan validates the action on the current version of the resource and returns the resource with config/status/state changes (if any) applied. Plan DOES NOT have side-effects on anything other thing than the resource. |
| 86 | + |
| 87 | +Here is the resource returned |
| 88 | + |
| 89 | +``` |
| 90 | +{ |
| 91 | + "urn": "urn:odpf:entropy:firehose:bar:foo", |
| 92 | + "created_at": "2022-04-28T11:00:00.000Z", |
| 93 | + "updated_at": "2022-04-28T11:00:00.000Z", |
| 94 | + "name": "foo", |
| 95 | + "parent": "bar", |
| 96 | + "kind": "firehose", |
| 97 | + "configs": { |
| 98 | + "log_level": "INFO" |
| 99 | + }, |
| 100 | + "dependencies": { |
| 101 | + "deployment_cluster": "urn:odpf:entropy:kubernetes:godata" |
| 102 | + }, |
| 103 | + "state": { |
| 104 | + "status": "STATUS_PENDING", |
| 105 | + "output": {}, |
| 106 | + "data": { |
| 107 | + "pending": ["helm_release"] |
| 108 | + } |
| 109 | + } |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +### 3. The Sync Phase |
| 114 | + |
| 115 | +Sync is called repeatedly by Entropy core until the returned state has `StatusCompleted`.Module implementation is free to execute an action in a single Sync() call or split into multiple steps for better feedback to the end-user about the progress. |
| 116 | + |
| 117 | +A job-queue model is used to handle sync operations. Every mutation (create/update/delete) on resources will lead to enqued jobs which will be processed later by workers. |
| 118 | + |
| 119 | +### 4. Get resource (After Sync completion) |
| 120 | + |
| 121 | +``` |
| 122 | +{ |
| 123 | + "urn": "urn:odpf:entropy:firehose:bar:foo", |
| 124 | + "kind": "firehose", |
| 125 | + "name": "foo", |
| 126 | + "project": "bar", |
| 127 | + "created_at": "2022-04-28T11:00:00.000Z", |
| 128 | + "updated_at": "2022-04-28T11:00:00.000Z", |
| 129 | + "configs": { |
| 130 | + "log_level": "INFO" |
| 131 | + }, |
| 132 | + "dependencies": { |
| 133 | + "deployment_cluster": "urn:odpf:entropy:kubernetes:godata" |
| 134 | + }, |
| 135 | + "state": { |
| 136 | + "status": "COMPLETED", |
| 137 | + "output": { |
| 138 | + "app": "entropy-firehose-bar-foo" |
| 139 | + } |
| 140 | + } |
| 141 | +} |
| 142 | +``` |
| 143 | + |
| 144 | +### 5. Execute Action |
| 145 | + |
| 146 | +``` |
| 147 | +POST /api/v1beta1/resources/urn:odpf:entropy:firehose:bar:foo/execute |
| 148 | +
|
| 149 | +{ |
| 150 | + "action": "increase_log_level" |
| 151 | +} |
| 152 | +``` |
| 153 | + |
| 154 | +This will trigger Plan again, and leave the resource in `STATUS_PENDING` state. These resource will be later picked by the Sync in entropy core. |
| 155 | + |
| 156 | +``` |
| 157 | +{ |
| 158 | + "urn": "urn:odpf:entropy:firehose:bar:foo", |
| 159 | + "kind": "firehose", |
| 160 | + "name": "foo", |
| 161 | + "project": "bar", |
| 162 | + "created_at": "2022-04-28T11:00:00.000Z", |
| 163 | + "updated_at": "2022-04-28T11:00:00.000Z", |
| 164 | + "configs": { |
| 165 | + "log_level": "WARN" |
| 166 | + }, |
| 167 | + "dependencies": { |
| 168 | + "deployment_cluster": "urn:odpf:entropy:kubernetes:godata" |
| 169 | + }, |
| 170 | + "state": { |
| 171 | + "status": "PENDING", |
| 172 | + "output": { |
| 173 | + "app": "entropy-firehose-bar-foo" |
| 174 | + }, |
| 175 | + "data": { |
| 176 | + "pending": ["helm_release"] |
| 177 | + } |
| 178 | + } |
| 179 | +} |
| 180 | +``` |
0 commit comments