-
Notifications
You must be signed in to change notification settings - Fork 84
/
controllers.go
211 lines (181 loc) · 5.89 KB
/
controllers.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package controllers
import (
"context"
"time"
"github.com/k3s-io/helm-controller/pkg/controllers/chart"
"github.com/k3s-io/helm-controller/pkg/controllers/common"
"github.com/k3s-io/helm-controller/pkg/generated/controllers/helm.cattle.io"
helmcontroller "github.com/k3s-io/helm-controller/pkg/generated/controllers/helm.cattle.io/v1"
"github.com/rancher/lasso/pkg/cache"
"github.com/rancher/lasso/pkg/client"
"github.com/rancher/lasso/pkg/controller"
"github.com/rancher/wrangler/v2/pkg/apply"
"github.com/rancher/wrangler/v2/pkg/generated/controllers/batch"
batchcontroller "github.com/rancher/wrangler/v2/pkg/generated/controllers/batch/v1"
"github.com/rancher/wrangler/v2/pkg/generated/controllers/core"
corecontroller "github.com/rancher/wrangler/v2/pkg/generated/controllers/core/v1"
"github.com/rancher/wrangler/v2/pkg/generated/controllers/rbac"
rbaccontroller "github.com/rancher/wrangler/v2/pkg/generated/controllers/rbac/v1"
"github.com/rancher/wrangler/v2/pkg/generic"
"github.com/rancher/wrangler/v2/pkg/leader"
"github.com/rancher/wrangler/v2/pkg/ratelimit"
"github.com/rancher/wrangler/v2/pkg/schemes"
"github.com/rancher/wrangler/v2/pkg/start"
"github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes"
typedv1 "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/record"
"k8s.io/client-go/util/workqueue"
"k8s.io/klog"
)
type appContext struct {
helmcontroller.Interface
K8s kubernetes.Interface
Core corecontroller.Interface
RBAC rbaccontroller.Interface
Batch batchcontroller.Interface
Apply apply.Apply
EventBroadcaster record.EventBroadcaster
ClientConfig clientcmd.ClientConfig
starters []start.Starter
}
func (a *appContext) start(ctx context.Context) error {
return start.All(ctx, 50, a.starters...)
}
func Register(ctx context.Context, systemNamespace, controllerName string, cfg clientcmd.ClientConfig, opts common.Options) error {
appCtx, err := newContext(cfg, systemNamespace, opts)
if err != nil {
return err
}
appCtx.EventBroadcaster.StartLogging(logrus.Infof)
appCtx.EventBroadcaster.StartRecordingToSink(&typedv1.EventSinkImpl{
Interface: appCtx.K8s.CoreV1().Events(systemNamespace),
})
recorder := appCtx.EventBroadcaster.NewRecorder(schemes.All, corev1.EventSource{
Component: "helm-controller",
Host: opts.NodeName,
})
if len(controllerName) == 0 {
controllerName = "helm-controller"
}
// apply custom DefaultJobImage option to Helm before starting charts controller
if opts.DefaultJobImage != "" {
chart.DefaultJobImage = opts.DefaultJobImage
}
chart.Register(ctx,
systemNamespace,
controllerName,
opts.JobClusterRole,
"6443",
appCtx.K8s,
appCtx.Apply,
recorder,
appCtx.HelmChart(),
appCtx.HelmChart().Cache(),
appCtx.HelmChartConfig(),
appCtx.HelmChartConfig().Cache(),
appCtx.Batch.Job(),
appCtx.Batch.Job().Cache(),
appCtx.RBAC.ClusterRoleBinding(),
appCtx.Core.ServiceAccount(),
appCtx.Core.ConfigMap(),
appCtx.Core.Secret())
klog.Infof("Starting helm controller with %d threads", opts.Threadiness)
klog.Infof("Using cluster role '%s' for jobs managing helm charts", opts.JobClusterRole)
klog.Infof("Using default image '%s' for jobs managing helm charts", chart.DefaultJobImage)
if len(systemNamespace) == 0 {
klog.Info("Starting helm controller with no namespace")
} else {
klog.Infof("Starting helm controller in namespace %s", systemNamespace)
}
leader.RunOrDie(ctx, systemNamespace, "helm-controller-lock", appCtx.K8s, func(ctx context.Context) {
if err := appCtx.start(ctx); err != nil {
klog.Fatal(err)
}
klog.Info("All controllers have been started")
})
return nil
}
func controllerFactory(rest *rest.Config) (controller.SharedControllerFactory, error) {
rateLimit := workqueue.NewItemExponentialFailureRateLimiter(5*time.Millisecond, 60*time.Second)
clientFactory, err := client.NewSharedClientFactory(rest, nil)
if err != nil {
return nil, err
}
cacheFactory := cache.NewSharedCachedFactory(clientFactory, nil)
return controller.NewSharedControllerFactory(cacheFactory, &controller.SharedControllerFactoryOptions{
DefaultRateLimiter: rateLimit,
DefaultWorkers: 50,
}), nil
}
func newContext(cfg clientcmd.ClientConfig, systemNamespace string, opts common.Options) (*appContext, error) {
client, err := cfg.ClientConfig()
if err != nil {
return nil, err
}
client.RateLimiter = ratelimit.None
apply, err := apply.NewForConfig(client)
if err != nil {
return nil, err
}
apply = apply.WithSetOwnerReference(false, false)
k8s, err := kubernetes.NewForConfig(client)
if err != nil {
return nil, err
}
scf, err := controllerFactory(client)
if err != nil {
return nil, err
}
core, err := core.NewFactoryFromConfigWithOptions(client, &generic.FactoryOptions{
SharedControllerFactory: scf,
Namespace: systemNamespace,
})
if err != nil {
return nil, err
}
corev := core.Core().V1()
batch, err := batch.NewFactoryFromConfigWithOptions(client, &generic.FactoryOptions{
SharedControllerFactory: scf,
Namespace: systemNamespace,
})
if err != nil {
return nil, err
}
batchv := batch.Batch().V1()
rbac, err := rbac.NewFactoryFromConfigWithOptions(client, &generic.FactoryOptions{
SharedControllerFactory: scf,
Namespace: systemNamespace,
})
if err != nil {
return nil, err
}
rbacv := rbac.Rbac().V1()
helm, err := helm.NewFactoryFromConfigWithOptions(client, &generic.FactoryOptions{
SharedControllerFactory: scf,
Namespace: systemNamespace,
})
if err != nil {
return nil, err
}
helmv := helm.Helm().V1()
return &appContext{
Interface: helmv,
K8s: k8s,
Core: corev,
Batch: batchv,
RBAC: rbacv,
Apply: apply,
EventBroadcaster: record.NewBroadcaster(),
ClientConfig: cfg,
starters: []start.Starter{
core,
batch,
rbac,
helm,
},
}, nil
}