37 Star 411 Fork 76

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
store.go 2.89 KB
一键复制 编辑 原始数据 按行查看 历史
package nodetemplate
import (
"fmt"
"github.com/rancher/norman/httperror"
"github.com/rancher/norman/types"
"github.com/rancher/norman/types/convert"
"github.com/rancher/norman/types/values"
"github.com/rancher/rancher/pkg/namespace"
"github.com/rancher/rancher/pkg/ref"
corev1 "github.com/rancher/types/apis/core/v1"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/labels"
"strings"
)
type Store struct {
types.Store
NodePoolLister v3.NodePoolLister
CloudCredentialLister corev1.SecretLister
}
func (s *Store) Delete(apiContext *types.APIContext, schema *types.Schema, id string) (map[string]interface{}, error) {
pools, err := s.NodePoolLister.List("", labels.Everything())
if err != nil {
return nil, err
}
for _, pool := range pools {
if pool.Spec.NodeTemplateName == id {
return nil, httperror.NewAPIError(httperror.MethodNotAllowed, "Template is in use by a node pool.")
}
}
return s.Store.Delete(apiContext, schema, id)
}
func (s *Store) Create(apiContext *types.APIContext, schema *types.Schema, data map[string]interface{}) (map[string]interface{}, error) {
if err := s.replaceCloudCredFields(data); err != nil {
return data, err
}
return s.Store.Create(apiContext, schema, data)
}
func (s *Store) Update(apiContext *types.APIContext, schema *types.Schema, data map[string]interface{}, id string) (map[string]interface{}, error) {
if err := s.replaceCloudCredFields(data); err != nil {
return data, err
}
return s.Store.Update(apiContext, schema, data, id)
}
func (s *Store) replaceCloudCredFields(data map[string]interface{}) error {
credID := convert.ToString(values.GetValueN(data, "cloudCredentialId"))
if credID == "" {
return nil
}
ns, name := ref.Parse(credID)
if ns == "" || name == "" {
return fmt.Errorf("invalid credID %s", credID)
}
cred, err := s.CloudCredentialLister.Get(namespace.GlobalNamespace, name)
if err != nil {
return fmt.Errorf("error getting cloud cred %s: %v", credID, err)
}
if len(cred.Data) == 0 {
return fmt.Errorf("empty credID data %s", credID)
}
configName, credConfigName := "", ""
for key := range cred.Data {
splitKey := strings.SplitN(key, "-", 2)
if len(splitKey) == 2 && strings.HasSuffix(splitKey[0], "credentialConfig") {
configName = strings.Replace(splitKey[0], "credential", "", 1)
credConfigName = splitKey[0]
break
}
}
if configName == "" {
return fmt.Errorf("empty configName for credID %s", configName)
}
toReplace := convert.ToMapInterface(values.GetValueN(data, configName))
if len(toReplace) == 0 {
return nil
}
var fields []string
for key := range cred.Data {
splitKey := strings.SplitN(key, "-", 2)
if len(splitKey) == 2 && splitKey[0] == credConfigName {
delete(toReplace, splitKey[1])
fields = append(fields, splitKey[1])
}
}
logrus.Debugf("replaceCloudCredFields: %v for credID %s", fields, credID)
return nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.3.0-alpha1

搜索帮助

0d507c66 1850385 C8b1a773 1850385