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 the bug when kubeconfig file content is collapsed to one line after workspace restart #1279

Merged
merged 3 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@ export async function exec(
return;
}

let message = Buffer.from(event.data.substr(1), 'base64').toString('utf-8');
message = message.replace(/\n/g, ' ').trim();
const message = Buffer.from(event.data.substr(1), 'base64').toString('utf-8').trim();

if (channel === CHANNELS[CHANNELS.STD_OUT]) {
stdOut += message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import { helpers } from '@eclipse-che/common';
import * as k8s from '@kubernetes/client-node';
import { parse, stringify } from 'yaml';

import { exec, ServerConfig } from '@/devworkspaceClient/services/helpers/exec';
import {
Expand Down Expand Up @@ -215,11 +216,11 @@ export class KubeConfigApiService implements IKubeConfigApi {

private setNamespaceInContext(kubeConfig: string, namespace: string): string {
try {
const kubeConfigJson = JSON.parse(kubeConfig);
for (const context of kubeConfigJson.contexts) {
const kubeConfigYaml = parse(kubeConfig);
for (const context of kubeConfigYaml.contexts) {
context.context.namespace = namespace;
}
return JSON.stringify(kubeConfigJson, undefined, ' ');
return stringify(kubeConfigYaml);
} catch (e) {
logger.error(e, 'Failed to parse kubeconfig');
return kubeConfig;
Expand All @@ -230,31 +231,27 @@ export class KubeConfigApiService implements IKubeConfigApi {
// If the inbounds kubeconfig match the kubeconfig format then merge them
private mergeKubeConfig(kubeconfigSource: string, generatedKubeconfig: string): string {
try {
const kubeConfigJson = JSON.parse(kubeconfigSource);
const generatedKubeConfigJson = JSON.parse(generatedKubeconfig);
for (const context of generatedKubeConfigJson.contexts) {
if (kubeConfigJson.contexts.find((c: any) => c.name === context.name)) {
kubeConfigJson.contexts = kubeConfigJson.contexts.filter(
(c: any) => c.name !== context.name,
);
const kubeConfig = parse(kubeconfigSource);
const generatedKubeConfig = parse(generatedKubeconfig);
for (const context of generatedKubeConfig.contexts) {
if (kubeConfig.contexts.find((c: any) => c.name === context.name)) {
kubeConfig.contexts = kubeConfig.contexts.filter((c: any) => c.name !== context.name);
}
kubeConfigJson.contexts.push(context);
kubeConfig.contexts.push(context);
}
for (const cluster of generatedKubeConfigJson.clusters) {
if (kubeConfigJson.clusters.find((c: any) => c.name === cluster.name)) {
kubeConfigJson.clusters = kubeConfigJson.clusters.filter(
(c: any) => c.name !== cluster.name,
);
for (const cluster of generatedKubeConfig.clusters) {
if (kubeConfig.clusters.find((c: any) => c.name === cluster.name)) {
kubeConfig.clusters = kubeConfig.clusters.filter((c: any) => c.name !== cluster.name);
}
kubeConfigJson.clusters.push(cluster);
kubeConfig.clusters.push(cluster);
}
for (const user of generatedKubeConfigJson.users) {
if (kubeConfigJson.users.find((c: any) => c.name === user.name)) {
kubeConfigJson.users = kubeConfigJson.users.filter((c: any) => c.name !== user.name);
for (const user of generatedKubeConfig.users) {
if (kubeConfig.users.find((c: any) => c.name === user.name)) {
kubeConfig.users = kubeConfig.users.filter((c: any) => c.name !== user.name);
}
kubeConfigJson.users.push(user);
kubeConfig.users.push(user);
}
return JSON.stringify(kubeConfigJson, undefined, ' ');
return stringify(kubeConfig);
} catch (e) {
logger.error(e, 'Failed to merge kubeconfig, returning source');
return kubeconfigSource;
Expand Down
Loading