|
| 1 | +package httprecorder |
| 2 | + |
| 3 | +import ( |
| 4 | + "sort" |
| 5 | + "strconv" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 11 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 12 | + "k8s.io/client-go/rest" |
| 13 | + "sigs.k8s.io/kubebuilder-declarative-pattern/applylib/forked/k8s.io/apimachinery/pkg/util/sets" |
| 14 | +) |
| 15 | + |
| 16 | +// placeholderTime is a placeholder time that is used to replace the timestamps, so that we can golden test. |
| 17 | +var placeholderTime = metav1.Date(2025, 4, 1, 0, 0, 0, 0, time.UTC) |
| 18 | + |
| 19 | +// NormalizeKubeRequestLog normalizes a kube request log for golden testing, |
| 20 | +// it replaces ephemeral values with placeholder values, |
| 21 | +// sorts requests into a predictable order, |
| 22 | +// and removes non-deterministic headers. |
| 23 | +func NormalizeKubeRequestLog(t *testing.T, requestLog *RequestLog, restConfig *rest.Config) { |
| 24 | + // Whether we're using a proxy or not (http vs https), IPv6 or not, we want to normalize the host to kube-apiserver |
| 25 | + apiserverRealHost := strings.ReplaceAll(restConfig.Host, "[::]", "127.0.0.1") |
| 26 | + apiserverRealHost = strings.TrimPrefix(apiserverRealHost, "https://") |
| 27 | + apiserverRealHost = strings.TrimPrefix(apiserverRealHost, "http://") |
| 28 | + apiserverRealHost = strings.TrimSuffix(apiserverRealHost, "/") |
| 29 | + requestLog.ReplaceURLPrefix("https://"+apiserverRealHost, "https://kube-apiserver") |
| 30 | + requestLog.ReplaceURLPrefix("http://"+apiserverRealHost, "https://kube-apiserver") |
| 31 | + apiserverRealHost = strings.ReplaceAll(apiserverRealHost, "127.0.0.1", "[::]") |
| 32 | + requestLog.ReplaceURLPrefix("https://"+apiserverRealHost, "https://kube-apiserver") |
| 33 | + requestLog.ReplaceURLPrefix("http://"+apiserverRealHost, "https://kube-apiserver") |
| 34 | + |
| 35 | + requestLog.RemoveUserAgent() |
| 36 | + |
| 37 | + requestLog.RemoveHeader("X-Kubernetes-Pf-Flowschema-Uid") |
| 38 | + requestLog.RemoveHeader("X-Kubernetes-Pf-Prioritylevel-Uid") |
| 39 | + requestLog.RemoveHeader("Audit-Id") |
| 40 | + |
| 41 | + requestLog.RewriteBodies(t, func(body map[string]any) { |
| 42 | + u := unstructured.Unstructured{Object: body} |
| 43 | + uid := u.GetUID() |
| 44 | + if uid != "" { |
| 45 | + u.SetUID("fake-uid") |
| 46 | + } |
| 47 | + |
| 48 | + replaceIfPresent(t, body, placeholderTime.Format(time.RFC3339), "metadata", "creationTimestamp") |
| 49 | + |
| 50 | + if managedFields := u.GetManagedFields(); managedFields != nil { |
| 51 | + for i := range managedFields { |
| 52 | + managedFields[i].Time = &placeholderTime |
| 53 | + } |
| 54 | + u.SetManagedFields(managedFields) |
| 55 | + } |
| 56 | + }) |
| 57 | + |
| 58 | + // Rewrite the resource version to a predictable value |
| 59 | + { |
| 60 | + resourceVersions := sets.New[int]() |
| 61 | + requestLog.RewriteBodies(t, func(body map[string]any) { |
| 62 | + u := unstructured.Unstructured{Object: body} |
| 63 | + rv := u.GetResourceVersion() |
| 64 | + if rv != "" { |
| 65 | + // These aren't guaranteed to be ints, but in practice they are, and this is test code. |
| 66 | + rvInt, err := strconv.Atoi(rv) |
| 67 | + if err != nil { |
| 68 | + t.Errorf("error converting resource version %q to int: %v", rv, err) |
| 69 | + return |
| 70 | + } |
| 71 | + resourceVersions.Insert(rvInt) |
| 72 | + } |
| 73 | + }) |
| 74 | + resourceVersionsList := resourceVersions.UnsortedList() |
| 75 | + sort.Ints(resourceVersionsList) |
| 76 | + rewriteResourceVersion := map[string]string{} |
| 77 | + for i, rv := range resourceVersionsList { |
| 78 | + rewriteResourceVersion[strconv.Itoa(rv)] = strconv.Itoa(1000 + i) |
| 79 | + } |
| 80 | + requestLog.RewriteBodies(t, func(body map[string]any) { |
| 81 | + u := unstructured.Unstructured{Object: body} |
| 82 | + rv := u.GetResourceVersion() |
| 83 | + u.SetResourceVersion(rewriteResourceVersion[rv]) |
| 84 | + }) |
| 85 | + } |
| 86 | + |
| 87 | + requestLog.SortGETs() |
| 88 | +} |
| 89 | + |
| 90 | +func replaceIfPresent(t *testing.T, body map[string]any, replace string, path ...string) { |
| 91 | + u := unstructured.Unstructured{Object: body} |
| 92 | + _, found, err := unstructured.NestedFieldNoCopy(u.Object, path...) |
| 93 | + if err != nil { |
| 94 | + t.Errorf("error getting nested field %v: %v", path, err) |
| 95 | + return |
| 96 | + } |
| 97 | + if found { |
| 98 | + unstructured.SetNestedField(u.Object, replace, path...) |
| 99 | + } |
| 100 | +} |
0 commit comments