Repository navigation
Feat: Supervise cortex-gateway as a sibling of cortex #1762
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
Merged
huang195
merged 17 commits into
rossoctl:main
from
huang195:feat/praxis-supervise-1720-ppr1
Nov 10, 2026
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
42bedec
Docs: Add the plan for praxis mode step 3, the supervised gateway
huang195 25631c6
Docs: Make the praxis step 3 plan exact
huang195 6e6de8f
Feat: Add the praxis listen and egress addresses
huang195 f44a5f3
Feat: Let cortex-gateway exit when its stdin closes
huang195 7b55c08
Feat: Know cortex-gateway in the port check, uninstall and install.sh…
huang195 d812350
Feat: Write the gateway token and gateway.yaml
huang195 b81276e
Test: Serve a request through the supervisor's gateway.yaml
huang195 821ee4c
Feat: Supervise cortex-gateway as a sibling of cortex
huang195 a45095f
Test: Cover a gateway that cannot start under -supervise
huang195 ea8f7e9
Test: Keep the stale fake gateway alive until the supervisor stops it
huang195 627b35d
Docs: Describe the supervised gateway
huang195 222bae4
Docs: Name the Linux service gap as a deviation
huang195 fd1c7c5
Fix: Start no gateway for a config that does not validate
huang195 a66d688
Fix: Run cortex alone when the gateway files cannot be written
huang195 e4f722e
Fix: Test the gateway with an empty env and fix the zombie check
huang195 16a9fbc
fix: Read ~/.cortex/config.yaml in the supervisor for --local
huang195 50cce61
fix: Keep the gateway token across supervisor starts
huang195 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "crypto/rand" | ||
| "encoding/hex" | ||
| "fmt" | ||
| "net" | ||
| "net/netip" | ||
| "os" | ||
| "path/filepath" | ||
|
|
||
| "gopkg.in/yaml.v3" | ||
|
|
||
| "github.com/rossoctl/cortex/core/config" | ||
| ) | ||
|
|
||
| // The gateway binary, and the two files the supervisor writes for it in the | ||
| // directory of the --config file. | ||
| const ( | ||
| gatewayBinary = "cortex-gateway" | ||
| gatewayTokenName = "gateway.token" | ||
| gatewayYAMLName = "gateway.yaml" | ||
| ) | ||
|
|
||
| // gatewayMaxRequestBytes is the forward proxy's maxRequestBodySize (32 MiB). | ||
| const gatewayMaxRequestBytes = 32 << 20 | ||
|
|
||
| type gatewayFile struct { | ||
| Insecure gatewayInsecure `yaml:"insecure_options"` | ||
| BodyLimits gatewayLimits `yaml:"body_limits"` | ||
| Listeners []gatewayListen `yaml:"listeners"` | ||
| FilterChains []gatewayChain `yaml:"filter_chains"` | ||
| } | ||
|
|
||
| type gatewayInsecure struct { | ||
| AllowUnboundedBody bool `yaml:"allow_unbounded_body"` | ||
| AllowPrivateUpstreams bool `yaml:"allow_private_upstreams"` | ||
| AllowPrivateEndpoints bool `yaml:"allow_private_endpoints"` | ||
| } | ||
|
|
||
| type gatewayLimits struct { | ||
| MaxRequestBytes int64 `yaml:"max_request_bytes"` | ||
| MaxResponseBytes *int64 `yaml:"max_response_bytes"` // nil writes null: no cap | ||
| } | ||
|
|
||
| type gatewayListen struct { | ||
| Name string `yaml:"name"` | ||
| Address string `yaml:"address"` | ||
| ForwardProxy map[string]bool `yaml:"forward_proxy"` | ||
| FilterChains []string `yaml:"filter_chains"` | ||
| } | ||
|
|
||
| type gatewayChain struct { | ||
| Name string `yaml:"name"` | ||
| Filters []gatewayFilter `yaml:"filters"` | ||
| } | ||
|
|
||
| // gatewayFilter puts the filter name first; its options follow in key order. | ||
| type gatewayFilter struct { | ||
| Filter string `yaml:"filter"` | ||
| Opts map[string]any `yaml:",inline"` | ||
| } | ||
|
|
||
| // gatewayYAML returns the cortex-gateway config for cfg (praxis mode spec, | ||
| // Section 4). Until the egress listener lands (#1721), one catch-all route | ||
| // sends every request to the egress cluster. | ||
| func gatewayYAML(cfg *config.Config, tokenPath string) ([]byte, error) { | ||
| ingest, err := ingestAddr(cfg.Listener.SessionAPIAddr) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| f := gatewayFile{ | ||
| Insecure: gatewayInsecure{true, true, true}, | ||
| BodyLimits: gatewayLimits{MaxRequestBytes: gatewayMaxRequestBytes}, | ||
| Listeners: []gatewayListen{{ | ||
| Name: "gateway", | ||
| Address: cfg.Praxis.ListenAddr(), | ||
| ForwardProxy: map[string]bool{"enabled": true}, | ||
| FilterChains: []string{"main"}, | ||
| }}, | ||
| FilterChains: []gatewayChain{{Name: "main", Filters: []gatewayFilter{ | ||
| {Filter: "ip_acl", Opts: map[string]any{"allow": []string{"127.0.0.1/32", "::1/128"}}}, | ||
| {Filter: "cortex_attribution", Opts: map[string]any{"token_file": tokenPath}}, | ||
| {Filter: "cortex_observe", Opts: map[string]any{"token_file": tokenPath, "ingest": ingest}}, | ||
| {Filter: "access_log"}, | ||
| {Filter: "router", Opts: map[string]any{"routes": []map[string]string{{"path_prefix": "/", "cluster": "egress"}}}}, | ||
| {Filter: "load_balancer", Opts: map[string]any{"clusters": []map[string]any{{"name": "egress", "endpoints": []string{cfg.Praxis.EgressAddr()}}}}}, | ||
| }}}, | ||
| } | ||
| var b bytes.Buffer | ||
| enc := yaml.NewEncoder(&b) | ||
| enc.SetIndent(2) | ||
| if err := enc.Encode(f); err != nil { | ||
| return nil, err | ||
| } | ||
| if err := enc.Close(); err != nil { | ||
| return nil, err | ||
| } | ||
| return b.Bytes(), nil | ||
| } | ||
|
|
||
| // ingestAddr returns the address the gateway posts rows to: the session API | ||
| // address, with an empty or wildcard host made 127.0.0.1. | ||
| func ingestAddr(sessionAPIAddr string) (string, error) { | ||
| host, port, err := net.SplitHostPort(sessionAPIAddr) | ||
| if err != nil { | ||
| return "", fmt.Errorf("listener.session_api_addr %q: %w", sessionAPIAddr, err) | ||
| } | ||
| if a, err := netip.ParseAddr(host); host == "" || (err == nil && a.IsUnspecified()) { | ||
| host = "127.0.0.1" | ||
| } | ||
| return net.JoinHostPort(host, port), nil | ||
| } | ||
|
|
||
| // writeGatewayFiles writes gateway.yaml into dir, and a random token when dir has | ||
| // none. Each file has mode 0600 and is written by rename, so a reader never sees a | ||
| // partial file. An existing token is kept: each child reads it once, at its start, | ||
| // so a second supervisor on the same dir that wrote a new token would break the | ||
| // pair of the first one at its next child restart. | ||
| func writeGatewayFiles(dir string, cfg *config.Config) (tokenPath, yamlPath string, err error) { | ||
| tokenPath = filepath.Join(dir, gatewayTokenName) | ||
| yamlPath = filepath.Join(dir, gatewayYAMLName) | ||
| y, err := gatewayYAML(cfg, tokenPath) | ||
| if err != nil { | ||
| return "", "", err | ||
| } | ||
| if b, err := os.ReadFile(tokenPath); err != nil || len(bytes.TrimSpace(b)) == 0 { | ||
| raw := make([]byte, 32) | ||
| if _, err := rand.Read(raw); err != nil { | ||
| return "", "", err | ||
| } | ||
| if err := writeFileAtomic(tokenPath, []byte(hex.EncodeToString(raw))); err != nil { | ||
| return "", "", err | ||
| } | ||
| } | ||
| if err := writeFileAtomic(yamlPath, y); err != nil { | ||
| return "", "", err | ||
| } | ||
| return tokenPath, yamlPath, nil | ||
| } | ||
|
|
||
| // writeFileAtomic writes b to a 0600 temp file next to path, then renames it. | ||
| func writeFileAtomic(path string, b []byte) error { | ||
| f, err := os.CreateTemp(filepath.Dir(path), "."+filepath.Base(path)+".*") | ||
| if err != nil { | ||
| return err | ||
| } | ||
| tmp := f.Name() | ||
| _, err = f.Write(b) | ||
| if err == nil { | ||
| err = f.Sync() | ||
| } | ||
| if cerr := f.Close(); err == nil { | ||
| err = cerr | ||
| } | ||
| if err == nil { | ||
| err = os.Rename(tmp, path) | ||
| } | ||
| if err != nil { | ||
| _ = os.Remove(tmp) | ||
| return fmt.Errorf("write %s: %w", path, err) | ||
| } | ||
| return nil | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
nit — A kept token file keeps whatever mode it already has. If someone (or an older tool) left
gateway.tokenat 0644, it stays that way, and the 0600 comment above no longer holds.~/.cortexis 0700, so the risk is small. A cheap guard:os.Chmod(tokenPath, 0o600)on the keep path, or treat a token that is not 0600 as missing and rewrite it.