37 Star 403 Fork 75

GVPrancher/rancher

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
Clone or Download
store.go 2.03 KB
Copy Edit Raw Blame History
Dan Ramich authored 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.5-rc9

Search