Skip to content

Commit

Permalink
[targets] Provide a way to specify endpoints statically (cloudprober#606
Browse files Browse the repository at this point in the history
)

- This is to make configs simpler and easier.
- Not only you can configure multiple endpoints directly in the config itself, but you can also specify URLs in the targets section, allowing you to club multiple targets and URLs into one probe, where everything else is same.
  • Loading branch information
manugarg authored Oct 30, 2023
1 parent e787146 commit d290713
Show file tree
Hide file tree
Showing 7 changed files with 668 additions and 177 deletions.
61 changes: 61 additions & 0 deletions targets/endpoint/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ package endpoint
import (
"fmt"
"net"
"net/url"
"sort"
"strconv"
"strings"
"time"

"github.com/cloudprober/cloudprober/common/iputils"
targetspb "github.com/cloudprober/cloudprober/targets/proto"
)

// Endpoint represents a targets and associated parameters.
Expand Down Expand Up @@ -109,3 +111,62 @@ func NamesFromEndpoints(endpoints []Endpoint) []string {
}
return result
}

func parseURL(s string) (scheme, host, path string, port int, err error) {
u, err := url.Parse(s)
if err != nil {
return "", "", "", 0, fmt.Errorf("invalid URL: %v", err)
}

scheme = u.Scheme
host = u.Hostname()
port, _ = strconv.Atoi(u.Port())
path = "/"

hostPath := strings.TrimPrefix(s, scheme+"://")
if i := strings.Index(hostPath, "/"); i != -1 {
path = hostPath[i:]
}
return scheme, host, path, port, nil
}

func FromProtoMessage(endpointspb []*targetspb.Endpoint) ([]Endpoint, error) {
var endpoints []Endpoint
seen := make(map[string]bool)
timestamp := time.Now()

for _, pb := range endpointspb {
ep := Endpoint{
Name: pb.GetName(),
Labels: pb.GetLabels(),
IP: net.ParseIP(pb.GetIp()),
Port: int(pb.GetPort()),
LastUpdated: timestamp,
}

if pb.GetUrl() != "" {
scheme, host, path, port, err := parseURL(pb.GetUrl())
if err != nil {
return nil, err
}
if ep.Labels == nil {
ep.Labels = make(map[string]string)
}
ep.Labels["__cp_scheme__"] = scheme
ep.Labels["__cp_host__"] = host
ep.Labels["__cp_path__"] = path

if ep.Port == 0 {
ep.Port = port
}
}
epKey := ep.Key()
if seen[epKey] {
return nil, fmt.Errorf("duplicate endpoint: %s", ep.Key())
}
seen[epKey] = true
endpoints = append(endpoints, ep)
}

return endpoints, nil
}
167 changes: 167 additions & 0 deletions targets/endpoint/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ import (
"fmt"
"net"
"testing"
"time"

targetspb "github.com/cloudprober/cloudprober/targets/proto"
"github.com/stretchr/testify/assert"
"google.golang.org/protobuf/proto"
)

func TestEndpointsFromNames(t *testing.T) {
Expand Down Expand Up @@ -109,3 +112,167 @@ func TestEndpointDst(t *testing.T) {
})
}
}

func TestParseURL(t *testing.T) {
type parts struct {
scheme, host, path string
port int
}
tests := []struct {
name string
s string
wantParts parts
wantErr bool
}{
{
s: "http://host:8080/path",
wantParts: parts{scheme: "http", host: "host", path: "/path", port: 8080},
},
{
s: "https://host:8080",
wantParts: parts{scheme: "https", host: "host", path: "/", port: 8080},
},
{
s: "http://host/path?query=1",
wantParts: parts{scheme: "http", host: "host", path: "/path?query=1", port: 0},
},
{
s: "http://[abcf::0000]/path?query=1",
wantParts: parts{scheme: "http", host: "abcf::0000", path: "/path?query=1", port: 0},
},
{
s: "http://[abcf::0000]:8080/path?query=1",
wantParts: parts{scheme: "http", host: "abcf::0000", path: "/path?query=1", port: 8080},
},
{
s: "http://[abcf::0000]:8080/path?query=1",
wantParts: parts{scheme: "http", host: "abcf::0000", path: "/path?query=1", port: 8080},
},
{
s: "http://[abcf::0000/path?query=1",
wantErr: true,
},
{
s: "http://[abcf::0000]:asd/path?query=1",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.s, func(t *testing.T) {
gotScheme, gotHost, gotPath, gotPort, err := parseURL(tt.s)
if (err != nil) != tt.wantErr {
t.Errorf("parseURL() error = %v, wantErr %v", err, tt.wantErr)
return
}
if err != nil {
return
}
assert.Equal(t, tt.wantParts.scheme, gotScheme, "scheme")
assert.Equal(t, tt.wantParts.host, gotHost, "host")
assert.Equal(t, tt.wantParts.path, gotPath, "path")
assert.Equal(t, tt.wantParts.port, gotPort, "port")
})
}
}

func TestFromProtoMessage(t *testing.T) {
tests := []struct {
name string
endpointspb []*targetspb.Endpoint
want []Endpoint
wantErr bool
}{
{
name: "static endpoints",
endpointspb: []*targetspb.Endpoint{
{
Name: proto.String("host1_url1"),
Url: proto.String("http://host1:8080/url1"),
},
{
Name: proto.String("host2"),
Url: proto.String("https://host2.com"),
Labels: map[string]string{"app": "frontend-cloudprober"},
},
},
want: []Endpoint{
{
Name: "host1_url1",
Labels: map[string]string{
"__cp_scheme__": "http",
"__cp_host__": "host1",
"__cp_path__": "/url1",
},
Port: 8080,
},
{
Name: "host2",
Labels: map[string]string{
"app": "frontend-cloudprober",
"__cp_scheme__": "https",
"__cp_host__": "host2.com",
"__cp_path__": "/",
},
},
},
},
{
name: "same keys error",
endpointspb: []*targetspb.Endpoint{
{
Name: proto.String("host1_url1"),
Url: proto.String("http://host1:8080/url1"),
},
{
Name: proto.String("host1_url1"),
Url: proto.String("http://host1:8080/url1"),
},
},
wantErr: true,
},
{
name: "different keys",
endpointspb: []*targetspb.Endpoint{
{
Name: proto.String("host1"),
Url: proto.String("http://host1/url1"),
},
{
Name: proto.String("host1"),
Url: proto.String("http://host1/url2"),
},
},
want: []Endpoint{
{
Name: "host1",
Labels: map[string]string{
"__cp_scheme__": "http",
"__cp_host__": "host1",
"__cp_path__": "/url1",
},
},
{
Name: "host1",
Labels: map[string]string{
"__cp_scheme__": "http",
"__cp_host__": "host1",
"__cp_path__": "/url2",
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := FromProtoMessage(tt.endpointspb)
if (err != nil) != tt.wantErr {
t.Errorf("FromProtoMessage() error = %v, wantErr %v", err, tt.wantErr)
return
}
for i := range got {
got[i].LastUpdated = time.Time{}
}
assert.Equal(t, tt.want, got, "endpoints")
})
}
}
Loading

0 comments on commit d290713

Please sign in to comment.