| 1 |
/* |
| 2 |
Copyright 2017 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 value |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
"errors" |
| 22 |
"fmt" |
| 23 |
"strings" |
| 24 |
"testing" |
| 25 |
"time" |
| 26 |
|
| 27 |
"google.golang.org/grpc/codes" |
| 28 |
"google.golang.org/grpc/status" |
| 29 |
|
| 30 |
"k8s.io/apiserver/pkg/endpoints/request" |
| 31 |
"k8s.io/component-base/metrics/legacyregistry" |
| 32 |
"k8s.io/component-base/metrics/testutil" |
| 33 |
) |
| 34 |
|
| 35 |
func TestTotals(t *testing.T) { |
| 36 |
nonStatusErr := errors.New("test error") |
| 37 |
failedPreconditionErr := status.Error(codes.FailedPrecondition, "test error") |
| 38 |
internalErr := status.Error(codes.Internal, "test error") |
| 39 |
wrappedErr := fmt.Errorf("some low level thing failed: %w", status.Error(codes.NotFound, "some error")) |
| 40 |
|
| 41 |
nonStatusErrTransformer := PrefixTransformer{Prefix: []byte("k8s:enc:kms:v1:"), Transformer: &testTransformer{err: nonStatusErr}} |
| 42 |
failedPreconditionErrTransformer := PrefixTransformer{Prefix: []byte("k8s:enc:kms:v1:"), Transformer: &testTransformer{err: failedPreconditionErr}} |
| 43 |
internalErrTransformer := PrefixTransformer{Prefix: []byte("k8s:enc:kms:v1:"), Transformer: &testTransformer{err: internalErr}} |
| 44 |
okTransformer := PrefixTransformer{Prefix: []byte("k8s:enc:kms:v1:"), Transformer: &testTransformer{from: []byte("value")}} |
| 45 |
wrappedErrTransformer := PrefixTransformer{Prefix: []byte("k8s:enc:kms:v1:"), Transformer: &testTransformer{err: wrappedErr}} |
| 46 |
|
| 47 |
testCases := []struct { |
| 48 |
desc string |
| 49 |
prefix Transformer |
| 50 |
metrics []string |
| 51 |
want string |
| 52 |
expectErr bool |
| 53 |
}{ |
| 54 |
{ |
| 55 |
desc: "non-status error", |
| 56 |
prefix: NewPrefixTransformers(nil, nonStatusErrTransformer), |
| 57 |
metrics: []string{ |
| 58 |
"apiserver_storage_transformation_operations_total", |
| 59 |
}, |
| 60 |
want: ` |
| 61 |
# HELP apiserver_storage_transformation_operations_total [ALPHA] Total number of transformations. Successful transformation will have a status 'OK' and a varied status string when the transformation fails. The status, resource, and transformation_type fields can be used for alerting purposes. For example, you can monitor for encryption/decryption failures using the transformation_type (e.g., from_storage for decryption and to_storage for encryption). Additionally, these fields can be used to ensure that the correct transformers are applied to each resource. |
| 62 |
# TYPE apiserver_storage_transformation_operations_total counter |
| 63 |
apiserver_storage_transformation_operations_total{resource="test",status="unknown-non-grpc",transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v1:"} 1 |
| 64 |
apiserver_storage_transformation_operations_total{resource="test",status="unknown-non-grpc",transformation_type="to_storage",transformer_prefix="k8s:enc:kms:v1:"} 1 |
| 65 |
`, |
| 66 |
expectErr: true, |
| 67 |
}, |
| 68 |
{ |
| 69 |
desc: "ok", |
| 70 |
prefix: NewPrefixTransformers(nil, okTransformer), |
| 71 |
metrics: []string{ |
| 72 |
"apiserver_storage_transformation_operations_total", |
| 73 |
}, |
| 74 |
want: ` |
| 75 |
# HELP apiserver_storage_transformation_operations_total [ALPHA] Total number of transformations. Successful transformation will have a status 'OK' and a varied status string when the transformation fails. The status, resource, and transformation_type fields can be used for alerting purposes. For example, you can monitor for encryption/decryption failures using the transformation_type (e.g., from_storage for decryption and to_storage for encryption). Additionally, these fields can be used to ensure that the correct transformers are applied to each resource. |
| 76 |
# TYPE apiserver_storage_transformation_operations_total counter |
| 77 |
apiserver_storage_transformation_operations_total{resource="test",status="OK",transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v1:"} 1 |
| 78 |
apiserver_storage_transformation_operations_total{resource="test",status="OK",transformation_type="to_storage",transformer_prefix="k8s:enc:kms:v1:"} 1 |
| 79 |
`, |
| 80 |
expectErr: false, |
| 81 |
}, |
| 82 |
{ |
| 83 |
desc: "failed precondition", |
| 84 |
prefix: NewPrefixTransformers(nil, failedPreconditionErrTransformer), |
| 85 |
metrics: []string{ |
| 86 |
"apiserver_storage_transformation_operations_total", |
| 87 |
}, |
| 88 |
want: ` |
| 89 |
# HELP apiserver_storage_transformation_operations_total [ALPHA] Total number of transformations. Successful transformation will have a status 'OK' and a varied status string when the transformation fails. The status, resource, and transformation_type fields can be used for alerting purposes. For example, you can monitor for encryption/decryption failures using the transformation_type (e.g., from_storage for decryption and to_storage for encryption). Additionally, these fields can be used to ensure that the correct transformers are applied to each resource. |
| 90 |
# TYPE apiserver_storage_transformation_operations_total counter |
| 91 |
apiserver_storage_transformation_operations_total{resource="test",status="FailedPrecondition",transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v1:"} 1 |
| 92 |
apiserver_storage_transformation_operations_total{resource="test",status="FailedPrecondition",transformation_type="to_storage",transformer_prefix="k8s:enc:kms:v1:"} 1 |
| 93 |
`, |
| 94 |
expectErr: true, |
| 95 |
}, |
| 96 |
{ |
| 97 |
desc: "internal", |
| 98 |
prefix: NewPrefixTransformers(nil, internalErrTransformer), |
| 99 |
metrics: []string{ |
| 100 |
"apiserver_storage_transformation_operations_total", |
| 101 |
}, |
| 102 |
want: ` |
| 103 |
# HELP apiserver_storage_transformation_operations_total [ALPHA] Total number of transformations. Successful transformation will have a status 'OK' and a varied status string when the transformation fails. The status, resource, and transformation_type fields can be used for alerting purposes. For example, you can monitor for encryption/decryption failures using the transformation_type (e.g., from_storage for decryption and to_storage for encryption). Additionally, these fields can be used to ensure that the correct transformers are applied to each resource. |
| 104 |
# TYPE apiserver_storage_transformation_operations_total counter |
| 105 |
apiserver_storage_transformation_operations_total{resource="test",status="Internal",transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v1:"} 1 |
| 106 |
apiserver_storage_transformation_operations_total{resource="test",status="Internal",transformation_type="to_storage",transformer_prefix="k8s:enc:kms:v1:"} 1 |
| 107 |
`, |
| 108 |
expectErr: true, |
| 109 |
}, |
| 110 |
{ |
| 111 |
desc: "wrapped not found error", |
| 112 |
prefix: NewPrefixTransformers(nil, wrappedErrTransformer), |
| 113 |
metrics: []string{ |
| 114 |
"apiserver_storage_transformation_operations_total", |
| 115 |
}, |
| 116 |
want: ` |
| 117 |
# HELP apiserver_storage_transformation_operations_total [ALPHA] Total number of transformations. Successful transformation will have a status 'OK' and a varied status string when the transformation fails. The status, resource, and transformation_type fields can be used for alerting purposes. For example, you can monitor for encryption/decryption failures using the transformation_type (e.g., from_storage for decryption and to_storage for encryption). Additionally, these fields can be used to ensure that the correct transformers are applied to each resource. |
| 118 |
# TYPE apiserver_storage_transformation_operations_total counter |
| 119 |
apiserver_storage_transformation_operations_total{resource="test",status="NotFound",transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v1:"} 1 |
| 120 |
apiserver_storage_transformation_operations_total{resource="test",status="NotFound",transformation_type="to_storage",transformer_prefix="k8s:enc:kms:v1:"} 1 |
| 121 |
`, |
| 122 |
expectErr: true, |
| 123 |
}, |
| 124 |
} |
| 125 |
|
| 126 |
RegisterMetrics() |
| 127 |
transformerOperationsTotal.Reset() |
| 128 |
reqCtx := request.WithRequestInfo(context.Background(), &request.RequestInfo{Resource: "test"}) |
| 129 |
|
| 130 |
for _, tt := range testCases { |
| 131 |
t.Run(tt.desc, func(t *testing.T) { |
| 132 |
_, err := tt.prefix.TransformToStorage(reqCtx, []byte("value"), nil) |
| 133 |
if (err != nil) != tt.expectErr { |
| 134 |
t.Fatal(err) |
| 135 |
} |
| 136 |
_, _, err = tt.prefix.TransformFromStorage(reqCtx, []byte("k8s:enc:kms:v1:value"), nil) |
| 137 |
if (err != nil) != tt.expectErr { |
| 138 |
t.Fatal(err) |
| 139 |
} |
| 140 |
defer transformerOperationsTotal.Reset() |
| 141 |
if err := testutil.GatherAndCompare(legacyregistry.DefaultGatherer, strings.NewReader(tt.want), tt.metrics...); err != nil { |
| 142 |
t.Fatal(err) |
| 143 |
} |
| 144 |
}) |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
func TestLatency(t *testing.T) { |
| 149 |
testCases := []struct { |
| 150 |
desc string |
| 151 |
prefix string |
| 152 |
transformationType string |
| 153 |
elapsed time.Duration |
| 154 |
metrics []string |
| 155 |
want string |
| 156 |
}{ |
| 157 |
{ |
| 158 |
desc: "transformation latency", |
| 159 |
prefix: "k8s:enc:kms:v1:", |
| 160 |
transformationType: "from_storage", |
| 161 |
elapsed: time.Duration(10) * time.Second, |
| 162 |
metrics: []string{ |
| 163 |
"apiserver_storage_transformation_duration_seconds", |
| 164 |
}, |
| 165 |
want: ` |
| 166 |
# HELP apiserver_storage_transformation_duration_seconds [ALPHA] Latencies in seconds of value transformation operations. |
| 167 |
# TYPE apiserver_storage_transformation_duration_seconds histogram |
| 168 |
apiserver_storage_transformation_duration_seconds_bucket{transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v1:",le="5e-06"} 0 |
| 169 |
apiserver_storage_transformation_duration_seconds_bucket{transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v1:",le="1e-05"} 0 |
| 170 |
apiserver_storage_transformation_duration_seconds_bucket{transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v1:",le="2e-05"} 0 |
| 171 |
apiserver_storage_transformation_duration_seconds_bucket{transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v1:",le="4e-05"} 0 |
| 172 |
apiserver_storage_transformation_duration_seconds_bucket{transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v1:",le="8e-05"} 0 |
| 173 |
apiserver_storage_transformation_duration_seconds_bucket{transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v1:",le="0.00016"} 0 |
| 174 |
apiserver_storage_transformation_duration_seconds_bucket{transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v1:",le="0.00032"} 0 |
| 175 |
apiserver_storage_transformation_duration_seconds_bucket{transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v1:",le="0.00064"} 0 |
| 176 |
apiserver_storage_transformation_duration_seconds_bucket{transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v1:",le="0.00128"} 0 |
| 177 |
apiserver_storage_transformation_duration_seconds_bucket{transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v1:",le="0.00256"} 0 |
| 178 |
apiserver_storage_transformation_duration_seconds_bucket{transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v1:",le="0.00512"} 0 |
| 179 |
apiserver_storage_transformation_duration_seconds_bucket{transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v1:",le="0.01024"} 0 |
| 180 |
apiserver_storage_transformation_duration_seconds_bucket{transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v1:",le="0.02048"} 0 |
| 181 |
apiserver_storage_transformation_duration_seconds_bucket{transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v1:",le="0.04096"} 0 |
| 182 |
apiserver_storage_transformation_duration_seconds_bucket{transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v1:",le="0.08192"} 0 |
| 183 |
apiserver_storage_transformation_duration_seconds_bucket{transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v1:",le="0.16384"} 0 |
| 184 |
apiserver_storage_transformation_duration_seconds_bucket{transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v1:",le="0.32768"} 0 |
| 185 |
apiserver_storage_transformation_duration_seconds_bucket{transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v1:",le="0.65536"} 0 |
| 186 |
apiserver_storage_transformation_duration_seconds_bucket{transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v1:",le="1.31072"} 0 |
| 187 |
apiserver_storage_transformation_duration_seconds_bucket{transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v1:",le="2.62144"} 0 |
| 188 |
apiserver_storage_transformation_duration_seconds_bucket{transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v1:",le="5.24288"} 0 |
| 189 |
apiserver_storage_transformation_duration_seconds_bucket{transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v1:",le="10.48576"} 1 |
| 190 |
apiserver_storage_transformation_duration_seconds_bucket{transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v1:",le="20.97152"} 1 |
| 191 |
apiserver_storage_transformation_duration_seconds_bucket{transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v1:",le="41.94304"} 1 |
| 192 |
apiserver_storage_transformation_duration_seconds_bucket{transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v1:",le="83.88608"} 1 |
| 193 |
apiserver_storage_transformation_duration_seconds_bucket{transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v1:",le="+Inf"} 1 |
| 194 |
apiserver_storage_transformation_duration_seconds_sum{transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v1:"} 10 |
| 195 |
apiserver_storage_transformation_duration_seconds_count{transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v1:"} 1 |
| 196 |
`, |
| 197 |
}, |
| 198 |
{ |
| 199 |
desc: "transformation latency 2", |
| 200 |
prefix: "k8s:enc:kms:v2:", |
| 201 |
transformationType: "from_storage", |
| 202 |
elapsed: time.Duration(5) * time.Second, |
| 203 |
metrics: []string{ |
| 204 |
"apiserver_storage_transformation_duration_seconds", |
| 205 |
}, |
| 206 |
want: ` |
| 207 |
# HELP apiserver_storage_transformation_duration_seconds [ALPHA] Latencies in seconds of value transformation operations. |
| 208 |
# TYPE apiserver_storage_transformation_duration_seconds histogram |
| 209 |
apiserver_storage_transformation_duration_seconds_bucket{transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v2:",le="5e-06"} 0 |
| 210 |
apiserver_storage_transformation_duration_seconds_bucket{transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v2:",le="1e-05"} 0 |
| 211 |
apiserver_storage_transformation_duration_seconds_bucket{transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v2:",le="2e-05"} 0 |
| 212 |
apiserver_storage_transformation_duration_seconds_bucket{transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v2:",le="4e-05"} 0 |
| 213 |
apiserver_storage_transformation_duration_seconds_bucket{transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v2:",le="8e-05"} 0 |
| 214 |
apiserver_storage_transformation_duration_seconds_bucket{transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v2:",le="0.00016"} 0 |
| 215 |
apiserver_storage_transformation_duration_seconds_bucket{transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v2:",le="0.00032"} 0 |
| 216 |
apiserver_storage_transformation_duration_seconds_bucket{transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v2:",le="0.00064"} 0 |
| 217 |
apiserver_storage_transformation_duration_seconds_bucket{transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v2:",le="0.00128"} 0 |
| 218 |
apiserver_storage_transformation_duration_seconds_bucket{transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v2:",le="0.00256"} 0 |
| 219 |
apiserver_storage_transformation_duration_seconds_bucket{transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v2:",le="0.00512"} 0 |
| 220 |
apiserver_storage_transformation_duration_seconds_bucket{transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v2:",le="0.01024"} 0 |
| 221 |
apiserver_storage_transformation_duration_seconds_bucket{transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v2:",le="0.02048"} 0 |
| 222 |
apiserver_storage_transformation_duration_seconds_bucket{transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v2:",le="0.04096"} 0 |
| 223 |
apiserver_storage_transformation_duration_seconds_bucket{transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v2:",le="0.08192"} 0 |
| 224 |
apiserver_storage_transformation_duration_seconds_bucket{transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v2:",le="0.16384"} 0 |
| 225 |
apiserver_storage_transformation_duration_seconds_bucket{transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v2:",le="0.32768"} 0 |
| 226 |
apiserver_storage_transformation_duration_seconds_bucket{transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v2:",le="0.65536"} 0 |
| 227 |
apiserver_storage_transformation_duration_seconds_bucket{transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v2:",le="1.31072"} 0 |
| 228 |
apiserver_storage_transformation_duration_seconds_bucket{transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v2:",le="2.62144"} 0 |
| 229 |
apiserver_storage_transformation_duration_seconds_bucket{transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v2:",le="5.24288"} 1 |
| 230 |
apiserver_storage_transformation_duration_seconds_bucket{transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v2:",le="10.48576"} 1 |
| 231 |
apiserver_storage_transformation_duration_seconds_bucket{transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v2:",le="20.97152"} 1 |
| 232 |
apiserver_storage_transformation_duration_seconds_bucket{transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v2:",le="41.94304"} 1 |
| 233 |
apiserver_storage_transformation_duration_seconds_bucket{transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v2:",le="83.88608"} 1 |
| 234 |
apiserver_storage_transformation_duration_seconds_bucket{transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v2:",le="+Inf"} 1 |
| 235 |
apiserver_storage_transformation_duration_seconds_sum{transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v2:"} 5 |
| 236 |
apiserver_storage_transformation_duration_seconds_count{transformation_type="from_storage",transformer_prefix="k8s:enc:kms:v2:"} 1 |
| 237 |
`, |
| 238 |
}, |
| 239 |
} |
| 240 |
|
| 241 |
RegisterMetrics() |
| 242 |
transformerLatencies.Reset() |
| 243 |
|
| 244 |
for _, tt := range testCases { |
| 245 |
t.Run(tt.desc, func(t *testing.T) { |
| 246 |
RecordTransformation("", tt.transformationType, tt.prefix, tt.elapsed, nil) |
| 247 |
defer transformerLatencies.Reset() |
| 248 |
if err := testutil.GatherAndCompare(legacyregistry.DefaultGatherer, strings.NewReader(tt.want), tt.metrics...); err != nil { |
| 249 |
t.Fatal(err) |
| 250 |
} |
| 251 |
}) |
| 252 |
} |
| 253 |
} |