forked from longtao.wu/droboticflow
417 lines
15 KiB
Go
417 lines
15 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"regexp"
|
|
"strings"
|
|
"time"
|
|
|
|
"droboticflow/internal/domain"
|
|
"droboticflow/internal/k8s"
|
|
)
|
|
|
|
const (
|
|
operatorImageArchiveFormMemoryBytes = 32 << 20
|
|
operatorImageArchiveSubdir = "operator-image-archives"
|
|
registryAuthFilePath = "/kaniko/.docker/config.json"
|
|
)
|
|
|
|
var (
|
|
imageRepositoryInvalidCharacters = regexp.MustCompile(`[^a-z0-9._/-]+`)
|
|
imageRepositoryRepeatedSlashes = regexp.MustCompile(`/+`)
|
|
imageTagPattern = regexp.MustCompile(`^[A-Za-z0-9_][A-Za-z0-9_.-]{0,127}$`)
|
|
imageDigestPattern = regexp.MustCompile(`^sha256:[a-f0-9]{64}$`)
|
|
)
|
|
|
|
type operatorImageArchiveImportJobInput struct {
|
|
ImportID string
|
|
ArchivePath string
|
|
ArchiveFormat string
|
|
DestinationImage string
|
|
DigestPath string
|
|
InspectPath string
|
|
TimeoutSeconds int
|
|
ImporterImage string
|
|
}
|
|
|
|
type operatorImageArchiveImportJobResult struct {
|
|
Reference *k8s.JobReference `json:"job,omitempty"`
|
|
Output string `json:"output,omitempty"`
|
|
Digest string `json:"digest"`
|
|
}
|
|
|
|
type operatorImageArchiveImporter interface {
|
|
Import(ctx context.Context, input operatorImageArchiveImportJobInput) (*operatorImageArchiveImportJobResult, error)
|
|
}
|
|
|
|
type k8sOperatorImageArchiveImporter struct {
|
|
namespace string
|
|
imagePullSecretName string
|
|
workspacePVCName string
|
|
workspaceMountPath string
|
|
jobTTLSeconds int
|
|
}
|
|
|
|
var operatorImageArchiveImporterFactory = func(server *server) (operatorImageArchiveImporter, error) {
|
|
if strings.TrimSpace(server.workspacePVCName) == "" {
|
|
return nil, fmt.Errorf("WORKSPACE_PVC_NAME is required to import Docker image archives")
|
|
}
|
|
if strings.TrimSpace(server.workspaceMountPath) == "" {
|
|
return nil, fmt.Errorf("WORKSPACE_MOUNT_PATH is required to import Docker image archives")
|
|
}
|
|
if strings.TrimSpace(server.k8sImagePullSecretName) == "" {
|
|
return nil, fmt.Errorf("K8S_IMAGE_PULL_SECRET_NAME is required to push imported operator images")
|
|
}
|
|
return k8sOperatorImageArchiveImporter{
|
|
namespace: server.k8sNamespace,
|
|
imagePullSecretName: server.k8sImagePullSecretName,
|
|
workspacePVCName: server.workspacePVCName,
|
|
workspaceMountPath: server.workspaceMountPath,
|
|
jobTTLSeconds: server.operatorJobTTLSeconds,
|
|
}, nil
|
|
}
|
|
|
|
func (importer k8sOperatorImageArchiveImporter) Import(ctx context.Context, input operatorImageArchiveImportJobInput) (*operatorImageArchiveImportJobResult, error) {
|
|
launcher, err := k8s.NewLauncher(
|
|
importer.namespace,
|
|
importer.imagePullSecretName,
|
|
importer.workspacePVCName,
|
|
importer.workspaceMountPath,
|
|
"",
|
|
"",
|
|
"",
|
|
importer.jobTTLSeconds,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
result, err := launcher.Launch(ctx, k8s.LaunchInput{
|
|
Image: input.ImporterImage,
|
|
Command: []string{"/bin/sh", "-lc"},
|
|
Args: []string{skopeoImageArchiveImportScript},
|
|
RunID: input.ImportID,
|
|
WorkflowID: "operator-image-import",
|
|
NodeID: "archive-import",
|
|
NodeLabel: "Custom operator image archive import",
|
|
TimeoutSeconds: input.TimeoutSeconds,
|
|
RegistryAuth: true,
|
|
Env: map[string]string{
|
|
"ARCHIVE_PATH": input.ArchivePath,
|
|
"ARCHIVE_FORMAT": input.ArchiveFormat,
|
|
"DEST_IMAGE": input.DestinationImage,
|
|
"DIGEST_PATH": input.DigestPath,
|
|
"INSPECT_PATH": input.InspectPath,
|
|
"REGISTRY_AUTH_FILE": registryAuthFilePath,
|
|
},
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
digestBytes, err := os.ReadFile(input.DigestPath)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("read imported image digest: %w", err)
|
|
}
|
|
digest := strings.TrimSpace(string(digestBytes))
|
|
if !imageDigestPattern.MatchString(digest) {
|
|
return nil, fmt.Errorf("imported image digest %q is not a valid sha256 digest", digest)
|
|
}
|
|
return &operatorImageArchiveImportJobResult{
|
|
Reference: result.Reference,
|
|
Output: result.Output,
|
|
Digest: digest,
|
|
}, nil
|
|
}
|
|
|
|
const skopeoImageArchiveImportScript = `set -eu
|
|
: "${ARCHIVE_PATH:?}"
|
|
: "${ARCHIVE_FORMAT:?}"
|
|
: "${DEST_IMAGE:?}"
|
|
: "${DIGEST_PATH:?}"
|
|
: "${INSPECT_PATH:?}"
|
|
: "${REGISTRY_AUTH_FILE:?}"
|
|
|
|
mkdir -p "$(dirname "$DIGEST_PATH")" "$(dirname "$INSPECT_PATH")"
|
|
archive_path="$ARCHIVE_PATH"
|
|
case "$ARCHIVE_PATH" in
|
|
*.tar.gz|*.tgz)
|
|
archive_path="$(dirname "$ARCHIVE_PATH")/image-archive-unpacked.tar"
|
|
gzip -dc "$ARCHIVE_PATH" > "$archive_path"
|
|
;;
|
|
esac
|
|
|
|
case "$ARCHIVE_FORMAT" in
|
|
docker)
|
|
source_ref="docker-archive:$archive_path"
|
|
;;
|
|
oci)
|
|
source_ref="oci-archive:$archive_path"
|
|
;;
|
|
*)
|
|
echo "unsupported archive format: $ARCHIVE_FORMAT" >&2
|
|
exit 64
|
|
;;
|
|
esac
|
|
|
|
skopeo copy --retry-times 3 --authfile "$REGISTRY_AUTH_FILE" "$source_ref" "docker://$DEST_IMAGE"
|
|
skopeo inspect --authfile "$REGISTRY_AUTH_FILE" "docker://$DEST_IMAGE" > "$INSPECT_PATH"
|
|
digest="$(skopeo inspect --authfile "$REGISTRY_AUTH_FILE" --format '{{.Digest}}' "docker://$DEST_IMAGE" | tr -d '[:space:]')"
|
|
printf '%s\n' "$digest" > "$DIGEST_PATH"
|
|
echo "DROBOTICFLOW_IMAGE=$DEST_IMAGE"
|
|
echo "DROBOTICFLOW_IMAGE_DIGEST=$digest"
|
|
`
|
|
|
|
func (server *server) handleOperatorImageArchiveImport(writer http.ResponseWriter, request *http.Request) {
|
|
maxArchiveBytes := int64(server.customOperatorImageArchiveMaxMiB) * 1024 * 1024
|
|
if maxArchiveBytes <= 0 {
|
|
maxArchiveBytes = 8192 * 1024 * 1024
|
|
}
|
|
request.Body = http.MaxBytesReader(writer, request.Body, maxArchiveBytes)
|
|
if err := request.ParseMultipartForm(operatorImageArchiveFormMemoryBytes); err != nil {
|
|
status := http.StatusBadRequest
|
|
if strings.Contains(strings.ToLower(err.Error()), "too large") {
|
|
status = http.StatusRequestEntityTooLarge
|
|
}
|
|
renderError(writer, status, fmt.Errorf("invalid multipart image archive upload: %w", err))
|
|
return
|
|
}
|
|
if request.MultipartForm != nil {
|
|
defer request.MultipartForm.RemoveAll()
|
|
}
|
|
|
|
if strings.TrimSpace(server.customOperatorImageRegistry) == "" {
|
|
renderValidationError(writer, validationIssue{Field: "registry", Message: "CUSTOM_OPERATOR_IMAGE_REGISTRY is required"})
|
|
return
|
|
}
|
|
if strings.TrimSpace(server.customOperatorImageImporterImage) == "" {
|
|
renderValidationError(writer, validationIssue{Field: "importerImage", Message: "CUSTOM_OPERATOR_IMAGE_IMPORTER_IMAGE is required"})
|
|
return
|
|
}
|
|
|
|
payload, issues := parseOperatorArchiveImportPayload(request)
|
|
if len(issues) > 0 {
|
|
renderValidationError(writer, issues...)
|
|
return
|
|
}
|
|
if payload.Type != "custom" {
|
|
renderValidationError(writer, validationIssue{Field: "type", Message: "type must be custom when importing an operator image archive"})
|
|
return
|
|
}
|
|
|
|
archiveFile, archiveHeader, err := request.FormFile("file")
|
|
if err != nil {
|
|
renderValidationError(writer, validationIssue{Field: "file", Message: "Docker image archive file is required"})
|
|
return
|
|
}
|
|
defer archiveFile.Close()
|
|
|
|
safeFilename, filenameIssue := safeOperatorImageArchiveFilename(archiveHeader.Filename)
|
|
if filenameIssue != nil {
|
|
renderValidationError(writer, *filenameIssue)
|
|
return
|
|
}
|
|
archiveFormat := normalizeArchiveFormat(request.FormValue("archiveFormat"))
|
|
if archiveFormat == "" {
|
|
renderValidationError(writer, validationIssue{Field: "archiveFormat", Message: "archiveFormat must be docker or oci"})
|
|
return
|
|
}
|
|
|
|
importID := domain.NewID("operator-image-import")
|
|
repository := normalizeImageRepository(request.FormValue("repository"))
|
|
if repository == "" {
|
|
repository = normalizeImageRepository(payload.Name)
|
|
}
|
|
if repository == "" {
|
|
repository = strings.TrimPrefix(importID, "operator-image-import-")
|
|
}
|
|
tag := strings.TrimSpace(request.FormValue("tag"))
|
|
if tag == "" {
|
|
tag = "import-" + time.Now().UTC().Format("20060102150405")
|
|
}
|
|
if !imageTagPattern.MatchString(tag) {
|
|
renderValidationError(writer, validationIssue{Field: "tag", Message: "tag must match [A-Za-z0-9_][A-Za-z0-9_.-]{0,127}"})
|
|
return
|
|
}
|
|
payload.Version = normalizeOperatorVersion(payload.Version, tag)
|
|
if issues := validateOperatorMetadata(payload.Name, payload.Type, payload.Category, payload.Description, payload.Version, payload.Inputs, payload.Outputs, payload.Resources); len(issues) > 0 {
|
|
renderValidationError(writer, issues...)
|
|
return
|
|
}
|
|
|
|
imageRepository := strings.TrimRight(server.customOperatorImageRegistry, "/") + "/" + repository
|
|
destinationImage := imageRepository + ":" + tag
|
|
if strings.Contains(strings.ToLower(destinationImage), "droboticflow-operator-runner") {
|
|
renderValidationError(writer, validationIssue{Field: "repository", Message: "custom operator image repository must not contain droboticflow-operator-runner"})
|
|
return
|
|
}
|
|
|
|
archiveDir := filepath.Join(server.workspaceMountPath, operatorImageArchiveSubdir, importID)
|
|
if err := os.MkdirAll(archiveDir, 0o755); err != nil {
|
|
renderError(writer, http.StatusInternalServerError, fmt.Errorf("create archive workspace: %w", err))
|
|
return
|
|
}
|
|
archivePath := filepath.Join(archiveDir, safeFilename)
|
|
if err := writeUploadedArchive(archivePath, archiveFile); err != nil {
|
|
renderError(writer, http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
|
|
importer, err := operatorImageArchiveImporterFactory(server)
|
|
if err != nil {
|
|
renderError(writer, http.StatusServiceUnavailable, err)
|
|
return
|
|
}
|
|
digestPath := filepath.Join(archiveDir, "image.digest")
|
|
inspectPath := filepath.Join(archiveDir, "image.inspect.json")
|
|
importResult, err := importer.Import(request.Context(), operatorImageArchiveImportJobInput{
|
|
ImportID: importID,
|
|
ArchivePath: archivePath,
|
|
ArchiveFormat: archiveFormat,
|
|
DestinationImage: destinationImage,
|
|
DigestPath: digestPath,
|
|
InspectPath: inspectPath,
|
|
TimeoutSeconds: server.customOperatorImageImportTimeoutSeconds,
|
|
ImporterImage: server.customOperatorImageImporterImage,
|
|
})
|
|
if err != nil {
|
|
renderError(writer, http.StatusBadGateway, fmt.Errorf("import Docker image archive: %w", err))
|
|
return
|
|
}
|
|
if importResult == nil || !imageDigestPattern.MatchString(strings.TrimSpace(importResult.Digest)) {
|
|
renderError(writer, http.StatusBadGateway, fmt.Errorf("import Docker image archive: importer did not return a valid sha256 digest"))
|
|
return
|
|
}
|
|
|
|
imageID := imageRepository + "@" + strings.TrimSpace(importResult.Digest)
|
|
payload.Image = imageID
|
|
payload.ImageID = imageID
|
|
server.assignTenant(request, &payload.OrganizationID)
|
|
item, err := server.store.CreateOperator(request.Context(), payload)
|
|
if err != nil {
|
|
renderError(writer, http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
|
|
renderJSON(writer, http.StatusCreated, map[string]any{
|
|
"importId": importID,
|
|
"repository": repository,
|
|
"tag": tag,
|
|
"version": payload.Version,
|
|
"image": destinationImage,
|
|
"imageId": imageID,
|
|
"digest": strings.TrimSpace(importResult.Digest),
|
|
"archive": map[string]any{
|
|
"path": archivePath,
|
|
"format": archiveFormat,
|
|
},
|
|
"job": importResult.Reference,
|
|
"operator": sanitizeOperator(item),
|
|
})
|
|
}
|
|
|
|
func parseOperatorArchiveImportPayload(request *http.Request) (domain.CreateOperatorInput, []validationIssue) {
|
|
payload := domain.CreateOperatorInput{
|
|
Name: strings.TrimSpace(request.FormValue("name")),
|
|
Type: normalizeOperatorType(request.FormValue("type")),
|
|
Category: strings.TrimSpace(request.FormValue("category")),
|
|
Description: strings.TrimSpace(request.FormValue("description")),
|
|
Version: strings.TrimSpace(request.FormValue("version")),
|
|
SkillDefinition: strings.TrimSpace(request.FormValue("skillDefinition")),
|
|
OrganizationID: strings.TrimSpace(request.FormValue("organizationId")),
|
|
}
|
|
if payload.Type == "" {
|
|
payload.Type = "custom"
|
|
}
|
|
issues := make([]validationIssue, 0)
|
|
if raw := strings.TrimSpace(request.FormValue("inputs")); raw != "" {
|
|
if err := json.Unmarshal([]byte(raw), &payload.Inputs); err != nil {
|
|
issues = append(issues, validationIssue{Field: "inputs", Message: "inputs must be valid JSON"})
|
|
}
|
|
}
|
|
if raw := strings.TrimSpace(request.FormValue("outputs")); raw != "" {
|
|
if err := json.Unmarshal([]byte(raw), &payload.Outputs); err != nil {
|
|
issues = append(issues, validationIssue{Field: "outputs", Message: "outputs must be valid JSON"})
|
|
}
|
|
}
|
|
if raw := strings.TrimSpace(request.FormValue("configSchema")); raw != "" {
|
|
if err := json.Unmarshal([]byte(raw), &payload.ConfigSchema); err != nil {
|
|
issues = append(issues, validationIssue{Field: "configSchema", Message: "configSchema must be valid JSON"})
|
|
}
|
|
}
|
|
if raw := strings.TrimSpace(request.FormValue("hyperparams")); raw != "" {
|
|
if err := json.Unmarshal([]byte(raw), &payload.Hyperparams); err != nil {
|
|
issues = append(issues, validationIssue{Field: "hyperparams", Message: "hyperparams must be valid JSON"})
|
|
}
|
|
}
|
|
if raw := strings.TrimSpace(request.FormValue("resources")); raw != "" {
|
|
var resources domain.OperatorResources
|
|
if err := json.Unmarshal([]byte(raw), &resources); err != nil {
|
|
issues = append(issues, validationIssue{Field: "resources", Message: "resources must be valid JSON"})
|
|
} else {
|
|
payload.Resources = &resources
|
|
}
|
|
}
|
|
payload.Inputs = normalizePortDefinitions(payload.Inputs)
|
|
payload.Outputs = normalizePortDefinitions(payload.Outputs)
|
|
payload.Resources = normalizeOperatorResources(payload.Resources)
|
|
return payload, issues
|
|
}
|
|
|
|
func normalizeArchiveFormat(value string) string {
|
|
switch strings.ToLower(strings.TrimSpace(value)) {
|
|
case "", "docker", "docker-archive":
|
|
return "docker"
|
|
case "oci", "oci-archive":
|
|
return "oci"
|
|
default:
|
|
return ""
|
|
}
|
|
}
|
|
|
|
func normalizeImageRepository(value string) string {
|
|
normalized := strings.ToLower(strings.TrimSpace(value))
|
|
normalized = strings.ReplaceAll(normalized, " ", "-")
|
|
normalized = imageRepositoryInvalidCharacters.ReplaceAllString(normalized, "-")
|
|
normalized = imageRepositoryRepeatedSlashes.ReplaceAllString(normalized, "/")
|
|
normalized = strings.Trim(normalized, "/._-")
|
|
parts := strings.Split(normalized, "/")
|
|
cleaned := make([]string, 0, len(parts))
|
|
for _, part := range parts {
|
|
part = strings.Trim(part, "._-")
|
|
if part != "" {
|
|
cleaned = append(cleaned, part)
|
|
}
|
|
}
|
|
return strings.Join(cleaned, "/")
|
|
}
|
|
|
|
func safeOperatorImageArchiveFilename(filename string) (string, *validationIssue) {
|
|
safe := filepath.Base(strings.TrimSpace(filename))
|
|
safe = strings.ReplaceAll(safe, string(filepath.Separator), "-")
|
|
if safe == "." || safe == "" {
|
|
return "", &validationIssue{Field: "file", Message: "Docker image archive filename is required"}
|
|
}
|
|
lower := strings.ToLower(safe)
|
|
if !strings.HasSuffix(lower, ".tar") && !strings.HasSuffix(lower, ".tar.gz") && !strings.HasSuffix(lower, ".tgz") {
|
|
return "", &validationIssue{Field: "file", Message: "Docker image archive must be a .tar, .tar.gz, or .tgz file"}
|
|
}
|
|
return safe, nil
|
|
}
|
|
|
|
func writeUploadedArchive(path string, reader io.Reader) error {
|
|
file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o644)
|
|
if err != nil {
|
|
return fmt.Errorf("create uploaded image archive: %w", err)
|
|
}
|
|
defer file.Close()
|
|
if _, err := io.Copy(file, reader); err != nil {
|
|
return fmt.Errorf("write uploaded image archive: %w", err)
|
|
}
|
|
return nil
|
|
}
|