|
| 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