37 Star 407 Fork 74

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
password.go 1.88 KB
一键复制 编辑 原始数据 按行查看 历史
kinarashah 提交于 2018-12-28 10:51 . password field fixes
package common
import (
"fmt"
"github.com/rancher/rancher/pkg/namespace"
"reflect"
"strings"
corev1 "github.com/rancher/types/apis/core/v1"
"k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const SecretsNamespace = namespace.GlobalNamespace
func CreateOrUpdateSecrets(secrets corev1.SecretInterface, secretInfo string, field string, authType string) error {
if secretInfo == "" {
return nil
}
name := fmt.Sprintf("%s-%s", authType, field)
secret := &v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: SecretsNamespace,
},
StringData: map[string]string{field: secretInfo},
Type: v1.SecretTypeOpaque,
}
curr, err := secrets.Controller().Lister().Get(SecretsNamespace, name)
if err != nil && !apierrors.IsNotFound(err) {
return fmt.Errorf("error getting secret for %s : %v", name, err)
}
if err == nil && !reflect.DeepEqual(curr.Data, secret.Data) {
_, err = secrets.Update(secret)
if err != nil {
return fmt.Errorf("error updating secret %s: %v", name, err)
}
} else if apierrors.IsNotFound(err) {
_, err = secrets.Create(secret)
if err != nil && !apierrors.IsAlreadyExists(err) {
return fmt.Errorf("error creating secret %s %v", name, err)
}
}
return nil
}
func ReadFromSecret(secrets corev1.SecretInterface, secretInfo string, field string) (string, error) {
split := strings.SplitN(secretInfo, ":", 2)
if len(split) == 2 {
secret, err := secrets.GetNamespaced(split[0], split[1], metav1.GetOptions{})
if err != nil {
return "", fmt.Errorf("error getting secret %s %v", secretInfo, err)
}
for key, val := range secret.Data {
if key == field {
return string(val), nil
}
}
}
return secretInfo, nil
}
func GetName(configType string, field string) string {
return fmt.Sprintf("%s:%s-%s", SecretsNamespace, strings.ToLower(configType), field)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.2.0-rc12

搜索帮助