@@ -2,6 +2,7 @@ package aws
2
2
3
3
import (
4
4
"strings"
5
+ "time"
5
6
6
7
"github.com/aws/aws-sdk-go/service/autoscaling"
7
8
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
@@ -34,14 +35,17 @@ const (
34
35
EnvRegion = "INFRAKIT_AWS_REGION"
35
36
36
37
// EnvStackName is the env for stack name
37
- EnvStackName = "INFRAKIT_AWS_STACKNAME "
38
+ EnvStackName = "INFRAKIT_AWS_STACK_NAME "
38
39
39
40
// EnvMetadataTemplateURL is the location of the template for Metadata plugin
40
41
EnvMetadataTemplateURL = "INFRAKIT_AWS_METADATA_TEMPLATE_URL"
41
42
42
43
// EnvMetadataPollInterval is the env to set fo polling for metadata updates
43
44
EnvMetadataPollInterval = "INFRAKIT_AWS_METADATA_POLL_INTERVAL"
44
45
46
+ // EnvMonitorPollInterval is the env to set fo polling for instance changes
47
+ EnvMonitorPollInterval = "INFRAKIT_AWS_MONITOR_POLL_INTERVAL"
48
+
45
49
// EnvNamespaceTags is the env to set for namespace tags. It's k=v,...
46
50
EnvNamespaceTags = "INFRAKIT_AWS_NAMESPACE_TAGS"
47
51
@@ -59,12 +63,16 @@ func init() {
59
63
60
64
// Options capture the options for starting up the plugin.
61
65
type Options struct {
66
+
62
67
// Namespace is a set of kv pairs for tags that namespaces the resource instances
63
68
Namespace map [string ]string
64
69
65
70
// ELBNames is a list of names for ELB instances to start the L4 plugins
66
71
ELBNames []string
67
72
73
+ // MonitorPollInterval is the interval for the polling to observe instance new/delete events
74
+ MonitorPollInterval time.Duration
75
+
68
76
aws_metadata.Options `json:",inline" yaml:",inline"`
69
77
}
70
78
@@ -82,13 +90,16 @@ func defaultNamespace() map[string]string {
82
90
83
91
// DefaultOptions return an Options with default values filled in.
84
92
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 (),
87
96
Options : aws_metadata.Options {
88
97
Template : local .Getenv (EnvMetadataTemplateURL , "" ),
89
98
StackName : local .Getenv (EnvStackName , "" ),
90
99
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" , "" ),
92
103
},
93
104
PollInterval : types .MustParseDuration (local .Getenv (EnvMetadataPollInterval , "60s" )),
94
105
},
@@ -105,27 +116,13 @@ func Run(scope scope.Scope, name plugin.Name,
105
116
return
106
117
}
107
118
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
-
115
119
var instancePlugin instance.Plugin
116
120
builder := aws_instance.Builder {Options : options .Options .Options }
117
121
instancePlugin , err = builder .BuildInstancePlugin (options .Namespace )
118
122
if err != nil {
119
123
return
120
124
}
121
125
122
- monitor := & aws_instance.Monitor {Plugin : instancePlugin }
123
-
124
- onStop = func () {
125
- close (stopMetadataPlugin )
126
- monitor .Stop ()
127
- }
128
-
129
126
autoscalingClient := autoscaling .New (builder .Config )
130
127
cloudWatchLogsClient := cloudwatchlogs .New (builder .Config )
131
128
dynamodbClient := dynamodb .New (builder .Config )
@@ -136,10 +133,6 @@ func Run(scope scope.Scope, name plugin.Name,
136
133
137
134
transport .Name = name
138
135
impls = map [run.PluginCode ]interface {}{
139
- run .Event : map [string ]event.Plugin {
140
- "ec2-instance" : monitor .Init (),
141
- },
142
- run .Metadata : metadataPlugin ,
143
136
run .Instance : map [string ]instance.Plugin {
144
137
"autoscaling-autoscalinggroup" : aws_instance .NewAutoScalingGroupPlugin (autoscalingClient , options .Namespace ),
145
138
"autoscaling-launchconfiguration" : aws_instance .NewLaunchConfigurationPlugin (autoscalingClient , options .Namespace ),
@@ -175,5 +168,37 @@ func Run(scope scope.Scope, name plugin.Name,
175
168
impls [run .L4 ] = func () (map [string ]loadbalancer.L4 , error ) { return l4Map , nil }
176
169
}
177
170
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
+
178
203
return
179
204
}
0 commit comments