Skip to content
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

Cherry pick of #7185 onto release-1.13 #7296

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions nodeup/pkg/model/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ go_library(
"//util/pkg/exec:go_default_library",
"//util/pkg/reflectutils:go_default_library",
"//util/pkg/vfs:go_default_library",
"//vendor/github.com/aws/aws-sdk-go/aws:go_default_library",
"//vendor/github.com/aws/aws-sdk-go/aws/ec2metadata:go_default_library",
"//vendor/github.com/aws/aws-sdk-go/aws/session:go_default_library",
"//vendor/github.com/aws/aws-sdk-go/service/ec2:go_default_library",
"//vendor/github.com/blang/semver:go_default_library",
"//vendor/k8s.io/api/core/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/api/resource:go_default_library",
Expand Down
45 changes: 32 additions & 13 deletions nodeup/pkg/model/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ import (
"k8s.io/kops/util/pkg/vfs"
"k8s.io/kubernetes/pkg/util/mount"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"

"github.com/blang/semver"
"k8s.io/klog"
)
Expand Down Expand Up @@ -529,25 +533,40 @@ func EvaluateHostnameOverride(hostnameOverride string) (string, error) {
return hostnameOverride, nil
}

// We recognize @aws as meaning "the local-hostname from the aws metadata service"
vBytes, err := vfs.Context.ReadFile("metadata://aws/meta-data/local-hostname")
// We recognize @aws as meaning "the private DNS name from AWS", to generate this we need to get a few pieces of information
azBytes, err := vfs.Context.ReadFile("metadata://aws/meta-data/placement/availability-zone")
if err != nil {
return "", fmt.Errorf("error reading local hostname from AWS metadata: %v", err)
return "", fmt.Errorf("error reading availability zone from AWS metadata: %v", err)
}

// The local-hostname gets it's hostname from the AWS DHCP Option Set, which
// may provide multiple hostnames separated by spaces. For now just choose
// the first one as the hostname.
domains := strings.Fields(string(vBytes))
if len(domains) == 0 {
klog.Warningf("Local hostname from AWS metadata service was empty")
return "", nil
instanceIDBytes, err := vfs.Context.ReadFile("metadata://aws/meta-data/instance-id")
if err != nil {
return "", fmt.Errorf("error reading instance-id from AWS metadata: %v", err)
}
instanceID := string(instanceIDBytes)

config := aws.NewConfig()
config = config.WithCredentialsChainVerboseErrors(true)

s, err := session.NewSession(config)
if err != nil {
return "", fmt.Errorf("error starting new AWS session: %v", err)
}
domain := domains[0]

klog.Infof("Using hostname from AWS metadata service: %s", domain)
svc := ec2.New(s, config.WithRegion(string(azBytes[:len(azBytes)-1])))

result, err := svc.DescribeInstances(&ec2.DescribeInstancesInput{
InstanceIds: []*string{&instanceID},
})

if len(result.Reservations) != 1 {
return "", fmt.Errorf("Too many reservations returned for the single instance-id")
}

return domain, nil
if len(result.Reservations[0].Instances) != 1 {
return "", fmt.Errorf("Too many instances returned for the single instance-id")
}
return *(result.Reservations[0].Instances[0].PrivateDnsName), nil
}

// FindCert is a helper method to retrieving a certificate from the store
Expand Down
41 changes: 28 additions & 13 deletions upup/pkg/fi/nodeup/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,25 +441,40 @@ func evaluateHostnameOverride(hostnameOverride string) (string, error) {
k = strings.ToLower(k)

if k == "@aws" {
// We recognize @aws as meaning "the local-hostname from the aws metadata service"
vBytes, err := vfs.Context.ReadFile("metadata://aws/meta-data/local-hostname")
// We recognize @aws as meaning "the private DNS name from AWS", to generate this we need to get a few pieces of information
azBytes, err := vfs.Context.ReadFile("metadata://aws/meta-data/placement/availability-zone")
if err != nil {
return "", fmt.Errorf("error reading local hostname from AWS metadata: %v", err)
return "", fmt.Errorf("error reading availability zone from AWS metadata: %v", err)
}

// The local-hostname gets it's hostname from the AWS DHCP Option Set, which
// may provide multiple hostnames separated by spaces. For now just choose
// the first one as the hostname.
domains := strings.Fields(string(vBytes))
if len(domains) == 0 {
klog.Warningf("Local hostname from AWS metadata service was empty")
return "", nil
instanceIDBytes, err := vfs.Context.ReadFile("metadata://aws/meta-data/instance-id")
if err != nil {
return "", fmt.Errorf("error reading instance-id from AWS metadata: %v", err)
}
instanceID := string(instanceIDBytes)

config := aws.NewConfig()
config = config.WithCredentialsChainVerboseErrors(true)

s, err := session.NewSession(config)
if err != nil {
return "", fmt.Errorf("error starting new AWS session: %v", err)
}

domain := domains[0]
klog.Infof("Using hostname from AWS metadata service: %s", domain)
svc := ec2.New(s, config.WithRegion(string(azBytes[:len(azBytes)-1])))

result, err := svc.DescribeInstances(&ec2.DescribeInstancesInput{
InstanceIds: []*string{&instanceID},
})

return domain, nil
if len(result.Reservations) != 1 {
return "", fmt.Errorf("Too many reservations returned for the single instance-id")
}

if len(result.Reservations[0].Instances) != 1 {
return "", fmt.Errorf("Too many instances returned for the single instance-id")
}
return *(result.Reservations[0].Instances[0].PrivateDnsName), nil
}

if k == "@digitalocean" {
Expand Down