Skip to content

Commit

Permalink
fix: remove check if the .kube/config exist and prevent replacing or …
Browse files Browse the repository at this point in the history
…reading it (#1153)

* fix: -f check if the file exist, copy paste error

Signed-off-by: Max batleforc <[email protected]>

* feat: add comments to explain what's done after fetching the possible kubeconfig

Signed-off-by: Max batleforc <[email protected]>

---------

Signed-off-by: Max batleforc <[email protected]>
  • Loading branch information
batleforc authored Aug 1, 2024
1 parent b8be17e commit 48e9057
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,15 @@ const spyExec = jest
if (command.some(c => c === 'printenv HOME')) {
// directory where to create the kubeconfig
return mockExecPrintenvHome();
} else if (
command.some(c =>
c.startsWith(`[ -f ${kubeConfigDir}/config ] || cat ${kubeConfigDir}/config`),
)
) {
} else if (command.some(c => c.startsWith(`cat ${kubeConfigDir}/config`))) {
// file empty
return mockExecCatKubeConfig();
} else if (command.some(c => c.startsWith('mkdir -p'))) {
// crete the directory
return Promise.resolve();
} else if (command.some(c => c.startsWith(`[ -f ${homeUserDir}`))) {
} else if (
command.some(c => c.startsWith(`echo '`) && c.endsWith(`' > ${kubeConfigDir}/config`))
) {
// sync config
return Promise.resolve();
}
Expand Down Expand Up @@ -134,7 +132,7 @@ describe('Kubernetes Config API Service', () => {
workspaceName,
namespace,
containerName,
['sh', '-c', `[ -f ${kubeConfigDir}/config ] || cat ${kubeConfigDir}/config`],
['sh', '-c', `cat ${kubeConfigDir}/config`],
expect.anything(),
);

Expand All @@ -144,7 +142,7 @@ describe('Kubernetes Config API Service', () => {
workspaceName,
namespace,
containerName,
['sh', '-c', `[ -f ${kubeConfigDir}/config ] || echo '${config}' > ${kubeConfigDir}/config`],
['sh', '-c', `echo '${config}' > ${kubeConfigDir}/config`],
expect.anything(),
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,15 @@ export class KubeConfigApiService implements IKubeConfigApi {
podName,
namespace,
containerName,
['sh', '-c', `[ -f ${kubeConfigDirectory}/config ] || cat ${kubeConfigDirectory}/config`],
['sh', '-c', `cat ${kubeConfigDirectory}/config`],
this.getServerConfig(),
);

// If there is no kubeconfig in the container, stdOut will be empty and stdError will container the error
if (stdError !== '') {
logger.warn(`Error reading kubeconfig from container: ${stdError}`);
}
// If no error and stdout is not empty, merge the kubeconfig
if (stdError === '' && stdOut !== '') {
kubeConfig = this.mergeKubeConfig(stdOut, kubeConfig);
}
Expand All @@ -104,11 +106,7 @@ export class KubeConfigApiService implements IKubeConfigApi {
podName,
namespace,
containerName,
[
'sh',
'-c',
`[ -f ${kubeConfigDirectory}/config ] || echo '${kubeConfig}' > ${kubeConfigDirectory}/config`,
],
['sh', '-c', `echo '${kubeConfig}' > ${kubeConfigDirectory}/config`],
this.getServerConfig(),
);

Expand Down Expand Up @@ -228,6 +226,8 @@ export class KubeConfigApiService implements IKubeConfigApi {
}
}

// Merge the kubeconfig from the source into the generated kubeconfig
// If the inbounds kubeconfig match the kubeconfig format then merge them
private mergeKubeConfig(kubeconfigSource: string, generatedKubeconfig: string): string {
try {
const kubeConfigJson = JSON.parse(kubeconfigSource);
Expand Down Expand Up @@ -256,7 +256,7 @@ export class KubeConfigApiService implements IKubeConfigApi {
}
return JSON.stringify(kubeConfigJson, undefined, ' ');
} catch (e) {
logger.error(e, 'Failed to merge kubeconfig');
logger.error(e, 'Failed to merge kubeconfig, returning source');
return kubeconfigSource;
}
}
Expand Down

0 comments on commit 48e9057

Please sign in to comment.