{"record":{"id":"c4e2487e8e98ee87","repo":"openimsdk/open-im-server","slug":"failed-to-get-service-s-v","errorCode":null,"errorMessage":"failed to get service %s: %v","messagePattern":"failed to get service (.+?): (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/common/discovery/kubernetes/kubernetes.go","lineNumber":221,"sourceCode":"func (k *KubernetesConnManager) Register(serviceName, host string, port int, opts ...grpc.DialOption) error {\n\treturn nil\n}\n\nfunc (k *KubernetesConnManager) UnRegister() error {\n\treturn nil\n}\n\nfunc (k *KubernetesConnManager) GetUserIdHashGatewayHost(ctx context.Context, userId string) (string, error) {\n\treturn \"\", nil\n}\n\nfunc (k *KubernetesConnManager) getServicePort(serviceName string) (int32, error) {\n\tvar svcPort int32\n\n\tsvc, err := k.clientset.CoreV1().Services(k.namespace).Get(context.Background(), serviceName, metav1.GetOptions{})\n\tif err != nil {\n\t\tfmt.Print(\"namespace:\", k.namespace)\n\t\treturn 0, fmt.Errorf(\"failed to get service %s: %v\", serviceName, err)\n\t}\n\n\tif len(svc.Spec.Ports) == 0 {\n\t\treturn 0, fmt.Errorf(\"service %s has no ports defined\", serviceName)\n\t}\n\n\tfor _, port := range svc.Spec.Ports {\n\t\t// fmt.Println(serviceName, \" Now Get Port:\", port.Port)\n\t\tif port.Port != 10001 {\n\t\t\tsvcPort = port.Port\n\t\t\tbreak\n\t\t}\n\t}\n\n\treturn svcPort, nil\n}\n\n// watchEndpoints listens for changes in Pod resources.","sourceCodeStart":203,"sourceCodeEnd":239,"githubUrl":"https://github.com/openimsdk/open-im-server/blob/175a7bb0673eca18e9d1b10bff4f728da6b1b513/pkg/common/discovery/kubernetes/kubernetes.go#L203-L239","documentation":"getServicePort looks up the Service object to find the gRPC port. If the API server Get fails (service missing, RBAC, API unreachable) it wraps the error with the service name. Without the port, endpoints cannot be dialed.","triggerScenarios":"clientset.CoreV1().Services(namespace).Get(ctx, serviceName, ...) returns an error inside getServicePort, called from initializeConns and GetConn.","commonSituations":"Service not created in the configured namespace; ServiceAccount missing get/services RBAC; typo in service name; connecting before the service manifest is applied.","solutions":["Confirm the Service exists: kubectl get svc <serviceName> -n <namespace>.","Check RBAC allows get on services for the pod's ServiceAccount.","Verify k.namespace matches the deployment namespace.","If this fires transiently during rollout, retry GetConns after the service becomes ready."],"exampleFix":"// before\nsvc, err := k.clientset.CoreV1().Services(k.namespace).Get(ctx, serviceName, metav1.GetOptions{})\n// after\nsvc, err := k.clientset.CoreV1().Services(k.namespace).Get(ctx, serviceName, metav1.GetOptions{})\nif err != nil {\n    return 0, fmt.Errorf(\"namespace=%q service=%q: %w\", k.namespace, serviceName, err)\n}","handlingStrategy":"try-catch","validationCode":"_, err := clientset.CoreV1().Services(ns).Get(ctx, svcName, metav1.GetOptions{})\nif err != nil { /* service missing or RBAC denied */ }","typeGuard":null,"tryCatchPattern":"port, err := mgr.GetConn(ctx, serviceName)\nif err != nil {\n    if apierrors.IsNotFound(errors.Unwrap(err)) {\n        log.Fatalf(\"service %s/%s not found\", ns, serviceName)\n    }\n}","preventionTips":["Apply Service manifests before starting RPC clients","Grant get on services in RBAC for the ServiceAccount","Keep service name and namespace in a single config source to avoid typos"],"tags":["kubernetes","rbac","service-discovery"],"backgroundTag":"kubernetes-service-not-found","analyzedSha":"175a7bb0673eca18e9d1b10bff4f728da6b1b513","analyzedAt":"2026-09-04T16:52:56.821Z","contentChangedAt":"2026-09-04T16:52:56.821Z","schemaVersion":2},"datasetVersion":"2026-09-11T21:17:09.523Z"}