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

v2.4.0 #142

Merged
merged 6 commits into from
Sep 17, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion src/KubeClient/KubeClientConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static class KubeClientConstants
public const string KubernetesServicePort = "KUBERNETES_SERVICE_PORT";

/// <summary>
/// Default path of mounted volume containing Kubernetes service account token and CA certificate
/// Default path of mounted volume containing Kubernetes service account token, CA certificate, and default namespace.
/// </summary>
public const string DefaultServiceAccountPath = "/var/run/secrets/kubernetes.io/serviceaccount";
}
Expand Down
7 changes: 5 additions & 2 deletions src/KubeClient/KubeClientOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public KubeClientOptions EnsureValid()
/// Create new <see cref="KubeClientOptions"/> using pod-level configuration.
/// </summary>
/// <param name="serviceAccountPath">
/// The location of the volume containing service account token and CA certificate
/// The location of the volume containing service account token, CA certificate, and default namespace.
/// </param>
/// <returns>
/// The configured <see cref="KubeClientOptions"/>.
Expand All @@ -204,6 +204,8 @@ public static KubeClientOptions FromPodServiceAccount(string serviceAccountPath
if (String.IsNullOrWhiteSpace(kubeServiceHost) || String.IsNullOrWhiteSpace(kubeServicePort))
throw new InvalidOperationException($"KubeApiClient.CreateFromPodServiceAccount can only be called when running in a Kubernetes Pod ({KubeClientConstants.KubernetesServiceHost} and/or {KubeClientConstants.KubernetesServicePort} environment variable is not defined).");

string defaultNamespace = File.ReadAllText(Path.Combine(serviceAccountPath, "namespace")).Trim();

string apiEndPoint = $"https://{kubeServiceHost}:{kubeServicePort}/";
string accessToken = File.ReadAllText(Path.Combine(serviceAccountPath, "token"));
var kubeCACertificate = new X509Certificate2(
Expand All @@ -215,7 +217,8 @@ public static KubeClientOptions FromPodServiceAccount(string serviceAccountPath
ApiEndPoint = new Uri(apiEndPoint),
AuthStrategy = KubeAuthStrategy.BearerToken,
AccessToken = accessToken,
CertificationAuthorityCertificate = kubeCACertificate
CertificationAuthorityCertificate = kubeCACertificate,
KubeNamespace = defaultNamespace
};
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/KubeClient/ResourceClients/EventClientV1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ static class Requests
/// <summary>
/// A collection-level Event (v1) request.
/// </summary>
public static readonly HttpRequest Collection = KubeRequest.Create("/api/v1/namespaces/{Namespace}/events?labelSelector={LabelSelector?}&fieldSelector={fieldSelector?}&resourceVersion={ResourceVersion?}");
public static readonly HttpRequest Collection = KubeRequest.Create("/api/v1/namespaces/{Namespace}/events?labelSelector={LabelSelector?}&fieldSelector={FieldSelector?}&resourceVersion={ResourceVersion?}");

/// <summary>
/// A get-by-name Event (v1) request.
Expand All @@ -140,7 +140,7 @@ static class Requests
/// <summary>
/// A collection-level Event watch (v1) request.
/// </summary>
public static readonly HttpRequest WatchCollection = KubeRequest.Create("/api/v1/watch/namespaces/{Namespace}/events?labelSelector={LabelSelector?}&fieldSelector={fieldSelector?}&resourceVersion={ResourceVersion?}");
public static readonly HttpRequest WatchCollection = KubeRequest.Create("/api/v1/watch/namespaces/{Namespace}/events?labelSelector={LabelSelector?}&fieldSelector={FieldSelector?}&resourceVersion={ResourceVersion?}");
}
}

Expand Down
28 changes: 21 additions & 7 deletions src/KubeClient/ResourceClients/PodClientV1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ public async Task<PodV1> Get(string name, string kubeNamespace = null, Cancellat
/// <param name="labelSelector">
/// An optional Kubernetes label selector expression used to filter the Pods.
/// </param>
/// <param name="fieldSelector">
/// An optional Kubernetes field selector expression used to filter the Pods.
/// </param>
/// <param name="kubeNamespace">
/// The target Kubernetes namespace (defaults to <see cref="KubeApiClient.DefaultNamespace"/>).
/// </param>
Expand All @@ -71,13 +74,14 @@ public async Task<PodV1> Get(string name, string kubeNamespace = null, Cancellat
/// <returns>
/// A <see cref="PodListV1"/> containing the Pods.
/// </returns>
public async Task<PodListV1> List(string labelSelector = null, string kubeNamespace = null, CancellationToken cancellationToken = default)
public async Task<PodListV1> List(string labelSelector = null, string fieldSelector = null, string kubeNamespace = null, CancellationToken cancellationToken = default)
{
return await GetResourceList<PodListV1>(
Requests.Collection.WithTemplateParameters(new
{
Namespace = kubeNamespace ?? KubeClient.DefaultNamespace,
LabelSelector = labelSelector
LabelSelector = labelSelector,
FieldSelector = fieldSelector
}),
cancellationToken: cancellationToken
);
Expand All @@ -89,22 +93,26 @@ public async Task<PodListV1> List(string labelSelector = null, string kubeNamesp
/// <param name="labelSelector">
/// An optional Kubernetes label selector expression used to filter the Pods.
/// </param>
/// <param name="fieldSelector">
/// An optional Kubernetes field selector expression used to filter the Pods.
/// </param>
/// <param name="kubeNamespace">
/// The target Kubernetes namespace (defaults to <see cref="KubeApiClient.DefaultNamespace"/>).
/// </param>
/// <returns>
/// An <see cref="IObservable{T}"/> representing the event stream.
/// </returns>
public IObservable<IResourceEventV1<PodV1>> WatchAll(string labelSelector = null, string kubeNamespace = null)
public IObservable<IResourceEventV1<PodV1>> WatchAll(string labelSelector = null, string fieldSelector = null, string kubeNamespace = null)
{
return ObserveEvents<PodV1>(
Requests.Collection.WithTemplateParameters(new
{
Namespace = kubeNamespace ?? KubeClient.DefaultNamespace,
LabelSelector = labelSelector,
FieldSelector = fieldSelector,
Watch = true
}),
operationDescription: $"watch all v1/Pods with label selector '{labelSelector ?? "<none>"}' in namespace {kubeNamespace ?? KubeClient.DefaultNamespace}"
operationDescription: $"watch all v1/Pods with label selector '{labelSelector ?? "<none>"}' and field selector '{fieldSelector ?? "<none>"}' in namespace {kubeNamespace ?? KubeClient.DefaultNamespace}"
);
}

Expand Down Expand Up @@ -270,7 +278,7 @@ public static class Requests
/// <summary>
/// A collection-level Pod (v1) request.
/// </summary>
public static readonly HttpRequest Collection = KubeRequest.Create("api/v1/namespaces/{Namespace}/pods?labelSelector={LabelSelector?}&watch={Watch?}");
public static readonly HttpRequest Collection = KubeRequest.Create("api/v1/namespaces/{Namespace}/pods?labelSelector={LabelSelector?}&fieldSelector={FieldSelector?}&watch={Watch?}");

/// <summary>
/// A get-by-name Pod (v1) request.
Expand Down Expand Up @@ -313,6 +321,9 @@ public interface IPodClientV1
/// <param name="labelSelector">
/// An optional Kubernetes label selector expression used to filter the Pods.
/// </param>
/// <param name="fieldSelector">
/// An optional Kubernetes field selector expression used to filter the Pods.
/// </param>
/// <param name="kubeNamespace">
/// The target Kubernetes namespace (defaults to <see cref="KubeApiClient.DefaultNamespace"/>).
/// </param>
Expand All @@ -322,21 +333,24 @@ public interface IPodClientV1
/// <returns>
/// A <see cref="PodListV1"/> containing the Pods.
/// </returns>
Task<PodListV1> List(string labelSelector = null, string kubeNamespace = null, CancellationToken cancellationToken = default);
Task<PodListV1> List(string labelSelector = null, string fieldSelector = null, string kubeNamespace = null, CancellationToken cancellationToken = default);

/// <summary>
/// Watch for events relating to Pods.
/// </summary>
/// <param name="labelSelector">
/// An optional Kubernetes label selector expression used to filter the Pods.
/// </param>
/// <param name="fieldSelector">
/// An optional Kubernetes field selector expression used to filter the Pods.
/// </param>
/// <param name="kubeNamespace">
/// The target Kubernetes namespace (defaults to <see cref="KubeApiClient.DefaultNamespace"/>).
/// </param>
/// <returns>
/// An <see cref="IObservable{T}"/> representing the event stream.
/// </returns>
IObservable<IResourceEventV1<PodV1>> WatchAll(string labelSelector = null, string kubeNamespace = null);
IObservable<IResourceEventV1<PodV1>> WatchAll(string labelSelector = null, string fieldSelector = null, string kubeNamespace = null);

/// <summary>
/// Get the combined logs for the Pod with the specified name.
Expand Down