forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 1
/
listener_controller.go
158 lines (138 loc) · 4.06 KB
/
listener_controller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package dynamiclistener
import (
"context"
"net/http"
"strings"
"github.com/rancher/norman/types/convert"
"github.com/rancher/rancher/pkg/cert"
"github.com/rancher/rancher/pkg/dynamiclistener"
"github.com/rancher/rancher/pkg/settings"
"github.com/rancher/types/apis/core/v1"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
)
type Controller struct {
listenConfig v3.ListenConfigInterface
listenConfigLister v3.ListenConfigLister
secrets v1.SecretInterface
server dynamiclistener.ServerInterface
}
type storageUpdater interface {
Update(*v3.ListenConfig) (*v3.ListenConfig, error)
}
type storage struct {
storageUpdater
v3.ListenConfigLister
}
func Start(ctx context.Context, context *config.ScaledContext, httpPort, httpsPort int, handler http.Handler) {
s := storage{
storageUpdater: context.Management.ListenConfigs(""),
ListenConfigLister: context.Management.ListenConfigs("").Controller().Lister(),
}
c := &Controller{
server: dynamiclistener.NewServer(ctx, s, handler, httpPort, httpsPort),
secrets: context.Core.Secrets(""),
listenConfig: context.Management.ListenConfigs(""),
listenConfigLister: context.Management.ListenConfigs("").Controller().Lister(),
}
context.Management.ListenConfigs("").AddHandler(ctx, "listener", c.sync)
go func() {
<-ctx.Done()
c.server.Shutdown()
}()
}
func (c *Controller) sync(key string, listener *v3.ListenConfig) (runtime.Object, error) {
if listener == nil {
return nil, nil
}
if listener.Enabled {
return nil, c.enable(listener)
}
c.server.Disable(listener)
allConfigs, err := c.listenConfigLister.List("", labels.Everything())
if err != nil {
return nil, 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 nil, c.enable(listener)
}
return nil, 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 {
caCerts := strings.TrimSpace(listener.CACerts)
settings.CACerts.Set(caCerts)
if listener.Key != "" && caCerts != "" && listener.Cert != "" {
certInfo, err := cert.Info(listener.Cert+"\n"+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 c.saveCACertToSecret(listener.CAKey, listener.CACert)
}
func (c *Controller) saveCACertToSecret(key, cert string) error {
if key == "" || cert == "" {
return nil
}
secret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "tls-rancher",
Namespace: "cattle-system",
},
StringData: map[string]string{
"tls.key": key,
"tls.crt": cert,
},
Type: corev1.SecretTypeTLS,
}
existing, err := c.secrets.GetNamespaced(secret.Namespace, secret.Name, metav1.GetOptions{})
if errors.IsNotFound(err) {
_, err = c.secrets.Create(secret)
return err
} else if err != nil {
return err
}
existing.StringData = secret.StringData
_, err = c.secrets.Update(existing)
return err
}