Skip to content

Commit c6b3653

Browse files
committed
add resources_monitoring logic
1 parent 054e9bc commit c6b3653

File tree

3 files changed

+142
-0
lines changed

3 files changed

+142
-0
lines changed

docs/resources/agent.md

+27
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ resource "kubernetes_pod" "dev" {
7979
- `metadata` (Block List) Each `metadata` block defines a single item consisting of a key/value pair. This feature is in alpha and may break in future releases. (see [below for nested schema](#nestedblock--metadata))
8080
- `motd_file` (String) The path to a file within the workspace containing a message to display to users when they login via SSH. A typical value would be `"/etc/motd"`.
8181
- `order` (Number) The order determines the position of agents in the UI presentation. The lowest order is shown first and agents with equal order are sorted by name (ascending order).
82+
- `resources_monitoring` (Block Set) The resources monitoring configuration for this agent. (see [below for nested schema](#nestedblock--resources_monitoring))
8283
- `shutdown_script` (String) A script to run before the agent is stopped. The script should exit when it is done to signal that the workspace can be stopped. This option is an alias for defining a `coder_script` resource with `run_on_stop` set to `true`.
8384
- `startup_script` (String) A script to run after the agent starts. The script should exit when it is done to signal that the agent is ready. This option is an alias for defining a `coder_script` resource with `run_on_start` set to `true`.
8485
- `startup_script_behavior` (String) This option sets the behavior of the `startup_script`. When set to `"blocking"`, the `startup_script` must exit before the workspace is ready. When set to `"non-blocking"`, the `startup_script` may run in the background and the workspace will be ready immediately. Default is `"non-blocking"`, although `"blocking"` is recommended. This option is an alias for defining a `coder_script` resource with `start_blocks_login` set to `true` (blocking).
@@ -116,3 +117,29 @@ Optional:
116117
- `display_name` (String) The user-facing name of this value.
117118
- `order` (Number) The order determines the position of agent metadata in the UI presentation. The lowest order is shown first and metadata with equal order are sorted by key (ascending order).
118119
- `timeout` (Number) The maximum time the command is allowed to run in seconds.
120+
121+
122+
<a id="nestedblock--resources_monitoring"></a>
123+
### Nested Schema for `resources_monitoring`
124+
125+
Optional:
126+
127+
- `memory` (Block Set) The memory monitoring configuration for this agent. (see [below for nested schema](#nestedblock--resources_monitoring--memory))
128+
- `volumes` (Block Set) The volumes monitoring configuration for this agent. (see [below for nested schema](#nestedblock--resources_monitoring--volumes))
129+
130+
<a id="nestedblock--resources_monitoring--memory"></a>
131+
### Nested Schema for `resources_monitoring.memory`
132+
133+
Optional:
134+
135+
- `enabled` (Boolean) Enable memory monitoring for this agent.
136+
- `threshold` (Number) The memory usage threshold in percentage at which to trigger an alert. Value should be between 0 and 100.
137+
138+
139+
<a id="nestedblock--resources_monitoring--volumes"></a>
140+
### Nested Schema for `resources_monitoring.volumes`
141+
142+
Optional:
143+
144+
- `enabled` (Boolean) Enable volume monitoring for this agent.
145+
- `threshold` (Number) The volume usage threshold in percentage at which to trigger an alert. Value should be between 0 and 100.

provider/agent.go

+62
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,68 @@ func agentResource() *schema.Resource {
259259
ForceNew: true,
260260
Optional: true,
261261
},
262+
"resources_monitoring": {
263+
Type: schema.TypeSet,
264+
Description: "The resources monitoring configuration for this agent.",
265+
ForceNew: true,
266+
Optional: true,
267+
Elem: &schema.Resource{
268+
Schema: map[string]*schema.Schema{
269+
"memory": {
270+
Type: schema.TypeSet,
271+
Description: "The memory monitoring configuration for this agent.",
272+
ForceNew: true,
273+
Optional: true,
274+
Elem: &schema.Resource{
275+
Schema: map[string]*schema.Schema{
276+
"enabled": {
277+
Type: schema.TypeBool,
278+
Description: "Enable memory monitoring for this agent.",
279+
ForceNew: true,
280+
Optional: true,
281+
Default: true,
282+
},
283+
"threshold": {
284+
Type: schema.TypeInt,
285+
Description: "The memory usage threshold in percentage at which to trigger an alert. Value should be between 0 and 100.",
286+
ForceNew: true,
287+
Optional: true,
288+
},
289+
},
290+
},
291+
},
292+
"volume": {
293+
Type: schema.TypeSet,
294+
Description: "The volumes monitoring configuration for this agent.",
295+
ForceNew: true,
296+
Optional: true,
297+
Elem: &schema.Resource{
298+
Schema: map[string]*schema.Schema{
299+
"path": {
300+
Type: schema.TypeString,
301+
Description: "The path of the volume to monitor.",
302+
ForceNew: true,
303+
Optional: true,
304+
},
305+
"enabled": {
306+
Type: schema.TypeBool,
307+
Description: "Enable volume monitoring for this agent.",
308+
ForceNew: true,
309+
Optional: true,
310+
Default: true,
311+
},
312+
"threshold": {
313+
Type: schema.TypeInt,
314+
Description: "The volume usage threshold in percentage at which to trigger an alert. Value should be between 0 and 100.",
315+
ForceNew: true,
316+
Optional: true,
317+
},
318+
},
319+
},
320+
},
321+
},
322+
},
323+
},
262324
},
263325
CustomizeDiff: func(ctx context.Context, rd *schema.ResourceDiff, i any) error {
264326
if !rd.HasChange("metadata") {

provider/agent_test.go

+53
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,59 @@ func TestAgent_Metadata(t *testing.T) {
211211
})
212212
}
213213

214+
func TestAgent_ResourcesMonitoring(t *testing.T) {
215+
t.Parallel()
216+
resource.Test(t, resource.TestCase{
217+
ProviderFactories: coderFactory(),
218+
IsUnitTest: true,
219+
Steps: []resource.TestStep{{
220+
Config: `
221+
provider "coder" {
222+
url = "https://example.com"
223+
}
224+
resource "coder_agent" "dev" {
225+
os = "linux"
226+
arch = "amd64"
227+
resources_monitoring {
228+
memory {
229+
enabled = true
230+
threshold = 80
231+
}
232+
volume {
233+
path = "/volume1"
234+
enabled = true
235+
threshold = 80
236+
}
237+
volume {
238+
path = "/volume2"
239+
enabled = true
240+
threshold = 100
241+
}
242+
}
243+
}`,
244+
Check: func(state *terraform.State) error {
245+
require.Len(t, state.Modules, 1)
246+
require.Len(t, state.Modules[0].Resources, 1)
247+
248+
resource := state.Modules[0].Resources["coder_agent.dev"]
249+
require.NotNil(t, resource)
250+
251+
t.Logf("resource: %v", resource.Primary.Attributes)
252+
253+
attr := resource.Primary.Attributes
254+
require.Equal(t, "1", attr["resources_monitoring.#"])
255+
require.Equal(t, "1", attr["resources_monitoring.0.memory.#"])
256+
require.Equal(t, "2", attr["resources_monitoring.0.volume.#"])
257+
require.Equal(t, "80", attr["resources_monitoring.0.memory.0.threshold"])
258+
require.Equal(t, "/volume1", attr["resources_monitoring.0.volume.0.path"])
259+
require.Equal(t, "100", attr["resources_monitoring.0.volume.1.threshold"])
260+
require.Equal(t, "/volume2", attr["resources_monitoring.0.volume.1.path"])
261+
return nil
262+
},
263+
}},
264+
})
265+
}
266+
214267
func TestAgent_MetadataDuplicateKeys(t *testing.T) {
215268
t.Parallel()
216269
resource.Test(t, resource.TestCase{

0 commit comments

Comments
 (0)