Skip to content

Conversation

vittoriasalim
Copy link
Contributor

@vittoriasalim vittoriasalim commented Jul 16, 2025

This PR introduces a new scenario in kperf to benchmark the latency of PATCH operations on Kubernetes resources. It focuses on measuring the performance impact of partial updates during the resource lifecycle, simulating realistic mutation patterns such as updating metadata and container specs.

Patch API Request

PATCH https://{api-server}/api/v1/namespaces/kperf/pods/test-pod4?timeout=1m0s 200 OK in 589 milliseconds

Patch Use Case

JSON Patch

- patch:
    version: v1
    resource: pods
    namespace: kperf
    patchType: json
    name: test-pod4
    body: |
      [
        {
          "op": "replace",
          "path": "/metadata/labels",
          "value": {}
        }
      ]
  shares: 100
  • Operation: Replaces all labels with an empty map using RFC 6902 JSON Patch.
  • Use Case: Precise, low-overhead field modification; ideal for fine-grained updates.

Merge Patch

- patch:
    version: v1
    resource: pods
    namespace: kperf
    patchType: merge
    name: test-pod4
    body: |
      {
        "metadata": {
          "labels": {
            "test-label": "mutation-test"
          }
        }
      }
  shares: 100
  • Operation: Adds or replaces the metadata.labels field.
  • Use Case: Simple map updates (e.g., labels or annotations); fast and widely supported.

Strategic-Merge Patch

- patch:
    version: v1
    resource: pods
    namespace: kperf
    patchType: strategic-merge
    name: test-pod4
    body: |
      {
        "spec": {
          "containers": [
            {
              "name": "fake-container",
              "image": "busybox:1.36"
            }
          ]
        }
      }
  shares: 100
  • Operation: Updates the container image by identifying the container via its name using Kubernetes' strategic merge strategy.
  • Use Case: Schema-aware patching for built-in resources; safely updates fields inside structured arrays like containers.

Comment on lines +347 to +358
// Validate patch type
_, ok := PatchTypeMapping[r.PatchType]
if !ok {
return fmt.Errorf("unknown patch type: %s (valid types: json, merge, strategic-merge)", r.PatchType)
}

// Validate JSON body and trim it
trimmed := strings.TrimSpace(r.Body)
if !json.Valid([]byte(trimmed)) {
return fmt.Errorf("invalid JSON in patch body: %q", r.Body)
}

Copy link
Contributor Author

@vittoriasalim vittoriasalim Jul 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed #187 (comment) #187 (comment)

Move validation to Request Patch Validate()
Error handling for incorrect patch types. If an invalid patch type is provided, the system will throw following error:

kperf: idx: 0 request: unknown patch type: strategic-mergd (valid types: json, merge, strategic-merge)

@vittoriasalim vittoriasalim requested a review from wonderyl July 21, 2025 03:32
return nil
}

var PatchTypeMapping = map[string]string{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion adding function instead of exported var.

func GetCliJSONPatchType(name string) (string, bool) {
        switch name {
        case "json":
             return "application/json-patch+json", false
        ...
}

You can handle it in the following-up.

@fuweid fuweid merged commit 63a978a into main Jul 22, 2025
4 checks passed
@fuweid fuweid deleted the add_PATCH branch July 22, 2025 19:51
fuweid pushed a commit that referenced this pull request Jul 23, 2025
Addressed
#187 (comment), replace
var with function for consistency as per suggestion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants