···871871}
872872873873// GetAgentPodIPs returns all agent pod IPs for a device type from HSMPool
874874-func (m *Manager) GetAgentPodIPs(ctx context.Context, deviceName, namespace string) ([]string, error) {
875875- // Get HSMPool for this device
876876- poolName := deviceName + "-pool"
877877- var hsmPool hsmv1alpha1.HSMPool
878878- if err := m.Get(ctx, types.NamespacedName{
879879- Name: poolName,
880880- Namespace: namespace,
881881- }, &hsmPool); err != nil {
882882- return nil, fmt.Errorf("failed to get HSMPool %s: %w", poolName, err)
883883- }
874874+func (m *Manager) GetAgentPodIPs(hsmPool *hsmv1alpha1.HSMPool) ([]string, error) {
875875+ // Extract device name from pool name (remove "-pool" suffix)
876876+ deviceName := strings.TrimSuffix(hsmPool.Name, "-pool")
884877885878 m.mu.RLock()
886879 defer m.mu.RUnlock()
···896889 }
897890898891 if len(allPodIPs) == 0 {
899899- return nil, fmt.Errorf("no active agents found for device %s in pool %s", deviceName, poolName)
892892+ return nil, fmt.Errorf("no active agents found for device %s in pool %s", deviceName, hsmPool.Name)
900893 }
901894902895 return allPodIPs, nil
···935928}
936929937930// GetGRPCEndpoints returns gRPC endpoints for all agent pods of a device
938938-func (m *Manager) GetGRPCEndpoints(ctx context.Context, deviceName, namespace string) ([]string, error) {
939939- podIPs, err := m.GetAgentPodIPs(ctx, deviceName, namespace)
931931+func (m *Manager) GetGRPCEndpoints(hsmPool *hsmv1alpha1.HSMPool) ([]string, error) {
932932+ podIPs, err := m.GetAgentPodIPs(hsmPool)
940933 if err != nil {
941934 return nil, err
942935 }
···949942 return endpoints, nil
950943}
951944952952-// CreateGRPCClient creates a gRPC client for the first available agent pod of a device
953953-func (m *Manager) CreateGRPCClient(ctx context.Context, deviceName, namespace string, logger logr.Logger) (hsm.Client, error) {
954954- endpoints, err := m.GetGRPCEndpoints(ctx, deviceName, namespace)
955955- if err != nil {
956956- return nil, err
945945+// CreateGRPCClient creates a gRPC client to the specific agent pod for the given DiscoveredDevice
946946+func (m *Manager) CreateGRPCClient(ctx context.Context, device hsmv1alpha1.DiscoveredDevice, logger logr.Logger) (hsm.Client, error) {
947947+ // Find the specific agent pod using labels based on the device's serial number
948948+ var podList corev1.PodList
949949+ listOpts := []client.ListOption{
950950+ client.MatchingLabels{
951951+ "app.kubernetes.io/name": "hsm-agent",
952952+ "app.kubernetes.io/component": "hsm-agent",
953953+ "hsm.j5t.io/serial-number": device.SerialNumber,
954954+ },
957955 }
958956959959- if len(endpoints) == 0 {
960960- return nil, fmt.Errorf("no agent endpoints available for device %s", deviceName)
957957+ if err := m.List(ctx, &podList, listOpts...); err != nil {
958958+ return nil, fmt.Errorf("failed to list agent pods for device %s: %w", device.SerialNumber, err)
959959+ }
960960+961961+ if len(podList.Items) == 0 {
962962+ return nil, fmt.Errorf("no agent pod found for device with serial number %s", device.SerialNumber)
961963 }
962964963963- // Use the first endpoint for single client
964964- grpcClient, err := NewGRPCClient(endpoints[0], deviceName, logger)
965965+ // Find the first running pod
966966+ var targetPod *corev1.Pod
967967+ for i := range podList.Items {
968968+ pod := &podList.Items[i]
969969+ if pod.Status.Phase == corev1.PodRunning && len(pod.Status.PodIPs) > 0 {
970970+ targetPod = pod
971971+ break
972972+ }
973973+ }
974974+975975+ if targetPod == nil {
976976+ return nil, fmt.Errorf("no running agent pod found for device with serial number %s", device.SerialNumber)
977977+ }
978978+979979+ // Get the pod IP and create gRPC endpoint
980980+ podIP := targetPod.Status.PodIPs[0].IP
981981+ endpoint := fmt.Sprintf("%s:%d", podIP, AgentPort)
982982+983983+ // Create gRPC client
984984+ grpcClient, err := NewGRPCClient(endpoint, logger)
965985 if err != nil {
966966- return nil, fmt.Errorf("failed to create gRPC client for %s: %w", endpoints[0], err)
986986+ return nil, fmt.Errorf("failed to create gRPC client for %s: %w", endpoint, err)
967987 }
968988969989 // Test the connection
···971991 if err := grpcClient.Close(); err != nil {
972992 logger.Error(err, "Failed to close gRPC client after failed initialization")
973993 }
974974- return nil, fmt.Errorf("failed to initialize gRPC client for %s: %w", endpoints[0], err)
994994+ return nil, fmt.Errorf("failed to initialize gRPC client for %s: %w", endpoint, err)
975995 }
976996977997 return grpcClient, nil
978998}
979999980980-// GetAvailableDevices finds all devices with ready HSMPools and active agents
981981-func (m *Manager) GetAvailableDevices(ctx context.Context, namespace string) ([]string, error) {
982982- // List all HSMPools cluster-wide to find all with active agents
10001000+// GetAvailableDevices finds all devices with ready HSMPools
10011001+func (m *Manager) GetAvailableDevices(ctx context.Context, namespace string) ([]hsmv1alpha1.DiscoveredDevice, error) {
10021002+ // List all HSMPools cluster-wide to find all ready pools
9831003 var hsmPoolList hsmv1alpha1.HSMPoolList
9841004 if err := m.List(ctx, &hsmPoolList); err != nil {
9851005 return nil, fmt.Errorf("failed to list HSM pools: %w", err)
9861006 }
9871007988988- var availableDevices []string
989989- // Check all pools that have active agents
10081008+ var availableDevices []hsmv1alpha1.DiscoveredDevice
10091009+ // Check all pools that are in Ready phase
9901010 for _, pool := range hsmPoolList.Items {
9911011 if pool.Status.Phase != hsmv1alpha1.HSMPoolPhaseReady {
9921012 continue
9931013 }
9941014995995- // Extract device name from pool name (remove "-pool" suffix)
996996- deviceName := strings.TrimSuffix(pool.Name, "-pool")
997997-998998- // Use the HSMPool's namespace for agent lookup
999999- if podIPs, err := m.GetAgentPodIPs(ctx, deviceName, pool.Namespace); err == nil && len(podIPs) > 0 {
10001000- availableDevices = append(availableDevices, deviceName)
10011001- }
10151015+ availableDevices = append(availableDevices, pool.Status.AggregatedDevices...)
10021016 }
1003101710041018 if len(availableDevices) == 0 {
10051005- return nil, fmt.Errorf("no available HSM agents found")
10191019+ return nil, fmt.Errorf("no available HSM devices found")
10061020 }
1007102110081022 return availableDevices, nil