Skip to content

Commit c908560

Browse files
mattia-eleuterimyasnikovdaniil
authored andcommitted
test(lineage): reject CAPK labels on objects that are not KubeVirt VMs
The bridge from a VirtualMachine to its KubevirtMachine is guarded by the object's API version and kind, but no test exercised that guard: weakening it to match on either field alone still passed, and let a labelled kubevirt.io Cluster or a VirtualMachine of another group resolve to the Kubernetes application. Assisted-by: LLM Signed-off-by: Mattia Eleuteri <[email protected]> (cherry picked from commit fd9245f) Signed-off-by: Myasnikov Daniil <[email protected]>
1 parent abcd8ae commit c908560

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

‎pkg/lineage/capk_test.go‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,34 @@ func TestWalkResolvesCAPKNodeVMToKubernetesApp(t *testing.T) {
8181
}
8282
}
8383

84+
func TestWalkFollowsCAPKLabelsOnlyFromKubeVirtVMs(t *testing.T) {
85+
// The CAPK labels name a valid KubevirtMachine in the same namespace in
86+
// every case: only the kind and API group of the labelled object differ, so
87+
// each case fails solely on the guard that restricts the bridge to
88+
// kubevirt.io/v1 VirtualMachines.
89+
capkLabels := map[string]string{
90+
KubevirtMachineNameLabel: "kubernetes-cluster-a-md0-x-y",
91+
KubevirtMachineNamespaceLabel: capkNamespace,
92+
}
93+
for _, tc := range []struct {
94+
name, apiVersion, kind string
95+
}{
96+
{name: "other kind in the kubevirt.io group", apiVersion: VMAPIVersion, kind: "Cluster"},
97+
{name: "VirtualMachineInstance", apiVersion: VMAPIVersion, kind: "VirtualMachineInstance"},
98+
{name: "VirtualMachine of another group", apiVersion: "example.com/v1", kind: VMKind},
99+
{name: "VirtualMachine of another kubevirt.io version", apiVersion: "kubevirt.io/v1alpha3", kind: VMKind},
100+
} {
101+
t.Run(tc.name, func(t *testing.T) {
102+
_, objs, mapper := capkNodeChain(nil)
103+
impostor := capkObject(tc.apiVersion, tc.kind, "impostor", capkLabels, nil)
104+
dyn := dynamicfake.NewSimpleDynamicClient(runtime.NewScheme(), objs...)
105+
if got := WalkOwnershipGraph(context.Background(), dyn, mapper, kubernetesAppMapper{}, impostor); len(got) != 0 {
106+
t.Fatalf("WalkOwnershipGraph(%s %s) = %+v, want no ancestors", tc.apiVersion, tc.kind, got)
107+
}
108+
})
109+
}
110+
}
111+
84112
func TestWalkIgnoresKubevirtMachineOfAnotherNamespace(t *testing.T) {
85113
// A VM pointing at a KubevirtMachine outside its own namespace (an external
86114
// infrastructure cluster, or a forged label) must not be resolved through it.

0 commit comments

Comments
 (0)