40 lines
927 B
Go
40 lines
927 B
Go
package kubernetes
|
|
|
|
import (
|
|
"path/filepath"
|
|
|
|
"github.com/go-kratos/kratos/v2/log"
|
|
|
|
k8sRegistry "github.com/go-kratos/kratos/contrib/registry/kubernetes/v2"
|
|
k8s "k8s.io/client-go/kubernetes"
|
|
k8sRest "k8s.io/client-go/rest"
|
|
k8sTools "k8s.io/client-go/tools/clientcmd"
|
|
k8sUtil "k8s.io/client-go/util/homedir"
|
|
|
|
conf "github.com/tx7do/kratos-bootstrap/api/gen/go/conf/v1"
|
|
)
|
|
|
|
// NewRegistry 创建一个注册发现客户端 - Kubernetes
|
|
func NewRegistry(_ *conf.Registry) *k8sRegistry.Registry {
|
|
restConfig, err := k8sRest.InClusterConfig()
|
|
if err != nil {
|
|
home := k8sUtil.HomeDir()
|
|
kubeConfig := filepath.Join(home, ".kube", "config")
|
|
restConfig, err = k8sTools.BuildConfigFromFlags("", kubeConfig)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
return nil
|
|
}
|
|
}
|
|
|
|
clientSet, err := k8s.NewForConfig(restConfig)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
return nil
|
|
}
|
|
|
|
reg := k8sRegistry.NewRegistry(clientSet)
|
|
|
|
return reg
|
|
}
|