Repository: kubernetes/kubernetes
File: test/e2e/dra/utils/deploy.go (Go)

Code before:
				return d.streamInterceptor(nodename, srv, ss, info, handler)
			}),
			kubeletplugin.NodeV1(d.NodeV1),
			kubeletplugin.NodeV1beta1(d.NodeV1beta1),

			kubeletplugin.RollingUpdate(rollingUpdateUID),
			kubeletplugin.Serialize(serialize),
			kubeletplugin.FlockDirectoryPath(nodes.tempDir),

			kubeletplugin.PluginDataDirectoryPath(pluginDataDirectoryPath),
			kubeletplugin.PluginListener(d.listen(tCtx, &pod, &listenerPort)),

			kubeletplugin.RegistrarDirectoryPath(registrarDirectoryPath),
			kubeletplugin.RegistrarListener(d.listen(tCtx, &pod, &listenerPort)),
		)
		tCtx.ExpectNoError(err, "start kubelet plugin for node %s", pod.Spec.NodeName)
		d.cleanup = append(d.cleanup, func(tCtx ktesting.TContext) {
			// Depends on cancel being called first.
			plugin.Stop()

			// Also explicitly stop all pods.
			tCtx.Log("scaling down driver proxy pods for", d.Name)
			rs, err := tCtx.Client().AppsV1().ReplicaSets(tCtx.Namespace()).Get(tCtx, rsName, metav1.GetOptions{})
			tCtx.ExpectNoError(err, "get ReplicaSet for driver "+d.Name)
			rs.Spec.Replicas = ptr.To(int32(0))
			rs, err = tCtx.Client().AppsV1().ReplicaSets(tCtx.Namespace()).Update(tCtx, rs, metav1.UpdateOptions{})
			tCtx.ExpectNoError(err, "scale down ReplicaSet for driver "+d.Name)
			if err := e2ereplicaset.WaitForReplicaSetTargetAvailableReplicas(tCtx, tCtx.Client(), rs, 0); err != nil {
				tCtx.ExpectNoError(err, "all kubelet plugin proxies stopped")
			}
		})
		d.Nodes[nodename] = KubeletPlugin{ExamplePlugin: plugin, ClientSet: driverClient}
	}

	if !d.WithKubelet {
		return
	}

	// Wait for registration.
	tCtx.Log("wait for plugin registration")
	tCtx.Eventually(func(tCtx ktesting.TContext) map[string][]app.GRPCCall {
		notRegistered := make(map[string][]app.GRPCCall)
		for nodename, plugin := range d.Nodes {
			calls := plugin.GetGRPCCalls()
			if contains, err := testdrivergomega.BeRegistered.Match(calls); err != nil || !contains {
				notRegistered[nodename] = calls
			}
		}
		return notRegistered
	}).WithTimeout(time.Minute).Should(gomega.BeEmpty(), "hosts where the plugin has not been registered yet")
}

Review comment (suggestion):
This preserves the original order because `d.cleanup` is first-in-first-out. 👍 





Code after:
				return d.streamInterceptor(nodename, srv, ss, info, handler)
			}),
			kubeletplugin.NodeV1(d.NodeV1),
			kubeletplugin.NodeV1beta1(d.NodeV1beta1),

			kubeletplugin.RollingUpdate(rollingUpdateUID),
			kubeletplugin.Serialize(serialize),
			kubeletplugin.FlockDirectoryPath(nodes.tempDir),

			kubeletplugin.PluginDataDirectoryPath(pluginDataDirectoryPath),
			kubeletplugin.PluginListener(d.listen(tCtx, &pod, &listenerPort)),

			kubeletplugin.RegistrarDirectoryPath(registrarDirectoryPath),
			kubeletplugin.RegistrarListener(d.listen(tCtx, &pod, &listenerPort)),
		)
		tCtx.ExpectNoError(err, "start kubelet plugin for node %s", pod.Spec.NodeName)
		d.cleanup = append(d.cleanup, func(tCtx ktesting.TContext) {
			// Depends on cancel being called first.
			plugin.Stop()
		})
		d.Nodes[nodename] = KubeletPlugin{ExamplePlugin: plugin, ClientSet: driverClient}
	}

	// Scale down the proxy ReplicaSet after all per-node plugins have
	// been stopped.
	d.cleanup = append(d.cleanup, func(tCtx ktesting.TContext) {
		tCtx.Log("scaling down driver proxy pods for", d.Name)
		rs, err := tCtx.Client().AppsV1().ReplicaSets(tCtx.Namespace()).Get(tCtx, rsName, metav1.GetOptions{})
		tCtx.ExpectNoError(err, "get ReplicaSet for driver "+d.Name)
		rs.Spec.Replicas = ptr.To(int32(0))
		rs, err = tCtx.Client().AppsV1().ReplicaSets(tCtx.Namespace()).Update(tCtx, rs, metav1.UpdateOptions{})
		tCtx.ExpectNoError(err, "scale down ReplicaSet for driver "+d.Name)
		if err := e2ereplicaset.WaitForReplicaSetTargetAvailableReplicas(tCtx, tCtx.Client(), rs, 0); err != nil {
			tCtx.ExpectNoError(err, "all kubelet plugin proxies stopped")
		}
	})

	if !d.WithKubelet {
		return
	}

	// Wait for registration.
	tCtx.Log("wait for plugin registration")
	tCtx.Eventually(func(tCtx ktesting.TContext) map[string][]app.GRPCCall {
		notRegistered := make(map[string][]app.GRPCCall)
		for nodename, plugin := range d.Nodes {
			calls := plugin.GetGRPCCalls()
			if contains, err := testdrivergomega.BeRegistered.Match(calls); err != nil || !contains {
				notRegistered[nodename] = calls
			}
		}