forked from longtao.wu/droboticflow
92 lines
3.5 KiB
Go
92 lines
3.5 KiB
Go
package worker
|
|
|
|
import (
|
|
"fmt"
|
|
"path"
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"droboticflow/internal/domain"
|
|
"droboticflow/internal/k8s"
|
|
)
|
|
|
|
const qwenBehaviorRoughCutOperatorID = "operator-builtin-qwen-behavior-rough-cut"
|
|
const qwenBehaviorRoughCutHandler = "qwen_behavior_rough_cut"
|
|
|
|
var qwenNodeCachePVCNamePattern = regexp.MustCompile(`^[a-z0-9]([-a-z0-9]*[a-z0-9])?$`)
|
|
|
|
// qwenNodeCacheConfig is deliberately separate from generic PVC mounts. The
|
|
// cache is an optimization for one verified builtin and must never become an
|
|
// ambient writable volume for arbitrary operators.
|
|
type qwenNodeCacheConfig struct {
|
|
enabled bool
|
|
pvcName string
|
|
mountPath string
|
|
nodeSelector map[string]string
|
|
staleStagingSeconds int
|
|
}
|
|
|
|
func newQwenNodeCacheConfig(enabled bool, pvcName string, mountPath string, nodeSelector string, staleStagingSeconds int) (qwenNodeCacheConfig, error) {
|
|
if !enabled {
|
|
return qwenNodeCacheConfig{}, nil
|
|
}
|
|
pvcName = strings.TrimSpace(pvcName)
|
|
if pvcName == "" || len(pvcName) > 63 || !qwenNodeCachePVCNamePattern.MatchString(pvcName) {
|
|
return qwenNodeCacheConfig{}, fmt.Errorf("DROBOTICFLOW_QWEN_NODE_CACHE_PVC must be a lowercase DNS label when cache is enabled")
|
|
}
|
|
mountPath = strings.TrimSpace(mountPath)
|
|
if mountPath == "" || !strings.HasPrefix(mountPath, "/") || strings.ContainsAny(mountPath, " \t\r\n") || path.Clean(mountPath) != mountPath || mountPath == "/" {
|
|
return qwenNodeCacheConfig{}, fmt.Errorf("DROBOTICFLOW_QWEN_NODE_CACHE_MOUNT_PATH must be a clean absolute non-root path when cache is enabled")
|
|
}
|
|
if staleStagingSeconds < 0 {
|
|
return qwenNodeCacheConfig{}, fmt.Errorf("DROBOTICFLOW_QWEN_NODE_CACHE_STALE_STAGING_SECONDS must be non-negative")
|
|
}
|
|
parsedSelector, err := k8s.ParseNodeSelector(nodeSelector)
|
|
if err != nil {
|
|
return qwenNodeCacheConfig{}, fmt.Errorf("invalid DROBOTICFLOW_QWEN_NODE_CACHE_NODE_SELECTOR: %w", err)
|
|
}
|
|
if len(parsedSelector) == 0 {
|
|
return qwenNodeCacheConfig{}, fmt.Errorf("DROBOTICFLOW_QWEN_NODE_CACHE_NODE_SELECTOR is required when cache is enabled")
|
|
}
|
|
return qwenNodeCacheConfig{
|
|
enabled: true,
|
|
pvcName: pvcName,
|
|
mountPath: mountPath,
|
|
nodeSelector: parsedSelector,
|
|
staleStagingSeconds: staleStagingSeconds,
|
|
}, nil
|
|
}
|
|
|
|
func (config qwenNodeCacheConfig) appliesTo(node domain.WorkflowNode) bool {
|
|
return config.enabled &&
|
|
node.Type == "operator" &&
|
|
strings.TrimSpace(node.Data.OperatorID) == qwenBehaviorRoughCutOperatorID &&
|
|
runtimeContractHandler(node.Data.RuntimeContract) == qwenBehaviorRoughCutHandler
|
|
}
|
|
|
|
func (config qwenNodeCacheConfig) apply(node domain.WorkflowNode, env map[string]string, mounts []k8s.PVCMount) (map[string]string, []k8s.PVCMount, map[string]string, error) {
|
|
if !config.appliesTo(node) {
|
|
return env, mounts, nil, nil
|
|
}
|
|
for _, mount := range mounts {
|
|
if mount.MountPath == config.mountPath || mount.Name == "qwen-node-cache" {
|
|
return nil, nil, nil, fmt.Errorf("Qwen node-local cache mount collides with configured PVC mount %q", mount.MountPath)
|
|
}
|
|
}
|
|
env["DROBOTICFLOW_QWEN_NODE_CACHE_ENABLED"] = "true"
|
|
env["DROBOTICFLOW_QWEN_NODE_CACHE_ROOT"] = config.mountPath
|
|
env["DROBOTICFLOW_QWEN_NODE_CACHE_STALE_STAGING_SECONDS"] = strconv.Itoa(config.staleStagingSeconds)
|
|
mounts = append(mounts, k8s.PVCMount{
|
|
Name: "qwen-node-cache",
|
|
PVCName: config.pvcName,
|
|
MountPath: config.mountPath,
|
|
ReadOnly: false,
|
|
})
|
|
selector := make(map[string]string, len(config.nodeSelector))
|
|
for key, value := range config.nodeSelector {
|
|
selector[key] = value
|
|
}
|
|
return env, mounts, selector, nil
|
|
}
|