Skip to content

Commit

Permalink
[targets] Provide a way to specify endpoints statically
Browse files Browse the repository at this point in the history
  • Loading branch information
manugarg committed Oct 30, 2023
1 parent e787146 commit 21afb10
Show file tree
Hide file tree
Showing 5 changed files with 224 additions and 122 deletions.
113 changes: 70 additions & 43 deletions targets/proto/targets.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions targets/proto/targets.proto
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,20 @@ message TargetsDef {
DummyTargets dummy_targets = 20;
}

// Static endpoints. These endpoints are merged with the resources returned
// by the targets type above.
// Example:
// endpoints {
// name: "service-gtwy-1"
// ip: "10.1.18.121"
// port: 8080
// labels {
// key: "service"
// value: "products-service"
// }
// }
repeated rds.Resource endpoints = 23;

// Regex to apply on the targets.
optional string regex = 21;

Expand Down
14 changes: 14 additions & 0 deletions targets/proto/targets_proto_gen.cue
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,20 @@ import (
dummyTargets: #DummyTargets @protobuf(20,DummyTargets,name=dummy_targets)
}

// Static endpoints. These endpoints are merged with the resources returned
// by the targets type above.
// Example:
// endpoints {
// name: "service-gtwy-1"
// ip: "10.1.18.121"
// port: 8080
// labels {
// key: "service"
// value: "products-service"
// }
// }
endpoints?: [...proto_1.#Resource] @protobuf(23,rds.Resource)

// Regex to apply on the targets.
regex?: string @protobuf(21,string)

Expand Down
65 changes: 37 additions & 28 deletions targets/targets.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2017-2019 The Cloudprober Authors.
// Copyright 2017-2023 The Cloudprober Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -119,11 +119,12 @@ func (d *dummy) Resolve(name string, ipVer int) (net.IP, error) {
// providing various filtering options. Currently filtering by regex and lameduck
// is supported.
type targets struct {
lister endpoint.Lister
resolver endpoint.Resolver
re *regexp.Regexp
ldLister endpoint.Lister
l *logger.Logger
lister endpoint.Lister
resolver endpoint.Resolver
staticEndpoints []endpoint.Endpoint
re *regexp.Regexp
ldLister endpoint.Lister
l *logger.Logger
}

// Resolve either resolves a target using the core resolver, or returns an error
Expand Down Expand Up @@ -186,14 +187,15 @@ func (t *targets) includeInResult(ep endpoint.Endpoint, ldMap map[string]endpoin
// associated metadata at all, those endpoint fields are left empty in that
// case.
func (t *targets) ListEndpoints() []endpoint.Endpoint {
if t.lister == nil {
t.l.Error("List(): Lister t.lister is nil")
if t.lister == nil && len(t.staticEndpoints) == 0 {
t.l.Error("no lister or static endpoints")
return []endpoint.Endpoint{}
}

var list []endpoint.Endpoint

list = t.lister.ListEndpoints()
list := append([]endpoint.Endpoint{}, t.staticEndpoints...)
if t.lister != nil {
list = append(list, t.lister.ListEndpoints()...)
}

ldMap := t.lameduckMap()
if t.re != nil || len(ldMap) != 0 {
Expand Down Expand Up @@ -222,6 +224,16 @@ func baseTargets(targetsDef *targetspb.TargetsDef, ldLister endpoint.Lister, l *
ldLister: ldLister,
}

for _, res := range targetsDef.GetEndpoints() {
tgts.staticEndpoints = append(tgts.staticEndpoints, endpoint.Endpoint{
Name: res.GetName(),
Port: int(res.GetPort()),
IP: net.ParseIP(res.GetIp()),
Labels: res.GetLabels(),
LastUpdated: time.Unix(res.GetLastUpdated(), 0),
})
}

if targetsDef == nil {
return tgts, nil
}
Expand Down Expand Up @@ -249,10 +261,7 @@ func StaticTargets(hosts string) Targets {
}

func StaticEndpoints(eps []endpoint.Endpoint) Targets {
t, _ := baseTargets(nil, nil, nil)
t.lister = &staticLister{list: eps}
t.resolver = globalResolver
return t
return &targets{staticEndpoints: eps}
}

// rdsClientConf converts RDS targets into RDS client configuration.
Expand Down Expand Up @@ -374,23 +383,27 @@ func New(targetsDef *targetspb.TargetsDef, ldLister endpoint.Lister, globalOpts
t.lister, t.resolver = dummy, dummy

default:
extT, err := getExtensionTargets(targetsDef, t.l)
if err != nil {
return nil, fmt.Errorf("targets.New(): %v", err)
targetsFunc, value := getExtensionTargets(targetsDef, t.l)
if targetsFunc != nil {
extT, err := targetsFunc(value, l)
if err != nil {
return nil, fmt.Errorf("targets.New(): targets extension: %v", err)
}
t.lister, t.resolver = extT, extT
}
t.lister, t.resolver = extT, extT
}

if t.lister == nil && len(t.staticEndpoints) == 0 {
return nil, fmt.Errorf("targets.New(): no targets type specified and no static endpoints")
}

return t, nil
}

func getExtensionTargets(pb *targetspb.TargetsDef, l *logger.Logger) (Targets, error) {
func getExtensionTargets(pb *targetspb.TargetsDef, l *logger.Logger) (newTargetsFunc func(interface{}, *logger.Logger) (Targets, error), value interface{}) {
extensionMapMu.Lock()
defer extensionMapMu.Unlock()

var newTargetsFunc func(interface{}, *logger.Logger) (Targets, error)
var value interface{}

proto.RangeExtensions(pb, func(xt protoreflect.ExtensionType, val interface{}) bool {
newTargetsFunc = extensionMap[int(xt.TypeDescriptor().Number())]
if newTargetsFunc != nil {
Expand All @@ -400,11 +413,7 @@ func getExtensionTargets(pb *targetspb.TargetsDef, l *logger.Logger) (Targets, e
return true
})

if newTargetsFunc == nil {
return nil, fmt.Errorf("no extension targets found in the targets config")
}

return newTargetsFunc(value, l)
return
}

// RegisterTargetsType registers a new targets type. New targets types are
Expand Down
Loading

0 comments on commit 21afb10

Please sign in to comment.