3 Star 0 Fork 0

neuro-netw0rk/server-golib

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
informer_service.go 2.57 KB
一键复制 编辑 原始数据 按行查看 历史
LeoWang 提交于 2024-12-16 16:45 . 整合golib和serverlib
package informer
import (
"context"
"gitee.com/neuro-netw0rk/server-golib/code"
"gitee.com/neuro-netw0rk/server-golib/errors"
"gitee.com/neuro-netw0rk/server-golib/k8s/meta"
v1 "k8s.io/api/core/v1"
k8serror "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/labels"
coreinformers "k8s.io/client-go/informers/core/v1"
)
var _ ServiceAction = (*services)(nil)
type Service interface {
InformerServices() ServiceAction
}
type ServiceAction interface {
List(ctx context.Context, options meta.ListOptions) ([]*v1.Service, error)
Get(ctx context.Context, options meta.GetOptions) (*v1.Service, error)
}
type services struct {
client coreinformers.ServiceInformer
}
func newServices(client coreinformers.ServiceInformer) *services {
return &services{
client: client,
}
}
func (d *services) List(ctx context.Context, options meta.ListOptions) ([]*v1.Service, error) {
var (
list = make([]*v1.Service, 0)
selector labels.Selector
err error
)
if len(options.IndexMap) != 0 {
// 只根据第一个索引查询
for key, value := range options.IndexMap {
services, err := d.client.Informer().GetIndexer().ByIndex(key, value)
if err != nil {
return nil, err
}
if options.Namespace != "" {
for _, tmpService := range services {
service := tmpService.(*v1.Service)
if options.Namespace == service.Namespace {
list = append(list, service)
}
}
} else {
for _, tmpService := range services {
service := tmpService.(*v1.Service)
list = append(list, service)
}
}
if len(list) == 0 {
return nil, errors.WithCode(code.ErrDataNotFound, "serviceList not found")
}
return list, nil
}
}
if options.Label != "" {
selector, err = labels.Parse(options.Label)
if err != nil {
return nil, errors.WithCode(code.ErrInternalServer, "label parse error")
}
} else {
selector = labels.Everything()
}
list, err = d.client.Lister().Services(options.Namespace).List(selector)
if err != nil {
return nil, err
}
if len(list) == 0 {
return nil, errors.WithCode(code.ErrDataNotFound, "serviceList with %s label in %s namespace not found", options.Label, options.Namespace)
}
return list, nil
}
func (d *services) Get(ctx context.Context, options meta.GetOptions) (*v1.Service, error) {
service, err := d.client.Lister().Services(options.Namespace).Get(options.ObjectName)
if err != nil {
if k8serror.IsNotFound(err) {
return nil, errors.WithCode(code.ErrDataNotFound, "service %s in %s namespace not found", options.ObjectName, options.Namespace)
}
return nil, err
}
return service, nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/neuro-netw0rk/server-golib.git
git@gitee.com:neuro-netw0rk/server-golib.git
neuro-netw0rk
server-golib
server-golib
v0.0.2-alpha

搜索帮助