37 Star 411 Fork 76

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
secretmanager.go 1.77 KB
一键复制 编辑 原始数据 按行查看 历史
Aiwantaozi 提交于 2019-01-09 12:03 . Refactor logging
package configsyncer
import (
"reflect"
"github.com/pkg/errors"
loggingconfig "github.com/rancher/rancher/pkg/controllers/user/logging/config"
"github.com/rancher/types/apis/core/v1"
"github.com/rancher/types/config"
k8scorev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// This manager is responsible for init/update config in secret
type SecretManager struct {
secrets v1.SecretInterface
}
func NewSecretManager(cluster *config.UserContext) *SecretManager {
return &SecretManager{
secrets: cluster.Core.Secrets(loggingconfig.LoggingNamespace),
}
}
func (s *SecretManager) newSecret(name, namespace string, data map[string][]byte) (err error) {
secret, err := s.secrets.Controller().Lister().Get(namespace, name)
if err != nil && !apierrors.IsNotFound(err) {
return err
}
if secret != nil {
return nil
}
secret = &k8scorev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
Data: data,
}
if _, err = s.secrets.Create(secret); err != nil && !apierrors.IsAlreadyExists(err) {
return err
}
return nil
}
func (s *SecretManager) updateSecret(name, namespace string, data map[string][]byte) error {
existSecret, err := s.secrets.GetNamespaced(namespace, name, metav1.GetOptions{})
if err != nil {
return errors.Wrapf(err, "get secret %s:%s failed", namespace, name)
}
newSecret := existSecret.DeepCopy()
if newSecret.Data == nil {
newSecret.Data = make(map[string][]byte)
}
for k, v := range data {
newSecret.Data[k] = v
}
if reflect.DeepEqual(existSecret.Data, newSecret.Data) {
return nil
}
if _, err = s.secrets.Update(newSecret); err != nil {
return errors.Wrapf(err, "update secret %s:%s failed", namespace, name)
}
return nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.2.0-alpha5

搜索帮助

0d507c66 1850385 C8b1a773 1850385