Skip to content

Commit fe1b339

Browse files
Merge pull request #19 from fabi200123/add-sec-group-option
[WIP] Add extra-specs option to add SecurityGroups to the runner
2 parents e56d555 + bbf03cc commit fe1b339

File tree

3 files changed

+63
-37
lines changed

3 files changed

+63
-37
lines changed

internal/client/aws.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -244,13 +244,14 @@ func (a *AwsCli) CreateRunningInstance(ctx context.Context, spec *spec.RunnerSpe
244244
}
245245

246246
resp, err := a.client.RunInstances(ctx, &ec2.RunInstancesInput{
247-
ImageId: aws.String(spec.BootstrapParams.Image),
248-
InstanceType: types.InstanceType(spec.BootstrapParams.Flavor),
249-
MaxCount: aws.Int32(1),
250-
MinCount: aws.Int32(1),
251-
SubnetId: aws.String(spec.SubnetID),
252-
UserData: aws.String(udata),
253-
KeyName: spec.SSHKeyName,
247+
ImageId: aws.String(spec.BootstrapParams.Image),
248+
InstanceType: types.InstanceType(spec.BootstrapParams.Flavor),
249+
MaxCount: aws.Int32(1),
250+
MinCount: aws.Int32(1),
251+
SubnetId: aws.String(spec.SubnetID),
252+
SecurityGroupIds: spec.SecurityGroupIds,
253+
UserData: aws.String(udata),
254+
KeyName: spec.SSHKeyName,
254255
TagSpecifications: []types.TagSpecification{
255256
{
256257
ResourceType: types.ResourceTypeInstance,

internal/spec/spec.go

+20-14
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,12 @@ func newExtraSpecsFromBootstrapData(data params.BootstrapInstance) (*extraSpecs,
7373
}
7474

7575
type extraSpecs struct {
76-
SubnetID *string `json:"subnet_id,omitempty" jsonschema:"pattern=^subnet-[0-9a-fA-F]{17}$"`
77-
SSHKeyName *string `json:"ssh_key_name,omitempty" jsonschema:"description=The name of the Key Pair to use for the instance."`
78-
DisableUpdates *bool `json:"disable_updates,omitempty" jsonschema:"description=Disable automatic updates on the VM."`
79-
EnableBootDebug *bool `json:"enable_boot_debug,omitempty" jsonschema:"description=Enable boot debug on the VM"`
80-
ExtraPackages []string `json:"extra_packages,omitempty" jsonschema:"description=Extra packages to install on the VM"`
76+
SubnetID *string `json:"subnet_id,omitempty" jsonschema:"pattern=^subnet-[0-9a-fA-F]{17}$"`
77+
SSHKeyName *string `json:"ssh_key_name,omitempty" jsonschema:"description=The name of the Key Pair to use for the instance."`
78+
SecurityGroupIds []string `json:"security_group_ids,omitempty" jsonschema:"description=The security groups IDs to associate with the instance. Default: Amazon EC2 uses the default security group."`
79+
DisableUpdates *bool `json:"disable_updates,omitempty" jsonschema:"description=Disable automatic updates on the VM."`
80+
EnableBootDebug *bool `json:"enable_boot_debug,omitempty" jsonschema:"description=Enable boot debug on the VM"`
81+
ExtraPackages []string `json:"extra_packages,omitempty" jsonschema:"description=Extra packages to install on the VM"`
8182
// The Cloudconfig struct from common package
8283
cloudconfig.CloudConfigSpec
8384
}
@@ -112,15 +113,16 @@ func GetRunnerSpecFromBootstrapParams(cfg *config.Config, data params.BootstrapI
112113
}
113114

114115
type RunnerSpec struct {
115-
Region string
116-
DisableUpdates bool
117-
ExtraPackages []string
118-
EnableBootDebug bool
119-
Tools params.RunnerApplicationDownload
120-
BootstrapParams params.BootstrapInstance
121-
SubnetID string
122-
SSHKeyName *string
123-
ControllerID string
116+
Region string
117+
DisableUpdates bool
118+
ExtraPackages []string
119+
EnableBootDebug bool
120+
Tools params.RunnerApplicationDownload
121+
BootstrapParams params.BootstrapInstance
122+
SecurityGroupIds []string
123+
SubnetID string
124+
SSHKeyName *string
125+
ControllerID string
124126
}
125127

126128
func (r *RunnerSpec) Validate() error {
@@ -142,6 +144,10 @@ func (r *RunnerSpec) MergeExtraSpecs(extraSpecs *extraSpecs) {
142144
r.SSHKeyName = extraSpecs.SSHKeyName
143145
}
144146

147+
if len(extraSpecs.SecurityGroupIds) > 0 {
148+
r.SecurityGroupIds = extraSpecs.SecurityGroupIds
149+
}
150+
145151
if extraSpecs.DisableUpdates != nil {
146152
r.DisableUpdates = *extraSpecs.DisableUpdates
147153
}

internal/spec/spec_test.go

+35-16
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,15 @@ func TestExtraSpecsFromBootstrapData(t *testing.T) {
3636
{
3737
name: "valid bootstrap data",
3838
input: params.BootstrapInstance{
39-
ExtraSpecs: json.RawMessage(`{"subnet_id": "subnet-0a0a0a0a0a0a0a0a0", "ssh_key_name": "ssh_key_name", "disable_updates": true, "enable_boot_debug": true, "extra_packages": ["package1", "package2"], "runner_install_template": "IyEvYmluL2Jhc2gKZWNobyBJbnN0YWxsaW5nIHJ1bm5lci4uLg==", "pre_install_scripts": {"setup.sh": "IyEvYmluL2Jhc2gKZWNobyBTZXR1cCBzY3JpcHQuLi4="}, "extra_context": {"key": "value"}}`),
39+
ExtraSpecs: json.RawMessage(`{"subnet_id": "subnet-0a0a0a0a0a0a0a0a0", "ssh_key_name": "ssh_key_name", "security_group_ids": ["sg-018c35963edfb1cce", "sg-018c35963edfb1cee"], "disable_updates": true, "enable_boot_debug": true, "extra_packages": ["package1", "package2"], "runner_install_template": "IyEvYmluL2Jhc2gKZWNobyBJbnN0YWxsaW5nIHJ1bm5lci4uLg==", "pre_install_scripts": {"setup.sh": "IyEvYmluL2Jhc2gKZWNobyBTZXR1cCBzY3JpcHQuLi4="}, "extra_context": {"key": "value"}}`),
4040
},
4141
expectedOutput: &extraSpecs{
42-
SubnetID: aws.String("subnet-0a0a0a0a0a0a0a0a0"),
43-
SSHKeyName: aws.String("ssh_key_name"),
44-
DisableUpdates: aws.Bool(true),
45-
EnableBootDebug: aws.Bool(true),
46-
ExtraPackages: []string{"package1", "package2"},
42+
SubnetID: aws.String("subnet-0a0a0a0a0a0a0a0a0"),
43+
SSHKeyName: aws.String("ssh_key_name"),
44+
SecurityGroupIds: []string{"sg-018c35963edfb1cce", "sg-018c35963edfb1cee"},
45+
DisableUpdates: aws.Bool(true),
46+
EnableBootDebug: aws.Bool(true),
47+
ExtraPackages: []string{"package1", "package2"},
4748
CloudConfigSpec: cloudconfig.CloudConfigSpec{
4849
RunnerInstallTemplate: []byte("#!/bin/bash\necho Installing runner..."),
4950
PreInstallScripts: map[string][]byte{
@@ -74,6 +75,15 @@ func TestExtraSpecsFromBootstrapData(t *testing.T) {
7475
},
7576
errString: "",
7677
},
78+
{
79+
name: "specs just with security_group_ids",
80+
input: params.BootstrapInstance{
81+
ExtraSpecs: json.RawMessage(`{"security_group_ids": ["sg-018c35963edfb1cce", "sg-018c35963edfb1cee"]}`),
82+
},
83+
expectedOutput: &extraSpecs{
84+
SecurityGroupIds: []string{"sg-018c35963edfb1cce", "sg-018c35963edfb1cee"},
85+
},
86+
},
7787
{
7888
name: "specs just with disable_updates",
7989
input: params.BootstrapInstance{
@@ -174,6 +184,14 @@ func TestExtraSpecsFromBootstrapData(t *testing.T) {
174184
expectedOutput: nil,
175185
errString: "ssh_key_name: Invalid type. Expected: string, given: integer",
176186
},
187+
{
188+
name: "invalid type for security_group_ids",
189+
input: params.BootstrapInstance{
190+
ExtraSpecs: json.RawMessage(`{"security_group_ids": "sg-018c35963edfb1cce"}`),
191+
},
192+
expectedOutput: nil,
193+
errString: "security_group_ids: Invalid type. Expected: array, given: string",
194+
},
177195
{
178196
name: "invalid type for disable_updates",
179197
input: params.BootstrapInstance{
@@ -267,7 +285,7 @@ func TestGetRunnerSpecFromBootstrapParams(t *testing.T) {
267285

268286
data := params.BootstrapInstance{
269287
Name: "mock-name",
270-
ExtraSpecs: json.RawMessage(`{"subnet_id": "subnet-0a0a0a0a0a0a0a0a0", "ssh_key_name": "ssh_key_name", "disable_updates": true, "enable_boot_debug": true, "extra_packages": ["package1", "package2"], "runner_install_template": "IyEvYmluL2Jhc2gKZWNobyBJbnN0YWxsaW5nIHJ1bm5lci4uLg==", "pre_install_scripts": {"setup.sh": "IyEvYmluL2Jhc2gKZWNobyBTZXR1cCBzY3JpcHQuLi4="}, "extra_context": {"key": "value"}}`),
288+
ExtraSpecs: json.RawMessage(`{"subnet_id": "subnet-0a0a0a0a0a0a0a0a0", "ssh_key_name": "ssh_key_name", "security_group_ids": ["sg-018c35963edfb1cce", "sg-018c35963edfb1cee"], "disable_updates": true, "enable_boot_debug": true, "extra_packages": ["package1", "package2"], "runner_install_template": "IyEvYmluL2Jhc2gKZWNobyBJbnN0YWxsaW5nIHJ1bm5lci4uLg==", "pre_install_scripts": {"setup.sh": "IyEvYmluL2Jhc2gKZWNobyBTZXR1cCBzY3JpcHQuLi4="}, "extra_context": {"key": "value"}}`),
271289
}
272290

273291
config := &config.Config{
@@ -283,15 +301,16 @@ func TestGetRunnerSpecFromBootstrapParams(t *testing.T) {
283301
Region: "region",
284302
}
285303
expectedRunnerSpec := &RunnerSpec{
286-
Region: "region",
287-
DisableUpdates: true,
288-
ExtraPackages: []string{"package1", "package2"},
289-
EnableBootDebug: true,
290-
SubnetID: "subnet-0a0a0a0a0a0a0a0a0",
291-
Tools: Mocktools,
292-
ControllerID: "controller_id",
293-
BootstrapParams: data,
294-
SSHKeyName: aws.String("ssh_key_name"),
304+
Region: "region",
305+
DisableUpdates: true,
306+
ExtraPackages: []string{"package1", "package2"},
307+
EnableBootDebug: true,
308+
SubnetID: "subnet-0a0a0a0a0a0a0a0a0",
309+
Tools: Mocktools,
310+
ControllerID: "controller_id",
311+
BootstrapParams: data,
312+
SSHKeyName: aws.String("ssh_key_name"),
313+
SecurityGroupIds: []string{"sg-018c35963edfb1cce", "sg-018c35963edfb1cee"},
295314
}
296315

297316
runnerSpec, err := GetRunnerSpecFromBootstrapParams(config, data, "controller_id")

0 commit comments

Comments
 (0)