37 Star 403 Fork 75

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
globalnamespace_store.go 1.54 KB
一键复制 编辑 原始数据 按行查看 历史
Dan Ramich 提交于 2019-06-12 13:38 . goimport linting changes
package globalresource
import (
"fmt"
"strings"
"github.com/rancher/norman/types"
"github.com/rancher/norman/types/convert"
"github.com/rancher/norman/types/values"
"github.com/rancher/rancher/pkg/namespace"
v1 "github.com/rancher/types/apis/core/v1"
client "github.com/rancher/types/client/management/v3"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
//GlobalNamespaceStore makes sure that the global resources are assigned to a global namespace, it creates one if not already present.
type GlobalNamespaceStore struct {
types.Store
NamespaceInterface v1.NamespaceInterface
}
func (s *GlobalNamespaceStore) Create(apiContext *types.APIContext, schema *types.Schema, data map[string]interface{}) (map[string]interface{}, error) {
ns, ok := values.GetValue(data, client.PreferenceFieldNamespaceId)
if ok && !strings.EqualFold(convert.ToString(ns), namespace.GlobalNamespace) {
return nil, fmt.Errorf("Error creating Global resource, cannot assign to %v since already assigned to %v namespace", namespace.GlobalNamespace, ns)
} else if !ok {
err := s.ensureGlobalNamespace(apiContext)
if err != nil {
return nil, err
}
data[client.PreferenceFieldNamespaceId] = namespace.GlobalNamespace
}
return s.Store.Create(apiContext, schema, data)
}
func (s *GlobalNamespaceStore) ensureGlobalNamespace(apiContext *types.APIContext) error {
_, err := s.NamespaceInterface.Get(namespace.GlobalNamespace, metav1.GetOptions{})
if err != nil {
return fmt.Errorf("Error listing global namespace %v: %v", namespace.GlobalNamespace, err)
}
return nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.2.9-rc2

搜索帮助