I get "BadRequest" error when executing the command "ConnectGetNamespacedPodExec" through Kubernetes API in a C#.Net console application

6/26/2020

I am working on a console C#.Net tool to access Kubernetes through its API, I was able to connect to the namespaces and the pods but I couldn't execute a command inside a pod. as I am getting the error "BadRequest" when reaching the last line of code, maybe there is something worng with the parameters of the command "ConnectGetNamespacedPodExec". I would appreciate if someone can help me.

KubernetesClientConfiguration myConfig = new KubernetesClientConfiguration();
myConfig.SkipTlsVerify = true;
myConfig.Host = "https://myURL.com";
myConfig.ClientCertificateData = "Removed";
myConfig.ClientCertificateKeyData = "Removed";

var k8sClient = new Kubernetes(myConfig);

//List Namespaces
var NameSpacesList = k8sClient.ListNamespace();
foreach (var item in NameSpacesList.Items)
{
    Console.WriteLine(item.Metadata.Name);
}

//List Pods
var PodsList = k8sClient.ListNamespacedPod(NameSpacesList.Items[5].Metadata.Name); //Karam
foreach (var item in PodsList.Items)
{
    Console.WriteLine(item.Metadata.Name);
}

//This is where the error happens
var str = k8sClient.ConnectGetNamespacedPodExec(name: PodsList.Items[0].Metadata.Name, namespaceParameter: NameSpacesList.Items[5].Metadata.Name, command:"ls", stderr: true, stdin: false, stdout: true, tty: true);
        

The error that I get:

An unhandled exception of type 'Microsoft.Rest.HttpOperationException' occurred in mscorlib.dll .. Additional information: Operation returned an invalid status code 'BadRequest'

Exception Details:

Microsoft.Rest.HttpOperationException was unhandled
  HResult=-2146233088
  Message=Operation returned an invalid status code 'BadRequest'
  Source=KubernetesClient
  StackTrace:
       at k8s.Kubernetes.<ConnectGetNamespacedPodExecWithHttpMessagesAsync>d__88.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
       at k8s.KubernetesExtensions.<ConnectGetNamespacedPodExecAsync>d__121.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
       at k8s.KubernetesExtensions.ConnectGetNamespacedPodExec(IKubernetes operations, String name, String namespaceParameter, String command, String container, Nullable`1 stderr, Nullable`1 stdin, Nullable`1 stdout, Nullable`1 tty)
       at LMT.Program.Main(String[] args) in c:\MK\Projects\LMT\LMT\LMT\Program.cs:line 97
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
-- Mahmoud Karam
.net
api
c#
kubernetes

0 Answers