-
Notifications
You must be signed in to change notification settings - Fork 90
sdk changes to retrieve upstrem #2577
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package policyengine | ||
|
|
||
| // UpstreamInfo identifies the resolved upstream target for one of an API's | ||
| // upstream slots (main or sandbox) or a named upstream definition: the Envoy | ||
| // cluster name (used for cluster_header dynamic routing) and the raw resolved | ||
| // URL. This is the shared wire shape used on both sides of the xDS route-config | ||
| // channel between gateway-controller and policy-engine. | ||
| type UpstreamInfo struct { | ||
| ClusterName string `json:"cluster_name" yaml:"cluster_name"` | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ClusterName is an internal thing. Why do we need to expose it? |
||
| URL string `json:"url" yaml:"url"` | ||
| BasePath string `json:"base_path" yaml:"base_path"` | ||
| } | ||
|
|
||
| // ToMap converts UpstreamInfo into a structpb-compatible map for embedding as a | ||
| // nested object in xDS route-config metadata (structpb.Struct only accepts | ||
| // string/bool/number/nil/slice/map values, not arbitrary structs). | ||
| func (u UpstreamInfo) ToMap() map[string]interface{} { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need this func? |
||
| return map[string]interface{}{ | ||
| "cluster_name": u.ClusterName, | ||
| "url": u.URL, | ||
| "base_path": u.BasePath, | ||
| } | ||
| } | ||
|
|
||
| // UpstreamInfoFromMap decodes an UpstreamInfo from the generic map produced when | ||
| // the xDS metadata struct is unmarshalled back to JSON on the consumer side. | ||
| func UpstreamInfoFromMap(m map[string]interface{}) UpstreamInfo { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And this func? |
||
| get := func(k string) string { | ||
| if s, ok := m[k].(string); ok { | ||
| return s | ||
| } | ||
| return "" | ||
| } | ||
| return UpstreamInfo{ | ||
| ClusterName: get("cluster_name"), | ||
| URL: get("url"), | ||
| BasePath: get("base_path"), | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package policyengine | ||
|
|
||
| import "testing" | ||
|
|
||
| func TestUpstreamInfoRoundTrip(t *testing.T) { | ||
| info := UpstreamInfo{ | ||
| ClusterName: "cluster_https_backend_example_com", | ||
| URL: "https://backend.example.com", | ||
| BasePath: "/v1", | ||
| } | ||
|
|
||
| got := UpstreamInfoFromMap(info.ToMap()) | ||
|
|
||
| if got != info { | ||
| t.Errorf("round trip mismatch: got %+v, want %+v", got, info) | ||
| } | ||
| } | ||
|
|
||
| func TestUpstreamInfoFromMapMissingFields(t *testing.T) { | ||
| got := UpstreamInfoFromMap(map[string]interface{}{"cluster_name": "c1"}) | ||
| want := UpstreamInfo{ClusterName: "c1"} | ||
| if got != want { | ||
| t.Errorf("got %+v, want %+v", got, want) | ||
| } | ||
| } | ||
|
|
||
| func TestUpstreamInfoFromMapWrongType(t *testing.T) { | ||
| got := UpstreamInfoFromMap(map[string]interface{}{"cluster_name": 42}) | ||
| want := UpstreamInfo{} | ||
| if got != want { | ||
| t.Errorf("got %+v, want %+v", got, want) | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not required right?