-
Notifications
You must be signed in to change notification settings - Fork 9
feat(instances): Add network interfaces and relays #481
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: prod-staging
Are you sure you want to change the base?
Changes from all commits
119a778
0e982c7
0c5a0bb
883fbb4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -128,8 +128,8 @@ type Instance struct { | |
| Roms []*InstanceRom `mirror:"instance.roms" field:",embed" create:"set" edit:"set,add,del=strings" flag:"rom" sep:"none" help:"Attach ROM." placeholder:"name=<name>,image=<ref>,at=<path>" example:"name=my-rom\\,image=myuser/my-rom:latest\\,at=/rom0,name=mydata\\,dir=./mydata\\,at=/rom"` | ||
| Plugins []*InstancePlugin `mirror:"instance.plugins" field:",embed" create:"set" edit:"set,add,del=strings" flag:"plugin" sep:"none" help:"Load plugin into the instance." placeholder:"name=<name>,image=<ref>[,config=<json>]" example:"name=sandbox\\,image=plugins/sandbox:latest,name=sandbox\\,image=plugins/sandbox:latest\\,config={\"persist_path\":\"/data\"}"` | ||
|
|
||
| Networks []InstanceNetwork `mirror:"instance.network_interfaces" field:",embed"` | ||
| Gpus []InstanceGpu `mirror:"instance.gpus" field:"gpus,embed"` | ||
| Networks []*InstanceNetwork `mirror:"instance.network_interfaces" field:",embed" create:"set" flag:"network" sep:"none" help:"Attach network interface.\n name: interface name\n relay.name: interface to route all traffic through\n relay.uuid: same, by uuid\n relay.dns: whether the relay forwards DNS (default true)\n ip: address in CIDR notation, requires tap-name\n mac: address, requires tap-name\n tap-name: TAP device to bring your own interface\n autoconfig: whether the guest configures the interface itself" placeholder:"<key>=<value>" example:"relay.name=my-router-eth0,relay.name=my-router-eth0\\,relay.dns=false,name=eth1\\,tap-name=tap0\\,ip=10.0.0.5/24"` | ||
| Gpus []InstanceGpu `mirror:"instance.gpus" field:"gpus,embed"` | ||
|
|
||
| Timestamps struct { | ||
| Created types.RelativeTime `mirror:"instance.created_at" field:",short"` | ||
|
|
@@ -178,9 +178,51 @@ type Instance struct { | |
| } | ||
|
|
||
| type InstanceNetwork struct { | ||
| UUID string `mirror:"uuid" field:",long"` | ||
| PrivateIP string `mirror:"private_ip" field:",long"` | ||
| MAC string `mirror:"mac" field:",long"` | ||
| Name string `name:"name" mirror:"name" json:"name,omitempty" field:",long"` | ||
| UUID string `name:"-" mirror:"uuid" json:"-" field:",long"` | ||
| PrivateIP string `name:"-" mirror:"private_ip" json:"-" field:",long"` | ||
| MAC string `name:"mac" mirror:"mac" json:"mac,omitempty" field:",long"` | ||
| TapName string `name:"tap-name" mirror:"tap_name" json:"tap-name,omitempty" field:"tap-name,long"` | ||
|
|
||
| Relay *InstanceNetworkRelay `name:"relay" mirror:"relay" json:"relay,omitempty" field:",embed"` | ||
|
|
||
| IP string `name:"ip" json:"ip,omitempty" field:"ip,invisible"` | ||
| Autoconfig *bool `name:"autoconfig" mirror:"autoconfig" json:"autoconfig,omitempty" field:",long"` | ||
| } | ||
|
|
||
| // InstanceNetworkRelay is the interface all of this interface's traffic is | ||
| // routed through. The target is another instance's interface, which has no | ||
| // API of its own, so this holds plain identifiers rather than a Link. | ||
| type InstanceNetworkRelay struct { | ||
| Name string `name:"name" mirror:"name" json:"name,omitempty" field:",long"` | ||
| UUID string `name:"uuid" mirror:"uuid" json:"uuid,omitempty" field:",long"` | ||
| // The API names this member relay_dns, inside the relay object itself. | ||
| DNS *bool `name:"dns" mirror:"relay_dns" json:"dns,omitempty" field:",long"` | ||
| } | ||
|
|
||
| func (n *InstanceNetwork) UnmarshalText(data []byte) error { | ||
| type alias InstanceNetwork | ||
| parsed, err := value.Parse[alias]([]string{string(data)}) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| *n = InstanceNetwork(parsed) | ||
| if n.Relay != nil && n.Relay.Name == "" && n.Relay.UUID == "" { | ||
| return fmt.Errorf("relay requires relay.name or relay.uuid") | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (n *InstanceNetwork) UnmarshalJSON(data []byte) error { | ||
| if len(data) != 0 && data[0] == '"' { | ||
| var text string | ||
| if err := json.Unmarshal(data, &text); err != nil { | ||
| return err | ||
| } | ||
| return n.UnmarshalText([]byte(text)) | ||
| } | ||
| type networkJSON InstanceNetwork // alias to avoid recursion | ||
| return json.Unmarshal(data, (*networkJSON)(n)) | ||
| } | ||
|
|
||
| type InstanceGpu struct { | ||
|
|
@@ -1349,6 +1391,35 @@ func (Instance) Create(ctx context.Context, fields []resource.Field) ([]resource | |
| } | ||
| req.Plugins = append(req.Plugins, reqPlugin) | ||
| } | ||
| case "networks": | ||
| for _, net := range field.Create.Set.([]*InstanceNetwork) { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed - |
||
| reqNet := platform.CreateInstanceRequestNetworkInterface{ | ||
| Name: ptr.NilIfZero(net.Name), | ||
| TapName: ptr.NilIfZero(net.TapName), | ||
| Ip: ptr.NilIfZero(net.IP), | ||
| Autoconfig: net.Autoconfig, | ||
| } | ||
| if net.Relay != nil { | ||
| if net.Relay.Name == "" && net.Relay.UUID == "" { | ||
| return nil, fmt.Errorf("relay requires a name or uuid") | ||
| } | ||
| reqNet.Relay = &platform.NetworkInterfaceRelay{ | ||
| Name: ptr.NilIfZero(net.Relay.Name), | ||
| Uuid: ptr.NilIfZero(net.Relay.UUID), | ||
| RelayDns: net.Relay.DNS, | ||
| } | ||
| } | ||
| if net.MAC != "" { | ||
| // HACK: the spec's network interface omits mac, though | ||
| // /v1/instances has accepted it since MAC-only custom | ||
| // interfaces landed. | ||
| macJSON, _ := json.Marshal(net.MAC) | ||
|
Comment on lines
+1413
to
+1416
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wait, why is the mac field not in the spec? 🤔 |
||
| reqNet.AdditionalProperties = map[string]jsontext.Value{ | ||
| "mac": jsontext.Value(macJSON), | ||
| } | ||
| } | ||
| req.NetworkInterfaces = append(req.NetworkInterfaces, reqNet) | ||
| } | ||
| case "service": | ||
| svc := field.Create.Set.(*InstanceService) | ||
| if req.ServiceGroup == nil { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there no better way to represent sub-structs? this is ugly 😔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See https://discord.com/channels/879723098881028167/1448722316782735463/1549777441785122928. Please do weight in 😁
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My weigh is in
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, is this okay then? Or do you have better suggestions 😓
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok for now, as I have no better suggestion 😔