Revert agent generations that cut off the control plane
This commit is contained in:
@@ -26,7 +26,9 @@ func NewClient(baseURL, device, token string) *Client {
|
||||
BaseURL: baseURL,
|
||||
Device: device,
|
||||
Token: token,
|
||||
HTTP: &http.Client{Timeout: 30 * time.Second},
|
||||
// No keep-alives: every request, the post-apply check included, opens a
|
||||
// fresh connection that must pass the current ruleset.
|
||||
HTTP: &http.Client{Timeout: 30 * time.Second, Transport: noKeepAlive()},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -94,10 +96,24 @@ func (c *Client) ReportRoutes(ctx context.Context, prefixes []string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// ReportStatus tells the control plane which generation this device has applied.
|
||||
func (c *Client) ReportStatus(ctx context.Context, generation int64) error {
|
||||
// Status values reported to POST /api/v1/devices/{name}/status.
|
||||
const (
|
||||
StatusApplied = "applied"
|
||||
StatusReverted = "reverted"
|
||||
StatusFailed = "failed"
|
||||
)
|
||||
|
||||
// Status is the outcome of applying one generation.
|
||||
type Status struct {
|
||||
Status string `json:"status"`
|
||||
Generation int64 `json:"generation"`
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
// ReportStatus tells the control plane the outcome of applying a generation.
|
||||
func (c *Client) ReportStatus(ctx context.Context, st Status) error {
|
||||
url := fmt.Sprintf("%s/api/v1/devices/%s/status", c.BaseURL, c.Device)
|
||||
payload, _ := json.Marshal(map[string]int64{"generation": generation})
|
||||
payload, _ := json.Marshal(st)
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(payload))
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -116,3 +132,9 @@ func (c *Client) ReportStatus(ctx context.Context, generation int64) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func noKeepAlive() http.RoundTripper {
|
||||
t := http.DefaultTransport.(*http.Transport).Clone()
|
||||
t.DisableKeepAlives = true
|
||||
return t
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user