Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 2 additions & 40 deletions api/types/load_traffic.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@
package types

import (
"encoding/json"
"fmt"
"strings"

apitypes "k8s.io/apimachinery/pkg/types"
)

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

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

// GetPatchType returns the Kubernetes PatchType for a given patch type string.
// Returns the PatchType and an error if the patch type is invalid.
func GetPatchType(patchType string) (apitypes.PatchType, bool) {
switch patchType {
case "json":
return apitypes.JSONPatchType, true
case "merge":
return apitypes.MergePatchType, true
case "strategic-merge":
return apitypes.StrategicMergePatchType, true
default:
return "", false
}
}

// Validate validates RequestPatch type.
func (r *RequestPatch) Validate() error {
if err := r.KubeGroupVersionResource.Validate(); err != nil {
Expand All @@ -362,24 +339,9 @@ func (r *RequestPatch) Validate() error {
if r.Name == "" {
return fmt.Errorf("name is required")
}
if r.Body == "" {
return fmt.Errorf("body is required")
}

// Validate patch type
_, ok := GetPatchType(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)
if r.Resource == "" {
return fmt.Errorf("resource is required")
}

r.Body = trimmed // Store the trimmed body

return nil
}

Expand Down
9 changes: 0 additions & 9 deletions contrib/internal/manifests/loadprofile/read_update.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,6 @@ loadProfile:
version: v1
resource: configmaps
namespace: default
patchType: merge
name: runkperf-cm-kperf-read-update
keySpaceSize: 100
body: |
{
"metadata": {
"labels": {
"test-label": "mutation-test"
}
}
}
shares: 50
40 changes: 17 additions & 23 deletions request/random.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func NewWeightedRandomRequests(spec *types.LoadProfileSpec) (*WeightedRandomRequ
case r.GetPodLog != nil:
builder = newRequestGetPodLogBuilder(r.GetPodLog, spec.MaxRetries)
case r.Patch != nil:
builder = newRequestPatchBuilder(r.Patch, "", spec.MaxRetries)
builder = newRequestPatchBuilder(r.Patch, spec.MaxRetries)
case r.PostDel != nil:
builder = newRequestPostDelBuilder(r.PostDel, "", spec.MaxRetries)
default:
Expand Down Expand Up @@ -363,33 +363,25 @@ func (b *requestGetPodLogBuilder) Build(cli rest.Interface) Requester {
}

type requestPatchBuilder struct {
version schema.GroupVersion
resource string
resourceVersion string
namespace string
name string
keySpaceSize int
patchType apitypes.PatchType
body interface{}
maxRetries int
version schema.GroupVersion
resource string
namespace string
name string
keySpaceSize int
maxRetries int
}

func newRequestPatchBuilder(src *types.RequestPatch, resourceVersion string, maxRetries int) *requestPatchBuilder {
patchType, _ := types.GetPatchType(src.PatchType)

func newRequestPatchBuilder(src *types.RequestPatch, maxRetries int) *requestPatchBuilder {
return &requestPatchBuilder{
version: schema.GroupVersion{
Group: src.Group,
Version: src.Version,
},
resource: src.Resource,
resourceVersion: resourceVersion,
namespace: src.Namespace,
name: src.Name,
keySpaceSize: src.KeySpaceSize,
patchType: patchType,
body: []byte(src.Body),
maxRetries: maxRetries,
resource: src.Resource,
namespace: src.Namespace,
name: src.Name,
keySpaceSize: src.KeySpaceSize,
maxRetries: maxRetries,
}
}

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

body := fmt.Sprintf(`{"metadata":{"annotations":{"force-update":"%d-%d"}}}`, suffix, time.Now().UnixNano())

return &DiscardRequester{
BaseRequester: BaseRequester{
method: "PATCH",
req: cli.Patch(b.patchType).AbsPath(comps...).
Body(b.body).
req: cli.Patch(apitypes.MergePatchType).AbsPath(comps...).
Body([]byte(body)).
MaxRetries(b.maxRetries),
},
}
Expand Down
Loading