Skip to content

Commit

Permalink
Merge pull request #36 from c445/roehrijn/route53-workspace-names
Browse files Browse the repository at this point in the history
feat: register machines in Route53 zone and use host lookup when connect
  • Loading branch information
pascalbreuninger authored Oct 9, 2024
2 parents e949dfd + b89ce75 commit 89d6c90
Show file tree
Hide file tree
Showing 107 changed files with 50,251 additions and 100 deletions.
51 changes: 12 additions & 39 deletions cmd/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,11 @@ func (cmd *CommandCmd) Run(
)
if err != nil {
return err
} else if len(instance.Reservations) == 0 {
} else if instance.Status == "" {
return fmt.Errorf("instance %s doesn't exist", providerAws.Config.MachineID)
}

if providerAws.Config.UseInstanceConnectEndpoint {
instanceID := *instance.Reservations[0].Instances[0].InstanceId
endpointID := providerAws.Config.InstanceConnectEndpointID

var err error
Expand All @@ -90,7 +89,7 @@ func (cmd *CommandCmd) Run(
connectArgs := []string{
"ec2-instance-connect",
"open-tunnel",
"--instance-id", instanceID,
"--instance-id", instance.InstanceID,
"--local-port", portStr,
}
if endpointID != "" {
Expand Down Expand Up @@ -124,8 +123,6 @@ func (cmd *CommandCmd) Run(

// try session manager
if providerAws.Config.UseSessionManager {
instanceID := *instance.Reservations[0].Instances[0].InstanceId

cancelCtx, cancel := context.WithCancel(ctx)
defer cancel()

Expand All @@ -136,7 +133,7 @@ func (cmd *CommandCmd) Run(
}

addr := fmt.Sprintf("localhost:%d", port)
connectArgs, err := aws.CommandArgsSSMTunneling(instanceID, port)
connectArgs, err := aws.CommandArgsSSMTunneling(instance.InstanceID, port)
if err != nil {
return err
}
Expand All @@ -163,40 +160,16 @@ func (cmd *CommandCmd) Run(
return ssh.Run(ctx, client, command, os.Stdin, os.Stdout, os.Stderr)
}

// try public ip
if instance.Reservations[0].Instances[0].PublicIpAddress != nil {
ip := *instance.Reservations[0].Instances[0].PublicIpAddress

sshClient, err := ssh.NewSSHClient("devpod", ip+":22", privateKey)
if err != nil {
logs.Debugf("error connecting to public ip [%s]: %v", ip, err)
} else {
// successfully connected to the public ip
defer sshClient.Close()

return ssh.Run(ctx, sshClient, command, os.Stdin, os.Stdout, os.Stderr)
}
}

// try private ip
if instance.Reservations[0].Instances[0].PrivateIpAddress != nil {
ip := *instance.Reservations[0].Instances[0].PrivateIpAddress

sshClient, err := ssh.NewSSHClient("devpod", ip+":22", privateKey)
if err != nil {
logs.Debugf("error connecting to private ip [%s]: %v", ip, err)
} else {
// successfully connected to the private ip
defer sshClient.Close()

return ssh.Run(ctx, sshClient, command, os.Stdin, os.Stdout, os.Stderr)
}
host := instance.Host()
sshClient, err := ssh.NewSSHClient("devpod", host+":22", privateKey)
if err != nil {
logs.Debugf("error connecting to ip [%s]: %v", host, err)
return err
} else {
// successfully connected to the public ip
defer sshClient.Close()
return ssh.Run(ctx, sshClient, command, os.Stdin, os.Stdout, os.Stderr)
}

return fmt.Errorf(
"instance %s is not reachable",
providerAws.Config.MachineID,
)
}

func waitForPort(ctx context.Context, addr string) {
Expand Down
8 changes: 3 additions & 5 deletions cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (cmd *DeleteCmd) Run(
machine *provider.Machine,
logs log.Logger,
) error {
instances, err := aws.GetDevpodInstance(
instance, err := aws.GetDevpodInstance(
ctx,
providerAws.AwsConfig,
providerAws.Config.MachineID,
Expand All @@ -53,10 +53,8 @@ func (cmd *DeleteCmd) Run(
return err
}

if len(instances.Reservations) > 0 {
instance := instances.Reservations[0].Instances[0]

err = aws.Delete(ctx, providerAws.AwsConfig, instance)
if instance.Status != "" {
err = aws.Delete(ctx, providerAws, instance)
if err != nil {
return err
}
Expand Down
8 changes: 3 additions & 5 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (cmd *StartCmd) Run(
machine *provider.Machine,
logs log.Logger,
) error {
instances, err := aws.GetDevpodStoppedInstance(
instance, err := aws.GetDevpodStoppedInstance(
ctx,
providerAws.AwsConfig,
providerAws.Config.MachineID,
Expand All @@ -53,10 +53,8 @@ func (cmd *StartCmd) Run(
return err
}

if len(instances.Reservations) > 0 {
targetID := instances.Reservations[0].Instances[0].InstanceId

err = aws.Start(ctx, providerAws.AwsConfig, *targetID)
if instance.Status != "" {
err = aws.Start(ctx, providerAws.AwsConfig, instance.InstanceID)
if err != nil {
return err
}
Expand Down
6 changes: 2 additions & 4 deletions cmd/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,8 @@ func (cmd *StopCmd) Run(
return err
}

if len(instances.Reservations) > 0 {
targetID := instances.Reservations[0].Instances[0].InstanceId

err = aws.Stop(ctx, providerAws.AwsConfig, *targetID)
if instances.Status != "" {
err = aws.Stop(ctx, providerAws.AwsConfig, instances.InstanceID)
if err != nil {
return err
}
Expand Down
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ module github.com/loft-sh/devpod-provider-aws
go 1.19

require (
github.com/aws/aws-sdk-go-v2 v1.18.0
github.com/aws/aws-sdk-go-v2 v1.18.1
github.com/aws/aws-sdk-go-v2/config v1.18.25
github.com/aws/aws-sdk-go-v2/service/ec2 v1.98.0
github.com/aws/aws-sdk-go-v2/service/iam v1.19.12
github.com/aws/aws-sdk-go-v2/service/route53 v1.28.3
github.com/loft-sh/devpod v0.0.3-0.20230512100016-aee23bbc9aad
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v1.6.1
Expand All @@ -19,8 +20,8 @@ require (
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.13.24 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.13.3 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.33 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.27 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.34 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.28 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.3.34 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.27 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.12.10 // indirect
Expand Down
11 changes: 8 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@ github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2 h1:+vx7roKuyA63n
github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2/go.mod h1:HBCaDeC1lPdgDeDbhX8XFpy1jqjK0IBG8W5K+xYqA0w=
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d h1:licZJFw2RwpHMqeKTCYkitsPqHNxTmd4SNR5r94FGM8=
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d/go.mod h1:asat636LX7Bqt5lYEZ27JNDcqxfjdBQuJ/MM4CN/Lzo=
github.com/aws/aws-sdk-go-v2 v1.18.0 h1:882kkTpSFhdgYRKVZ/VCgf7sd0ru57p2JCxz4/oN5RY=
github.com/aws/aws-sdk-go-v2 v1.18.0/go.mod h1:uzbQtefpm44goOPmdKyAlXSNcwlRgF3ePWVW6EtJvvw=
github.com/aws/aws-sdk-go-v2 v1.18.1 h1:+tefE750oAb7ZQGzla6bLkOwfcQCEtC5y2RqoqCeqKo=
github.com/aws/aws-sdk-go-v2 v1.18.1/go.mod h1:uzbQtefpm44goOPmdKyAlXSNcwlRgF3ePWVW6EtJvvw=
github.com/aws/aws-sdk-go-v2/config v1.18.25 h1:JuYyZcnMPBiFqn87L2cRppo+rNwgah6YwD3VuyvaW6Q=
github.com/aws/aws-sdk-go-v2/config v1.18.25/go.mod h1:dZnYpD5wTW/dQF0rRNLVypB396zWCcPiBIvdvSWHEg4=
github.com/aws/aws-sdk-go-v2/credentials v1.13.24 h1:PjiYyls3QdCrzqUN35jMWtUK1vqVZ+zLfdOa/UPFDp0=
github.com/aws/aws-sdk-go-v2/credentials v1.13.24/go.mod h1:jYPYi99wUOPIFi0rhiOvXeSEReVOzBqFNOX5bXYoG2o=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.13.3 h1:jJPgroehGvjrde3XufFIJUZVK5A2L9a3KwSFgKy9n8w=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.13.3/go.mod h1:4Q0UFP0YJf0NrsEuEYHpM9fTSEVnD16Z3uyEF7J9JGM=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.33 h1:kG5eQilShqmJbv11XL1VpyDbaEJzWxd4zRiCG30GSn4=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.33/go.mod h1:7i0PF1ME/2eUPFcjkVIwq+DOygHEoK92t5cDqNgYbIw=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.27 h1:vFQlirhuM8lLlpI7imKOMsjdQLuN9CPi+k44F/OFVsk=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.34 h1:A5UqQEmPaCFpedKouS4v+dHCTUo2sKqhoKO9U5kxyWo=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.34/go.mod h1:wZpTEecJe0Btj3IYnDx/VlUzor9wm3fJHyvLpQF0VwY=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.27/go.mod h1:UrHnn3QV/d0pBZ6QBAEQcqFLf8FAzLmoUfPVIueOvoM=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.28 h1:srIVS45eQuewqz6fKKu6ZGXaq6FuFg5NzgQBAM6g8Y4=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.28/go.mod h1:7VRpKQQedkfIEXb4k52I7swUnZP0wohVajJMRn3vsUw=
github.com/aws/aws-sdk-go-v2/internal/ini v1.3.34 h1:gGLG7yKaXG02/jBlg210R7VgQIotiQntNhsCFejawx8=
github.com/aws/aws-sdk-go-v2/internal/ini v1.3.34/go.mod h1:Etz2dj6UHYuw+Xw830KfzCfWGMzqvUTCjUj5b76GVDc=
github.com/aws/aws-sdk-go-v2/service/ec2 v1.98.0 h1:WblDV33AG9dhv0zFEPEmGtD5UECSNpKMxtdENULfR8M=
Expand All @@ -26,6 +29,8 @@ github.com/aws/aws-sdk-go-v2/service/iam v1.19.12 h1:JH1H7POlsZt41X9JYIBLZoXW0Qv
github.com/aws/aws-sdk-go-v2/service/iam v1.19.12/go.mod h1:kAnokExGCYs7zfvZEZdFHvQ/x4ZKIci0Raps6mZI1Ag=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.27 h1:0iKliEXAcCa2qVtRs7Ot5hItA2MsufrphbRFlz1Owxo=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.27/go.mod h1:EOwBD4J4S5qYszS5/3DpkejfuK+Z5/1uzICfPaZLtqw=
github.com/aws/aws-sdk-go-v2/service/route53 v1.28.3 h1:nJbE4+tHd8xpM1RB1ZF0/xTJnFd/ATz42ZC35lwXx0w=
github.com/aws/aws-sdk-go-v2/service/route53 v1.28.3/go.mod h1:Cd4MnFoV+6fELBrgWXJ4Y09FrSkn/VjNPkOr1Yr1muU=
github.com/aws/aws-sdk-go-v2/service/sso v1.12.10 h1:UBQjaMTCKwyUYwiVnUt6toEJwGXsLBI6al083tpjJzY=
github.com/aws/aws-sdk-go-v2/service/sso v1.12.10/go.mod h1:ouy2P4z6sJN70fR3ka3wD3Ro3KezSxU6eKGQI2+2fjI=
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.14.10 h1:PkHIIJs8qvq0e5QybnZoG1K/9QTrLr9OsqCIo59jOBA=
Expand Down
9 changes: 9 additions & 0 deletions hack/provider/provider.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ optionGroups:
- AWS_USE_SPOT_INSTANCE
- AWS_USE_SESSION_MANAGER
- AWS_KMS_KEY_ARN_FOR_SESSION_MANAGER
- AWS_USE_ROUTE53
- AWS_ROUTE53_ZONE_NAME
name: "AWS options"
defaultVisible: false
- options:
Expand Down Expand Up @@ -251,6 +253,13 @@ options:
AWS_KMS_KEY_ARN_FOR_SESSION_MANAGER:
description: "Specify the KMS key ARN to use for the AWS Session Manager"
default: ""
AWS_USE_ROUTE53:
description: "If defined, will try to create a Route53 record for the machine's IP address and use that hostname upon machine connection. If activated, the Route53 zone can be configured by AWS_ROUTE53_ZONE_NAME or of not, it is tried to lookup by the tag `devpod=devpod`"
type: boolean
default: false
AWS_ROUTE53_ZONE_NAME:
description: "The zone name of a Route53 hosted zone to use for the machine's DNS name"
default: ""
INACTIVITY_TIMEOUT:
description: If defined, will automatically stop the VM after the inactivity period.
default: 10m
Expand Down
Loading

0 comments on commit 89d6c90

Please sign in to comment.