37 Star 411 Fork 76

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
cluster_store.go 6.61 KB
一键复制 编辑 原始数据 按行查看 历史
package cluster
import (
"fmt"
"net/http"
"strconv"
"strings"
"sync"
"github.com/rancher/norman/api/access"
"github.com/rancher/norman/httperror"
"github.com/rancher/norman/store/transform"
"github.com/rancher/norman/types"
"github.com/rancher/norman/types/convert"
"github.com/rancher/norman/types/values"
ccluster "github.com/rancher/rancher/pkg/api/customization/cluster"
"github.com/rancher/rancher/pkg/clustermanager"
"github.com/rancher/rancher/pkg/controllers/management/clusterstatus"
"github.com/rancher/rancher/pkg/settings"
managementv3 "github.com/rancher/types/client/management/v3"
"github.com/rancher/types/config"
)
type Store struct {
types.Store
ShellHandler types.RequestHandler
mu sync.Mutex
}
func SetClusterStore(schema *types.Schema, mgmt *config.ScaledContext, clusterManager *clustermanager.Manager, k8sProxy http.Handler) {
t := &transform.Store{
Store: schema.Store,
Transformer: func(apiContext *types.APIContext, schema *types.Schema, data map[string]interface{}, opt *types.QueryOptions) (map[string]interface{}, error) {
data = transformSetNilSnapshotFalse(data)
return data, nil
},
}
linkHandler := &ccluster.ShellLinkHandler{
Proxy: k8sProxy,
ClusterManager: clusterManager,
}
s := &Store{
Store: t,
ShellHandler: linkHandler.LinkHandler,
}
schema.Store = s
}
func transformSetNilSnapshotFalse(data map[string]interface{}) map[string]interface{} {
var (
etcd interface{}
found bool
)
etcd, found = values.GetValue(data, "appliedSpec", "rancherKubernetesEngineConfig", "services", "etcd")
if found {
etcd := convert.ToMapInterface(etcd)
val, found := values.GetValue(etcd, "snapshot")
if !found || val == nil {
values.PutValue(data, false, "appliedSpec", "rancherKubernetesEngineConfig", "services", "etcd", "snapshot")
}
}
etcd, found = values.GetValue(data, "rancherKubernetesEngineConfig", "services", "etcd")
if found {
etcd := convert.ToMapInterface(etcd)
val, found := values.GetValue(etcd, "snapshot")
if !found || val == nil {
values.PutValue(data, false, "rancherKubernetesEngineConfig", "services", "etcd", "snapshot")
}
}
return data
}
func (r *Store) ByID(apiContext *types.APIContext, schema *types.Schema, id string) (map[string]interface{}, error) {
// Really we want a link handler but the URL parse makes it impossible to add links to clusters for now. So this
// is basically a hack
if apiContext.Query.Get("shell") == "true" {
return nil, r.ShellHandler(apiContext, nil)
}
return r.Store.ByID(apiContext, schema, id)
}
func (r *Store) Create(apiContext *types.APIContext, schema *types.Schema, data map[string]interface{}) (map[string]interface{}, error) {
name := convert.ToString(data["name"])
if name == "" {
return nil, httperror.NewFieldAPIError(httperror.MissingRequired, "Cluster name", "")
}
r.mu.Lock()
defer r.mu.Unlock()
if err := canUseClusterName(apiContext, name); err != nil {
return nil, err
}
setKubernetesVersion(data)
if err := validateNetworkFlag(data, true); err != nil {
return nil, httperror.NewFieldAPIError(httperror.InvalidOption, "enableNetworkPolicy", err.Error())
}
if eksConfig := data[managementv3.ClusterFieldAmazonElasticContainerServiceConfig]; eksConfig != nil {
sessionToken, _ := values.GetValue(data, managementv3.ClusterFieldAmazonElasticContainerServiceConfig, managementv3.AmazonElasticContainerServiceConfigFieldSessionToken)
annotation, _ := values.GetValue(data, managementv3.ClusterFieldAnnotations)
m := toMap(annotation)
m[clusterstatus.TemporaryCredentialsAnnotationKey] = strconv.FormatBool(sessionToken != nil)
values.PutValue(data, m, managementv3.ClusterFieldAnnotations)
}
return r.Store.Create(apiContext, schema, data)
}
func toMap(rawMap interface{}) map[string]interface{} {
if theMap, ok := rawMap.(map[string]interface{}); ok {
return theMap
}
return make(map[string]interface{})
}
func (r *Store) Update(apiContext *types.APIContext, schema *types.Schema, data map[string]interface{}, id string) (map[string]interface{}, error) {
updatedName := convert.ToString(data["name"])
if updatedName == "" {
return nil, httperror.NewFieldAPIError(httperror.MissingRequired, "Cluster name", "")
}
existingCluster, err := r.ByID(apiContext, schema, id)
if err != nil {
return nil, err
}
clusterName, ok := existingCluster["name"].(string)
if !ok {
clusterName = ""
}
if !strings.EqualFold(updatedName, clusterName) {
r.mu.Lock()
defer r.mu.Unlock()
if err := canUseClusterName(apiContext, updatedName); err != nil {
return nil, err
}
}
setKubernetesVersion(data)
if err := validateNetworkFlag(data, false); err != nil {
return nil, httperror.NewFieldAPIError(httperror.InvalidOption, "enableNetworkPolicy", err.Error())
}
return r.Store.Update(apiContext, schema, data, id)
}
func canUseClusterName(apiContext *types.APIContext, requestedName string) error {
var clusters []managementv3.Cluster
if err := access.List(apiContext, apiContext.Version, managementv3.ClusterType, &types.QueryOptions{}, &clusters); err != nil {
return err
}
for _, c := range clusters {
if c.Removed == "" && strings.EqualFold(c.Name, requestedName) {
//cluster exists by this name
return httperror.NewFieldAPIError(httperror.NotUnique, "Cluster name", "")
}
}
return nil
}
func setKubernetesVersion(data map[string]interface{}) {
rkeConfig, ok := values.GetValue(data, "rancherKubernetesEngineConfig")
if ok && rkeConfig != nil {
k8sVersion := values.GetValueN(data, "rancherKubernetesEngineConfig", "kubernetesVersion")
if k8sVersion == nil || k8sVersion == "" {
//set k8s version to system default on the spec
defaultVersion := settings.KubernetesVersion.Get()
values.PutValue(data, defaultVersion, "rancherKubernetesEngineConfig", "kubernetesVersion")
}
}
}
func validateNetworkFlag(data map[string]interface{}, create bool) error {
enableNetworkPolicy := values.GetValueN(data, "enableNetworkPolicy")
rkeConfig := values.GetValueN(data, "rancherKubernetesEngineConfig")
plugin := convert.ToString(values.GetValueN(convert.ToMapInterface(rkeConfig), "network", "plugin"))
if enableNetworkPolicy == nil {
// setting default values for new clusters if value not passed
values.PutValue(data, false, "enableNetworkPolicy")
} else if value := convert.ToBool(enableNetworkPolicy); value {
if rkeConfig == nil {
if create {
values.PutValue(data, false, "enableNetworkPolicy")
return nil
}
return fmt.Errorf("enableNetworkPolicy should be false for non-RKE clusters")
}
if plugin != "canal" {
return fmt.Errorf("plugin %s should have enableNetworkPolicy %v", plugin, !value)
}
}
return nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.1.2-rc15

搜索帮助

0d507c66 1850385 C8b1a773 1850385