Skip to content

Commit

Permalink
Merge pull request #5619 from justinsb/cherrypick_5511_release-1.10
Browse files Browse the repository at this point in the history
Cherry pick of #5511 onto release-1.10
  • Loading branch information
k8s-ci-robot committed Aug 15, 2018
2 parents 1bc0366 + 4a60159 commit 839340b
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 3 deletions.
73 changes: 73 additions & 0 deletions pkg/resources/aws/aws.go
Expand Up @@ -21,6 +21,7 @@ import (
"bytes"
"fmt"
"strings"
"sync"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/autoscaling"
Expand Down Expand Up @@ -417,6 +418,69 @@ func ListInstances(cloud fi.Cloud, clusterName string) ([]*resources.Resource, e
return resourceTrackers, nil
}

// getDumpState gets the dumpState from the dump context, or creates one if not yet initialized
func getDumpState(dumpContext *resources.DumpOperation) *dumpState {
if dumpContext.CloudState == nil {
dumpContext.CloudState = &dumpState{
cloud: dumpContext.Cloud.(awsup.AWSCloud),
}
}
return dumpContext.CloudState.(*dumpState)
}

type imageInfo struct {
SSHUser string
}

type dumpState struct {
cloud awsup.AWSCloud
mutex sync.Mutex
images map[string]*imageInfo
}

func (s *dumpState) getImageInfo(imageID string) (*imageInfo, error) {
s.mutex.Lock()
defer s.mutex.Unlock()

if s.images == nil {
s.images = make(map[string]*imageInfo)
}

info := s.images[imageID]
if info == nil {
image, err := s.cloud.ResolveImage(imageID)
if err != nil {
return nil, err
}
info = &imageInfo{}

if image != nil {
sshUser := guessSSHUser(image)
if sshUser == "" {
glog.Warningf("unable to guess SSH user for image: %+v", image)
}
info.SSHUser = sshUser
}

s.images[imageID] = info
}

return info, nil
}

func guessSSHUser(image *ec2.Image) string {
owner := aws.StringValue(image.OwnerId)
switch owner {
case awsup.WellKnownAccountAmazonSystemLinux2:
return "ec2-user"
case awsup.WellKnownAccountCoreOS:
return "core"
case awsup.WellKnownAccountKopeio:
return "admin"
}
return ""
}

func DumpInstance(op *resources.DumpOperation, r *resources.Resource) error {
data := make(map[string]interface{})
data["id"] = r.ID
Expand Down Expand Up @@ -444,6 +508,15 @@ func DumpInstance(op *resources.DumpOperation, r *resources.Resource) error {
role := strings.TrimPrefix(key, awsup.TagNameRolePrefix)
i.Roles = append(i.Roles, role)
}

imageID := aws.StringValue(ec2Instance.ImageId)
imageInfo, err := getDumpState(op).getImageInfo(imageID)
if err != nil {
glog.Warningf("unable to fetch image %q: %v", imageID, err)
} else if imageInfo != nil {
i.SSHUser = imageInfo.SSHUser
}

op.Dump.Instances = append(op.Dump.Instances, i)

return nil
Expand Down
1 change: 1 addition & 0 deletions pkg/resources/dumpmodel.go
Expand Up @@ -21,6 +21,7 @@ type Instance struct {
Name string `json:"name,omitempty"`
PublicAddresses []string `json:"publicAddresses,omitempty"`
Roles []string `json:"roles,omitempty"`
SSHUser string `json:"sshUser,omitempty"`
}

// Subnet is the type for an subnetwork in a dump
Expand Down
7 changes: 4 additions & 3 deletions upup/pkg/fi/cloudup/awsup/aws_cloud.go
Expand Up @@ -81,9 +81,10 @@ const TagNameKopsRole = "kubernetes.io/kops/role"
const TagNameClusterOwnershipPrefix = "kubernetes.io/cluster/"

const (
WellKnownAccountKopeio = "383156758163"
WellKnownAccountRedhat = "309956199498"
WellKnownAccountCoreOS = "595879546273"
WellKnownAccountKopeio = "383156758163"
WellKnownAccountRedhat = "309956199498"
WellKnownAccountCoreOS = "595879546273"
WellKnownAccountAmazonSystemLinux2 = "137112412989"
)

type AWSCloud interface {
Expand Down

0 comments on commit 839340b

Please sign in to comment.