Skip to content

Commit

Permalink
feat(instance): allow to create IPv6 only instances
Browse files Browse the repository at this point in the history
With the routed IP, when creating a new instances with `ip=<IPv6_UUID>`
you still end up with an IPv4 attached to your instance due to the
`dynamic_ip_required` defaulting to `true`.
This change allows one to create IPv6 only instances when passing the
new option `ipv4=false` (which defaults to `true` to remain backward
compatible).
  • Loading branch information
ziirish committed Oct 17, 2024
1 parent 02c8e16 commit 661b76a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
7 changes: 7 additions & 0 deletions internal/namespaces/instance/v1/custom_server_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type instanceCreateServerRequest struct {
IP string
Tags []string
IPv6 bool
IPv4 bool
Stopped bool
SecurityGroupID string
PlacementGroupID string
Expand Down Expand Up @@ -100,6 +101,11 @@ func serverCreateCommand() *core.Command {
Name: "ipv6",
Short: "Enable IPv6, to be used with routed-ip-enabled=false",
},
{
Name: "ipv4",
Short: "Enable/disable IPv4. Usefull for routed IPs",
Default: core.DefaultValueSetter("true"),
},
{
Name: "stopped",
Short: "Do not start server after its creation",
Expand Down Expand Up @@ -209,6 +215,7 @@ func instanceServerCreateRun(ctx context.Context, argsI interface{}) (i interfac
AddOrganizationID(args.OrganizationID).
AddProjectID(args.ProjectID).
AddEnableIPv6(scw.BoolPtr(args.IPv6)).
AddEnableIPv4(scw.BoolPtr(args.IPv4)).
AddTags(args.Tags).
AddRoutedIPEnabled(args.RoutedIPEnabled).
AddAdminPasswordEncryptionSSHKeyID(args.AdminPasswordEncryptionSSHKeyID).
Expand Down
21 changes: 21 additions & 0 deletions internal/namespaces/instance/v1/custom_server_create_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ type ServerBuilder struct {
apiInstance *instance.API
apiBlock *block.API

// IPv4 enabled
enableIPv4 bool

// serverType is filled with the ServerType if CommercialType is found in the API.
serverType *instance.ServerType
// serverImage is filled with the Image if one is provided
Expand Down Expand Up @@ -82,6 +85,17 @@ func (sb *ServerBuilder) AddEnableIPv6(enableIPv6 *bool) *ServerBuilder {
return sb
}

func (sb *ServerBuilder) AddEnableIPv4(enableIPv4 *bool) *ServerBuilder {
sb.enableIPv4 = *enableIPv4 //nolint: staticcheck

// disable dynamic IP in that case
if !sb.enableIPv4 {
sb.createReq.DynamicIPRequired = enableIPv4
}

return sb
}

func (sb *ServerBuilder) AddTags(tags []string) *ServerBuilder {
if len(tags) > 0 {
sb.createReq.Tags = tags
Expand Down Expand Up @@ -132,9 +146,16 @@ func (sb *ServerBuilder) rootVolumeIsSBS() bool {
func (sb *ServerBuilder) defaultIPType() instance.IPType {
if sb.createReq.RoutedIPEnabled != nil { //nolint: staticcheck // Field is deprecated but still supported
if *sb.createReq.RoutedIPEnabled { //nolint: staticcheck // Field is deprecated but still supported
if *sb.createReq.EnableIPv6 || !sb.enableIPv4 { //nolint: staticcheck // Field is deprecated but still supported
return instance.IPTypeRoutedIPv6
}
return instance.IPTypeRoutedIPv4
}
// routed_ip_enabled defaults to true, you can only have NAT IPs when specifying it to false explicitly
return instance.IPTypeNat
} else if (sb.createReq.EnableIPv6 != nil && *sb.createReq.EnableIPv6) || !sb.enableIPv4 { //nolint: staticcheck // Field is deprecated but still supported
// instances now have routed_ip_enabled by default
return instance.IPTypeRoutedIPv6
}

return ""
Expand Down

0 comments on commit 661b76a

Please sign in to comment.