|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the MIT License. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Net.Http; |
| 6 | +using System.Threading; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Newtonsoft.Json; |
| 9 | + |
| 10 | +namespace Microsoft.Azure.WebJobs.Script |
| 11 | +{ |
| 12 | + internal class KubernetesClient |
| 13 | + { |
| 14 | + private const int LeaseRenewDeadline = 10; |
| 15 | + private readonly HttpClient _httpClient; |
| 16 | + private readonly string _httpLeaderEndpoint; |
| 17 | + |
| 18 | + internal KubernetesClient(IEnvironment environment, HttpClient httpClient = null) |
| 19 | + { |
| 20 | + _httpClient = httpClient ?? new HttpClient(); |
| 21 | + _httpLeaderEndpoint = environment.GetHttpLeaderEndpoint(); |
| 22 | + } |
| 23 | + |
| 24 | + internal async Task<KubernetesLockHandle> GetLock(string lockName, CancellationToken cancellationToken) |
| 25 | + { |
| 26 | + if (string.IsNullOrEmpty(lockName)) |
| 27 | + { |
| 28 | + throw new ArgumentNullException(nameof(lockName)); |
| 29 | + } |
| 30 | + |
| 31 | + var request = new HttpRequestMessage() |
| 32 | + { |
| 33 | + Method = HttpMethod.Get, |
| 34 | + RequestUri = GetRequestUri($"?name={lockName}") |
| 35 | + }; |
| 36 | + |
| 37 | + var response = await _httpClient.SendAsync(request, cancellationToken); |
| 38 | + |
| 39 | + response.EnsureSuccessStatusCode(); |
| 40 | + |
| 41 | + var responseString = await response.Content.ReadAsStringAsync(); |
| 42 | + return JsonConvert.DeserializeObject<KubernetesLockHandle>(responseString); |
| 43 | + } |
| 44 | + |
| 45 | + internal async Task<KubernetesLockHandle> TryAcquireLock (string lockId, string ownerId, TimeSpan lockPeriod, CancellationToken cancellationToken) |
| 46 | + { |
| 47 | + if (string.IsNullOrEmpty(lockId)) |
| 48 | + { |
| 49 | + throw new ArgumentNullException(nameof(lockId)); |
| 50 | + } |
| 51 | + |
| 52 | + if (string.IsNullOrEmpty(ownerId)) |
| 53 | + { |
| 54 | + throw new ArgumentNullException(nameof(ownerId)); |
| 55 | + } |
| 56 | + |
| 57 | + var lockHandle = new KubernetesLockHandle(); |
| 58 | + var request = new HttpRequestMessage() |
| 59 | + { |
| 60 | + Method = HttpMethod.Post, |
| 61 | + RequestUri = GetRequestUri($"/acquire?name={lockId}&owner={ownerId}&duration={lockPeriod.TotalSeconds}&renewDeadline={LeaseRenewDeadline}"), |
| 62 | + }; |
| 63 | + |
| 64 | + var response = await _httpClient.SendAsync(request, cancellationToken); |
| 65 | + if (response.IsSuccessStatusCode) |
| 66 | + { |
| 67 | + lockHandle.LockId = lockId; |
| 68 | + lockHandle.Owner = ownerId; |
| 69 | + } |
| 70 | + return lockHandle; |
| 71 | + } |
| 72 | + |
| 73 | + internal async Task<HttpResponseMessage> ReleaseLock(string lockId, string ownerId) |
| 74 | + { |
| 75 | + if (string.IsNullOrEmpty(lockId)) |
| 76 | + { |
| 77 | + throw new ArgumentNullException(nameof(lockId)); |
| 78 | + } |
| 79 | + |
| 80 | + var request = new HttpRequestMessage() |
| 81 | + { |
| 82 | + Method = HttpMethod.Post, |
| 83 | + RequestUri = GetRequestUri($"/release?name={lockId}&owner={ownerId}") |
| 84 | + }; |
| 85 | + |
| 86 | + return await _httpClient.SendAsync(request); |
| 87 | + } |
| 88 | + |
| 89 | + internal Uri GetRequestUri(string requestStem) |
| 90 | + { |
| 91 | + return new Uri($"{_httpLeaderEndpoint}/lock{requestStem}"); |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments