diff --git a/README.md b/README.md index 756dc6f..27774c6 100644 --- a/README.md +++ b/README.md @@ -139,6 +139,13 @@ To this end, this provider supports the following extra specs schema: "type": "string", "description": "The source snapshot to create this disk." }, + "ssh_keys": { + "type": "array", + "description": "A list of SSH keys to be added to the instance. The format is USERNAME:SSH_KEY", + "items": { + "type": "string" + } + }, "enable_boot_debug": { "type": "boolean", "description": "Enable boot debug on the VM." @@ -155,7 +162,7 @@ To this end, this provider supports the following extra specs schema: } } }, - "additionalProperties": false + "additionalProperties": false } ``` @@ -169,7 +176,8 @@ An example of extra specs json would look like this: "nic_type": "VIRTIO_NET", "custom_labels": {"environment":"production","project":"myproject"}, "network_tags": ["web-server", "production"], - "source_snapshot": "projects/garm-testing/global/snapshots/garm-snapshot" + "source_snapshot": "projects/garm-testing/global/snapshots/garm-snapshot", + "ssh_keys": ["username1:ssh_key1", "username2:ssh_key2"] } ``` diff --git a/internal/client/gcp.go b/internal/client/gcp.go index 6bb9ca2..27aab8c 100644 --- a/internal/client/gcp.go +++ b/internal/client/gcp.go @@ -159,6 +159,10 @@ func (g *GcpCli) CreateInstance(ctx context.Context, spec *spec.RunnerSpec) (*co Key: proto.String("runner_name"), Value: proto.String(spec.BootstrapParams.Name), }, + { + Key: proto.String("ssh-keys"), + Value: proto.String(spec.SSHKeys), + }, }, }, Labels: spec.CustomLabels, diff --git a/internal/spec/spec.go b/internal/spec/spec.go index 00262b0..5bc1d4d 100644 --- a/internal/spec/spec.go +++ b/internal/spec/spec.go @@ -77,6 +77,13 @@ const ( "type": "string", "description": "The source snapshot to create this disk." }, + "ssh_keys": { + "type": "array", + "description": "A list of SSH keys to be added to the instance.", + "items": { + "type": "string" + } + }, "enable_boot_debug": { "type": "boolean", "description": "Enable boot debug on the VM." @@ -181,6 +188,7 @@ type extraSpecs struct { CustomLabels map[string]string `json:"custom_labels,omitempty"` NetworkTags []string `json:"network_tags,omitempty"` SourceSnapshot string `json:"source_snapshot,omitempty"` + SSHKeys []string `json:"ssh_keys,omitempty"` EnableBootDebug *bool `json:"enable_boot_debug"` } @@ -230,6 +238,7 @@ type RunnerSpec struct { CustomLabels map[string]string NetworkTags []string SourceSnapshot string + SSHKeys string EnableBootDebug bool } @@ -255,6 +264,11 @@ func (r *RunnerSpec) MergeExtraSpecs(extraSpecs *extraSpecs) { if extraSpecs.SourceSnapshot != "" { r.SourceSnapshot = extraSpecs.SourceSnapshot } + if len(extraSpecs.SSHKeys) > 0 { + for key := range extraSpecs.SSHKeys { + r.SSHKeys = r.SSHKeys + "\n" + extraSpecs.SSHKeys[key] + } + } if extraSpecs.EnableBootDebug != nil { r.EnableBootDebug = *extraSpecs.EnableBootDebug } diff --git a/internal/spec/spec_test.go b/internal/spec/spec_test.go index f1da448..bef7023 100644 --- a/internal/spec/spec_test.go +++ b/internal/spec/spec_test.go @@ -42,6 +42,7 @@ func TestJsonSchemaValidation(t *testing.T) { }, "network_tags": ["example_tag"], "source_snapshot": "snapshot-id", + "ssh_keys": ["ssh-key", "ssh-key2"], "enable_boot_debug": true, "runner_install_template": "install-template", "extra_context": { @@ -99,6 +100,7 @@ func TestMergeExtraSpecs(t *testing.T) { CustomLabels: map[string]string{"key1": "value1"}, NetworkTags: []string{"tag1", "tag2"}, SourceSnapshot: "projects/garm-testing/global/snapshots/garm-snapshot", + SSHKeys: []string{"ssh-key1", "ssh-key2"}, EnableBootDebug: &enable_boot_debug, }, },