Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix IPv6 Compatibility for Backend #3557

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions backend/src/plugins/kube.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,29 @@ const CONSOLE_CONFIG_YAML_FIELD = 'console-config.yaml';
const kc = new k8s.KubeConfig();
kc.loadFromDefault();

const normalizeServerUrl = (url: string): string => {
const parsedUrl = new URL(url);
const hostname = parsedUrl.hostname;

// Add brackets for IPv6 and remove trailing slashes
const formattedHost = hostname.includes(':') && !hostname.startsWith('[')
? `[${hostname}]`
: hostname;
parsedUrl.hostname = formattedHost;
return parsedUrl.toString().replace(/\/+$/, ''); // Remove trailing slashes
};

// Normalize all cluster server URLs
kc.clusters = kc.clusters.map((cluster) => {
if (cluster.server) {
return {
...cluster,
server: normalizeServerUrl(cluster.server),
};
}
return cluster;
});

const currentContext = kc.getCurrentContext();
const customObjectsApi = kc.makeApiClient(k8s.CustomObjectsApi);
const coreV1Api = kc.makeApiClient(k8s.CoreV1Api);
Expand Down
11 changes: 10 additions & 1 deletion backend/src/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as path from 'path';
import './dotenv';
import { DashboardConfig, KnownLabels, NotebookSize } from '../types';
import os from 'os';

export const PORT = Number(process.env.PORT) || Number(process.env.BACKEND_PORT) || 8080;
export const IP = process.env.IP || '0.0.0.0';
export const LOG_LEVEL = process.env.FASTIFY_LOG_LEVEL || process.env.LOG_LEVEL || 'info';
export const LOG_DIR = path.join(__dirname, '../../../logs');
export const DEV_MODE = process.env.APP_ENV === 'development';
Expand All @@ -17,6 +17,15 @@ export const USER_ACCESS_TOKEN = 'x-forwarded-access-token';
export const yamlRegExp = /\.ya?ml$/;
export const mdRegExp = /\.md$/;

const getDefaultIP = (): string => {
const interfaces = os.networkInterfaces();
const hasIPv6 = Object.values(interfaces).some((iface) =>
iface?.some((address) => address.family === 'IPv6' && !address.internal)
);
return hasIPv6 ? '::' : '0.0.0.0'; // Default to IPv6-capable if available
};
export const IP = process.env.IP || getDefaultIP();

export const IMAGE_ANNOTATIONS = {
DESC: 'opendatahub.io/notebook-image-desc',
DISP_NAME: 'opendatahub.io/notebook-image-name',
Expand Down