refactor: separate workload class from placement driver
This commit is contained in:
@@ -6,7 +6,6 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"slices"
|
||||
"strconv"
|
||||
|
||||
"gitea.dev/actionslib/pkg/model"
|
||||
@@ -16,34 +15,27 @@ import (
|
||||
"git.ddupan.top/panxiao81/gitea-dynamic-runner/internal/taskidentity"
|
||||
)
|
||||
|
||||
const wireVersion = 1
|
||||
|
||||
type Backend string
|
||||
|
||||
const (
|
||||
BackendPod Backend = "pod"
|
||||
BackendVM Backend = "vm"
|
||||
)
|
||||
const wireVersion = 2
|
||||
|
||||
// Assignment is the only document persisted in the handoff queue.
|
||||
type Assignment struct {
|
||||
ID string
|
||||
Backend Backend
|
||||
Task *runnerv1.Task
|
||||
Identity taskidentity.Identity
|
||||
ID string
|
||||
Placement Placement
|
||||
Task *runnerv1.Task
|
||||
Identity taskidentity.Identity
|
||||
}
|
||||
|
||||
// FromMetadata reconstructs the minimal assignment needed to authorize an
|
||||
// already-running executor after a controller restart. Backend metadata was
|
||||
// already-running executor after a controller restart. Placement metadata was
|
||||
// originally derived from the trusted Gitea task and is validated again here.
|
||||
func FromMetadata(labels, annotations map[string]string, trustDomain string) (Assignment, error) {
|
||||
taskID, err := strconv.ParseInt(labels["ci.ddupan.top/task-id"], 10, 64)
|
||||
if err != nil || taskID < 1 {
|
||||
return Assignment{}, errors.New("backend metadata has invalid task ID")
|
||||
}
|
||||
backend := Backend(labels["ci.ddupan.top/backend"])
|
||||
if backend != BackendPod && backend != BackendVM {
|
||||
return Assignment{}, errors.New("backend metadata has invalid backend")
|
||||
placement := Placement{Class: WorkloadClass(labels["ci.ddupan.top/workload-class"]), Driver: Driver(labels["ci.ddupan.top/driver"])}
|
||||
if err := placement.Validate(); err != nil {
|
||||
return Assignment{}, err
|
||||
}
|
||||
id := labels["ci.ddupan.top/assignment-id"]
|
||||
if id != fmt.Sprintf("gitea-task-%d", taskID) {
|
||||
@@ -56,15 +48,15 @@ func FromMetadata(labels, annotations map[string]string, trustDomain string) (As
|
||||
if err != nil {
|
||||
return Assignment{}, err
|
||||
}
|
||||
return Assignment{ID: id, Backend: backend, Task: &runnerv1.Task{Id: taskID}, Identity: identity}, nil
|
||||
return Assignment{ID: id, Placement: placement, Task: &runnerv1.Task{Id: taskID}, Identity: identity}, nil
|
||||
}
|
||||
|
||||
type envelope struct {
|
||||
Version int `json:"version"`
|
||||
ID string `json:"id"`
|
||||
Backend Backend `json:"backend"`
|
||||
Task []byte `json:"task"`
|
||||
Identity taskidentity.Identity `json:"identity"`
|
||||
Version int `json:"version"`
|
||||
ID string `json:"id"`
|
||||
Placement Placement `json:"placement"`
|
||||
Task []byte `json:"task"`
|
||||
Identity taskidentity.Identity `json:"identity"`
|
||||
}
|
||||
|
||||
// New derives all trusted assignment fields from the task fetched from Gitea.
|
||||
@@ -76,40 +68,28 @@ func New(task *runnerv1.Task, trustDomain string) (Assignment, error) {
|
||||
if err != nil {
|
||||
return Assignment{}, err
|
||||
}
|
||||
backend, err := backendFromTask(task)
|
||||
placement, err := placementFromTask(task)
|
||||
if err != nil {
|
||||
return Assignment{}, err
|
||||
}
|
||||
return Assignment{
|
||||
ID: fmt.Sprintf("gitea-task-%d", task.GetId()),
|
||||
Backend: backend,
|
||||
Task: task,
|
||||
Identity: identity,
|
||||
ID: fmt.Sprintf("gitea-task-%d", task.GetId()),
|
||||
Placement: placement,
|
||||
Task: task,
|
||||
Identity: identity,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func backendFromTask(task *runnerv1.Task) (Backend, error) {
|
||||
func placementFromTask(task *runnerv1.Task) (Placement, error) {
|
||||
workflow, err := model.ReadWorkflow(bytes.NewReader(task.GetWorkflowPayload()))
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("parse task workflow for backend: %w", err)
|
||||
return Placement{}, fmt.Errorf("parse task workflow for placement: %w", err)
|
||||
}
|
||||
jobIDs := workflow.GetJobIDs()
|
||||
if len(jobIDs) != 1 || workflow.GetJob(jobIDs[0]) == nil {
|
||||
return "", fmt.Errorf("task workflow must contain exactly one non-empty job")
|
||||
return Placement{}, fmt.Errorf("task workflow must contain exactly one non-empty job")
|
||||
}
|
||||
labels := workflow.GetJob(jobIDs[0]).RunsOnLabels()
|
||||
if !slices.Contains(labels, "self-hosted") {
|
||||
return "", fmt.Errorf("task runs-on labels must include self-hosted: %v", labels)
|
||||
}
|
||||
hasPod := slices.Contains(labels, string(BackendPod))
|
||||
hasVM := slices.Contains(labels, string(BackendVM)) || slices.Contains(labels, "vm-dev")
|
||||
if hasPod == hasVM {
|
||||
return "", fmt.Errorf("task runs-on labels must select exactly one of pod or vm: %v", labels)
|
||||
}
|
||||
if hasPod {
|
||||
return BackendPod, nil
|
||||
}
|
||||
return BackendVM, nil
|
||||
return PlacementFromLabels(workflow.GetJob(jobIDs[0]).RunsOnLabels())
|
||||
}
|
||||
|
||||
// Marshal encodes a versioned assignment. Protobuf preserves the exact Gitea task.
|
||||
@@ -117,13 +97,16 @@ func Marshal(assignment Assignment) ([]byte, error) {
|
||||
if assignment.Task == nil {
|
||||
return nil, errors.New("assignment task is required")
|
||||
}
|
||||
if err := assignment.Placement.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
task, err := proto.Marshal(assignment.Task)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("marshal Gitea task: %w", err)
|
||||
}
|
||||
return json.Marshal(envelope{
|
||||
Version: wireVersion,
|
||||
ID: assignment.ID, Backend: assignment.Backend,
|
||||
ID: assignment.ID, Placement: assignment.Placement,
|
||||
Task: task, Identity: assignment.Identity,
|
||||
})
|
||||
}
|
||||
@@ -145,7 +128,7 @@ func Unmarshal(data []byte, trustDomain string) (Assignment, error) {
|
||||
if err != nil {
|
||||
return Assignment{}, err
|
||||
}
|
||||
if wire.ID != canonical.ID || wire.Backend != canonical.Backend || wire.Identity != canonical.Identity {
|
||||
if wire.ID != canonical.ID || wire.Placement != canonical.Placement || wire.Identity != canonical.Identity {
|
||||
return Assignment{}, errors.New("assignment metadata does not match its Gitea task")
|
||||
}
|
||||
return canonical, nil
|
||||
|
||||
Reference in New Issue
Block a user