{"record":{"id":"4c985b3c6193a2b4","repo":"openimsdk/open-im-server","slug":"failed-to-create-in-cluster-config-v","errorCode":null,"errorMessage":"failed to create in-cluster config: %v","messagePattern":"failed to create in-cluster config: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/common/discovery/kubernetes/kubernetes.go","lineNumber":37,"sourceCode":")\n\ntype KubernetesConnManager struct {\n\tclientset   *kubernetes.Clientset\n\tnamespace   string\n\tdialOptions []grpc.DialOption\n\n\trpcTargets map[string]string\n\tselfTarget string\n\n\tmu      sync.RWMutex\n\tconnMap map[string][]*grpc.ClientConn\n}\n\n// NewKubernetesConnManager creates a new connection manager that uses Kubernetes services for service discovery.\nfunc NewKubernetesConnManager(namespace string, options ...grpc.DialOption) (*KubernetesConnManager, error) {\n\tconfig, err := rest.InClusterConfig()\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to create in-cluster config: %v\", err)\n\t}\n\n\tclientset, err := kubernetes.NewForConfig(config)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to create clientset: %v\", err)\n\t}\n\n\tk := &KubernetesConnManager{\n\t\tclientset:   clientset,\n\t\tnamespace:   namespace,\n\t\tdialOptions: options,\n\t\tconnMap:     make(map[string][]*grpc.ClientConn),\n\t}\n\n\tgo k.watchEndpoints()\n\n\treturn k, nil\n}","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/openimsdk/open-im-server/blob/175a7bb0673eca18e9d1b10bff4f728da6b1b513/pkg/common/discovery/kubernetes/kubernetes.go#L19-L55","documentation":"NewKubernetesConnManager builds service discovery on in-cluster Kubernetes credentials via rest.InClusterConfig(), which requires the pod's service account token and ca.crt mounted at /var/run/secrets/kubernetes.io/serviceaccount/. When those are absent or unreadable (i.e. not running inside a K8s cluster), it fails and the error is wrapped as 'failed to create in-cluster config: %v'.","triggerScenarios":"Calling NewKubernetesConnManager from a binary running outside a Kubernetes pod (local dev, docker without K8s mounts), or a pod whose service account token volume isn't mounted / RBAC disabled token projection.","commonSituations":"Running openim locally while the kubernetes discovery module is selected in config; K8s pod started without a service account or with automountServiceAccountToken: false; very old clusters lacking projected service account tokens.","solutions":["Use a discovery config suited to your environment (e.g. the direct/etcd/zookeeper discovery) when not running in Kubernetes","Set automountServiceAccountToken: true (or a serviceAccountName) on the pod spec so the token is mounted","Verify /var/run/secrets/kubernetes.io/serviceaccount/token and ca.crt exist inside the pod","Ensure the underlying rest error (shown in %v) is fixed — e.g. missing KUBERNETES_SERVICE_HOST env means you're not in-cluster"],"exampleFix":"// before (pod spec)\nspec: { automountServiceAccountToken: false }\n// after\nspec:\n  serviceAccountName: openim-sa\n  automountServiceAccountToken: true","handlingStrategy":"fallback","validationCode":"func inCluster() bool {\n\tif os.Getenv(\"KUBERNETES_SERVICE_HOST\") == \"\" { return false }\n\t_, err := os.Stat(\"/var/run/secrets/kubernetes.io/serviceaccount/token\")\n\treturn err == nil\n}\n// choose kubernetes discovery only if inCluster()","typeGuard":null,"tryCatchPattern":"mgr, err := discovery.NewKubernetesConnManager(ns, opts...)\nif err != nil && strings.Contains(err.Error(), \"failed to create in-cluster config\") {\n\tlog.ZWarn(ctx, \"not in kubernetes, falling back to direct discovery\", err)\n\tmgr, err = discovery.NewDirectConnManager(opts...)\n}\nif err != nil { return err }","preventionTips":["Select a non-kubernetes discovery mode for local/docker deployments","Set automountServiceAccountToken: true and a serviceAccountName in pod specs","Verify service account token/ca.crt mounts before rollout","Read the wrapped %v error to distinguish missing env vs missing token files"],"tags":["kubernetes","service-discovery","configuration"],"backgroundTag":"not-running-in-cluster","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"}