@@ -2,14 +2,22 @@ package watched
2
2
3
3
import (
4
4
"context"
5
+ "fmt"
6
+ "log/slog"
5
7
6
8
"github.com/signadot/routesapi/go-routesapi"
7
9
"google.golang.org/protobuf/proto"
8
10
)
9
11
10
12
// BaselineWatched wraps [watched.Watched] with a [routesapi.BaselineWorkload].
11
13
type BaselineWatched interface {
14
+ // Get returns a workload routing rule associated with
15
+ // the routing key rk for the associated baseline workload.
12
16
Get (rk string ) * routesapi.WorkloadRoutingRule
17
+
18
+ // RoutesTo returns true if traffic destined to the associated
19
+ // baseline workload with routing key rk should be directed to
20
+ // the sandbox named sbName.
13
21
RoutesTo (rk string , sbName string ) bool
14
22
}
15
23
@@ -18,6 +26,10 @@ type baselineWatched struct {
18
26
baseline * routesapi.BaselineWorkload
19
27
}
20
28
29
+ // NewBaselineWatched creates a BaselineWatched instance using
30
+ // ctx for the underlying grpc watch rpc, using the routeserver
31
+ // specified in [cfg.Addr], and associated with the baseline
32
+ // specified in b.
21
33
func NewBaselineWatched (ctx context.Context , cfg * Config , b * routesapi.BaselineWorkload ) (BaselineWatched , error ) {
22
34
bb := proto .Clone (b ).(* routesapi.BaselineWorkload )
23
35
w , err := NewWatched (ctx , cfg , & routesapi.WorkloadRoutingRulesRequest {
@@ -32,6 +44,30 @@ func NewBaselineWatched(ctx context.Context, cfg *Config, b *routesapi.BaselineW
32
44
}, nil
33
45
}
34
46
47
+ // BaselineWatchedFromEnv attempts to construct a BaselineWatched instance
48
+ // using configuration from the environment, by using
49
+ // [BaselineFromEnv] and [GetRouteServerAddr] and calling
50
+ // [NewBaselineWatched]. The context associated with the watch
51
+ // is the result of calling [context.Background] and the logger
52
+ // is the result of calling [slog.Default]. For more control,
53
+ // please use [NewBaselineWatched] directly.
54
+ func BaselineWatchedFromEnv () (BaselineWatched , error ) {
55
+ baseline , err := BaselineFromEnv ()
56
+ if err != nil {
57
+ return nil , fmt .Errorf ("could not get baseline from env: %w" , err )
58
+ }
59
+ cfg := & Config {
60
+ Addr : GetRouteServerAddr (),
61
+ Log : slog .Default (),
62
+ }
63
+ apiBaseline := & routesapi.BaselineWorkload {
64
+ Kind : baseline .Kind ,
65
+ Namespace : baseline .Namespace ,
66
+ Name : baseline .Name ,
67
+ }
68
+ return NewBaselineWatched (context .Background (), cfg , apiBaseline )
69
+ }
70
+
35
71
func (bw * baselineWatched ) Get (rk string ) * routesapi.WorkloadRoutingRule {
36
72
return bw .watched .Get (bw .baseline , rk )
37
73
}
0 commit comments