Skip to content

Commit

Permalink
ec2,eks: handle duplicate key creation error
Browse files Browse the repository at this point in the history
Signed-off-by: Gyuho Lee <leegyuho@amazon.com>
  • Loading branch information
gyuho committed Jul 9, 2020
1 parent 7c668ff commit 3dcaaa7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
7 changes: 6 additions & 1 deletion ec2/key-pair.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"
"time"

"github.com/aws/aws-k8s-tester/pkg/fileutil"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/request"
Expand Down Expand Up @@ -43,7 +44,11 @@ func (ts *Tester) createKeyPair() (err error) {
KeyName: aws.String(ts.cfg.RemoteAccessKeyName),
})
if err != nil {
return err
ev, ok := err.(awserr.Error)
if ok && ev.Code() == "InvalidKeyPair.Duplicate" && fileutil.Exist(ts.cfg.RemoteAccessPrivateKeyPath) {
ts.lg.Warn("key pair already created, private key locally exists, skipping EC2 key pair creation")
return nil
}
}
if aws.StringValue(output.KeyName) != ts.cfg.RemoteAccessKeyName {
return fmt.Errorf("unexpected key name %q, expected %q", aws.StringValue(output.KeyName), ts.cfg.RemoteAccessKeyName)
Expand Down
6 changes: 6 additions & 0 deletions eks/key-pair.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"
"time"

"github.com/aws/aws-k8s-tester/pkg/fileutil"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/request"
Expand Down Expand Up @@ -43,6 +44,11 @@ func (ts *Tester) createKeyPair() (err error) {
KeyName: aws.String(ts.cfg.RemoteAccessKeyName),
})
if err != nil {
ev, ok := err.(awserr.Error)
if ok && ev.Code() == "InvalidKeyPair.Duplicate" && fileutil.Exist(ts.cfg.RemoteAccessPrivateKeyPath) {
ts.lg.Warn("key pair already created, private key locally exists, skipping EC2 key pair creation")
return nil
}
return err
}
if aws.StringValue(output.KeyName) != ts.cfg.RemoteAccessKeyName {
Expand Down

0 comments on commit 3dcaaa7

Please sign in to comment.