forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
192 lines (157 loc) · 4.91 KB
/
app.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package app
import (
"context"
"github.com/rancher/norman/leader"
"github.com/rancher/norman/pkg/k8scheck"
"github.com/rancher/rancher/pkg/audit"
"github.com/rancher/rancher/pkg/auth/providerrefresh"
"github.com/rancher/rancher/pkg/auth/providers/common"
"github.com/rancher/rancher/pkg/auth/tokens"
"github.com/rancher/rancher/pkg/clustermanager"
managementController "github.com/rancher/rancher/pkg/controllers/management"
"github.com/rancher/rancher/pkg/dialer"
"github.com/rancher/rancher/pkg/settings"
"github.com/rancher/rancher/pkg/telemetry"
"github.com/rancher/rancher/pkg/tls"
"github.com/rancher/rancher/pkg/tunnelserver"
"github.com/rancher/rancher/server"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config"
"github.com/sirupsen/logrus"
"k8s.io/client-go/rest"
)
type Config struct {
ACMEDomains []string
AddLocal string
Embedded bool
KubeConfig string
HTTPListenPort int
HTTPSListenPort int
K8sMode string
Debug bool
NoCACerts bool
ListenConfig *v3.ListenConfig
AuditLogPath string
AuditLogMaxage int
AuditLogMaxsize int
AuditLogMaxbackup int
AuditLevel int
}
func buildScaledContext(ctx context.Context, kubeConfig rest.Config, cfg *Config) (*config.ScaledContext, *clustermanager.Manager, error) {
scaledContext, err := config.NewScaledContext(kubeConfig)
if err != nil {
return nil, nil, err
}
scaledContext.LocalConfig = &kubeConfig
cfg.ListenConfig, err = tls.ReadTLSConfig(cfg.ACMEDomains, cfg.NoCACerts)
if err != nil {
return nil, nil, err
}
if err := k8scheck.Wait(ctx, kubeConfig); err != nil {
return nil, nil, err
}
dialerFactory, err := dialer.NewFactory(scaledContext)
if err != nil {
return nil, nil, err
}
scaledContext.Dialer = dialerFactory
scaledContext.PeerManager, err = tunnelserver.NewPeerManager(ctx, scaledContext, dialerFactory.TunnelServer)
if err != nil {
return nil, nil, err
}
manager := clustermanager.NewManager(cfg.HTTPSListenPort, scaledContext)
scaledContext.AccessControl = manager
scaledContext.ClientGetter = manager
userManager, err := common.NewUserManager(scaledContext)
if err != nil {
return nil, nil, err
}
scaledContext.UserManager = userManager
return scaledContext, manager, nil
}
func Run(ctx context.Context, kubeConfig rest.Config, cfg *Config) error {
scaledContext, clusterManager, err := buildScaledContext(ctx, kubeConfig, cfg)
if err != nil {
return err
}
auditLogWriter := audit.NewLogWriter(cfg.AuditLogPath, cfg.AuditLevel, cfg.AuditLogMaxage, cfg.AuditLogMaxbackup, cfg.AuditLogMaxsize)
if err := server.Start(ctx, cfg.HTTPListenPort, cfg.HTTPSListenPort, localClusterEnabled(*cfg), scaledContext, clusterManager, auditLogWriter); err != nil {
return err
}
if err := scaledContext.Start(ctx); err != nil {
return err
}
go leader.RunOrDie(ctx, "", "cattle-controllers", scaledContext.K8sClient, func(ctx context.Context) {
if scaledContext.PeerManager != nil {
scaledContext.PeerManager.Leader()
}
if err := telemetry.Start(ctx, cfg.HTTPSListenPort, scaledContext); err != nil {
panic(err)
}
management, err := scaledContext.NewManagementContext()
if err != nil {
panic(err)
}
managementController.Register(ctx, management, scaledContext.ClientGetter.(*clustermanager.Manager))
if err := management.Start(ctx); err != nil {
panic(err)
}
if err := addData(management, *cfg); err != nil {
panic(err)
}
tokens.StartPurgeDaemon(ctx, management)
cronTime := settings.AuthUserInfoResyncCron.Get()
maxAge := settings.AuthUserInfoMaxAgeSeconds.Get()
providerrefresh.StartRefreshDaemon(ctx, scaledContext, management, cronTime, maxAge)
logrus.Infof("Rancher startup complete")
<-ctx.Done()
})
<-ctx.Done()
if auditLogWriter != nil {
auditLogWriter.Output.Close()
}
return ctx.Err()
}
func addData(management *config.ManagementContext, cfg Config) error {
if err := addListenConfig(management, cfg); err != nil {
return err
}
adminName, err := addRoles(management)
if err != nil {
return err
}
if localClusterEnabled(cfg) {
if err := addLocalCluster(cfg.Embedded, adminName, management); err != nil {
return err
}
} else if cfg.AddLocal == "false" {
if err := removeLocalCluster(management); err != nil {
return err
}
}
if err := addAuthConfigs(management); err != nil {
return err
}
if err := addCatalogs(management); err != nil {
return err
}
if err := addSetting(); err != nil {
return err
}
if err := addDefaultPodSecurityPolicyTemplates(management); err != nil {
return err
}
if err := addKontainerDrivers(management); err != nil {
return err
}
if err := addCattleGlobalNamespace(management); err != nil {
return err
}
return addMachineDrivers(management)
}
func localClusterEnabled(cfg Config) bool {
if cfg.AddLocal == "true" || (cfg.AddLocal == "auto" && !cfg.Embedded) {
return true
}
return false
}