-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #591 from xmidt-org/denopink/feat/feedback-for-web…
…hook-v2-branch chore: feedback for @maurafortino 's #587 PR
- Loading branch information
Showing
5 changed files
with
123 additions
and
75 deletions.
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
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,80 @@ | ||
package sink | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"net" | ||
"net/http" | ||
"sort" | ||
|
||
"github.com/xmidt-org/webhook-schema" | ||
) | ||
|
||
func NewSRVRecordDailer(dnsSrvRecord webhook.DNSSrvRecord) (http.RoundTripper, error) { | ||
if len(dnsSrvRecord.FQDNs) == 0 { | ||
return http.DefaultTransport, nil | ||
} | ||
|
||
d := SRVRecordDialer{dnsSrvRecord: dnsSrvRecord} | ||
|
||
var errs error | ||
for _, fqdn := range d.dnsSrvRecord.FQDNs { | ||
_, addrs, err := net.LookupSRV("", "", fqdn) | ||
if err != nil { | ||
errs = errors.Join(errs, | ||
fmt.Errorf("srv lookup failure: `%s`", fqdn), | ||
err, | ||
) | ||
continue | ||
} | ||
|
||
d.srvs = append(d.srvs, addrs...) | ||
} | ||
|
||
// TODO: ask wes/john whether 1 or more net.LookupSRV error should trigger an error from NewSRVRecordDailer | ||
if len(d.srvs) == 0 { | ||
return nil, errors.Join(fmt.Errorf("expected atleast 1 srv record from fqdn list `%v`", d.dnsSrvRecord.FQDNs), errs) | ||
} | ||
|
||
switch d.dnsSrvRecord.LoadBalancingScheme { | ||
case "weight": | ||
sort.Slice(d.srvs, func(i, j int) bool { | ||
return d.srvs[i].Weight > d.srvs[j].Weight | ||
}) | ||
case "priortiy": | ||
sort.Slice(d.srvs, func(i, j int) bool { | ||
return d.srvs[i].Priority < d.srvs[j].Priority | ||
}) | ||
default: | ||
return nil, fmt.Errorf("unknown loadBalancingScheme type: %s", d.dnsSrvRecord.LoadBalancingScheme) | ||
} | ||
|
||
return &http.Transport{ | ||
DialContext: (&d).DialContext, | ||
}, nil | ||
|
||
} | ||
|
||
type SRVRecordDialer struct { | ||
srvs []*net.SRV | ||
dnsSrvRecord webhook.DNSSrvRecord | ||
} | ||
|
||
func (d *SRVRecordDialer) DialContext(ctx context.Context, _, _ string) (net.Conn, error) { | ||
var errs error | ||
for _, addr := range d.srvs { | ||
host := net.JoinHostPort(addr.Target, string(addr.Port)) | ||
conn, err := net.Dial("", host) | ||
if err != nil { | ||
errs = errors.Join(errs, | ||
fmt.Errorf("%v: host `%s` [weight: %s, priortiy: %s] from srv record `%v`", | ||
err, host, addr.Weight, addr.Priority, d.dnsSrvRecord.FQDNs)) | ||
continue | ||
} | ||
|
||
return conn, nil | ||
} | ||
|
||
return nil, errs | ||
} |