Skip to content

Commit c0a2261

Browse files
committed
Simplify PATCH request configuration
Removed body and type methods from PATCH operations for easier setup. Replaced annotations with timestamps to ensure updates are triggered consistently.
1 parent 14d3322 commit c0a2261

File tree

3 files changed

+19
-72
lines changed

3 files changed

+19
-72
lines changed

api/types/load_traffic.go

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@
44
package types
55

66
import (
7-
"encoding/json"
87
"fmt"
9-
"strings"
10-
11-
apitypes "k8s.io/apimachinery/pkg/types"
128
)
139

1410
// ContentType represents the format of response.
@@ -165,10 +161,6 @@ type RequestPatch struct {
165161
Name string `json:"name" yaml:"name"`
166162
// KeySpaceSize is used to generate random number as name's suffix.
167163
KeySpaceSize int `json:"keySpaceSize" yaml:"keySpaceSize"`
168-
// PatchType is the type of patch, e.g. "json", "merge", "strategic-merge".
169-
PatchType string `json:"patchType" yaml:"patchType"`
170-
// Body is the request body, for fields to be changed.
171-
Body string `json:"body" yaml:"body"`
172164
}
173165

174166
// RequestGetPodLog defines GetLog request for target pod.
@@ -339,21 +331,6 @@ func (m *KubeGroupVersionResource) Validate() error {
339331
return nil
340332
}
341333

342-
// GetPatchType returns the Kubernetes PatchType for a given patch type string.
343-
// Returns the PatchType and an error if the patch type is invalid.
344-
func GetPatchType(patchType string) (apitypes.PatchType, bool) {
345-
switch patchType {
346-
case "json":
347-
return apitypes.JSONPatchType, true
348-
case "merge":
349-
return apitypes.MergePatchType, true
350-
case "strategic-merge":
351-
return apitypes.StrategicMergePatchType, true
352-
default:
353-
return "", false
354-
}
355-
}
356-
357334
// Validate validates RequestPatch type.
358335
func (r *RequestPatch) Validate() error {
359336
if err := r.KubeGroupVersionResource.Validate(); err != nil {
@@ -362,24 +339,9 @@ func (r *RequestPatch) Validate() error {
362339
if r.Name == "" {
363340
return fmt.Errorf("name is required")
364341
}
365-
if r.Body == "" {
366-
return fmt.Errorf("body is required")
367-
}
368-
369-
// Validate patch type
370-
_, ok := GetPatchType(r.PatchType)
371-
if !ok {
372-
return fmt.Errorf("unknown patch type: %s (valid types: json, merge, strategic-merge)", r.PatchType)
373-
}
374-
375-
// Validate JSON body and trim it
376-
trimmed := strings.TrimSpace(r.Body)
377-
if !json.Valid([]byte(trimmed)) {
378-
return fmt.Errorf("invalid JSON in patch body: %q", r.Body)
342+
if r.Resource == "" {
343+
return fmt.Errorf("resource is required")
379344
}
380-
381-
r.Body = trimmed // Store the trimmed body
382-
383345
return nil
384346
}
385347

contrib/internal/manifests/loadprofile/read_update.yaml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,6 @@ loadProfile:
2020
version: v1
2121
resource: configmaps
2222
namespace: default
23-
patchType: merge
2423
name: runkperf-cm-kperf-read-update
2524
keySpaceSize: 100
26-
body: |
27-
{
28-
"metadata": {
29-
"labels": {
30-
"test-label": "mutation-test"
31-
}
32-
}
33-
}
3425
shares: 50

request/random.go

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func NewWeightedRandomRequests(spec *types.LoadProfileSpec) (*WeightedRandomRequ
6161
case r.GetPodLog != nil:
6262
builder = newRequestGetPodLogBuilder(r.GetPodLog, spec.MaxRetries)
6363
case r.Patch != nil:
64-
builder = newRequestPatchBuilder(r.Patch, "", spec.MaxRetries)
64+
builder = newRequestPatchBuilder(r.Patch, spec.MaxRetries)
6565
case r.PostDel != nil:
6666
builder = newRequestPostDelBuilder(r.PostDel, "", spec.MaxRetries)
6767
default:
@@ -363,33 +363,25 @@ func (b *requestGetPodLogBuilder) Build(cli rest.Interface) Requester {
363363
}
364364

365365
type requestPatchBuilder struct {
366-
version schema.GroupVersion
367-
resource string
368-
resourceVersion string
369-
namespace string
370-
name string
371-
keySpaceSize int
372-
patchType apitypes.PatchType
373-
body interface{}
374-
maxRetries int
366+
version schema.GroupVersion
367+
resource string
368+
namespace string
369+
name string
370+
keySpaceSize int
371+
maxRetries int
375372
}
376373

377-
func newRequestPatchBuilder(src *types.RequestPatch, resourceVersion string, maxRetries int) *requestPatchBuilder {
378-
patchType, _ := types.GetPatchType(src.PatchType)
379-
374+
func newRequestPatchBuilder(src *types.RequestPatch, maxRetries int) *requestPatchBuilder {
380375
return &requestPatchBuilder{
381376
version: schema.GroupVersion{
382377
Group: src.Group,
383378
Version: src.Version,
384379
},
385-
resource: src.Resource,
386-
resourceVersion: resourceVersion,
387-
namespace: src.Namespace,
388-
name: src.Name,
389-
keySpaceSize: src.KeySpaceSize,
390-
patchType: patchType,
391-
body: []byte(src.Body),
392-
maxRetries: maxRetries,
380+
resource: src.Resource,
381+
namespace: src.Namespace,
382+
name: src.Name,
383+
keySpaceSize: src.KeySpaceSize,
384+
maxRetries: maxRetries,
393385
}
394386
}
395387

@@ -413,11 +405,13 @@ func (b *requestPatchBuilder) Build(cli rest.Interface) Requester {
413405
finalName := fmt.Sprintf("%s-%d", b.name, suffix)
414406
comps = append(comps, b.resource, finalName)
415407

408+
body := fmt.Sprintf(`{"metadata":{"annotations":{"force-update":"%d-%d"}}}`, suffix, time.Now().UnixNano())
409+
416410
return &DiscardRequester{
417411
BaseRequester: BaseRequester{
418412
method: "PATCH",
419-
req: cli.Patch(b.patchType).AbsPath(comps...).
420-
Body(b.body).
413+
req: cli.Patch(apitypes.MergePatchType).AbsPath(comps...).
414+
Body([]byte(body)).
421415
MaxRetries(b.maxRetries),
422416
},
423417
}

0 commit comments

Comments
 (0)