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
22 changes: 16 additions & 6 deletions api/types/load_traffic.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,12 @@ type RequestPostDel struct {
}

type PostDelPodOptions struct {
// Labels adds metadata labels to created Pods.
Labels map[string]string `json:"labels,omitempty" yaml:"labels,omitempty"`
// ServiceAccountName selects a pre-created ServiceAccount.
ServiceAccountName string `json:"serviceAccountName,omitempty" yaml:"serviceAccountName,omitempty"`
// SchedulerName selects a scheduler; an unhandled name keeps Pods unscheduled.
SchedulerName string `json:"schedulerName,omitempty" yaml:"schedulerName,omitempty"`
// NodeSelector schedules workload Pods separately from runner Pods, for
// example on a dedicated node pool:
//
Expand Down Expand Up @@ -440,14 +446,18 @@ func (r *RequestPostDel) Validate() error {
return fmt.Errorf("payloadSize must >= 0: %v", r.PayloadSize)
}

if r.Pod != nil && len(r.Pod.NodeSelector) > 0 {
if r.Pod != nil {
if r.Group != "" || r.Version != "v1" || r.Resource != "pods" {
return fmt.Errorf("pod nodeSelector only supports core/v1/pods")
return fmt.Errorf("pod options only support core/v1/pods")
}

errs := metav1validation.ValidateLabels(r.Pod.NodeSelector, field.NewPath("pod", "nodeSelector"))
if len(errs) > 0 {
return fmt.Errorf("invalid pod nodeSelector: %v", errs.ToAggregate())
if errs := metav1validation.ValidateLabels(r.Pod.Labels, field.NewPath("pod", "labels")); len(errs) > 0 {
return fmt.Errorf("invalid pod labels: %v", errs.ToAggregate())
}
if len(r.Pod.NodeSelector) > 0 {
Comment thread
liyu-ma marked this conversation as resolved.
errs := metav1validation.ValidateLabels(r.Pod.NodeSelector, field.NewPath("pod", "nodeSelector"))
if len(errs) > 0 {
return fmt.Errorf("invalid pod nodeSelector: %v", errs.ToAggregate())
}
}
}

Expand Down
26 changes: 26 additions & 0 deletions api/types/load_traffic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,32 @@ func TestRequestPostDelNodeSelectorValidation(t *testing.T) {
}
}

func TestRequestPostDelLabelsValidation(t *testing.T) {
for _, tt := range []struct {
name string
labels map[string]string
wantErr bool
}{
{name: "omitted"},
{name: "valid", labels: map[string]string{"example.com/workload": "benchmark", "app": ""}},
{name: "invalid key", labels: map[string]string{"invalid key": "benchmark"}, wantErr: true},
{name: "invalid value", labels: map[string]string{"app": "invalid value"}, wantErr: true},
} {
t.Run(tt.name, func(t *testing.T) {
request := RequestPostDel{
KubeGroupVersionResource: KubeGroupVersionResource{Version: "v1", Resource: "pods"},
Pod: &PostDelPodOptions{Labels: tt.labels},
}
err := request.Validate()
if tt.wantErr {
require.ErrorContains(t, err, "invalid pod labels")
} else {
require.NoError(t, err)
}
})
}
}

func TestRequestPostDelNodeSelectorUnmarshalFromYAML(t *testing.T) {
input := `
version: v1
Expand Down
17 changes: 14 additions & 3 deletions contrib/internal/manifests/workload/pods/templates/pod.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,26 @@
apiVersion: v1
kind: Pod
metadata:
name: {{ $name }}
namespace: {{ $namespace }}
name: {{ printf "%q" $name }}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe we can just use "{{ $value }}" here? The YAML quotes already keep the value as a string, and Kubernetes label values cannot contain characters that need %q escaping.

namespace: {{ printf "%q" $namespace }}
labels:
{{- range $key, $value := .Values.labels }}
{{ printf "%q" $key }}: {{ printf "%q" $value }}
{{- end }}
{{- if eq .Values.labels.app nil }}
app: fake-pod
{{- end }}
spec:
{{- if .Values.serviceAccountName }}
serviceAccountName: {{ printf "%q" .Values.serviceAccountName }}
{{- end }}
{{- if .Values.schedulerName }}
schedulerName: {{ printf "%q" .Values.schedulerName }}
{{- end }}
{{- if $nodeSelector }}
nodeSelector:
{{- range $key, $value := $nodeSelector }}
"{{ $key }}": "{{ $value }}"
{{ printf "%q" $key }}: {{ printf "%q" $value }}
{{- end }}
{{- end }}
# Use pause image so pods reach Running with few events, avoiding an
Expand Down
Loading
Loading