37 Star 407 Fork 74

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
settings.go 2.61 KB
一键复制 编辑 原始数据 按行查看 历史
Dan Ramich 提交于 2019-06-12 13:38 . goimport linting changes
package settings
import (
"fmt"
"os"
"github.com/rancher/rancher/pkg/settings"
v3 "github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config"
"k8s.io/apimachinery/pkg/api/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func Register(context *config.ScaledContext) error {
sp := &settingsProvider{
settings: context.Management.Settings(""),
settingsLister: context.Management.Settings("").Controller().Lister(),
}
if err := settings.SetProvider(sp); err != nil {
return err
}
return nil
}
type settingsProvider struct {
settings v3.SettingInterface
settingsLister v3.SettingLister
fallback map[string]string
}
func (s *settingsProvider) Get(name string) string {
value := os.Getenv(settings.GetEnvKey(name))
if value != "" {
return value
}
obj, err := s.settingsLister.Get("", name)
if err != nil {
return s.fallback[name]
}
if obj.Value == "" {
return obj.Default
}
return obj.Value
}
func (s *settingsProvider) Set(name, value string) error {
envValue := os.Getenv(settings.GetEnvKey(name))
if envValue != "" {
return fmt.Errorf("setting %s can not be set because it is from environment variable", name)
}
obj, err := s.settings.Get(name, v1.GetOptions{})
if err != nil {
return err
}
obj.Value = value
_, err = s.settings.Update(obj)
return err
}
func (s *settingsProvider) SetIfUnset(name, value string) error {
obj, err := s.settings.Get(name, v1.GetOptions{})
if err != nil {
return err
}
if obj.Value != "" {
return nil
}
obj.Value = value
_, err = s.settings.Update(obj)
return err
}
func (s *settingsProvider) SetAll(settingsMap map[string]settings.Setting) error {
fallback := map[string]string{}
for name, setting := range settingsMap {
key := settings.GetEnvKey(name)
value := os.Getenv(key)
obj, err := s.settings.Get(setting.Name, v1.GetOptions{})
if errors.IsNotFound(err) {
newSetting := &v3.Setting{
ObjectMeta: v1.ObjectMeta{
Name: setting.Name,
},
Default: setting.Default,
}
if value != "" {
newSetting.Value = value
}
fallback[newSetting.Name] = newSetting.Value
_, err := s.settings.Create(newSetting)
if err != nil {
return err
}
} else if err != nil {
return err
} else {
update := false
if obj.Default != setting.Default {
obj.Default = setting.Default
update = true
}
if value != "" && obj.Value != value {
obj.Value = value
update = true
}
fallback[obj.Name] = obj.Value
if update {
_, err := s.settings.Update(obj)
if err != nil {
return err
}
}
}
}
s.fallback = fallback
return nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.2.7-rc6

搜索帮助