Search code examples
kuberneteskubernetes-servicek3s

Difference between Kubernetes C# client APIs


I have KubernetesClient code running my app on K3s Orchestrator.

I want to understand the difference(use-case) between two K3s APIs PatchNamespacedServiceWithHttpMessagesAsync and ReplaceNamespacedServiceWithHttpMessagesAsync [link to these APIs]. Apart from this link I cant find any place to read about K3s APIs use cases. Please help me here.

PS:

  1. Basically I am trying to update the existing Service, so want to understand the difference between above two APIs, either of which I will be calling with updated Patch body (updated service deployment).
  2. This que is extension of my previous que

Solution

  • TL;DR

    ReplaceNamespacedServiceWithHttpMessagesAsync use PUT HTTP method. PatchNamespacedServiceWithHttpMessagesAsync use PATCH HTTP method.

    PUT method is to update or create a new object. If such an object already exists, all data are updated, if not, a new object is created on the basis of the information sent. The PATCH method, like PUT, is used to update data about an object, but it requires the object to exist. This is because it does not send complete data in the request, but only the data that is to be updated.


    Overall, both APIs are very similar to each other. They only differ in a few places:

    We have a different name in the first line: In the ReplaceNamespacedServiceWithHttpMessagesAsync

    public async Task<HttpOperationResponse<V1Service>> ReplaceNamespacedServiceWithHttpMessagesAsync(
                 V1Service body,
    

    and in the PatchNamespacedServiceWithHttpMessagesAsync:

    public async Task<HttpOperationResponse<V1Service>> PatchNamespacedServiceWithHttpMessagesAsync(
                V1Patch body,
    

    A bool is added to the 7th line in the PatchNamespacedServiceWithHttpMessagesAsync

    bool? force = null,
    

    and in the 36th line:

    tracingParameters.Add("force", force);
    

    Lines 37th for ReplaceNamespacedServiceWithHttpMessagesAsync and 39th for PatchNamespacedServiceWithHttpMessagesAsyncare also different:

    ServiceClientTracing.Enter(_invocationId, this, "ReplaceNamespacedService", tracingParameters);
    

    vs

    ServiceClientTracing.Enter(_invocationId, this, "PatchNamespacedService", tracingParameters);
    

    Then is added a fragment (from 56th to 59th ) line for PatchNamespacedServiceWithHttpMessagesAsync:

    if (force != null)
                 {
                     _queryParameters.Add(string.Format("force={0}", System.Uri.EscapeDataString(SafeJsonConvert.SerializeObject(force, SerializationSettings).Trim('"'))));
                 }
    

    The last and most important difference is the 65th line in the ReplaceNamespacedServiceWithHttpMessagesAsync and 71th line in the PatchNamespacedServiceWithHttpMessagesAsync .

    _httpRequest.Method = HttpMethod.Put;
    

    vs

    _httpRequest.Method = HttpMethod.Patch;