37 Star 407 Fork 74

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
store.go 2.03 KB
一键复制 编辑 原始数据 按行查看 历史
Dan Ramich 提交于 2019-06-12 13:38 . goimport linting changes
package kontainerdriver
import (
"fmt"
errorsutil "github.com/pkg/errors"
"github.com/rancher/norman/httperror"
"github.com/rancher/norman/types"
v3 "github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/client-go/tools/cache"
)
type store struct {
types.Store
KontainerDriverLister v3.KontainerDriverLister
ClusterIndexer cache.Indexer
}
func NewStore(management *config.ScaledContext, s types.Store) types.Store {
clusterInformer := management.Management.Clusters("").Controller().Informer()
// use an indexer instead of expensive k8s api calls
clusterInformer.AddIndexers(map[string]cache.IndexFunc{
clusterByKontainerDriverKey: clusterByKontainerDriver,
})
kd := management.Management.KontainerDrivers("").Controller().Lister()
storeObj := store{
Store: s,
KontainerDriverLister: kd,
ClusterIndexer: clusterInformer.GetIndexer(),
}
return &storeObj
}
// Delete removes the KontainerDriver if it is not in use by a cluster
func (s *store) Delete(apiContext *types.APIContext, schema *types.Schema, id string) (map[string]interface{}, error) {
//need to get the full driver since just the id is not enough see if a cluster uses it
driver, err := s.KontainerDriverLister.Get("", id)
if err != nil {
if !errors.IsNotFound(err) {
return nil, errorsutil.WithMessage(err, fmt.Sprintf("error getting kontainer driver %s", id))
}
//if driver is not found, don't return error
return nil, nil
}
clustersWithKontainerDriver, err := s.ClusterIndexer.ByIndex(clusterByKontainerDriverKey, driver.Status.DisplayName)
if err != nil {
return nil, errorsutil.WithMessage(err, fmt.Sprintf("error determing if kontainer driver [%s] was in use", driver.Status.DisplayName))
}
if len(clustersWithKontainerDriver) != 0 {
return nil, httperror.NewAPIError(httperror.MethodNotAllowed, "cluster Driver is in use by one or more clusters, delete clusters before deleting this driver")
}
return s.Store.Delete(apiContext, schema, id)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.2.13-rc3

搜索帮助