Skip to content

Commit 40154ba

Browse files
committed
Add updateAnyOp to scheduler_perf
1 parent 5fc4e71 commit 40154ba

File tree

3 files changed

+187
-14
lines changed

3 files changed

+187
-14
lines changed

test/integration/scheduler_perf/create.go

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -104,22 +104,13 @@ func (c *createAny) create(tCtx ktesting.TContext, env map[string]any) {
104104

105105
// Not caching the discovery result isn't very efficient, but good enough when
106106
// createAny isn't done often.
107-
discoveryCache := memory.NewMemCacheClient(tCtx.Client().Discovery())
108-
restMapper := restmapper.NewDeferredDiscoveryRESTMapper(discoveryCache)
109-
gv, err := schema.ParseGroupVersion(obj.GetAPIVersion())
107+
mapping, err := restMappingFromUnstructuredObj(tCtx, obj)
110108
if err != nil {
111-
tCtx.Fatalf("%s: extract group+version from object %q: %v", c.TemplatePath, klog.KObj(obj), err)
109+
tCtx.Fatalf("%s: %v", c.TemplatePath, err)
112110
}
113-
gk := schema.GroupKind{Group: gv.Group, Kind: obj.GetKind()}
111+
resourceClient := tCtx.Dynamic().Resource(mapping.Resource)
114112

115113
create := func() error {
116-
mapping, err := restMapper.RESTMapping(gk, gv.Version)
117-
if err != nil {
118-
// Cached mapping might be stale, refresh on next try.
119-
restMapper.Reset()
120-
return fmt.Errorf("map %q to resource: %v", gk, err)
121-
}
122-
resourceClient := tCtx.Dynamic().Resource(mapping.Resource)
123114
options := metav1.CreateOptions{
124115
// If the YAML input is invalid, then we want the
125116
// apiserver to tell us via an error. This can
@@ -129,12 +120,12 @@ func (c *createAny) create(tCtx ktesting.TContext, env map[string]any) {
129120
}
130121
if c.Namespace != "" {
131122
if mapping.Scope.Name() != meta.RESTScopeNameNamespace {
132-
return fmt.Errorf("namespace %q set for %q, but %q has scope %q", c.Namespace, c.TemplatePath, gk, mapping.Scope.Name())
123+
return fmt.Errorf("namespace %q set for %q, but %q has scope %q", c.Namespace, c.TemplatePath, mapping.GroupVersionKind, mapping.Scope.Name())
133124
}
134125
_, err = resourceClient.Namespace(c.Namespace).Create(tCtx, obj, options)
135126
} else {
136127
if mapping.Scope.Name() != meta.RESTScopeNameRoot {
137-
return fmt.Errorf("namespace not set for %q, but %q has scope %q", c.TemplatePath, gk, mapping.Scope.Name())
128+
return fmt.Errorf("namespace not set for %q, but %q has scope %q", c.TemplatePath, mapping.GroupVersionKind, mapping.Scope.Name())
138129
}
139130
_, err = resourceClient.Create(tCtx, obj, options)
140131
}
@@ -175,3 +166,21 @@ func getSpecFromTextTemplateFile(path string, env map[string]any, spec interface
175166

176167
return yaml.UnmarshalStrict(buffer.Bytes(), spec)
177168
}
169+
170+
func restMappingFromUnstructuredObj(tCtx ktesting.TContext, obj *unstructured.Unstructured) (*meta.RESTMapping, error) {
171+
discoveryCache := memory.NewMemCacheClient(tCtx.Client().Discovery())
172+
restMapper := restmapper.NewDeferredDiscoveryRESTMapper(discoveryCache)
173+
gv, err := schema.ParseGroupVersion(obj.GetAPIVersion())
174+
if err != nil {
175+
return nil, fmt.Errorf("extract group+version from object %q: %w", klog.KObj(obj), err)
176+
}
177+
gk := schema.GroupKind{Group: gv.Group, Kind: obj.GetKind()}
178+
179+
mapping, err := restMapper.RESTMapping(gk, gv.Version)
180+
if err != nil {
181+
// Cached mapping might be stale, refresh on next try.
182+
restMapper.Reset()
183+
return nil, fmt.Errorf("failed mapping %q to resource: %w", gk, err)
184+
}
185+
return mapping, nil
186+
}

test/integration/scheduler_perf/scheduler_perf.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ const (
8686
createResourceClaimsOpcode operationCode = "createResourceClaims"
8787
createResourceDriverOpcode operationCode = "createResourceDriver"
8888
churnOpcode operationCode = "churn"
89+
updateAnyOpcode operationCode = "updateAny"
8990
barrierOpcode operationCode = "barrier"
9091
sleepOpcode operationCode = "sleep"
9192
startCollectingMetricsOpcode operationCode = "startCollectingMetrics"
@@ -437,6 +438,7 @@ func (op *op) UnmarshalJSON(b []byte) error {
437438
createResourceClaimsOpcode: &createResourceClaimsOp{},
438439
createResourceDriverOpcode: &createResourceDriverOp{},
439440
churnOpcode: &churnOp{},
441+
updateAnyOpcode: &updateAny{},
440442
barrierOpcode: &barrierOp{},
441443
sleepOpcode: &sleepOp{},
442444
startCollectingMetricsOpcode: &startCollectingMetricsOp{},
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
/*
2+
Copyright 2024 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package benchmark
18+
19+
import (
20+
"fmt"
21+
"time"
22+
23+
"k8s.io/apimachinery/pkg/api/meta"
24+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
26+
"k8s.io/kubernetes/test/utils/ktesting"
27+
"k8s.io/utils/ptr"
28+
)
29+
30+
// updateAny defines an op where some object gets updated from a YAML file.
31+
// The nameset can be specified.
32+
type updateAny struct {
33+
// Must match updateAny.
34+
Opcode operationCode
35+
// Namespace the object should be updated in. Must be empty for cluster-scoped objects.
36+
Namespace string
37+
// Path to spec file describing the object to update.
38+
// This will be processed with text/template.
39+
// .Index will be in the range [0, Count-1] when updating
40+
// more than one object. .Count is the total number of objects.
41+
TemplatePath string
42+
// Count determines how many objects get updated. Defaults to 1 if unset.
43+
Count *int
44+
// Template parameter for Count.
45+
CountParam string
46+
// Number of objects to be updated per second.
47+
// If set to 0, all objects are updated at once.
48+
// Optional
49+
UpdatePerSecond int
50+
// Internal field of the struct used for caching the mapping.
51+
cachedMapping *meta.RESTMapping
52+
}
53+
54+
var _ runnableOp = &updateAny{}
55+
56+
func (c *updateAny) isValid(allowParameterization bool) error {
57+
if c.TemplatePath == "" {
58+
return fmt.Errorf("TemplatePath must be set")
59+
}
60+
if c.UpdatePerSecond < 0 {
61+
return fmt.Errorf("invalid UpdatePerSecond=%d; should be non-negative", c.UpdatePerSecond)
62+
}
63+
// The namespace can only be checked during later because we don't know yet
64+
// whether the object is namespaced or cluster-scoped.
65+
return nil
66+
}
67+
68+
func (c *updateAny) collectsMetrics() bool {
69+
return false
70+
}
71+
72+
func (c updateAny) patchParams(w *workload) (realOp, error) {
73+
if c.CountParam != "" {
74+
count, err := w.Params.get(c.CountParam[1:])
75+
if err != nil {
76+
return nil, err
77+
}
78+
c.Count = ptr.To(count)
79+
}
80+
c.cachedMapping = nil
81+
return &c, c.isValid(false)
82+
}
83+
84+
func (c *updateAny) requiredNamespaces() []string {
85+
if c.Namespace == "" {
86+
return nil
87+
}
88+
return []string{c.Namespace}
89+
}
90+
91+
func (c *updateAny) run(tCtx ktesting.TContext) {
92+
count := 1
93+
if c.Count != nil {
94+
count = *c.Count
95+
}
96+
97+
if c.UpdatePerSecond == 0 {
98+
for index := 0; index < count; index++ {
99+
err := c.update(tCtx, map[string]any{"Index": index, "Count": count})
100+
if err != nil {
101+
tCtx.Fatalf("Failed to update object: %w", err)
102+
}
103+
}
104+
return
105+
}
106+
107+
ticker := time.NewTicker(time.Second / time.Duration(c.UpdatePerSecond))
108+
defer ticker.Stop()
109+
for index := 0; index < count; index++ {
110+
select {
111+
case <-ticker.C:
112+
err := c.update(tCtx, map[string]any{"Index": index, "Count": count})
113+
if err != nil {
114+
tCtx.Fatalf("Failed to update object: %w", err)
115+
}
116+
case <-tCtx.Done():
117+
return
118+
}
119+
}
120+
}
121+
122+
func (c *updateAny) update(tCtx ktesting.TContext, env map[string]any) error {
123+
var obj *unstructured.Unstructured
124+
if err := getSpecFromTextTemplateFile(c.TemplatePath, env, &obj); err != nil {
125+
return fmt.Errorf("%s: parsing failed: %w", c.TemplatePath, err)
126+
}
127+
128+
if c.cachedMapping == nil {
129+
mapping, err := restMappingFromUnstructuredObj(tCtx, obj)
130+
if err != nil {
131+
return err
132+
}
133+
c.cachedMapping = mapping
134+
}
135+
resourceClient := tCtx.Dynamic().Resource(c.cachedMapping.Resource)
136+
137+
options := metav1.UpdateOptions{
138+
// If the YAML input is invalid, then we want the
139+
// apiserver to tell us via an error. This can
140+
// happen because decoding into an unstructured object
141+
// doesn't validate.
142+
FieldValidation: "Strict",
143+
}
144+
if c.Namespace != "" {
145+
if c.cachedMapping.Scope.Name() != meta.RESTScopeNameNamespace {
146+
return fmt.Errorf("namespace %q set for %q, but %q has scope %q", c.Namespace, c.TemplatePath, c.cachedMapping.GroupVersionKind, c.cachedMapping.Scope.Name())
147+
}
148+
_, err := resourceClient.Namespace(c.Namespace).Update(tCtx, obj, options)
149+
if err != nil {
150+
return fmt.Errorf("failed to update object in namespace %q: %w", c.Namespace, err)
151+
}
152+
return nil
153+
}
154+
if c.cachedMapping.Scope.Name() != meta.RESTScopeNameRoot {
155+
return fmt.Errorf("namespace not set for %q, but %q has scope %q", c.TemplatePath, c.cachedMapping.GroupVersionKind, c.cachedMapping.Scope.Name())
156+
}
157+
_, err := resourceClient.Update(tCtx, obj, options)
158+
if err != nil {
159+
return fmt.Errorf("failed to update object: %w", err)
160+
}
161+
return nil
162+
}

0 commit comments

Comments
 (0)