|
| 1 | +// Copyright 2021 The Kubernetes Authors. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package e2e |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + |
| 10 | + . "github.com/onsi/ginkgo/v2" |
| 11 | + . "github.com/onsi/gomega" |
| 12 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 13 | + "sigs.k8s.io/cli-utils/pkg/apply" |
| 14 | + "sigs.k8s.io/cli-utils/test/e2e/e2eutil" |
| 15 | + "sigs.k8s.io/cli-utils/test/e2e/invconfig" |
| 16 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 17 | +) |
| 18 | + |
| 19 | +const v1EventTemplate = ` |
| 20 | +apiVersion: v1 |
| 21 | +involvedObject: |
| 22 | + apiVersion: v1 |
| 23 | + kind: Pod |
| 24 | + name: pod |
| 25 | + namespace: {{.Namespace}} |
| 26 | +kind: Event |
| 27 | +message: Back-off restarting failed container |
| 28 | +metadata: |
| 29 | + name: test |
| 30 | + namespace: {{.Namespace}} |
| 31 | +reason: BackOff |
| 32 | +type: Warning |
| 33 | +` |
| 34 | + |
| 35 | +const v1EventsEventTemplate = ` |
| 36 | +apiVersion: events.k8s.io/v1 |
| 37 | +eventTime: null |
| 38 | +kind: Event |
| 39 | +metadata: |
| 40 | + name: test |
| 41 | + namespace: {{.Namespace}} |
| 42 | +note: Back-off restarting failed container |
| 43 | +reason: BackOff |
| 44 | +regarding: |
| 45 | + apiVersion: v1 |
| 46 | + kind: Pod |
| 47 | + name: pod |
| 48 | + namespace: {{.Namespace}} |
| 49 | +type: Warning |
| 50 | +` |
| 51 | + |
| 52 | +// Note this tests the scenario of "cohabitating" k8s objects (an object available via multiple apiGroups), but having the same UID. |
| 53 | +// As of k8s 1.25 an example of such "cohabitating" kinds is Event which is available via both "v1" and "events.k8s.io/v1". |
| 54 | +// See the full list of cohabitating resources on the storage level here: |
| 55 | +// - https://github.com/kubernetes/kubernetes/blob/v1.25.0/pkg/kubeapiserver/default_storage_factory_builder.go#L124-L131 |
| 56 | +// We test that when the user upgrades their manifest from one cohabitated apiGroup to the other, then: |
| 57 | +// - it should not result in object being pruned |
| 58 | +// - object pruning should be skipped due to CurrentUIDFilter (even though a diff is found) |
| 59 | +// - inventory should not double-track the object i.e. we should hold reference only to the object with the groupKind that was most recently applied |
| 60 | +func currentUIDFilterTest(ctx context.Context, c client.Client, invConfig invconfig.InventoryConfig, inventoryName, namespaceName string) { |
| 61 | + applier := invConfig.ApplierFactoryFunc() |
| 62 | + inventoryID := fmt.Sprintf("%s-%s", inventoryName, namespaceName) |
| 63 | + inventoryInfo := invconfig.CreateInventoryInfo(invConfig, inventoryName, namespaceName, inventoryID) |
| 64 | + |
| 65 | + templateFields := struct{ Namespace string }{Namespace: namespaceName} |
| 66 | + v1Event := e2eutil.TemplateToUnstructured(v1EventTemplate, templateFields) |
| 67 | + v1EventsEvent := e2eutil.TemplateToUnstructured(v1EventsEventTemplate, templateFields) |
| 68 | + |
| 69 | + By("Apply resource with deprecated groupKind") |
| 70 | + resources := []*unstructured.Unstructured{ |
| 71 | + v1Event, |
| 72 | + } |
| 73 | + err := e2eutil.Run(applier.Run(ctx, inventoryInfo, resources, apply.ApplierOptions{})) |
| 74 | + Expect(err).ToNot(HaveOccurred()) |
| 75 | + |
| 76 | + By("Verify resource available in both apiGroups") |
| 77 | + objDeprecated := e2eutil.AssertUnstructuredExists(ctx, c, v1Event) |
| 78 | + objNew := e2eutil.AssertUnstructuredExists(ctx, c, v1EventsEvent) |
| 79 | + |
| 80 | + By("Verify UID matches for cohabitating resources") |
| 81 | + uid := objDeprecated.GetUID() |
| 82 | + Expect(uid).ToNot(BeEmpty()) |
| 83 | + Expect(objDeprecated.GetUID()).To(Equal(objNew.GetUID())) |
| 84 | + |
| 85 | + By("Verify only 1 item in inventory") |
| 86 | + invConfig.InvSizeVerifyFunc(ctx, c, inventoryName, namespaceName, inventoryID, 1, 1) |
| 87 | + |
| 88 | + By("Apply resource with new groupKind") |
| 89 | + resources = []*unstructured.Unstructured{ |
| 90 | + v1EventsEvent, |
| 91 | + } |
| 92 | + err = e2eutil.Run(applier.Run(ctx, inventoryInfo, resources, apply.ApplierOptions{})) |
| 93 | + Expect(err).ToNot(HaveOccurred()) |
| 94 | + |
| 95 | + By("Verify resource still available in both apiGroups") |
| 96 | + objDeprecated = e2eutil.AssertUnstructuredExists(ctx, c, v1Event) |
| 97 | + objNew = e2eutil.AssertUnstructuredExists(ctx, c, v1EventsEvent) |
| 98 | + |
| 99 | + By("Verify UID matches for cohabitating resources") |
| 100 | + Expect(objDeprecated.GetUID()).To(Equal(objNew.GetUID())) |
| 101 | + |
| 102 | + By("Verify UID matches the UID from previous apply") |
| 103 | + Expect(objDeprecated.GetUID()).To(Equal(uid)) |
| 104 | + |
| 105 | + By("Verify still only 1 item in inventory") |
| 106 | + // Expecting statusCount=2: |
| 107 | + // one object applied and one prune skipped |
| 108 | + invConfig.InvSizeVerifyFunc(ctx, c, inventoryName, namespaceName, inventoryID, 1, 2) |
| 109 | +} |
0 commit comments