37 Star 411 Fork 76

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
listenconfig_data.go 2.20 KB
一键复制 编辑 原始数据 按行查看 历史
package app
import (
"bytes"
"crypto/x509"
"encoding/pem"
"github.com/rancher/types/config"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/util/cert"
)
func addListenConfig(management *config.ManagementContext, cfg Config) error {
userCACerts := cfg.ListenConfig.CACerts
existing, err := management.Management.ListenConfigs("").Get(cfg.ListenConfig.Name, v1.GetOptions{})
if err != nil && !apierrors.IsNotFound(err) {
return err
}
if apierrors.IsNotFound(err) {
existing = nil
}
if existing != nil {
if cfg.ListenConfig.Cert == "" {
cfg.ListenConfig.Cert = existing.Cert
cfg.ListenConfig.CACerts = existing.CACerts
cfg.ListenConfig.Key = existing.Key
cfg.ListenConfig.CAKey = existing.CAKey
cfg.ListenConfig.CACert = existing.CACert
cfg.ListenConfig.KnownIPs = existing.KnownIPs
}
}
if (cfg.ListenConfig.Key == "" || cfg.ListenConfig.Cert == "") && cfg.ListenConfig.CACert == "" && cfg.ListenConfig.Mode != "acme" {
caKey, err := cert.NewPrivateKey()
if err != nil {
return err
}
caCert, err := cert.NewSelfSignedCACert(cert.Config{
CommonName: "cattle-ca",
Organization: []string{"the-ranch"},
}, caKey)
if err != nil {
return err
}
caCertBuffer := bytes.Buffer{}
if err := pem.Encode(&caCertBuffer, &pem.Block{
Type: cert.CertificateBlockType,
Bytes: caCert.Raw,
}); err != nil {
return err
}
caKeyBuffer := bytes.Buffer{}
if err := pem.Encode(&caKeyBuffer, &pem.Block{
Type: cert.RSAPrivateKeyBlockType,
Bytes: x509.MarshalPKCS1PrivateKey(caKey),
}); err != nil {
return err
}
cfg.ListenConfig.CACert = string(caCertBuffer.Bytes())
cfg.ListenConfig.CACerts = cfg.ListenConfig.CACert
cfg.ListenConfig.CAKey = string(caKeyBuffer.Bytes())
}
if cfg.NoCACerts || cfg.ListenConfig.Mode == "acme" {
cfg.ListenConfig.CACerts = ""
} else if userCACerts != "" {
cfg.ListenConfig.CACerts = userCACerts
}
if existing == nil {
_, err := management.Management.ListenConfigs("").Create(cfg.ListenConfig)
return err
}
cfg.ListenConfig.ResourceVersion = existing.ResourceVersion
_, err = management.Management.ListenConfigs("").Update(cfg.ListenConfig)
return err
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.0.2-rc1

搜索帮助