Skip to content

fix: replace broken API config with centralized axios instance#268

Open
gautamsidhwani29 wants to merge 3 commits into
volcano-sh:mainfrom
gautamsidhwani29:fix/centralize-api-config
Open

fix: replace broken API config with centralized axios instance#268
gautamsidhwani29 wants to merge 3 commits into
volcano-sh:mainfrom
gautamsidhwani29:fix/centralize-api-config

Conversation

@gautamsidhwani29
Copy link
Copy Markdown

Problem

src/config/api.js was created to centralize API configuration but was effectively dead code. It used process.env.APP_SERVER_PORT (not supported in Vite), hardcoded http://localhost:3001 as the base URL (breaking staging and production), and was never imported by any component.

Solution

Rewrote api.js as a single shared axios instance with baseURL: '/api' and migrated all direct fetch/axios calls across the frontend to use it.

Fixes #263

Changes

  • Replaced broken process.env + hardcoded localhost URL in api.js with a shared axios instance (baseURL: '/api')
  • Updated API_ENDPOINTS to use relative paths
  • Replaced all direct fetch/axios calls with apiClient in:
    • Dashboard.jsx
    • Jobs.jsx
    • JobTable.jsx
    • Pods.jsx
    • QueueTable.jsx
    • EditQueueDialog.jsx
    • utils.js

Signed-off-by: gautamsidhwani29 <gautamsidhwani405@gmail.com>
@volcano-sh-bot
Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign monokaix for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request centralizes frontend API requests by introducing an Axios-based apiClient and an API_ENDPOINTS configuration object. It refactors components like Dashboard, Jobs, Pods, and Queues to use these new utilities instead of direct fetch or axios calls, improving consistency and error handling. The reviewer feedback suggests further improving this architecture by migrating all remaining hardcoded paths to the centralized configuration and moving URL encoding logic into the endpoint helper functions to reduce boilerplate and prevent bugs.

throw new Error(errText);
}
const response = await apiClient.put(
`${API_ENDPOINTS.queues.list}/${updated.metadata.name}`,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Use the detail helper from API_ENDPOINTS. This also ensures the queue name is properly URL-encoded, which was missing in the current implementation.

Suggested change
`${API_ENDPOINTS.queues.list}/${updated.metadata.name}`,
API_ENDPOINTS.queues.detail(updated.metadata.name),

Comment thread frontend/src/components/jobs/Jobs.jsx Outdated
import React, { useCallback, useEffect, useMemo, useState } from "react";
import { Box, Typography, useTheme } from "@mui/material";
import axios from "axios";
import apiClient from "../../config/api";
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Import API_ENDPOINTS to use the centralized path constants.

Suggested change
import apiClient from "../../config/api";
import apiClient, { API_ENDPOINTS } from "../../config/api";

Comment on lines +24 to +26
apiClient.get("/jobs?limit=1000"),
apiClient.get("/queues?limit=1000"),
apiClient.get("/pods?limit=1000"),
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Use the centralized API_ENDPOINTS constants instead of hardcoded strings.

Suggested change
apiClient.get("/jobs?limit=1000"),
apiClient.get("/queues?limit=1000"),
apiClient.get("/pods?limit=1000"),
apiClient.get(`${API_ENDPOINTS.jobs.list}?limit=1000`),
apiClient.get(`${API_ENDPOINTS.queues.list}?limit=1000`),
apiClient.get(`${API_ENDPOINTS.pods.list}?limit=1000`),

Comment on lines 8 to +22
export const API_ENDPOINTS = {
jobs: {
list: `${API_CONFIG.baseURL}/api/jobs`,
detail: (namespace, name) =>
`${API_CONFIG.baseURL}/jobs/${namespace}/${name}`,
list: '/jobs',
detail: (namespace, name) => `/jobs/${namespace}/${name}`,
},
queues: {
list: `${API_CONFIG.baseURL}/api/queues`,
list: '/queues',
},
pods: {
list: `${API_CONFIG.baseURL}/api/pods`,
list: '/pods',
},
podgroups: {
list: `${API_CONFIG.baseURL}/api/podgroups`,
list: '/podgroups',
},
};
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To fully leverage the centralized configuration, we should include all used API paths in API_ENDPOINTS. Additionally, moving encodeURIComponent into these helper functions ensures consistency and prevents bugs where developers might forget to encode URL parameters (as seen in some parts of this PR).

export const API_ENDPOINTS = {
    jobs: {
        list: '/jobs',
        detail: (namespace, name) => `/jobs/${encodeURIComponent(namespace)}/${encodeURIComponent(name)}`,
        yaml: (namespace, name) => `/job/${encodeURIComponent(namespace)}/${encodeURIComponent(name)}/yaml`,
    },
    queues: {
        list: '/queues',
        all: '/all-queues',
        detail: (name) => `/queues/${encodeURIComponent(name)}`,
    },
    pods: {
        list: '/pods',
        yaml: (namespace, name) => `/pod/${encodeURIComponent(namespace)}/${encodeURIComponent(name)}/yaml`,
    },
    namespaces: {
        list: '/namespaces',
    },
    podgroups: {
        list: '/podgroups',
    },
};

Comment thread frontend/src/components/jobs/Jobs.jsx Outdated

try {
const response = await axios.get(`/api/jobs`, {
const response = await apiClient.get("/jobs", {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Use the centralized API_ENDPOINTS constant.

Suggested change
const response = await apiClient.get("/jobs", {
const response = await apiClient.get(API_ENDPOINTS.jobs.list, {

import DashboardHeader from "./DashboardHeader";
import StatCardsContainer from "./StatCardsContainer";
import ChartsContainer from "./ChartsContainer";
import apiClient from "../../config/api";
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Import API_ENDPOINTS to use the centralized path constants.

Suggested change
import apiClient from "../../config/api";
import apiClient, { API_ENDPOINTS } from "../../config/api";

},
},
await apiClient.delete(
`${API_ENDPOINTS.queues.list}/${encodeURIComponent(queueToDelete)}`
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Use the detail helper from API_ENDPOINTS for consistency.

Suggested change
`${API_ENDPOINTS.queues.list}/${encodeURIComponent(queueToDelete)}`
API_ENDPOINTS.queues.detail(queueToDelete)

Comment thread frontend/src/components/utils.js Outdated
@@ -1,15 +1,11 @@
import apiClient from "../config/api";
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Import API_ENDPOINTS to use the centralized path constants.

Suggested change
import apiClient from "../config/api";
import apiClient, { API_ENDPOINTS } from "../config/api";

Comment thread frontend/src/components/utils.js Outdated

const data = await response.json();

const response = await apiClient.get("/namespaces");
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Use the centralized API_ENDPOINTS constant.

Suggested change
const response = await apiClient.get("/namespaces");
const response = await apiClient.get(API_ENDPOINTS.namespaces.list);

Comment thread frontend/src/components/utils.js Outdated

const data = await response.json();

const response = await apiClient.get("/all-queues");
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Use the centralized API_ENDPOINTS constant.

Suggested change
const response = await apiClient.get("/all-queues");
const response = await apiClient.get(API_ENDPOINTS.queues.all);

…omponents

Signed-off-by: gautamsidhwani29 <gautamsidhwani405@gmail.com>
Signed-off-by: gautamsidhwani29 <gautamsidhwani405@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: replace broken API config with centralized axios instance

2 participants