Repository: kubernetes/kubernetes
File: pkg/kubelet/cm/dra/manager_test.go (Go)

Code before:
	manager, err := NewManager(logger, fakeKubeClient, t.TempDir())
	require.NoError(t, err)

	manager.initDRAPluginManager(tCtx, getFakeNode, time.Second)

	// create two claims
	firstClaim := genTestClaim("test-claim", driverName, deviceName, podUID)
	secondClaim := genTestClaim("test-claim-second", driverName, deviceName, podUID)

	_, err = fakeKubeClient.ResourceV1().
		ResourceClaims(namespace).
		Create(tCtx, firstClaim, metav1.CreateOptions{})
	require.NoError(t, err)

	_, err = fakeKubeClient.ResourceV1().
		ResourceClaims(namespace).
		Create(tCtx, secondClaim, metav1.CreateOptions{})
	require.NoError(t, err)

	// first claim already prepared in cache
	preparedClaimInfo := genTestClaimInfo(
		firstClaim.UID,
		[]string{podUID},
		true,
	)
	manager.cache.add(preparedClaimInfo)

	resp := &drapb.NodePrepareResourcesResponse{
		Claims: map[string]*drapb.NodePrepareResourceResponse{
			string(secondClaim.UID): {
				Devices: []*drapb.Device{
					{
						PoolName:     poolName,
						DeviceName:   deviceName,
						RequestNames: []string{requestName},
						CdiDeviceIds: []string{cdiID},
					},
				},
			},
		},
	}

	draServerInfo, err := setupFakeDRADriverGRPCServer(tCtx, false, nil, resp, nil, nil)
	require.NoError(t, err)
	defer draServerInfo.teardownFn()

	plg := manager.GetWatcherHandler()
	require.NoError(t, plg.RegisterPlugin(driverName, draServerInfo.socketName, []string{drapb.DRAPluginService}, nil))

	podWithTwoClaims := &v1.Pod{
		ObjectMeta: metav1.ObjectMeta{

Review comment (performance):
Just wondering would it be a bit more straightforward to create a pod with first resource claim and call `PrepareResources` for it? Manipulating internal cache directly looks a bit low level to me.

Code after:
# TYPE dra_grpc_operations_duration_seconds histogram
dra_grpc_operations_duration_seconds_bucket{driver_name="test-driver",grpc_status_code="OK",method_name="/k8s.io.kubelet.pkg.apis.dra.v1.DRAPlugin/NodePrepareResources",le="+Inf"} 1
dra_grpc_operations_duration_seconds_sum{driver_name="test-driver",grpc_status_code="OK",method_name="/k8s.io.kubelet.pkg.apis.dra.v1.DRAPlugin/NodePrepareResources"} 0
dra_grpc_operations_duration_seconds_count{driver_name="test-driver",grpc_status_code="OK",method_name="/k8s.io.kubelet.pkg.apis.dra.v1.DRAPlugin/NodePrepareResources"} 1
# HELP dra_operations_duration_seconds [ALPHA] Latency histogram in seconds for the duration of handling all ResourceClaims referenced by a pod when the pod starts or stops. Identified by the name of the operation (PrepareResources or UnprepareResources) and separated by the success of the operation. The number of failed operations is provided through the histogram's overall count.
# TYPE dra_operations_duration_seconds histogram
dra_operations_duration_seconds_bucket{is_error="false",operation_name="PrepareResources",le="+Inf"} 1
dra_operations_duration_seconds_sum{is_error="false",operation_name="PrepareResources"} 0
dra_operations_duration_seconds_count{is_error="false",operation_name="PrepareResources"} 1
`,
		},
		{
			description:    "claim UIDs mismatch",
			driverName:     driverName,
			pod:            genTestPod(),
			claim:          genTestClaim(claimName, driverName, deviceName, podUID),
			claimInfo:      genTestClaimInfo(anotherClaimUID, []string{podUID}, false),
			expectedErrMsg: fmt.Sprintf("old ResourceClaim with same name %s and different UID %s still exists", claimName, anotherClaimUID),
			expectedMetric: `# HELP dra_operations_duration_seconds [ALPHA] Latency histogram in seconds for the duration of handling all ResourceClaims referenced by a pod when the pod starts or stops. Identified by the name of the operation (PrepareResources or UnprepareResources) and separated by the success of the operation. The number of failed operations is provided through the histogram's overall count.
# TYPE dra_operations_duration_seconds histogram
dra_operations_duration_seconds_bucket{is_error="true",operation_name="PrepareResources",le="+Inf"} 1
dra_operations_duration_seconds_sum{is_error="true",operation_name="PrepareResources"} 0
dra_operations_duration_seconds_count{is_error="true",operation_name="PrepareResources"} 1
`,
		},
		{
			description: "should prepare resources with share id",
			driverName:  driverName,
			pod:         genTestPod(),
			claim: func() *resourceapi.ResourceClaim {
				claim := genTestClaim(claimName, driverName, deviceName, podUID)
				claim.Status.Allocation.Devices.Results[0].ShareID = &shareUID
				return claim
			}(),
			expectedClaimInfoState: genClaimInfoStateWithShareID(cdiID),
			resp: &drapb.NodePrepareResourcesResponse{Claims: map[string]*drapb.NodePrepareResourceResponse{
				string(claimUID): {
					Devices: []*drapb.Device{
						{
							PoolName:     poolName,
							DeviceName:   deviceName,
							RequestNames: []string{requestName},
							ShareId:      &shareID,
							CdiDeviceIds: []string{cdiID},
						},
					},
				},
			}},
			expectedPrepareCalls: 1,
			expectedMetric: `# HELP dra_grpc_operations_duration_seconds [ALPHA] Duration in seconds of the DRA gRPC operations
# TYPE dra_grpc_operations_duration_seconds histogram