-
Notifications
You must be signed in to change notification settings - Fork 309
Add an 'exec' example. Clean up some websockets code. #114
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using k8s; | ||
using k8s.Models; | ||
|
||
namespace exec | ||
{ | ||
internal class Exec | ||
{ | ||
private static async Task Main(string[] args) | ||
{ | ||
var config = KubernetesClientConfiguration.BuildConfigFromConfigFile(); | ||
IKubernetes client = new Kubernetes(config); | ||
Console.WriteLine("Starting Request!"); | ||
|
||
var list = client.ListNamespacedPod("default"); | ||
var pod = list.Items[0]; | ||
await ExecInPod(client, pod); | ||
} | ||
|
||
private async static Task ExecInPod(IKubernetes client, V1Pod pod) { | ||
var webSocket = await client.WebSocketNamespacedPodExecAsync(pod.Metadata.Name, "default", "ls", pod.Spec.Containers[0].Name); | ||
|
||
var demux = new StreamDemuxer(webSocket); | ||
demux.Start(); | ||
|
||
var buff = new byte[4096]; | ||
var stream = demux.GetStream(1, 1); | ||
var read = stream.Read(buff, 0, 4096); | ||
var str = System.Text.Encoding.Default.GetString(buff); | ||
Console.WriteLine(str); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\KubernetesClient.csproj" /> | ||
</ItemGroup> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp2.1</TargetFramework> | ||
<LangVersion>7.1</LangVersion> | ||
</PropertyGroup> | ||
|
||
</Project> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -226,18 +226,20 @@ public partial class Kubernetes | |
|
||
#if NETCOREAPP2_1 | ||
if (this.CaCert != null) | ||
{ | ||
webSocketBuilder.ExpectServerCertificate(this.CaCert); | ||
else | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Aren't these mutually exclusive? I suppose throwing an exception would have been clearer when I first wrote this... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe? I sort of lean on "the customer is always right" or "the caller is always right" If they ship us a config with a ca Cert and noVerifyTLS who are we to judge? We should obey their wishes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah fair enough, that reasoning makes sense to me :) My personal approach to API design is "forgiving, but also willing to point out when the caller may be making a mistake". If someone specifies both But in this case, correct behaviour is dictated by the K8s client config spec which doesn't prevent them specifying both (so I agree with you that there's not much point in stopping them from doing so). |
||
} | ||
if (this.SkipTlsVerify) | ||
{ | ||
webSocketBuilder.SkipServerCertificateValidation(); | ||
|
||
} | ||
webSocketBuilder.Options.RequestedSubProtocols.Add(K8sProtocol.ChannelV1); | ||
#endif // NETCOREAPP2_1 | ||
|
||
// Send Request | ||
cancellationToken.ThrowIfCancellationRequested(); | ||
|
||
WebSocket webSocket = null; | ||
|
||
try | ||
{ | ||
webSocket = await webSocketBuilder.BuildAndConnectAsync(uri, CancellationToken.None).ConfigureAwait(false); | ||
|
@@ -258,7 +260,6 @@ public partial class Kubernetes | |
ServiceClientTracing.Exit(invocationId, null); | ||
} | ||
} | ||
|
||
return webSocket; | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,34 +39,10 @@ public WebSocketBuilder AddClientCertificate(X509Certificate2 certificate) | |
|
||
public WebSocketBuilder ExpectServerCertificate(X509Certificate2 serverCertificate) | ||
{ | ||
Options.ServerCertificateCustomValidationCallback = (sender, certificate, chain, sslPolicyErrors) => | ||
Options.ServerCertificateCustomValidationCallback = (sender, certificate, chain, sslPolicyErrors) => | ||
{ | ||
if (sslPolicyErrors != SslPolicyErrors.RemoteCertificateChainErrors) | ||
{ | ||
return false; | ||
} | ||
|
||
try | ||
{ | ||
using (X509Chain certificateChain = new X509Chain()) | ||
{ | ||
certificateChain.ChainPolicy.ExtraStore.Add(serverCertificate); | ||
certificateChain.ChainPolicy.VerificationFlags = X509VerificationFlags.AllowUnknownCertificateAuthority; | ||
certificateChain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck; | ||
|
||
return certificateChain.Build( | ||
(X509Certificate2)certificate | ||
); | ||
} | ||
} | ||
catch (Exception chainException) | ||
{ | ||
Debug.WriteLine(chainException); | ||
|
||
return false; | ||
} | ||
return Kubernetes.CertificateValidationCallBack(sender, serverCertificate, certificate, chain, sslPolicyErrors); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops! Sorry, must have missed that :) |
||
}; | ||
|
||
return this; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Noted for next time :)