Skip to content

Commit

Permalink
Deconstructed the backend code
Browse files Browse the repository at this point in the history
  • Loading branch information
Himanshityagii24 committed Feb 18, 2025
1 parent a20477b commit 787cec9
Show file tree
Hide file tree
Showing 9 changed files with 456 additions and 0 deletions.
113 changes: 113 additions & 0 deletions backend/controllers/jobController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import { k8sCoreApi, k8sApi } from '../services/k8sClient.js';


// Get all jobs with filters
export const getAllJobs = async (req, res) => {
try {
const { namespace = '', search = '', queue = '', status = '' } = req.query;

let response;
if (namespace === '' || namespace === 'All') {
response = await k8sApi.listClusterCustomObject('batch.volcano.sh', 'v1alpha1', 'jobs', true);
} else {
response = await k8sApi.listNamespacedCustomObject('batch.volcano.sh', 'v1alpha1', namespace, 'jobs', true);
}

let filteredJobs = response.body.items || [];

if (search) {
filteredJobs = filteredJobs.filter(job =>
job.metadata.name.toLowerCase().includes(search.toLowerCase())
);
}

if (queue && queue !== 'All') {
filteredJobs = filteredJobs.filter(job => job.spec.queue === queue);
}

if (status && status !== 'All') {
filteredJobs = filteredJobs.filter(job => job.status.state.phase === status);
}

res.json({
items: filteredJobs,
totalCount: filteredJobs.length,
});
} catch (err) {
console.error('Error fetching jobs:', err);
res.status(500).json({
error: 'Failed to fetch jobs',
details: err.message,
});
}
};

// Get details of a specific job
export const getJobDetails = async (req, res) => {
try {
const { namespace, name } = req.params;
const response = await k8sApi.getNamespacedCustomObject(
'batch.volcano.sh',
'v1alpha1',
namespace,
'jobs',
name
);
res.json(response.body);
} catch (err) {
console.error('Error fetching job:', err);
res.status(500).json({
error: 'Failed to fetch job',
details: err.message,
});
}
};

// Get YAML of a specific job
export const getJobYaml = async (req, res) => {
try {
const { namespace, name } = req.params;
const response = await k8sApi.getNamespacedCustomObject(
'batch.volcano.sh',
'v1alpha1',
namespace,
'jobs',
name
);
const formattedYaml = yaml.dump(response.body, {
indent: 2,
lineWidth: -1,
noRefs: true,
sortKeys: false,
});
res.setHeader('Content-Type', 'text/yaml');
res.send(formattedYaml);
} catch (error) {
console.error('Error fetching job YAML:', error);
res.status(500).json({
error: 'Failed to fetch job YAML',
details: error.message,
});
}
};

// Get all jobs without pagination
export const getAllJobsWithoutPagination = async (req, res) => {
try {
const response = await k8sApi.listClusterCustomObject(
'batch.volcano.sh',
'v1alpha1',
'jobs',
{
pretty: true,
}
);
res.json({
items: response.body.items,
totalCount: response.body.items.length,
});
} catch (err) {
console.error('Error fetching all jobs:', err);
res.status(500).json({ error: 'Failed to fetch all jobs' });
}
};
83 changes: 83 additions & 0 deletions backend/controllers/podController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { k8sCoreApi, k8sApi } from '../services/k8sClient.js';

// Get all Pods based on query params
export const getAllPods = async (req, res) => {
try {
const namespace = req.query.namespace || "";
const searchTerm = req.query.search || "";
const statusFilter = req.query.status || "";

let response;
if (namespace === "" || namespace === "All") {
response = await k8sCoreApi.listPodForAllNamespaces();
} else {
response = await k8sCoreApi.listNamespacedPod(namespace);
}

let filteredPods = response.body.items || [];

// Apply search filter
if (searchTerm) {
filteredPods = filteredPods.filter((pod) =>
pod.metadata.name.toLowerCase().includes(searchTerm.toLowerCase())
);
}
if (statusFilter && statusFilter !== "All") {
filteredPods = filteredPods.filter((pod) =>
pod.status.phase === statusFilter
);
}

res.json({
items: filteredPods,
totalCount: filteredPods.length,
});
} catch (err) {
console.error("Error fetching pods:", err);
res.status(500).json({
error: "Failed to fetch pods",
details: err.message,
});
}
};

// Get YAML details of a specific Pod
export const getPodYaml = async (req, res) => {
try {
const { namespace, name } = req.params;
const response = await k8sCoreApi.readNamespacedPod(name, namespace);

// Convert JSON to formatted YAML
const formattedYaml = yaml.dump(response.body, {
indent: 2,
lineWidth: -1,
noRefs: true,
sortKeys: false,
});

res.setHeader("Content-Type", "text/yaml");
res.send(formattedYaml);
} catch (error) {
console.error("Error fetching pod YAML:", error);
res.status(500).json({
error: "Failed to fetch pod YAML",
details: error.message,
});
}
};

// Get all Kubernetes namespaces
export const getNamespaces = async (req, res) => {
try {
const response = await k8sCoreApi.listNamespace();
res.json({
items: response.body.items,
});
} catch (error) {
console.error("Error fetching namespaces:", error);
res.status(500).json({
error: "Failed to fetch namespaces",
details: error.message,
});
}
};
110 changes: 110 additions & 0 deletions backend/controllers/queuecontroller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import { k8sCoreApi, k8sApi } from '../services/k8sClient.js';

// Fetch all Queues
export const getAllQueues = async (req, res) => {
try {
const page = parseInt(req.query.page) || 1;
const limit = parseInt(req.query.limit) || 10;
const searchTerm = req.query.search || "";
const stateFilter = req.query.state || "";
console.log('Fetching queues with params:', { page, limit, searchTerm, stateFilter });

const response = await k8sApi.listClusterCustomObject(
"scheduling.volcano.sh",
"v1beta1",
"queues"
);

let filteredQueues = response.body.items || [];

// Apply search filter
if (searchTerm) {
filteredQueues = filteredQueues.filter((queue) =>
queue.metadata.name.toLowerCase().includes(searchTerm.toLowerCase())
);
}

// Apply state filter
if (stateFilter && stateFilter !== "All") {
filteredQueues = filteredQueues.filter((queue) =>
queue.status.state === stateFilter
);
}

const totalCount = filteredQueues.length;
const startIndex = (page - 1) * limit;
const endIndex = Math.min(startIndex + limit, totalCount);
const paginatedQueues = filteredQueues.slice(startIndex, endIndex);

res.json({
items: paginatedQueues,
totalCount: totalCount,
page: page,
limit: limit,
totalPages: Math.ceil(totalCount / limit)
});
} catch (error) {
console.error("Error fetching queues:", error);
res.status(500).json({
error: "Failed to fetch queues",
details: error.message,
});
}
};


// Get details of a specific Queue
export const getQueueDetails = async (req, res) => {
const queueName = req.params.name;
try {
console.log("Fetching details for queue:", queueName);

const response = await k8sApi.getClusterCustomObject(
"scheduling.volcano.sh",
"v1beta1",
"queues",
queueName
);

console.log("Queue details response:", response.body);

res.json(response.body);
} catch (error) {
console.error("Error fetching queue details:", error);
res.status(500).json({
error: "Failed to fetch queue details",
details: error.message,
});
}
};


// Get YAML of a specific Queue
export const getQueueYaml = async (req, res) => {
try {
const response = await k8sApi.getClusterCustomObject(
"scheduling.volcano.sh",
"v1beta1",
"queues",
req.params.name
);
const formattedYaml = yaml.dump(response.body, {
indent: 2,
lineWidth: -1,
noRefs: true,
sortKeys: false
});

// Set the content type to text/yaml and send the response
res.setHeader('Content-Type', 'text/yaml');
res.send(formattedYaml);
} catch (error) {
console.error("Error fetching queue YAML:", error);
res.status(500).json({
error: "Failed to fetch queue YAML",
details: error.message
});
}
};


70 changes: 70 additions & 0 deletions backend/controllers/withoutPaginationController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@

import { k8sCoreApi, k8sApi } from '../services/k8sClient.js';

// Controller function to fetch all Kubernetes jobs
export const getAllJobs = async (req, res) => {
try {

const response = await k8sApi.listClusterCustomObject(
"batch.volcano.sh",
"v1alpha1",
"jobs",
{ pretty: true }
);


const jobs = response.body.items.map(job => ({
...job,
status: {
state: job.status?.state || 'Unknown',
phase: job.status?.phase || 'Running'
}
}));


res.json({
items: jobs,
totalCount: jobs.length
});
} catch (err) {

console.error("Error fetching all jobs:", err);
res.status(500).json({ error: "Failed to fetch all jobs" });
}
};
// Controller function to fetch all Kubernetes queues

export const getAllQueues = async (req, res) => {
try {
const response = await k8sApi.listClusterCustomObject(
"scheduling.volcano.sh",
"v1beta1",
"queues"
);

res.json({
items: response.body.items,
totalCount: response.body.items.length
});
} catch (error) {

console.error("Error fetching all queues:", error);
res.status(500).json({ error: "Failed to fetch all queues" });
}
};

// Controller function to fetch all Kubernetes pods
export const getAllPods = async (req, res) => {
try {
const response = await k8sCoreApi.listPodForAllNamespaces();

res.json({
items: response.body.items,
totalCount: response.body.items.length
});
} catch (error) {

console.error('Error fetching all pods:', error);
res.status(500).json({ error: 'Failed to fetch all pods' });
}
};
Loading

0 comments on commit 787cec9

Please sign in to comment.