ek8s

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 17, 2022 License: MIT Imports: 19 Imported by: 0

README

ek8s 组件使用指南

goproxy.cn Release Example Doc

1 简洁

  • 规范了标准配置格式,提供了统一的 Load().Build() 方法。
  • 支持查询K8S信息
  • 根据K8S Endpoints调用gRPC

2 Example

3 K8S配置

type Config struct {
    Addr                    string     // 地址
    Debug                   bool       // 调试信息
    Token                   string     // token信息
    Namespaces              []string   // 命名空间列表
    DeploymentPrefix        string     // 前缀
    TLSClientConfigInsecure bool       // 是否开启tls
}

4 默认配置

  • host: KUBERNETES_SERVICE_HOST 环境变量
  • port: KUBERNETES_SERVICE_PORT 环境变量
  • token: /var/run/secrets/kubernetes.io/serviceaccount/token 文件路径

5 根据K8S信息,调用gRPC

5.1 K8S配置
[k8s]
addr=""
token=""
namespaces=["default"]

[grpc.test]
debug = true # 开启后并加上export EGO_DEBUG=true,可以看到每次grpc请求,配置名、地址、耗时、请求数据、响应数据
addr = "k8s:///test:9090"
#balancerName = "round_robin" # 默认值
#dialTimeout = "1s" # 默认值
#enableAccessInterceptor = true
#enableAccessInterceptorRes = true
#enableAccessInterceptorReq = true
5.2 用户代码

配置创建一个 k8s 的配置项,其中内容按照上文配置进行填写。以上这个示例里这个配置key是k8s

代码中创建一个 k8s 客户端, ek8s.Load("k8s").Build(),代码中的 key 和配置中的 key 要保持一致。创建完 k8s 客户端后, 将他添加到你所需要的Registry里即可。

// 构建k8s registry,并注册为grpc resolver
registry.DefaultContainer().Build(registry.WithClient(ek8s.Load("k8s").Build()))
// 构建gRPC.ClientConn组件
grpcConn := egrpc.Load("grpc.test").Build()
// 构建gRPC Client组件
grpcComp := helloworld.NewGreeterClient(grpcConn.ClientConn)
fmt.Printf("client--------------->"+"%+v\n", grpcComp)

Documentation

Index

Constants

View Source
const (
	KindPods      = "pods"
	KindEndpoints = "endpoints"
)
View Source
const PackageName = "component.ek8s"

Variables

This section is empty.

Functions

This section is empty.

Types

type Component

type Component struct {
	*kubernetes.Clientset
	// contains filtered or unexported fields
}

Component ...

func (*Component) Config

func (c *Component) Config() Config

func (*Component) ListEndpoints

func (c *Component) ListEndpoints(option ListOptions) (endPoints []*v1.EndpointsList, err error)

func (*Component) ListEndpointsByName

func (c *Component) ListEndpointsByName(name string) (endPoints []*v1.Endpoints, err error)

func (*Component) ListPods

func (c *Component) ListPods(option ListOptions) (pods []*v1.PodList, err error)

func (*Component) ListPodsByName

func (c *Component) ListPodsByName(name string) (pods []*v1.Pod, err error)

func (*Component) NewWatcherApp

func (c *Component) NewWatcherApp(ctx context.Context, appName string, kind string) (app *WatcherApp, err error)

type Config

type Config struct {
	Addr                    string
	Debug                   bool
	Token                   string
	Namespaces              []string
	DeploymentPrefix        string // 命名前缀
	TLSClientConfigInsecure bool
}

Config ...

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig 返回默认配置

type Container

type Container struct {
	// contains filtered or unexported fields
}

func DefaultContainer

func DefaultContainer() *Container

func Load

func Load(key string) *Container

func (*Container) Build

func (c *Container) Build(options ...Option) *Component

Build ...

type KubernetesEvent

type KubernetesEvent struct {
	IPs       []string
	EventType watch.EventType
}

type ListOptions

type ListOptions = metav1.ListOptions

type Option

type Option func(c *Container)

Option 可选项

func WithAddr

func WithAddr(addr string) Option

func WithDebug

func WithDebug(debug bool) Option

func WithDeploymentPrefix

func WithDeploymentPrefix(deploymentPrefix string) Option

func WithNamespaces

func WithNamespaces(namespaces []string) Option

func WithTLSClientConfigInsecure

func WithTLSClientConfigInsecure(insecure bool) Option

func WithToken

func WithToken(token string) Option

type WatcherApp

type WatcherApp struct {
	*kubernetes.Clientset
	// contains filtered or unexported fields
}

func (*WatcherApp) ProcessWorkItem

func (c *WatcherApp) ProcessWorkItem(f func(info *KubernetesEvent) error) bool

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL