| 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 |
"bytes" |
| 21 |
"context" |
| 22 |
"errors" |
| 23 |
"flag" |
| 24 |
"fmt" |
| 25 |
"regexp" |
| 26 |
"strings" |
| 27 |
"testing" |
| 28 |
|
| 29 |
genericapirequest "k8s.io/apiserver/pkg/endpoints/request" |
| 30 |
"k8s.io/component-base/metrics/legacyregistry" |
| 31 |
"k8s.io/component-base/metrics/testutil" |
| 32 |
"k8s.io/klog/v2" |
| 33 |
) |
| 34 |
|
| 35 |
type testTransformer struct { |
| 36 |
from, to []byte |
| 37 |
err error |
| 38 |
stale bool |
| 39 |
receivedFrom, receivedTo []byte |
| 40 |
} |
| 41 |
|
| 42 |
func (t *testTransformer) TransformFromStorage(ctx context.Context, data []byte, dataCtx Context) (out []byte, stale bool, err error) { |
| 43 |
t.receivedFrom = data |
| 44 |
return t.from, t.stale, t.err |
| 45 |
} |
| 46 |
|
| 47 |
func (t *testTransformer) TransformToStorage(ctx context.Context, data []byte, dataCtx Context) (out []byte, err error) { |
| 48 |
t.receivedTo = data |
| 49 |
return t.to, t.err |
| 50 |
} |
| 51 |
|
| 52 |
func TestPrefixFrom(t *testing.T) { |
| 53 |
testErr := fmt.Errorf("test error") |
| 54 |
transformErr := fmt.Errorf("transform error") |
| 55 |
transformers := []PrefixTransformer{ |
| 56 |
{Prefix: []byte("first:"), Transformer: &testTransformer{from: []byte("value1")}}, |
| 57 |
{Prefix: []byte("second:"), Transformer: &testTransformer{from: []byte("value2")}}, |
| 58 |
{Prefix: []byte("fails:"), Transformer: &testTransformer{err: transformErr}}, |
| 59 |
{Prefix: []byte("stale:"), Transformer: &testTransformer{from: []byte("value3"), stale: true}}, |
| 60 |
} |
| 61 |
p := NewPrefixTransformers(testErr, transformers...) |
| 62 |
|
| 63 |
testCases := []struct { |
| 64 |
input []byte |
| 65 |
expect []byte |
| 66 |
stale bool |
| 67 |
err error |
| 68 |
match int |
| 69 |
}{ |
| 70 |
{[]byte("first:value"), []byte("value1"), false, nil, 0}, |
| 71 |
{[]byte("second:value"), []byte("value2"), true, nil, 1}, |
| 72 |
{[]byte("third:value"), nil, false, testErr, -1}, |
| 73 |
{[]byte("fails:value"), nil, false, transformErr, 2}, |
| 74 |
{[]byte("stale:value"), []byte("value3"), true, nil, 3}, |
| 75 |
} |
| 76 |
reqCtx := genericapirequest.WithRequestInfo(context.Background(), &genericapirequest.RequestInfo{Resource: "test"}) |
| 77 |
for i, test := range testCases { |
| 78 |
got, stale, err := p.TransformFromStorage(reqCtx, test.input, nil) |
| 79 |
if err != test.err || stale != test.stale || !bytes.Equal(got, test.expect) { |
| 80 |
t.Errorf("%d: unexpected out: %q %t %#v", i, string(got), stale, err) |
| 81 |
continue |
| 82 |
} |
| 83 |
if test.match != -1 && !bytes.Equal([]byte("value"), transformers[test.match].Transformer.(*testTransformer).receivedFrom) { |
| 84 |
t.Errorf("%d: unexpected value received by transformer: %s", i, transformers[test.match].Transformer.(*testTransformer).receivedFrom) |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
func TestPrefixTo(t *testing.T) { |
| 90 |
testErr := fmt.Errorf("test error") |
| 91 |
transformErr := fmt.Errorf("test error") |
| 92 |
testCases := []struct { |
| 93 |
transformers []PrefixTransformer |
| 94 |
expect []byte |
| 95 |
err error |
| 96 |
}{ |
| 97 |
{[]PrefixTransformer{{Prefix: []byte("first:"), Transformer: &testTransformer{to: []byte("value1")}}}, []byte("first:value1"), nil}, |
| 98 |
{[]PrefixTransformer{{Prefix: []byte("second:"), Transformer: &testTransformer{to: []byte("value2")}}}, []byte("second:value2"), nil}, |
| 99 |
{[]PrefixTransformer{{Prefix: []byte("fails:"), Transformer: &testTransformer{err: transformErr}}}, nil, transformErr}, |
| 100 |
} |
| 101 |
reqCtx := genericapirequest.WithRequestInfo(context.Background(), &genericapirequest.RequestInfo{Resource: "test"}) |
| 102 |
for i, test := range testCases { |
| 103 |
p := NewPrefixTransformers(testErr, test.transformers...) |
| 104 |
got, err := p.TransformToStorage(reqCtx, []byte("value"), nil) |
| 105 |
if err != test.err || !bytes.Equal(got, test.expect) { |
| 106 |
t.Errorf("%d: unexpected out: %q %#v", i, string(got), err) |
| 107 |
continue |
| 108 |
} |
| 109 |
if !bytes.Equal([]byte("value"), test.transformers[0].Transformer.(*testTransformer).receivedTo) { |
| 110 |
t.Errorf("%d: unexpected value received by transformer: %s", i, test.transformers[0].Transformer.(*testTransformer).receivedTo) |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
func TestPrefixFromMetrics(t *testing.T) { |
| 116 |
testErr := fmt.Errorf("test error") |
| 117 |
transformerErr := fmt.Errorf("test error") |
| 118 |
identityTransformer := PrefixTransformer{Prefix: []byte{}, Transformer: &testTransformer{from: []byte("value1")}} |
| 119 |
identityTransformerErr := PrefixTransformer{Prefix: []byte{}, Transformer: &testTransformer{err: transformerErr}} |
| 120 |
otherTransformer := PrefixTransformer{Prefix: []byte("other:"), Transformer: &testTransformer{from: []byte("value1")}} |
| 121 |
otherTransformerErr := PrefixTransformer{Prefix: []byte("other:"), Transformer: &testTransformer{err: transformerErr}} |
| 122 |
|
| 123 |
testCases := []struct { |
| 124 |
desc string |
| 125 |
input []byte |
| 126 |
prefix Transformer |
| 127 |
metrics []string |
| 128 |
want string |
| 129 |
expectErr bool |
| 130 |
}{ |
| 131 |
{ |
| 132 |
desc: "identity prefix", |
| 133 |
input: []byte("value"), |
| 134 |
prefix: NewPrefixTransformers(testErr, identityTransformer, otherTransformer), |
| 135 |
metrics: []string{ |
| 136 |
"apiserver_storage_transformation_operations_total", |
| 137 |
}, |
| 138 |
want: ` |
| 139 |
# 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. |
| 140 |
# TYPE apiserver_storage_transformation_operations_total counter |
| 141 |
apiserver_storage_transformation_operations_total{resource="test",status="OK",transformation_type="from_storage",transformer_prefix="identity"} 1 |
| 142 |
`, |
| 143 |
expectErr: false, |
| 144 |
}, |
| 145 |
{ |
| 146 |
desc: "other prefix (ok)", |
| 147 |
input: []byte("other:value"), |
| 148 |
prefix: NewPrefixTransformers(testErr, identityTransformerErr, otherTransformer), |
| 149 |
metrics: []string{ |
| 150 |
"apiserver_storage_transformation_operations_total", |
| 151 |
}, |
| 152 |
want: ` |
| 153 |
# 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. |
| 154 |
# TYPE apiserver_storage_transformation_operations_total counter |
| 155 |
apiserver_storage_transformation_operations_total{resource="test",status="OK",transformation_type="from_storage",transformer_prefix="other:"} 1 |
| 156 |
`, |
| 157 |
expectErr: false, |
| 158 |
}, |
| 159 |
{ |
| 160 |
desc: "other prefix (error)", |
| 161 |
input: []byte("other:value"), |
| 162 |
prefix: NewPrefixTransformers(testErr, identityTransformerErr, otherTransformerErr), |
| 163 |
metrics: []string{ |
| 164 |
"apiserver_storage_transformation_operations_total", |
| 165 |
}, |
| 166 |
want: ` |
| 167 |
# 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. |
| 168 |
# TYPE apiserver_storage_transformation_operations_total counter |
| 169 |
apiserver_storage_transformation_operations_total{resource="test",status="unknown-non-grpc",transformation_type="from_storage",transformer_prefix="other:"} 1 |
| 170 |
`, |
| 171 |
expectErr: true, |
| 172 |
}, |
| 173 |
{ |
| 174 |
desc: "unknown prefix", |
| 175 |
input: []byte("foo:value"), |
| 176 |
prefix: NewPrefixTransformers(testErr, identityTransformerErr, otherTransformer), |
| 177 |
metrics: []string{ |
| 178 |
"apiserver_storage_transformation_operations_total", |
| 179 |
}, |
| 180 |
want: ` |
| 181 |
# 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. |
| 182 |
# TYPE apiserver_storage_transformation_operations_total counter |
| 183 |
apiserver_storage_transformation_operations_total{resource="test",status="unknown-non-grpc",transformation_type="from_storage",transformer_prefix="unknown"} 1 |
| 184 |
`, |
| 185 |
expectErr: true, |
| 186 |
}, |
| 187 |
} |
| 188 |
|
| 189 |
RegisterMetrics() |
| 190 |
transformerOperationsTotal.Reset() |
| 191 |
reqCtx := genericapirequest.WithRequestInfo(context.Background(), &genericapirequest.RequestInfo{Resource: "test"}) |
| 192 |
for _, tc := range testCases { |
| 193 |
t.Run(tc.desc, func(t *testing.T) { |
| 194 |
_, _, err := tc.prefix.TransformFromStorage(reqCtx, tc.input, nil) |
| 195 |
if (err != nil) != tc.expectErr { |
| 196 |
t.Fatal(err) |
| 197 |
} |
| 198 |
defer transformerOperationsTotal.Reset() |
| 199 |
if err := testutil.GatherAndCompare(legacyregistry.DefaultGatherer, strings.NewReader(tc.want), tc.metrics...); err != nil { |
| 200 |
t.Fatal(err) |
| 201 |
} |
| 202 |
}) |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
func TestPrefixToMetrics(t *testing.T) { |
| 207 |
testErr := fmt.Errorf("test error") |
| 208 |
transformerErr := fmt.Errorf("test error") |
| 209 |
otherTransformer := PrefixTransformer{Prefix: []byte("other:"), Transformer: &testTransformer{from: []byte("value1")}} |
| 210 |
otherTransformerErr := PrefixTransformer{Prefix: []byte("other:"), Transformer: &testTransformer{err: transformerErr}} |
| 211 |
reqCtx := genericapirequest.WithRequestInfo(context.Background(), &genericapirequest.RequestInfo{Resource: "test"}) |
| 212 |
testCases := []struct { |
| 213 |
desc string |
| 214 |
input []byte |
| 215 |
prefix Transformer |
| 216 |
metrics []string |
| 217 |
want string |
| 218 |
expectErr bool |
| 219 |
ctx context.Context |
| 220 |
}{ |
| 221 |
{ |
| 222 |
desc: "ok", |
| 223 |
input: []byte("value"), |
| 224 |
prefix: NewPrefixTransformers(testErr, otherTransformer), |
| 225 |
metrics: []string{ |
| 226 |
"apiserver_storage_transformation_operations_total", |
| 227 |
}, |
| 228 |
want: ` |
| 229 |
# 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. |
| 230 |
# TYPE apiserver_storage_transformation_operations_total counter |
| 231 |
apiserver_storage_transformation_operations_total{resource="test",status="OK",transformation_type="to_storage",transformer_prefix="other:"} 1 |
| 232 |
`, |
| 233 |
expectErr: false, |
| 234 |
ctx: reqCtx, |
| 235 |
}, |
| 236 |
{ |
| 237 |
desc: "missing request context", |
| 238 |
input: []byte("value"), |
| 239 |
prefix: NewPrefixTransformers(testErr, otherTransformer), |
| 240 |
metrics: []string{ |
| 241 |
"apiserver_storage_transformation_operations_total", |
| 242 |
}, |
| 243 |
want: ` |
| 244 |
# 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. |
| 245 |
# TYPE apiserver_storage_transformation_operations_total counter |
| 246 |
apiserver_storage_transformation_operations_total{resource="",status="OK",transformation_type="to_storage",transformer_prefix="other:"} 1 |
| 247 |
`, |
| 248 |
expectErr: false, |
| 249 |
ctx: context.Background(), |
| 250 |
}, |
| 251 |
{ |
| 252 |
desc: "request context with api group", |
| 253 |
input: []byte("value"), |
| 254 |
prefix: NewPrefixTransformers(testErr, otherTransformer), |
| 255 |
metrics: []string{ |
| 256 |
"apiserver_storage_transformation_operations_total", |
| 257 |
}, |
| 258 |
want: ` |
| 259 |
# 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. |
| 260 |
# TYPE apiserver_storage_transformation_operations_total counter |
| 261 |
apiserver_storage_transformation_operations_total{resource="test.testGroup",status="OK",transformation_type="to_storage",transformer_prefix="other:"} 1 |
| 262 |
`, |
| 263 |
expectErr: false, |
| 264 |
ctx: genericapirequest.WithRequestInfo(context.Background(), &genericapirequest.RequestInfo{APIGroup: "testGroup", Resource: "test"}), |
| 265 |
}, |
| 266 |
{ |
| 267 |
desc: "error", |
| 268 |
input: []byte("value"), |
| 269 |
prefix: NewPrefixTransformers(testErr, otherTransformerErr), |
| 270 |
metrics: []string{ |
| 271 |
"apiserver_storage_transformation_operations_total", |
| 272 |
}, |
| 273 |
want: ` |
| 274 |
# 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. |
| 275 |
# TYPE apiserver_storage_transformation_operations_total counter |
| 276 |
apiserver_storage_transformation_operations_total{resource="test",status="unknown-non-grpc",transformation_type="to_storage",transformer_prefix="other:"} 1 |
| 277 |
`, |
| 278 |
expectErr: true, |
| 279 |
ctx: reqCtx, |
| 280 |
}, |
| 281 |
} |
| 282 |
|
| 283 |
RegisterMetrics() |
| 284 |
transformerOperationsTotal.Reset() |
| 285 |
for _, tc := range testCases { |
| 286 |
t.Run(tc.desc, func(t *testing.T) { |
| 287 |
_, err := tc.prefix.TransformToStorage(tc.ctx, tc.input, nil) |
| 288 |
if (err != nil) != tc.expectErr { |
| 289 |
t.Fatal(err) |
| 290 |
} |
| 291 |
defer transformerOperationsTotal.Reset() |
| 292 |
if err := testutil.GatherAndCompare(legacyregistry.DefaultGatherer, strings.NewReader(tc.want), tc.metrics...); err != nil { |
| 293 |
t.Fatal(err) |
| 294 |
} |
| 295 |
}) |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
func TestLogTransformErr(t *testing.T) { |
| 300 |
klog.InitFlags(nil) |
| 301 |
tests := []struct { |
| 302 |
name string |
| 303 |
ctx context.Context |
| 304 |
err error |
| 305 |
message string |
| 306 |
expectedLog string |
| 307 |
}{ |
| 308 |
{ |
| 309 |
name: "log error with request info", |
| 310 |
ctx: genericapirequest.WithRequestInfo(testContext(t), &genericapirequest.RequestInfo{ |
| 311 |
APIGroup: "awesome.bears.com", |
| 312 |
APIVersion: "v1", |
| 313 |
Resource: "pandas", |
| 314 |
Subresource: "status", |
| 315 |
Namespace: "kube-system", |
| 316 |
Name: "panda", |
| 317 |
Verb: "update", |
| 318 |
}), |
| 319 |
err: errors.New("encryption failed"), |
| 320 |
message: "failed to encrypt data", |
| 321 |
expectedLog: "\"failed to encrypt data\" err=\"encryption failed\" group=\"awesome.bears.com\" version=\"v1\" resource=\"pandas\" subresource=\"status\" verb=\"update\" namespace=\"kube-system\" name=\"panda\"\n", |
| 322 |
}, |
| 323 |
{ |
| 324 |
name: "log error without request info", |
| 325 |
ctx: context.Background(), |
| 326 |
err: errors.New("decryption failed"), |
| 327 |
message: "failed to decrypt data", |
| 328 |
expectedLog: "\"no request info on context\"\n\"failed to decrypt data\" err=\"decryption failed\" group=\"\" version=\"\" resource=\"\" subresource=\"\" verb=\"\" namespace=\"\" name=\"\"\n", |
| 329 |
}, |
| 330 |
} |
| 331 |
|
| 332 |
for _, tt := range tests { |
| 333 |
t.Run(tt.name, func(t *testing.T) { |
| 334 |
var buf bytes.Buffer |
| 335 |
flag.Set("v", "6") |
| 336 |
flag.Parse() |
| 337 |
klog.SetOutput(&buf) |
| 338 |
klog.LogToStderr(false) |
| 339 |
defer klog.LogToStderr(true) |
| 340 |
|
| 341 |
logTransformErr(tt.ctx, tt.err, tt.message) |
| 342 |
|
| 343 |
// remove timestamp and goroutine id from log message to make it easier to compare |
| 344 |
gotLog := regexp.MustCompile(`\w+ \d+:\d+:\d+\.\d+.*\d+.*(transformer_test\.go|transformer\.go):\d+].`).ReplaceAllString(buf.String(), "") |
| 345 |
|
| 346 |
if gotLog != tt.expectedLog { |
| 347 |
t.Errorf("expected log message %q, got %q", tt.expectedLog, gotLog) |
| 348 |
} |
| 349 |
}) |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
func testContext(t *testing.T) context.Context { |
| 354 |
ctx, cancel := context.WithCancel(context.Background()) |
| 355 |
t.Cleanup(cancel) |
| 356 |
return ctx |
| 357 |
} |