diff --git a/api/client.go b/api/client.go index 545c1a27..cbeb891c 100644 --- a/api/client.go +++ b/api/client.go @@ -2,6 +2,7 @@ package api import ( "encoding/json" + "errors" "time" "github.com/nats-io/nats.go" @@ -168,8 +169,16 @@ func (c *ControlAPIClient) GetInfo(nodeId string) (*models.InfoResponse, error) return resp, nil } -func (c *ControlAPIClient) SetLameDuck(nodeId string) (*models.LameduckResponse, error) { - req := models.LameduckRequest{} +func (c *ControlAPIClient) SetLameDuck(nodeId string, tag map[string]string) (*models.LameduckResponse, error) { + if nodeId == "" && len(tag) != 1 { + return nil, errors.New("invalid inputs to lameduck request") + } + + req := models.LameduckRequest{ + NodeId: nodeId, + Tag: tag, + } + req_b, err := json.Marshal(req) if err != nil { return nil, err diff --git a/cmd/nex/node.go b/cmd/nex/node.go index ea15030a..70f115ab 100644 --- a/cmd/nex/node.go +++ b/cmd/nex/node.go @@ -11,6 +11,7 @@ import ( "github.com/nats-io/nkeys" + control "github.com/synadia-io/nex/api" options "github.com/synadia-io/nex/models" "github.com/synadia-io/nex/node" ) @@ -113,16 +114,6 @@ func (l LameDuck) Validate() error { if len(l.Label) > 1 { return errors.New("only one label allowed") } - - switch { - case l.NodeID != "": - fmt.Println("Putting node in lameduck") - case len(l.Label) == 1: - fmt.Println("Putting all nodes with label in lameduck") - default: - return errors.New("used must provide valid Node ID or one label") - } - return nil } @@ -130,7 +121,27 @@ func (l LameDuck) Run(ctx context.Context, globals *Globals) error { if globals.Check { return printTable("Node Lameduck Configuration", append(globals.Table(), l.Table()...)...) } - fmt.Println("run lameduck") + + nc, err := configureNatsConnection(globals) + if err != nil { + return err + } + + controller, err := control.NewControlApiClient(nc) + if err != nil { + return err + } + + resp, err := controller.SetLameDuck(l.NodeID, l.Label) + if err != nil { + return err + } + + if !resp.Success { + return fmt.Errorf("failed to put node in lameduck mode: %s", resp.Msg) + } + + fmt.Printf("Node %s is now in lameduck mode. Workloads will begin stopping gracefully.\n", l.NodeID) return nil } diff --git a/models/controlapi.go b/models/controlapi.go index 3213e93d..959977e3 100644 --- a/models/controlapi.go +++ b/models/controlapi.go @@ -12,5 +12,11 @@ type UndeployRequest struct{} type UndeployResponse struct{} type InfoRequest struct{} type InfoResponse struct{} -type LameduckRequest struct{} -type LameduckResponse struct{} +type LameduckRequest struct { + NodeId string + Tag map[string]string +} +type LameduckResponse struct { + Success bool + Msg string +}