-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
[WIP] Beta AWS ELB Autodiscovery Provider #8680
Closed
Closed
Changes from 9 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
ac7e148
AWS Autodisco
andrewvc 5cd5d0d
Checkpoint
andrewvc f7b3d80
Checkpoint
andrewvc 003b5d9
Simpler concurrency for ELB autodiscover
andrewvc 6e0f9ad
More cleanup
andrewvc 9e91b9b
Checkpoint
andrewvc 432c8d1
Checkpoint
andrewvc b4c18c5
Improve hash consistency for aws elb autodisco
andrewvc 5b387b5
Moar
andrewvc 3c4bdb6
moar
andrewvc 701d52c
Tighten up for initial review
andrewvc 293c7b9
Revert changes to autodiscover.go
andrewvc 0b4468c
Add preliminary docs
andrewvc 873e183
Improved docs
andrewvc 41b8907
Improved
andrewvc 2ffab1f
Improved
andrewvc 26556a0
Moar
andrewvc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package aws |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package elb | ||
|
||
import ( | ||
"github.com/elastic/beats/libbeat/autodiscover/template" | ||
"github.com/elastic/beats/libbeat/common" | ||
) | ||
|
||
// Config for docker autodiscover provider | ||
type Config struct { | ||
Region string `config:"region" validate:"required"` | ||
Type string `config:"type"` | ||
HintsEnabled bool `config:"hints.enabled"` | ||
Builders []*common.Config `config:"builders"` | ||
Appenders []*common.Config `config:"appenders"` | ||
Templates template.MapperSettings `config:"templates"` | ||
} | ||
|
||
func defaultConfig() *Config { | ||
return &Config{} | ||
} | ||
|
||
func (c *Config) Validate() { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package elb | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/aws/aws-sdk-go-v2/service/elbv2" | ||
"go.uber.org/multierr" | ||
|
||
"github.com/elastic/beats/libbeat/common/atomic" | ||
) | ||
|
||
type Fetcher interface { | ||
fetch() ([]*lbListener, error) | ||
} | ||
|
||
type APIFetcher struct { | ||
client *elbv2.ELBV2 | ||
} | ||
|
||
func NewAPIFetcher(client *elbv2.ELBV2) Fetcher { | ||
return &APIFetcher{client} | ||
} | ||
|
||
func (f *APIFetcher) fetch() ([]*lbListener, error) { | ||
var pageSize int64 = 50 | ||
req := f.client.DescribeLoadBalancersRequest(&elbv2.DescribeLoadBalancersInput{PageSize: &pageSize}) | ||
|
||
// Limit concurrency against the AWS API to 5 | ||
taskPool := sync.Pool{} | ||
for i := 0; i < 5; i++ { | ||
taskPool.Put(nil) | ||
} | ||
|
||
ir := &fetchRequest{ | ||
req.Paginate(), | ||
f.client, | ||
atomic.MakeBool(true), | ||
[]*lbListener{}, | ||
[]error{}, | ||
sync.Mutex{}, | ||
taskPool, | ||
sync.WaitGroup{}, | ||
} | ||
|
||
return ir.fetch() | ||
} | ||
|
||
type fetchRequest struct { | ||
paginator elbv2.DescribeLoadBalancersPager | ||
client *elbv2.ELBV2 | ||
running atomic.Bool | ||
lbListeners []*lbListener | ||
errs []error | ||
resultsLock sync.Mutex | ||
taskPool sync.Pool | ||
pendingTasks sync.WaitGroup | ||
} | ||
|
||
func (p *fetchRequest) fetch() ([]*lbListener, error) { | ||
p.dispatch(p.fetchNextPage) | ||
|
||
p.pendingTasks.Wait() | ||
|
||
// Acquire the results lock to ensure memory | ||
// consistency between the last write and this read | ||
p.resultsLock.Lock() | ||
defer p.resultsLock.Unlock() | ||
|
||
if len(p.errs) > 0 { | ||
return nil, multierr.Combine(p.errs...) | ||
} | ||
|
||
return p.lbListeners, nil | ||
} | ||
|
||
func (p *fetchRequest) fetchNextPage() { | ||
if !p.running.Load() { | ||
return | ||
} | ||
|
||
if p.paginator.Next() { | ||
for _, lb := range p.paginator.CurrentPage().LoadBalancers { | ||
p.dispatch(func() { p.fetchListeners(lb) }) | ||
} | ||
} | ||
|
||
if p.paginator.Err() != nil { | ||
p.recordErrResult(p.paginator.Err()) | ||
} | ||
} | ||
|
||
func (p *fetchRequest) dispatch(fn func()) { | ||
p.pendingTasks.Add(1) | ||
|
||
go func() { | ||
slot := p.taskPool.Get() | ||
defer p.taskPool.Put(slot) | ||
defer p.pendingTasks.Done() | ||
|
||
fn() | ||
}() | ||
} | ||
|
||
func (p *fetchRequest) recordGoodResult(lb *elbv2.LoadBalancer, lbl *elbv2.Listener) { | ||
p.resultsLock.Lock() | ||
defer p.resultsLock.Unlock() | ||
|
||
p.lbListeners = append(p.lbListeners, &lbListener{lb, lbl}) | ||
} | ||
|
||
func (p *fetchRequest) recordErrResult(err error) { | ||
p.resultsLock.Lock() | ||
defer p.resultsLock.Unlock() | ||
|
||
p.errs = append(p.errs, err) | ||
|
||
// Try to stop execution early | ||
p.running.Store(false) | ||
} | ||
|
||
func (p *fetchRequest) fetchListeners(lb elbv2.LoadBalancer) { | ||
listenReq := p.client.DescribeListenersRequest(&elbv2.DescribeListenersInput{LoadBalancerArn: lb.LoadBalancerArn}) | ||
listen := listenReq.Paginate() | ||
for listen.Next() && p.running.Load() { | ||
for _, listener := range listen.CurrentPage().Listeners { | ||
p.recordGoodResult(&lb, &listener) | ||
} | ||
} | ||
if listen.Err() != nil { | ||
p.recordErrResult(listen.Err()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package elb | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/aws/aws-sdk-go-v2/service/elbv2" | ||
|
||
"github.com/elastic/beats/libbeat/common" | ||
) | ||
|
||
func lblToMap(l *elbv2.LoadBalancer, listener *elbv2.Listener) { | ||
|
||
} | ||
|
||
type lbListener struct { | ||
lb *elbv2.LoadBalancer | ||
listener *elbv2.Listener | ||
} | ||
|
||
func (l *lbListener) toMap() common.MapStr { | ||
m := common.MapStr{} | ||
|
||
m["host"] = *l.lb.DNSName | ||
m["port"] = *l.listener.Port | ||
m["type"] = string(l.lb.Type) | ||
m["scheme"] = l.lb.Scheme | ||
m["availability_zones"] = l.azStrings() | ||
m["created"] = l.lb.CreatedTime | ||
m["state"] = l.stateMap() | ||
m["load_balancer_arn"] = *l.lb.LoadBalancerArn | ||
m["ip_address_type"] = string(l.lb.IpAddressType) | ||
m["security_groups"] = l.lb.SecurityGroups | ||
m["vpc_id"] = *l.lb.VpcId | ||
m["protocol"] = l.listener.Protocol | ||
m["ssl_policy"] = l.listener.SslPolicy | ||
|
||
return m | ||
} | ||
|
||
func (l *lbListener) uuid() string { | ||
return fmt.Sprintf("%s|%s", *l.lb.LoadBalancerArn, *l.listener.ListenerArn) | ||
} | ||
|
||
func (l *lbListener) azStrings() []string { | ||
azs := l.lb.AvailabilityZones | ||
res := make([]string, 0, len(azs)) | ||
for _, az := range azs { | ||
res = append(res, *az.ZoneName) | ||
} | ||
return res | ||
} | ||
|
||
func (l *lbListener) stateMap() (stateMap common.MapStr) { | ||
state := l.lb.State | ||
stateMap = common.MapStr{} | ||
if state.Reason != nil { | ||
stateMap["reason"] = *state.Reason | ||
} | ||
stateMap["code"] = state.Code | ||
return stateMap | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Temporary changes while I work on this feature.