37 Star 404 Fork 74

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
listener_controller.go 2.72 KB
一键复制 编辑 原始数据 按行查看 历史
Darren Shepherd 提交于 2018-04-04 03:20 . Use lister for cli-config
package dynamiclistener
import (
"net/http"
"context"
"github.com/rancher/norman/types/convert"
"github.com/rancher/rancher/pkg/cert"
"github.com/rancher/rancher/pkg/settings"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config"
"k8s.io/apimachinery/pkg/labels"
)
type Controller struct {
listenConfig v3.ListenConfigInterface
listenConfigLister v3.ListenConfigLister
server *server
}
func Start(ctx context.Context, context *config.ScaledContext, httpPort, httpsPort int, handler http.Handler) {
c := &Controller{
server: newServer(ctx, context.Management.ListenConfigs(""),
context.Management.ListenConfigs("").Controller().Lister(),
handler, httpPort, httpsPort),
listenConfig: context.Management.ListenConfigs(""),
listenConfigLister: context.Management.ListenConfigs("").Controller().Lister(),
}
context.Management.ListenConfigs("").AddHandler("listener", c.sync)
go func() {
<-ctx.Done()
c.server.Shutdown()
}()
}
func (c *Controller) sync(key string, listener *v3.ListenConfig) error {
if listener == nil {
return nil
}
if listener.Enabled {
return c.enable(listener)
}
c.server.Disable(listener)
allConfigs, err := c.listenConfigLister.List("", labels.Everything())
if err != nil {
return err
}
var lastConfig *v3.ListenConfig
for _, config := range allConfigs {
if !config.Enabled || config.DeletionTimestamp != nil {
continue
}
if lastConfig == nil || lastConfig.CreationTimestamp.Before(&config.CreationTimestamp) {
lastConfig = config
}
}
if lastConfig != nil {
return c.enable(listener)
}
return nil
}
func (c *Controller) enable(listener *v3.ListenConfig) error {
current, err := c.server.Enable(listener)
if err != nil {
return err
}
if current {
return c.updateCurrent(listener)
}
return nil
}
func (c *Controller) updateCurrent(listener *v3.ListenConfig) error {
settings.CACerts.Set(listener.CACerts)
if listener.Key != "" && listener.CACerts != "" && listener.Cert != "" {
certInfo, err := cert.Info(listener.Cert+"\n"+listener.CACerts, listener.Key)
if err != nil {
return err
}
if certInfo.SerialNumber != listener.SerialNumber {
copy := listener.DeepCopy()
copy.CertFingerprint = certInfo.Fingerprint
copy.CN = certInfo.CN
copy.Version = certInfo.Version
copy.ExpiresAt = convert.ToString(certInfo.ExpiresAt)
copy.Issuer = certInfo.Issuer
copy.IssuedAt = convert.ToString(certInfo.IssuedAt)
copy.Algorithm = certInfo.Algorithm
copy.SerialNumber = certInfo.SerialNumber
copy.KeySize = certInfo.KeySize
copy.SubjectAlternativeNames = certInfo.SubjectAlternativeNames
_, err := c.listenConfig.Update(copy)
if err != nil {
return err
}
}
}
return nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.0.1-rc3

搜索帮助