Skip to content

Commit 5112498

Browse files
committed
Fix the docs and add an appropriate FromEnv func for message queues
1 parent d8598a0 commit 5112498

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

go-routesapi/docs/message-queues.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ rules for a baseline workload from the environment:
5555

5656
```go
5757
import "github.com/signadot/routesapi/go-routesapi/watched"
58-
routing, err := watched.BaselineFromEnv()
58+
routing, err := watched.BaselineWatchedFromEnv()
5959
```
6060

6161
## Using `routesapi` Go SDK
@@ -75,7 +75,7 @@ In a sandboxed workload:
7575
import "github.com/signadot/routesapi/go-routesapi/watched"
7676

7777
func ShouldProcess(routing watched.BaselineWatched, routingKey string) bool {
78-
return routing.RoutesTo(routingKey, os.Getenv("SIGNADOT_SANDBOX_ROUTING_KEY"))
78+
return routing.RoutesTo(routingKey, os.Getenv("SIGNADOT_SANDBOX_NAME"))
7979
}
8080
```
8181

@@ -84,8 +84,8 @@ unconditionally. This can be accomplished as follows:
8484

8585
```go
8686
func ShouldProcess(routing watched.BaselineWatched, routingKey string) bool {
87-
if sbID := os.Getenv("SIGNADOT_SANDBOX_ROUTING_KEY"); sbID != "" {
88-
return routing.RoutesTo(routingKey, sbID)
87+
if sb := os.Getenv("SIGNADOT_SANDBOX_NAME"); sb != "" {
88+
return routing.RoutesTo(routingKey, sb)
8989
}
9090
return routing.Get(routingKey) == nil
9191
}

go-routesapi/watched/baseline_watched.go

+36
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,22 @@ package watched
22

33
import (
44
"context"
5+
"fmt"
6+
"log/slog"
57

68
"github.com/signadot/routesapi/go-routesapi"
79
"google.golang.org/protobuf/proto"
810
)
911

1012
// BaselineWatched wraps [watched.Watched] with a [routesapi.BaselineWorkload].
1113
type BaselineWatched interface {
14+
// Get returns a workload routing rule associated with
15+
// the routing key rk for the associated baseline workload.
1216
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.
1321
RoutesTo(rk string, sbName string) bool
1422
}
1523

@@ -18,6 +26,10 @@ type baselineWatched struct {
1826
baseline *routesapi.BaselineWorkload
1927
}
2028

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.
2133
func NewBaselineWatched(ctx context.Context, cfg *Config, b *routesapi.BaselineWorkload) (BaselineWatched, error) {
2234
bb := proto.Clone(b).(*routesapi.BaselineWorkload)
2335
w, err := NewWatched(ctx, cfg, &routesapi.WorkloadRoutingRulesRequest{
@@ -32,6 +44,30 @@ func NewBaselineWatched(ctx context.Context, cfg *Config, b *routesapi.BaselineW
3244
}, nil
3345
}
3446

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+
3571
func (bw *baselineWatched) Get(rk string) *routesapi.WorkloadRoutingRule {
3672
return bw.watched.Get(bw.baseline, rk)
3773
}

0 commit comments

Comments
 (0)