|
1 |
| -using k8s; |
| 1 | +using Json.Patch; |
| 2 | + |
| 3 | +using k8s; |
2 | 4 | using k8s.Models;
|
3 | 5 |
|
4 | 6 | using KubeOps.Abstractions.Entities;
|
@@ -320,6 +322,142 @@ Task<TEntity> UpdateStatusAsync<TEntity>(TEntity entity, CancellationToken cance
|
320 | 322 | TEntity UpdateStatus<TEntity>(TEntity entity)
|
321 | 323 | where TEntity : IKubernetesObject<V1ObjectMeta>;
|
322 | 324 |
|
| 325 | + /// <summary> |
| 326 | + /// Patch a given entity on the Kubernetes API by calculating the diff between the current entity and the provided entity. |
| 327 | + /// This method fetches the current entity from the API, computes the patch, and applies it. |
| 328 | + /// </summary> |
| 329 | + /// <typeparam name="TEntity">The type of the Kubernetes entity.</typeparam> |
| 330 | + /// <param name="entity">The entity containing the desired updates.</param> |
| 331 | + /// <param name="cancellationToken">Cancellation token to monitor for cancellation requests.</param> |
| 332 | + /// <returns>The patched entity.</returns> |
| 333 | + /// <exception cref="InvalidOperationException">Thrown if the entity to be patched does not exist on the API.</exception> |
| 334 | + Task<TEntity> PatchAsync<TEntity>(TEntity entity, CancellationToken cancellationToken = default) |
| 335 | + where TEntity : IKubernetesObject<V1ObjectMeta> |
| 336 | + { |
| 337 | + var currentEntity = Get<TEntity>(entity.Name(), entity.Namespace()); |
| 338 | + if (currentEntity is null) |
| 339 | + { |
| 340 | + throw new InvalidOperationException( |
| 341 | + $"Cannot patch entity {typeof(TEntity).Name} with name {entity.Name()} in namespace {entity.Namespace()}: Entity does not exist."); |
| 342 | + } |
| 343 | + |
| 344 | + return PatchAsync( |
| 345 | + currentEntity, |
| 346 | + entity.WithResourceVersion(currentEntity.ResourceVersion()), |
| 347 | + cancellationToken); |
| 348 | + } |
| 349 | + |
| 350 | + /// <summary> |
| 351 | + /// Patch a given entity on the Kubernetes API by calculating the diff between two provided entities. |
| 352 | + /// </summary> |
| 353 | + /// <typeparam name="TEntity">The type of the Kubernetes entity.</typeparam> |
| 354 | + /// <param name="from">The current/original entity.</param> |
| 355 | + /// <param name="to">The updated entity with desired changes.</param> |
| 356 | + /// <param name="cancellationToken">Cancellation token to monitor for cancellation requests.</param> |
| 357 | + /// <returns>The patched entity.</returns> |
| 358 | + Task<TEntity> PatchAsync<TEntity>(TEntity from, TEntity to, CancellationToken cancellationToken = default) |
| 359 | + where TEntity : IKubernetesObject<V1ObjectMeta> => |
| 360 | + PatchAsync(from, from.CreateJsonPatch(to), cancellationToken); |
| 361 | + |
| 362 | + /// <summary> |
| 363 | + /// Patch a given entity on the Kubernetes API using a <see cref="JsonPatch"/> object. |
| 364 | + /// </summary> |
| 365 | + /// <typeparam name="TEntity">The type of the Kubernetes entity.</typeparam> |
| 366 | + /// <param name="entity">The entity to patch.</param> |
| 367 | + /// <param name="patch">The <see cref="JsonPatch"/> representing the changes to apply.</param> |
| 368 | + /// <param name="cancellationToken">Cancellation token to monitor for cancellation requests.</param> |
| 369 | + /// <returns>The patched entity.</returns> |
| 370 | + Task<TEntity> PatchAsync<TEntity>(TEntity entity, JsonPatch patch, CancellationToken cancellationToken = default) |
| 371 | + where TEntity : IKubernetesObject<V1ObjectMeta> => |
| 372 | + PatchAsync(entity, patch.ToKubernetesPatch(), cancellationToken); |
| 373 | + |
| 374 | + /// <summary> |
| 375 | + /// Patch a given entity on the Kubernetes API using a <see cref="V1Patch"/> object. |
| 376 | + /// </summary> |
| 377 | + /// <typeparam name="TEntity">The type of the Kubernetes entity.</typeparam> |
| 378 | + /// <param name="entity">The entity to patch.</param> |
| 379 | + /// <param name="patch">The <see cref="V1Patch"/> representing the changes to apply.</param> |
| 380 | + /// <param name="cancellationToken">Cancellation token to monitor for cancellation requests.</param> |
| 381 | + /// <returns>The patched entity.</returns> |
| 382 | + Task<TEntity> PatchAsync<TEntity>(TEntity entity, V1Patch patch, CancellationToken cancellationToken = default) |
| 383 | + where TEntity : IKubernetesObject<V1ObjectMeta> => |
| 384 | + PatchAsync<TEntity>(patch, entity.Name(), entity.Namespace(), cancellationToken); |
| 385 | + |
| 386 | + /// <summary> |
| 387 | + /// Patch a given entity on the Kubernetes API by name and namespace using a <see cref="V1Patch"/> object. |
| 388 | + /// </summary> |
| 389 | + /// <typeparam name="TEntity">The type of the Kubernetes entity.</typeparam> |
| 390 | + /// <param name="patch">The <see cref="V1Patch"/> representing the changes to apply.</param> |
| 391 | + /// <param name="name">The name of the entity to patch.</param> |
| 392 | + /// <param name="namespace">The namespace of the entity to patch (if applicable).</param> |
| 393 | + /// <param name="cancellationToken">Cancellation token to monitor for cancellation requests.</param> |
| 394 | + /// <returns>The patched entity.</returns> |
| 395 | + Task<TEntity> PatchAsync<TEntity>( |
| 396 | + V1Patch patch, |
| 397 | + string name, |
| 398 | + string? @namespace = null, |
| 399 | + CancellationToken cancellationToken = default) |
| 400 | + where TEntity : IKubernetesObject<V1ObjectMeta>; |
| 401 | + |
| 402 | + /// <summary> |
| 403 | + /// Patch a given entity on the Kubernetes API by calculating the diff between the current entity and the provided entity. |
| 404 | + /// </summary> |
| 405 | + /// <typeparam name="TEntity">The type of the Kubernetes entity.</typeparam> |
| 406 | + /// <param name="entity">The entity containing the desired updates.</param> |
| 407 | + /// <returns>The patched entity.</returns> |
| 408 | + /// <exception cref="InvalidOperationException">Thrown if the entity to be patched does not exist on the API.</exception> |
| 409 | + TEntity Patch<TEntity>(TEntity entity) |
| 410 | + where TEntity : IKubernetesObject<V1ObjectMeta> |
| 411 | + => PatchAsync(entity).GetAwaiter().GetResult(); |
| 412 | + |
| 413 | + /// <summary> |
| 414 | + /// Patch a given entity on the Kubernetes API by calculating the diff between two provided entities. |
| 415 | + /// </summary> |
| 416 | + /// <typeparam name="TEntity">The type of the Kubernetes entity.</typeparam> |
| 417 | + /// <param name="from">The current/original entity.</param> |
| 418 | + /// <param name="to">The updated entity with desired changes.</param> |
| 419 | + /// <returns>The patched entity.</returns> |
| 420 | + TEntity Patch<TEntity>(TEntity from, TEntity to) |
| 421 | + where TEntity : IKubernetesObject<V1ObjectMeta> |
| 422 | + => PatchAsync(from, to).GetAwaiter().GetResult(); |
| 423 | + |
| 424 | + /// <summary> |
| 425 | + /// Patch a given entity on the Kubernetes API using a <see cref="JsonPatch"/> object. |
| 426 | + /// </summary> |
| 427 | + /// <typeparam name="TEntity">The type of the Kubernetes entity.</typeparam> |
| 428 | + /// <param name="entity">The entity to patch.</param> |
| 429 | + /// <param name="patch">The <see cref="JsonPatch"/> representing the changes to apply.</param> |
| 430 | + /// <returns>The patched entity.</returns> |
| 431 | + TEntity Patch<TEntity>(TEntity entity, JsonPatch patch) |
| 432 | + where TEntity : IKubernetesObject<V1ObjectMeta> |
| 433 | + => PatchAsync(entity, patch).GetAwaiter().GetResult(); |
| 434 | + |
| 435 | + /// <summary> |
| 436 | + /// Patch a given entity on the Kubernetes API using a <see cref="V1Patch"/> object. |
| 437 | + /// </summary> |
| 438 | + /// <typeparam name="TEntity">The type of the Kubernetes entity.</typeparam> |
| 439 | + /// <param name="entity">The entity to patch.</param> |
| 440 | + /// <param name="patch">The <see cref="V1Patch"/> representing the changes to apply.</param> |
| 441 | + /// <returns>The patched entity.</returns> |
| 442 | + TEntity Patch<TEntity>(TEntity entity, V1Patch patch) |
| 443 | + where TEntity : IKubernetesObject<V1ObjectMeta> |
| 444 | + => PatchAsync(entity, patch).GetAwaiter().GetResult(); |
| 445 | + |
| 446 | + /// <summary> |
| 447 | + /// Patch a given entity on the Kubernetes API by name and namespace using a <see cref="V1Patch"/> object. |
| 448 | + /// </summary> |
| 449 | + /// <typeparam name="TEntity">The type of the Kubernetes entity.</typeparam> |
| 450 | + /// <param name="patch">The <see cref="V1Patch"/> representing the changes to apply.</param> |
| 451 | + /// <param name="name">The name of the entity to patch.</param> |
| 452 | + /// <param name="namespace">The namespace of the entity to patch (if applicable).</param> |
| 453 | + /// <returns>The patched entity.</returns> |
| 454 | + TEntity Patch<TEntity>( |
| 455 | + V1Patch patch, |
| 456 | + string name, |
| 457 | + string? @namespace = null) |
| 458 | + where TEntity : IKubernetesObject<V1ObjectMeta> |
| 459 | + => PatchAsync<TEntity>(patch, name, @namespace).GetAwaiter().GetResult(); |
| 460 | + |
323 | 461 | /// <inheritdoc cref="Delete{TEntity}(TEntity)"/>
|
324 | 462 | /// <returns>A task that completes when the call was made.</returns>
|
325 | 463 | Task DeleteAsync<TEntity>(TEntity entity, CancellationToken cancellationToken = default)
|
|
0 commit comments