代码拉取完成,页面将自动刷新
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
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。