|
8 | 8 | import yaml from "js-yaml"; |
9 | 9 |
|
10 | 10 | export const app = express(); |
| 11 | +app.use(express.json()); |
| 12 | + |
11 | 13 | app.use(cors({ origin: "*" })); |
12 | 14 |
|
13 | 15 | const kc = new KubeConfig(); |
@@ -479,6 +481,249 @@ app.get("/api/all-pods", async (req, res) => { |
479 | 481 | } |
480 | 482 | }); |
481 | 483 |
|
| 484 | +// POST /api/pods - Create a pod |
| 485 | +app.post("/api/pods", async (req, res) => { |
| 486 | + let podManifest = req.body; |
| 487 | + console.log("Received pod manifest:", JSON.stringify(podManifest, null, 2)); |
| 488 | + |
| 489 | + try { |
| 490 | + // Defensive: check manifest shape |
| 491 | + if ( |
| 492 | + !podManifest || |
| 493 | + !podManifest.metadata || |
| 494 | + !podManifest.metadata.name || |
| 495 | + !podManifest.spec |
| 496 | + ) { |
| 497 | + return res.status(400).json({ error: "Invalid pod manifest" }); |
| 498 | + } |
| 499 | + |
| 500 | + // Fix the apiVersion for regular Pods |
| 501 | + if (podManifest.apiVersion === "scheduling.volcano.sh/v1beta1") { |
| 502 | + podManifest.apiVersion = "v1"; |
| 503 | + } |
| 504 | + |
| 505 | + // Fix the kind |
| 506 | + if (podManifest.kind !== "Pod") { |
| 507 | + podManifest.kind = "Pod"; |
| 508 | + } |
| 509 | + |
| 510 | + // Validate that this is a proper Pod spec (not Queue spec) |
| 511 | + if (podManifest.spec.weight || podManifest.spec.reclaimable) { |
| 512 | + return res.status(400).json({ |
| 513 | + error: "Invalid Pod spec. Use proper Pod specification with containers, not Queue fields.", |
| 514 | + }); |
| 515 | + } |
| 516 | + |
| 517 | + // Ensure Pod spec has required containers field |
| 518 | + if ( |
| 519 | + !podManifest.spec.containers || |
| 520 | + !Array.isArray(podManifest.spec.containers) |
| 521 | + ) { |
| 522 | + return res.status(400).json({ |
| 523 | + error: "Pod spec must include 'containers' array", |
| 524 | + }); |
| 525 | + } |
| 526 | + |
| 527 | + // Defensive: never allow "All", fallback to "default" if missing |
| 528 | + let namespace = podManifest.metadata.namespace; |
| 529 | + if ( |
| 530 | + !namespace || |
| 531 | + namespace === "All" || |
| 532 | + typeof namespace !== "string" || |
| 533 | + !namespace.trim() |
| 534 | + ) { |
| 535 | + namespace = "default"; |
| 536 | + podManifest.metadata.namespace = namespace; |
| 537 | + } |
| 538 | + |
| 539 | + console.log("Creating pod in namespace:", namespace); |
| 540 | + console.log( |
| 541 | + "Final pod manifest:", |
| 542 | + JSON.stringify(podManifest, null, 2), |
| 543 | + ); |
| 544 | + |
| 545 | + // Use object-based syntax for consistency |
| 546 | + const response = await k8sCoreApi.createNamespacedPod({ |
| 547 | + namespace: namespace, |
| 548 | + body: podManifest, |
| 549 | + }); |
| 550 | + |
| 551 | + res.status(201).json({ |
| 552 | + message: "Pod created successfully", |
| 553 | + data: response.body, |
| 554 | + }); |
| 555 | + } catch (error) { |
| 556 | + console.error("Error creating pod:", error?.body || error); |
| 557 | + let msg = "Failed to create pod"; |
| 558 | + if (error?.body?.message) { |
| 559 | + msg = error.body.message; |
| 560 | + } else if (error?.message) { |
| 561 | + msg = error.message; |
| 562 | + } |
| 563 | + res.status(500).json({ error: msg }); |
| 564 | + } |
| 565 | +}); |
| 566 | +// POST /api/jobs - Create a job (Volcano custom job) |
| 567 | +app.post("/api/jobs", async (req, res) => { |
| 568 | + const jobManifest = req.body; |
| 569 | + try { |
| 570 | + if ( |
| 571 | + !jobManifest || |
| 572 | + !jobManifest.metadata || |
| 573 | + !jobManifest.metadata.name || |
| 574 | + !jobManifest.spec |
| 575 | + ) { |
| 576 | + return res.status(400).json({ error: "Invalid job manifest" }); |
| 577 | + } |
| 578 | + |
| 579 | + const namespace = jobManifest.metadata.namespace || "default"; |
| 580 | + |
| 581 | + const response = await k8sApi.createNamespacedCustomObject({ |
| 582 | + group: "batch.volcano.sh", |
| 583 | + version: "v1alpha1", |
| 584 | + namespace, |
| 585 | + plural: "jobs", |
| 586 | + body: jobManifest, |
| 587 | + }); |
| 588 | + |
| 589 | + res.status(201).json({ |
| 590 | + message: "Job created successfully", |
| 591 | + data: response.body, |
| 592 | + }); |
| 593 | + } catch (error) { |
| 594 | + console.error("Error creating job:", error?.body || error); |
| 595 | + let msg = "Failed to create job"; |
| 596 | + if (error?.body?.message) msg = error.body.message; |
| 597 | + res.status(500).json({ error: msg }); |
| 598 | + } |
| 599 | +}); |
| 600 | + |
| 601 | +//create a queue |
| 602 | +app.post("/api/queues", async (req, res) => { |
| 603 | + const queueManifest = req.body; |
| 604 | + |
| 605 | + try { |
| 606 | + if ( |
| 607 | + !queueManifest || |
| 608 | + !queueManifest.metadata || |
| 609 | + !queueManifest.metadata.name || |
| 610 | + !queueManifest.spec |
| 611 | + ) { |
| 612 | + return res.status(400).json({ error: "Invalid queue manifest" }); |
| 613 | + } |
| 614 | + |
| 615 | + const customAnnotations = queueManifest.annotations || {}; |
| 616 | + queueManifest.metadata.annotations = { |
| 617 | + ...(queueManifest.metadata.annotations || {}), |
| 618 | + ...customAnnotations, |
| 619 | + }; |
| 620 | + |
| 621 | + // Debug the API client |
| 622 | + console.log("API client type:", typeof k8sApi); |
| 623 | + console.log( |
| 624 | + "createClusterCustomObject method exists:", |
| 625 | + typeof k8sApi.createClusterCustomObject, |
| 626 | + ); |
| 627 | + |
| 628 | + // Test if we can list queues first (simpler operation) |
| 629 | + console.log("Testing list operation first..."); |
| 630 | + try { |
| 631 | + const listResponse = await k8sApi.listClusterCustomObject({ |
| 632 | + group: "scheduling.volcano.sh", |
| 633 | + version: "v1beta1", |
| 634 | + plural: "queues", |
| 635 | + }); |
| 636 | + console.log( |
| 637 | + "List operation successful, found", |
| 638 | + listResponse.items?.length || 0, |
| 639 | + "queues", |
| 640 | + ); |
| 641 | + } catch (listError) { |
| 642 | + console.error("List operation failed:", listError.message); |
| 643 | + } |
| 644 | + |
| 645 | + // Add timeout and more detailed logging |
| 646 | + console.log("About to call k8sApi.createClusterCustomObject..."); |
| 647 | + |
| 648 | + const response = await Promise.race([ |
| 649 | + k8sApi.createClusterCustomObject({ |
| 650 | + group: "scheduling.volcano.sh", |
| 651 | + version: "v1beta1", |
| 652 | + plural: "queues", |
| 653 | + body: queueManifest, |
| 654 | + }), |
| 655 | + new Promise((_, reject) => |
| 656 | + setTimeout( |
| 657 | + () => reject(new Error("API call timeout after 30s")), |
| 658 | + 30000, |
| 659 | + ), |
| 660 | + ), |
| 661 | + ]); |
| 662 | + |
| 663 | + console.log("API call successful, response:", response); |
| 664 | + |
| 665 | + res.status(201).json({ |
| 666 | + message: "Queue created successfully", |
| 667 | + data: response.body, |
| 668 | + }); |
| 669 | + } catch (error) { |
| 670 | + console.error("Error creating queue:", error?.body || error); |
| 671 | + let msg = "Failed to create queue"; |
| 672 | + if (error?.body?.message) msg = error.body.message; |
| 673 | + res.status(500).json({ error: msg }); |
| 674 | + } |
| 675 | +}); |
| 676 | +// Delete a Volcano Job |
| 677 | +app.delete("/api/jobs/:namespace/:name", async (req, res) => { |
| 678 | + const { namespace, name } = req.params; |
| 679 | + try { |
| 680 | + // Try to delete the job directly (Kubernetes will 404 if not found) |
| 681 | + const response = await k8sApi.deleteNamespacedCustomObject({ |
| 682 | + group: "batch.volcano.sh", |
| 683 | + version: "v1alpha1", |
| 684 | + namespace, |
| 685 | + plural: "jobs", |
| 686 | + name, |
| 687 | + body: { propagationPolicy: "Foreground" }, |
| 688 | + }); |
| 689 | + |
| 690 | + return res.json({ |
| 691 | + message: "Job deleted successfully", |
| 692 | + data: response.body, |
| 693 | + }); |
| 694 | + } catch (err) { |
| 695 | + // If Kubernetes provides a status code, use it |
| 696 | + const statusCode = err?.statusCode || err?.response?.statusCode || 500; |
| 697 | + let details = "Unknown error"; |
| 698 | + let k8sBody = err?.body || err?.response?.body; |
| 699 | + |
| 700 | + // Try to parse the Kubernetes error body as JSON for best info |
| 701 | + try { |
| 702 | + if (typeof k8sBody === "string") { |
| 703 | + k8sBody = JSON.parse(k8sBody); |
| 704 | + } |
| 705 | + if (k8sBody?.message) { |
| 706 | + details = k8sBody.message; |
| 707 | + } else if (k8sBody?.details) { |
| 708 | + details = k8sBody.details; |
| 709 | + } |
| 710 | + } catch { |
| 711 | + // If parsing fails, just use the raw body string |
| 712 | + details = typeof k8sBody === "string" ? k8sBody : details; |
| 713 | + } |
| 714 | + |
| 715 | + // Log full error object for debugging |
| 716 | + console.error("Full Kubernetes Error Object:", err); |
| 717 | + |
| 718 | + // Respond with as much info from Kubernetes as possible |
| 719 | + return res.status(statusCode).json({ |
| 720 | + error: k8sBody?.reason || "KubernetesError", |
| 721 | + message: details, |
| 722 | + code: statusCode, |
| 723 | + k8s: k8sBody, |
| 724 | + }); |
| 725 | + } |
| 726 | +}); |
482 | 727 | app.delete("/api/queues/:name", async (req, res) => { |
483 | 728 | const { name } = req.params; |
484 | 729 | const queueName = name.toLowerCase(); |
|
0 commit comments