|
| 1 | +package pods |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log/slog" |
| 7 | + |
| 8 | + "github.com/cobaltcore-dev/cortex/api/delegation/pods" |
| 9 | + "github.com/cobaltcore-dev/cortex/api/v1alpha1" |
| 10 | + "github.com/cobaltcore-dev/cortex/internal/scheduling/lib" |
| 11 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 12 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 13 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 14 | +) |
| 15 | + |
| 16 | +// GangFilter ensures that pods belonging to a PodGroup are only scheduled |
| 17 | +// if the PodGroup resource exists. |
| 18 | +type GangFilter struct { |
| 19 | + client client.Client |
| 20 | +} |
| 21 | + |
| 22 | +func (f *GangFilter) Init(ctx context.Context, client client.Client, step v1alpha1.Step) error { |
| 23 | + f.client = client |
| 24 | + return nil |
| 25 | +} |
| 26 | + |
| 27 | +func (f *GangFilter) Run(traceLog *slog.Logger, request pods.PodPipelineRequest) (*lib.StepResult, error) { |
| 28 | + activations := make(map[string]float64, len(request.Nodes)) |
| 29 | + stats := make(map[string]lib.StepStatistics) |
| 30 | + |
| 31 | + pod := request.Pod |
| 32 | + if pod == nil { |
| 33 | + traceLog.Warn("gang-filter: pod is nil in request") |
| 34 | + return nil, fmt.Errorf("pod is nil in request") |
| 35 | + } |
| 36 | + |
| 37 | + // Check for Workload API |
| 38 | + // Fetch the full pod object to inspect new fields if they are not in the struct |
| 39 | + workloadName := "" |
| 40 | + // Note: We cannot access pod.Spec.WorkloadRef directly if the struct is old. |
| 41 | + // Use unstructured to attempt to find it. |
| 42 | + uPod := &unstructured.Unstructured{} |
| 43 | + uPod.SetGroupVersionKind(schema.GroupVersionKind{Group: "", Version: "v1", Kind: "Pod"}) |
| 44 | + if err := f.client.Get(context.Background(), client.ObjectKey{Name: pod.Name, Namespace: pod.Namespace}, uPod); err == nil { |
| 45 | + val, found, _ := unstructured.NestedString(uPod.Object, "spec", "workloadRef", "name") |
| 46 | + if found { |
| 47 | + workloadName = val |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + if workloadName != "" { |
| 52 | + traceLog.Info("gang-filter: checking for workload", "workloadName", workloadName) |
| 53 | + workload := &unstructured.Unstructured{} |
| 54 | + workload.SetGroupVersionKind(schema.GroupVersionKind{ |
| 55 | + Group: "scheduling.k8s.io", |
| 56 | + Version: "v1alpha1", |
| 57 | + Kind: "Workload", |
| 58 | + }) |
| 59 | + if err := f.client.Get(context.Background(), client.ObjectKey{Name: workloadName, Namespace: pod.Namespace}, workload); err != nil { |
| 60 | + traceLog.Error("gang-filter: failed to fetch workload", "error", err) |
| 61 | + // Deny all nodes if the gang resource is missing or cannot be fetched. |
| 62 | + return &lib.StepResult{Activations: activations, Statistics: stats}, nil |
| 63 | + } |
| 64 | + traceLog.Info("gang-filter: workload found, allowing scheduling") |
| 65 | + for _, node := range request.Nodes { |
| 66 | + activations[node.Name] = 1.0 |
| 67 | + } |
| 68 | + return &lib.StepResult{Activations: activations, Statistics: stats}, nil |
| 69 | + } |
| 70 | + |
| 71 | + // Fallback: Check if the pod belongs to a gang via Label |
| 72 | + // We use the label "pod-group.scheduling.k8s.io/name" which is standard for gang scheduling. |
| 73 | + gangName, ok := pod.Labels["pod-group.scheduling.k8s.io/name"] |
| 74 | + if !ok { |
| 75 | + // Not a gang pod, allow it. |
| 76 | + for _, node := range request.Nodes { |
| 77 | + activations[node.Name] = 1.0 |
| 78 | + } |
| 79 | + return &lib.StepResult{Activations: activations, Statistics: stats}, nil |
| 80 | + } |
| 81 | + |
| 82 | + traceLog.Info("gang-filter: checking for pod group", "gangName", gangName) |
| 83 | + |
| 84 | + // Fetch the PodGroup. |
| 85 | + // We use Unstructured because the PodGroup CRD might not be compiled into this binary. |
| 86 | + // We assume the group is scheduling.k8s.io |
| 87 | + podGroup := &unstructured.Unstructured{} |
| 88 | + podGroup.SetGroupVersionKind(schema.GroupVersionKind{ |
| 89 | + Group: "scheduling.k8s.io", |
| 90 | + Version: "v1alpha1", |
| 91 | + Kind: "PodGroup", |
| 92 | + }) |
| 93 | + |
| 94 | + err := f.client.Get(context.Background(), client.ObjectKey{ |
| 95 | + Name: gangName, |
| 96 | + Namespace: pod.Namespace, |
| 97 | + }, podGroup) |
| 98 | + |
| 99 | + if err != nil { |
| 100 | + traceLog.Error("gang-filter: failed to fetch pod group", "error", err) |
| 101 | + // Deny all nodes if the gang resource is missing or cannot be fetched. |
| 102 | + return &lib.StepResult{Activations: activations, Statistics: stats}, nil |
| 103 | + } |
| 104 | + |
| 105 | + // If we found the PodGroup, we currently allow scheduling. |
| 106 | + // In a full implementation, we would check 'minMember' and other status fields here. |
| 107 | + traceLog.Info("gang-filter: pod group found, allowing scheduling") |
| 108 | + for _, node := range request.Nodes { |
| 109 | + activations[node.Name] = 1.0 |
| 110 | + } |
| 111 | + |
| 112 | + return &lib.StepResult{Activations: activations, Statistics: stats}, nil |
| 113 | +} |
0 commit comments