mirror of
https://github.com/mulgadc/spinifex.git
synced 2026-10-22 12:16:52 +00:00
9a6bf692ca
* feat(ecs): provision EC2 container-instance capacity via IMDS instance-role Adds a ProvisionCapacity gateway action that launches spinifex-ecs-node EC2 instances which register as ECS container instances, fixing RESOURCE:placement when a cluster has no capacity. Agent control-plane now SigV4-signs gateway calls with instance-role credentials read from IMDS (no static keys seeded in user-data). The gateway already verifies STS/temp creds, so the instance role carries an ecs:* policy + ec2 trust; ensureECSInstanceProfile find-or-creates the ecsInstanceRole instance profile attached at launch. Backend resolves the spinifex-ecs-node AMI by spinifex:managed-by=ecs tag, renders keyless user-data, and launches via the customer RunInstances path. Frontend adds a hand-signed JSON-1.1 ProvisionCapacity call, a provision-capacity dialog on the container-instances tab, and the ECS console section nav + system-image gating. * feat: auto-assign EIPs to awsvpc service tasks; add ECS task/service detail pages Wire AssignPublicIp=ENABLED through the service reconciler so awsvpc service tasks automatically allocate and associate an Elastic IP on the RUNNING transition, and release it on STOP. assignTaskPublicIP/releaseTaskPublicIP mirror the target-group registration hooks in recordTaskState and forceStopTask; the public IP and allocation ID are persisted on the task record and surfaced in DescribeTasks attachments as publicIPv4Address. Add AWS-parity ECS task and service detail pages showing private and public endpoints, container state, and networking configuration, reachable by clicking a task or service in the cluster tabs. * test: cover natsEIPManager allocate/associate/release over NATS
130 lines
4.6 KiB
Go
130 lines
4.6 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
"github.com/aws/aws-sdk-go/service/ecs"
|
|
|
|
"github.com/mulgadc/spinifex/cmd/ecs-agent/credentials"
|
|
"github.com/mulgadc/spinifex/internal/ecsgw"
|
|
handlers_ecs "github.com/mulgadc/spinifex/spinifex/handlers/ecs"
|
|
"github.com/mulgadc/spinifex/spinifex/handlers/ecs/bus"
|
|
)
|
|
|
|
// controlPlane is the agent's channel to the AWS gateway. It replaces the direct
|
|
// NATS publisher: every method is a SigV4-signed gateway request, so the NATS bus
|
|
// stays host-internal. Tests inject a fake; production uses gatewayControlPlane.
|
|
type controlPlane interface {
|
|
// Register (re-)registers the container instance. Called at boot and on every
|
|
// heartbeat tick — the gateway's RegisterContainerInstance is idempotent and
|
|
// refreshes LastSeen, folding liveness into registration (no bus.Heartbeat).
|
|
Register(id identity) error
|
|
// SubmitTaskState reports a task's RUNNING/STOPPED transition.
|
|
SubmitTaskState(st bus.TaskState) error
|
|
// PollAssignments drains this instance's assignment inbox, acking the taskIDs
|
|
// accepted on the previous poll. Returns the still-pending assignments.
|
|
PollAssignments(cluster, instance string, ack []string) ([]bus.Assign, error)
|
|
}
|
|
|
|
// gatewayControlPlane implements controlPlane over the SigV4 ECS gateway client.
|
|
type gatewayControlPlane struct {
|
|
client *ecsgw.Client
|
|
}
|
|
|
|
var _ controlPlane = (*gatewayControlPlane)(nil)
|
|
|
|
// pollTimeout bounds a single PollAssignments request; larger than the register
|
|
// timeout to leave room for a future server-side long-poll.
|
|
const pollTimeout = 30 * time.Second
|
|
|
|
// newGatewayControlPlane builds the SigV4 client signing with the instance-role
|
|
// credentials from IMDS (fetched per call so rotation is transparent) + pinned CA.
|
|
func newGatewayControlPlane(cfg config, creds credentials.CredentialsProvider) (*gatewayControlPlane, error) {
|
|
client, err := ecsgw.New(cfg.GatewayURL, cfg.GatewayCA, credentialsFunc(creds), cfg.Region, pollTimeout)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &gatewayControlPlane{client: client}, nil
|
|
}
|
|
|
|
// credentialsFunc adapts the agent's IMDS CredentialsProvider to the ecsgw
|
|
// per-call credentials hook.
|
|
func credentialsFunc(p credentials.CredentialsProvider) ecsgw.CredentialsFunc {
|
|
return func(ctx context.Context) (string, string, string, error) {
|
|
c, err := p.Retrieve(ctx)
|
|
if err != nil {
|
|
return "", "", "", err
|
|
}
|
|
return c.AccessKeyID, c.SecretAccessKey, c.SessionToken, nil
|
|
}
|
|
}
|
|
|
|
func (g *gatewayControlPlane) Register(id identity) error {
|
|
in := &ecs.RegisterContainerInstanceInput{
|
|
Cluster: aws.String(id.ClusterName),
|
|
InstanceIdentityDocument: aws.String(id.InstanceID),
|
|
TotalResources: []*ecs.Resource{
|
|
{Name: aws.String("CPU"), Type: aws.String("INTEGER"), IntegerValue: aws.Int64(int64(id.Capacity.CPU))},
|
|
{Name: aws.String("MEMORY"), Type: aws.String("INTEGER"), IntegerValue: aws.Int64(int64(id.Capacity.MemoryMiB))},
|
|
},
|
|
VersionInfo: &ecs.VersionInfo{AgentVersion: aws.String(id.AgentVersion)},
|
|
}
|
|
body, err := json.Marshal(in)
|
|
if err != nil {
|
|
return fmt.Errorf("marshal register: %w", err)
|
|
}
|
|
if _, err := g.client.Call("RegisterContainerInstance", body); err != nil {
|
|
return fmt.Errorf("register: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (g *gatewayControlPlane) SubmitTaskState(st bus.TaskState) error {
|
|
in := &ecs.SubmitTaskStateChangeInput{
|
|
Cluster: aws.String(st.ClusterName),
|
|
Task: aws.String(st.TaskID),
|
|
Status: aws.String(st.LastStatus),
|
|
Reason: aws.String(st.Reason),
|
|
}
|
|
for _, c := range st.Containers {
|
|
csc := &ecs.ContainerStateChange{
|
|
ContainerName: aws.String(c.Name),
|
|
Status: aws.String(c.Status),
|
|
RuntimeId: aws.String(c.ContainerID),
|
|
}
|
|
if c.ExitCode != nil {
|
|
csc.ExitCode = aws.Int64(int64(*c.ExitCode))
|
|
}
|
|
in.Containers = append(in.Containers, csc)
|
|
}
|
|
body, err := json.Marshal(in)
|
|
if err != nil {
|
|
return fmt.Errorf("marshal task-state: %w", err)
|
|
}
|
|
if _, err := g.client.Call("SubmitTaskStateChange", body); err != nil {
|
|
return fmt.Errorf("submit task-state: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (g *gatewayControlPlane) PollAssignments(cluster, instance string, ack []string) ([]bus.Assign, error) {
|
|
in := &handlers_ecs.PollAssignmentsInput{Cluster: cluster, ContainerInstance: instance, AckTaskIDs: ack}
|
|
body, err := json.Marshal(in)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("marshal poll: %w", err)
|
|
}
|
|
resp, err := g.client.Call("PollAssignments", body)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("poll: %w", err)
|
|
}
|
|
var out handlers_ecs.PollAssignmentsOutput
|
|
if err := json.Unmarshal(resp, &out); err != nil {
|
|
return nil, fmt.Errorf("decode poll: %w", err)
|
|
}
|
|
return out.Assignments, nil
|
|
}
|