Files
k3k/tests/cli/tests_suite_test.go
Enrico CandinoandGitHub 3a4d93f5fb Fix dangling Pods (#1021)
* Add tests asserting GetPods is scoped to the node's own Pods

Adds a unit test (fake host + virtual clients, Pods across multiple nodes plus
a dangling one) and a multi-node e2e test (a Deployment with one nginx Pod per
node via required anti-affinity; restart every k3k-kubelet agent Pod; assert no
workload Pod is deleted from the host or virtual cluster). Both pin the intended
behavior and fail against the current code; the fix follows in the next commit.

* Scope Provider.GetPods to the node's own Pods to prevent cross-node dangling-pod deletion

GetPods() listed host Pods cluster-wide (by the k3k.io/clusterName label only).
The vendored virtual-kubelet library's deleteDanglingPods reconciliation deletes
any Pod returned here that is missing from this instance's virtual Pod lister,
which is scoped to spec.nodeName == agentHostname. On a multi-node host cluster,
every restarting k3k-kubelet instance therefore saw Pods owned by other nodes as
'dangling' and deleted them from the host (and, in turn, the virtual cluster).

Scope GetPods by the *virtual* Pod's spec.nodeName -- the same ownership signal
the framework uses -- excluding Pods owned by other nodes while still returning
own-node Pods and genuinely dangling ones. The host Pod's physical node is not a
reliable owner (it is scheduled with only a soft, sometimes-absent affinity), so
it must not be used. Virtual Pods are read live to avoid a startup cache-sync
race. Makes the previous commit's tests pass.

* Refactor GetPods to scope to the cluster namespace and update tests accordingly

* Add AgentNameLabel to track Pods synced by the k3k-kubelet agent and update tests accordingly

* Add failing test for updatePod
2026-07-15 17:02:30 +02:00

48 lines
874 B
Go

package cli_test
import (
"context"
"testing"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
fwclient "github.com/rancher/k3k/tests/framework/client"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
const (
k3sVersion = "v1.36.2-k3s1"
k3sOldVersion = "v1.36.0-k3s1"
)
func TestTests(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Tests Suite")
}
var (
restcfg *rest.Config
k8s *kubernetes.Clientset
k8sClient client.Client
)
var _ = BeforeSuite(func() {
ctx := context.Background()
initKubernetesClient(ctx)
})
func initKubernetesClient(ctx context.Context) {
scheme := fwclient.NewScheme()
config, err := fwclient.InitFromKubeconfig(ctx, scheme)
Expect(err).NotTo(HaveOccurred())
restcfg = config.RestConfig
k8s = config.Clientset
k8sClient = config.Client
}