Skip to content
Open
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
16 changes: 12 additions & 4 deletions catalog/consul.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package catalog

import (
"fmt"
"strconv"
"strings"
"sync"
"time"

"github.com/hashicorp/consul/api"
"github.com/hashicorp/go-hclog"
hclog "github.com/hashicorp/go-hclog"
)

const (
Expand Down Expand Up @@ -302,24 +303,31 @@ func (c *consul) create(services map[string]service) int {
meta[ConsulSourceKey] = ConsulAWSTag
meta[ConsulAWSNS] = ns
meta[ConsulAWSID] = n.awsID

p, err := strconv.Atoi(n.attributes["Port"])
if err != nil {
panic("Bad port")
}

service := api.AgentService{
ID: id,
Service: name,
Tags: []string{ConsulAWSTag},
Address: h,
Address: n.attributes["Address"],
Port: p,
Meta: meta,
}
if n.port != 0 {
service.Port = n.port
}
reg := api.CatalogRegistration{
Node: ConsulAWSNodeName,
Address: h,
Address: "127.0.0.1",
NodeMeta: map[string]string{ConsulSourceKey: ConsulAWSTag},
SkipNodeUpdate: true,
Service: &service,
}
_, err := c.client.Catalog().Register(&reg, nil)
_, err = c.client.Catalog().Register(&reg, nil)
if err != nil {
c.log.Error("cannot create service", "error", err.Error())
} else {
Expand Down
36 changes: 35 additions & 1 deletion subcommand/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,43 @@ package subcommand

import (
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/aws/ec2metadata"
"github.com/aws/aws-sdk-go-v2/aws/external"
hclog "github.com/hashicorp/go-hclog"
"os"
"time"
)

type WithEC2MetadataRegion struct {
Client *ec2metadata.EC2Metadata
}

func (p WithEC2MetadataRegion) GetRegion() (string, error) {
return p.Client.Region()
}

func AWSConfig() (aws.Config, error) {
return external.LoadDefaultAWSConfig()
cfg, err := external.LoadDefaultAWSConfig()
if err != nil {
panic("unable to load SDK config, " + err.Error())
}

region, ok := os.LookupEnv("AWS_REGION")
if !ok {

originalTimeout := cfg.HTTPClient.Timeout
cfg.HTTPClient.Timeout = 2 * time.Second
metaClient := ec2metadata.New(cfg)

if !metaClient.Available() {
panic("Metadata service cannot be reached.")
}

cfg.HTTPClient.Timeout = originalTimeout
hclog.Default().Info("Autodetected region")
region, _ = metaClient.Region()
}

cfg.Region = region
return cfg, err
}
34 changes: 32 additions & 2 deletions subcommand/sync-catalog/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/hashicorp/consul-aws/catalog"
"github.com/hashicorp/consul-aws/subcommand"
"github.com/hashicorp/consul/command/flags"
hclog "github.com/hashicorp/go-hclog"
"github.com/mitchellh/cli"
)

Expand All @@ -24,6 +25,7 @@ type Command struct {
http *flags.HTTPFlags
flagToConsul bool
flagToAWS bool
flagAWSNamespace string
flagAWSNamespaceID string
flagAWSServicePrefix string
flagAWSDeprecatedPullInterval string
Expand All @@ -42,6 +44,8 @@ func (c *Command) init() {
"If true, AWS services will be synced to Consul. (Defaults to false)")
c.flags.BoolVar(&c.flagToAWS, "to-aws", false,
"If true, Consul services will be synced to AWS. (Defaults to false)")
c.flags.StringVar(&c.flagAWSNamespace, "aws-namespace",
"", "The AWS namespace to sync with Consul services.")
c.flags.StringVar(&c.flagAWSNamespaceID, "aws-namespace-id",
"", "The AWS namespace to sync with Consul services.")
c.flags.StringVar(&c.flagAWSServicePrefix, "aws-service-prefix",
Expand Down Expand Up @@ -78,8 +82,12 @@ func (c *Command) Run(args []string) int {
c.UI.Error("Should have no non-flag arguments.")
return 1
}
if len(c.flagAWSNamespaceID) == 0 {
c.UI.Error("Please provide -aws-namespace-id.")
if len(c.flagAWSNamespaceID) == 0 && len(c.flagAWSNamespace) == 0 {
c.UI.Error("Please provide -aws-namespace or -aws-namespace-id.")
return 1
}
if len(c.flagAWSNamespaceID) > 0 && len(c.flagAWSNamespace) > 0 {
c.UI.Error("Please provide -aws-namespace or -aws-namespace-id.")
return 1
}
config, err := subcommand.AWSConfig()
Expand All @@ -89,6 +97,28 @@ func (c *Command) Run(args []string) int {
}
awsClient := sd.New(config)

if len(c.flagAWSNamespace) > 0 {

resp := awsClient.ListNamespacesRequest(&sd.ListNamespacesInput{})
pager := resp.Paginate()
for pager.Next() {
page := pager.CurrentPage()
for _, namespace := range page.Namespaces {
if c.flagAWSNamespace == *namespace.Name {
hclog.Default().Info("NamespaceId found", *namespace.Id)
c.flagAWSNamespaceID = *namespace.Id
}
}
}
if err := pager.Err(); err != nil {
panic("paging failed, " + err.Error())
}
}

if len(c.flagAWSNamespaceID) == 0 {
panic("Namespace not found, aborting...")
}

consulClient, err := c.http.APIClient()
if err != nil {
c.UI.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err))
Expand Down