-
Notifications
You must be signed in to change notification settings - Fork 538
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
cp.cpToPod() does not work #982
Comments
I am facing the same issue. In my case, the js code running this library is my local system and the target pod is on minikube. I have not tested this when both the js code running this library is also on k8s. Anyway, an observation I have made is that Line 81 in 6aa7539
The last line doesn't call exec with an await . Since the function returns void, the calling function of cpToPod has no way to wait for the copy to be completed as this.execInstance.exec() will return immediately.
When I test locally by adding an |
@vip30 kindly suggest any changes? |
@w3ichen I think I have figured out a reason why this could happen. After much trial and error, I noticed that the copy was actually working if I wait for the pod to get into the Maybe the maintainers would be able to confirm if my observations are correct. EDIT: Upon further debugging the above project, I see that the items are not copied until the program exits. I have put a breakpoint in |
I think I have found out why the program wants to exit before the copying is completed. I created the above PR to fix that. @w3ichen maybe you can check that PR and see if that works for you. |
@SayakMukhopadhyay I tested your PR and it works for me. The way you're doing it by listening to when the websocket closes seems like the only way right now because the exec will resolve once the websocket is established and it doesn't call any callback functions. |
I'm closing this issue now because the In order for
Improvements:
import * as k8s from "@kubernetes/client-node";
import { Cp } from "@kubernetes/client-node";
import { WritableStreamBuffer } from "stream-buffers";
import { Callback, Headers, pack } from "tar-stream";
export type TarEntry = [Headers, string, Callback?];
export class CustomCp extends Cp {
public async cpFromPodToString(namespace: string, podName: string, containerName: string, srcPath: string): Promise<string> {
return new Promise((resolve, reject) => {
const command = ["cat", srcPath];
const writerStream = new WritableStreamBuffer();
const errStream = new WritableStreamBuffer();
this.execInstance.exec(
namespace,
podName,
containerName,
command,
writerStream,
errStream,
null,
false,
async ({ status }) => {
if (status === "Failure" || errStream.size())
reject(`Error from cpFromPodToString - details: \n ${errStream.getContentsAsString()}`);
resolve(writerStream.getContentsAsString() || "");
}
);
});
}
public async cpStringToPod(
namespace: string,
podName: string,
containerName: string,
srcFiles: TarEntry[],
tgtPath: string
): Promise<void> {
const readStream = pack();
srcFiles.forEach((tarEntry) => {
readStream.entry(...tarEntry);
});
readStream.finalize();
const command = ["tar", "xf", "-", "-C", tgtPath];
const errStream = new WritableStreamBuffer();
const conn = await this.execInstance.exec(
namespace,
podName,
containerName,
command,
null,
errStream,
readStream,
false,
async ({ status }) => {
// Does not reach here for unknown reasons
if (status === "Failure" || errStream.size()) {
throw new Error(`Error from cpStringToPod - details: \n ${errStream.getContentsAsString()}`);
}
}
);
return new Promise((resolve) => {
conn.onclose = (event) => {
resolve();
};
});
}
}
const kc = new k8s.KubeConfig();
kc.loadFromDefault();
const exec = new k8s.Exec(kc);
export const cp = new CustomCp(kc, exec); |
@w3ichen Could you please keep the issue opened since I have the same issue and I want to ensure that the maintainers see this as an open item and not something that has been "fixed". |
The Kubernetes project currently lacks enough contributors to adequately respond to all issues. This bot triages un-triaged issues according to the following rules:
You can:
Please send feedback to sig-contributor-experience at kubernetes/community. /lifecycle stale |
/remove-lifecycle stale |
The Kubernetes project currently lacks enough contributors to adequately respond to all issues. This bot triages un-triaged issues according to the following rules:
You can:
Please send feedback to sig-contributor-experience at kubernetes/community. /lifecycle stale |
The Kubernetes project currently lacks enough active contributors to adequately respond to all issues. This bot triages un-triaged issues according to the following rules:
You can:
Please send feedback to sig-contributor-experience at kubernetes/community. /lifecycle rotten |
Describe the bug
cp.cpToPod()
does not actually copy the file to the pod. No error is raised and the function resolves, that is, there is no indication that the copying had failed. However, the file does not exist in the pod.After inspecting the source code, I found that the function resolves because a
Promise
is not used.I found that the root cause is because the
const command = ['tar', 'xf', '-', '-C', tgtPath];
hangs forever and displays no errors. It seems to me that the commandtar xf -C
is waiting for something and hangs infinitely.Client Version
"@kubernetes/client-node": "^0.18.0"
To Reproduce
nginx-4217019353-9gl4s
indefault
namespace with container callednginx
.Expected behavior
I expect the file to be copied into the pod, and for the file to exist in the pod.
Environment:
Additional context
I'm currently using a hack to work around this by spawning a process to call
kubectl cp
.However, it's not ideal since it depends on the
kubectl
command line tool:The text was updated successfully, but these errors were encountered: