Skip to content
This repository was archived by the owner on Jan 21, 2020. It is now read-only.

Commit 46d0977

Browse files
author
David Chung
committed
clean up aws plugin; fix up environment variables
Signed-off-by: David Chung <[email protected]>
1 parent 3a54da3 commit 46d0977

File tree

4 files changed

+70
-40
lines changed

4 files changed

+70
-40
lines changed

pkg/provider/aws/plugin/instance/monitor.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ const (
1818
// list of known instances, and report anything it hasn't seen before, or
1919
// if anything that disappeared.
2020
type Monitor struct {
21-
stop chan struct{}
22-
topics map[string]interface{}
21+
PollInterval time.Duration
22+
stop chan struct{}
23+
topics map[string]interface{}
2324

2425
// Plugin is the instance plugin to use
2526
Plugin instance.Plugin
@@ -64,7 +65,10 @@ func (m *Monitor) PublishOn(c chan<- *event.Event) {
6465

6566
log.Infoln("Start monitoring instances", c)
6667

67-
ticker := time.Tick(2 * time.Second)
68+
ticker := time.Tick(5 * time.Second)
69+
if m.PollInterval > 0 {
70+
ticker = time.Tick(m.PollInterval)
71+
}
6872

6973
instances := map[instance.ID]instance.Description{}
7074
last := mapset.NewSet()

pkg/rpc/client/client.go

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ import (
88
"net/http"
99
"net/http/httputil"
1010
"net/url"
11-
"os"
11+
// "os"
1212
"path"
1313
"sync"
1414
"time"
1515

1616
logutil "github.com/docker/infrakit/pkg/log"
1717
"github.com/docker/infrakit/pkg/rpc"
18+
"github.com/docker/infrakit/pkg/run/local"
1819
"github.com/docker/infrakit/pkg/spi"
1920
"github.com/gorilla/rpc/v2/json2"
2021
)
@@ -74,17 +75,6 @@ func New(address string, api spi.InterfaceSpec) (Client, error) {
7475
return cl, nil
7576
}
7677

77-
// ClientTimeoutEnv environment variable for the client timeout (in time.Duration)
78-
const ClientTimeoutEnv = "INFRAKIT_CLIENT_TIMEOUT"
79-
80-
// timeout returns the client rpc http timeout
81-
func timeout() time.Duration {
82-
if parsed, err := time.ParseDuration(os.Getenv(ClientTimeoutEnv)); err == nil {
83-
return parsed
84-
}
85-
return time.Duration(1 * time.Second)
86-
}
87-
8878
func parseAddress(address string) (*url.URL, *http.Client, error) {
8979
if path.Ext(address) == ".listen" {
9080
buff, err := ioutil.ReadFile(address)
@@ -105,7 +95,7 @@ func parseAddress(address string) (*url.URL, *http.Client, error) {
10595
u.Host = "h"
10696
u.Path = "" // clear it since it's a file path and we are using it to connect.
10797
return u, &http.Client{
108-
Timeout: timeout(),
98+
Timeout: local.ClientTimeout(),
10999
Transport: &http.Transport{
110100
// TODO(chungers) - fix this deprecation
111101
Dial: func(proto, addr string) (conn net.Conn, err error) {
@@ -122,9 +112,9 @@ func parseAddress(address string) (*url.URL, *http.Client, error) {
122112
case "http", "https":
123113
transport := &http.Transport{
124114
Dial: (&net.Dialer{
125-
Timeout: timeout(),
115+
Timeout: local.ClientTimeout(),
126116
}).Dial,
127-
TLSHandshakeTimeout: timeout(),
117+
TLSHandshakeTimeout: local.ClientTimeout(),
128118
}
129119
return u, &http.Client{Transport: transport}, nil
130120

pkg/run/local/local.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import (
55
"os"
66
"os/user"
77
"path/filepath"
8+
"time"
9+
10+
"github.com/docker/infrakit/pkg/types"
811
)
912

1013
const (
@@ -14,8 +17,16 @@ const (
1417

1518
// EnvPlaybooks is the environment variable for storing the playbooks file
1619
EnvPlaybooks = "INFRAKIT_PLAYBOOKS_FILE"
20+
21+
// EnvClientTimeout is the timeout used by the rpc client
22+
EnvClientTimeout = "INFRAKIT_CLIENT_TIMEOUT"
1723
)
1824

25+
// ClientTimeout returns the client timeout
26+
func ClientTimeout() time.Duration {
27+
return types.MustParseDuration(Getenv(EnvClientTimeout, "15s")).Duration()
28+
}
29+
1930
// InfrakitHome returns the directory of INFRAKIT_HOME if specified. Otherwise, it will return
2031
// the user's home directory. If that cannot be determined, then it returns the current working
2132
// directory. If that still cannot be determined, a temporary directory is returned.

pkg/run/v0/aws/aws.go

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package aws
22

33
import (
44
"strings"
5+
"time"
56

67
"github.com/aws/aws-sdk-go/service/autoscaling"
78
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
@@ -34,14 +35,17 @@ const (
3435
EnvRegion = "INFRAKIT_AWS_REGION"
3536

3637
// EnvStackName is the env for stack name
37-
EnvStackName = "INFRAKIT_AWS_STACKNAME"
38+
EnvStackName = "INFRAKIT_AWS_STACK_NAME"
3839

3940
// EnvMetadataTemplateURL is the location of the template for Metadata plugin
4041
EnvMetadataTemplateURL = "INFRAKIT_AWS_METADATA_TEMPLATE_URL"
4142

4243
// EnvMetadataPollInterval is the env to set fo polling for metadata updates
4344
EnvMetadataPollInterval = "INFRAKIT_AWS_METADATA_POLL_INTERVAL"
4445

46+
// EnvMonitorPollInterval is the env to set fo polling for instance changes
47+
EnvMonitorPollInterval = "INFRAKIT_AWS_MONITOR_POLL_INTERVAL"
48+
4549
// EnvNamespaceTags is the env to set for namespace tags. It's k=v,...
4650
EnvNamespaceTags = "INFRAKIT_AWS_NAMESPACE_TAGS"
4751

@@ -59,12 +63,16 @@ func init() {
5963

6064
// Options capture the options for starting up the plugin.
6165
type Options struct {
66+
6267
// Namespace is a set of kv pairs for tags that namespaces the resource instances
6368
Namespace map[string]string
6469

6570
// ELBNames is a list of names for ELB instances to start the L4 plugins
6671
ELBNames []string
6772

73+
// MonitorPollInterval is the interval for the polling to observe instance new/delete events
74+
MonitorPollInterval time.Duration
75+
6876
aws_metadata.Options `json:",inline" yaml:",inline"`
6977
}
7078

@@ -82,13 +90,16 @@ func defaultNamespace() map[string]string {
8290

8391
// DefaultOptions return an Options with default values filled in.
8492
var DefaultOptions = Options{
85-
Namespace: defaultNamespace(),
86-
ELBNames: strings.Split(local.Getenv(EnvELBNames, ""), ","),
93+
Namespace: defaultNamespace(),
94+
ELBNames: strings.Split(local.Getenv(EnvELBNames, ""), ","),
95+
MonitorPollInterval: types.MustParseDuration(local.Getenv(EnvMonitorPollInterval, "0s")).Duration(),
8796
Options: aws_metadata.Options{
8897
Template: local.Getenv(EnvMetadataTemplateURL, ""),
8998
StackName: local.Getenv(EnvStackName, ""),
9099
Options: aws_instance.Options{
91-
Region: local.Getenv(EnvRegion, ""), // empty string trigger auto-detect
100+
Region: local.Getenv(EnvRegion, ""), // empty string trigger auto-detect
101+
AccessKeyID: local.Getenv("AWS_ACCESS_KEY_ID", ""),
102+
SecretAccessKey: local.Getenv("AWS_SECRET_ACCESS_KEY", ""),
92103
},
93104
PollInterval: types.MustParseDuration(local.Getenv(EnvMetadataPollInterval, "60s")),
94105
},
@@ -105,27 +116,13 @@ func Run(scope scope.Scope, name plugin.Name,
105116
return
106117
}
107118

108-
var metadataPlugin metadata.Plugin
109-
stopMetadataPlugin := make(chan struct{})
110-
metadataPlugin, err = aws_metadata.NewPlugin(options.Options, stopMetadataPlugin)
111-
if err != nil {
112-
return
113-
}
114-
115119
var instancePlugin instance.Plugin
116120
builder := aws_instance.Builder{Options: options.Options.Options}
117121
instancePlugin, err = builder.BuildInstancePlugin(options.Namespace)
118122
if err != nil {
119123
return
120124
}
121125

122-
monitor := &aws_instance.Monitor{Plugin: instancePlugin}
123-
124-
onStop = func() {
125-
close(stopMetadataPlugin)
126-
monitor.Stop()
127-
}
128-
129126
autoscalingClient := autoscaling.New(builder.Config)
130127
cloudWatchLogsClient := cloudwatchlogs.New(builder.Config)
131128
dynamodbClient := dynamodb.New(builder.Config)
@@ -136,10 +133,6 @@ func Run(scope scope.Scope, name plugin.Name,
136133

137134
transport.Name = name
138135
impls = map[run.PluginCode]interface{}{
139-
run.Event: map[string]event.Plugin{
140-
"ec2-instance": monitor.Init(),
141-
},
142-
run.Metadata: metadataPlugin,
143136
run.Instance: map[string]instance.Plugin{
144137
"autoscaling-autoscalinggroup": aws_instance.NewAutoScalingGroupPlugin(autoscalingClient, options.Namespace),
145138
"autoscaling-launchconfiguration": aws_instance.NewLaunchConfigurationPlugin(autoscalingClient, options.Namespace),
@@ -175,5 +168,37 @@ func Run(scope scope.Scope, name plugin.Name,
175168
impls[run.L4] = func() (map[string]loadbalancer.L4, error) { return l4Map, nil }
176169
}
177170

171+
if options.MonitorPollInterval > 0 {
172+
log.Info("run the event source for watching instance add/removes", "poll", options.MonitorPollInterval)
173+
monitor := &aws_instance.Monitor{Plugin: instancePlugin}
174+
impls[run.Event] = map[string]event.Plugin{
175+
"ec2-instance": monitor.Init(),
176+
}
177+
onStop = func() {
178+
monitor.Stop()
179+
}
180+
}
181+
182+
if u := local.Getenv(EnvMetadataTemplateURL, ""); u != "" {
183+
184+
log.Info("Include metadata plugin", "url", u)
185+
186+
var metadataPlugin metadata.Plugin
187+
stopMetadataPlugin := make(chan struct{})
188+
metadataPlugin, err = aws_metadata.NewPlugin(options.Options, stopMetadataPlugin)
189+
if err != nil {
190+
return
191+
}
192+
impls[run.Metadata] = metadataPlugin
193+
194+
cleanup := onStop
195+
onStop = func() {
196+
close(stopMetadataPlugin)
197+
if cleanup != nil {
198+
cleanup()
199+
}
200+
}
201+
}
202+
178203
return
179204
}

0 commit comments

Comments
 (0)