forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deployer.go
262 lines (232 loc) · 6.73 KB
/
deployer.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package deploy
import (
"github.com/pkg/errors"
"github.com/rancher/norman/controller"
"github.com/rancher/rancher/pkg/controllers/user/alert/manager"
"github.com/rancher/rancher/pkg/image"
"github.com/rancher/types/apis/apps/v1beta2"
"github.com/rancher/types/apis/core/v1"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config"
"gopkg.in/yaml.v2"
appsv1beta2 "k8s.io/api/apps/v1beta2"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
)
func NewDeployer(cluster *config.UserContext, manager *manager.Manager) *Deployer {
return &Deployer{
nsClient: cluster.Core.Namespaces(""),
appsClient: cluster.Apps.Deployments(""),
secretClient: cluster.Core.Secrets(""),
svcClient: cluster.Core.Services(""),
clusterAlertLister: cluster.Management.Management.ClusterAlerts(cluster.ClusterName).Controller().Lister(),
projectAlertLister: cluster.Management.Management.ProjectAlerts("").Controller().Lister(),
notifierLister: cluster.Management.Management.Notifiers(cluster.ClusterName).Controller().Lister(),
alertManager: manager,
clusterName: cluster.ClusterName,
}
}
type Deployer struct {
nsClient v1.NamespaceInterface
appsClient v1beta2.DeploymentInterface
secretClient v1.SecretInterface
svcClient v1.ServiceInterface
projectAlertLister v3.ProjectAlertLister
clusterAlertLister v3.ClusterAlertLister
notifierLister v3.NotifierLister
alertManager *manager.Manager
clusterName string
}
func (d *Deployer) ProjectSync(key string, alert *v3.ProjectAlert) error {
return d.sync()
}
func (d *Deployer) ClusterSync(key string, alert *v3.ClusterAlert) error {
return d.sync()
}
//deploy or clean up resources(alertmanager deployment, service, namespace) required by alerting.
func (d *Deployer) sync() error {
needDeploy, err := d.needDeploy()
if err != nil {
return errors.Wrapf(err, "Check alertmanager deployment")
}
if needDeploy {
return d.deploy()
}
return d.cleanup()
}
//only deploy the alertmanager when notifier is configured and alert is using it.
func (d *Deployer) needDeploy() (bool, error) {
notifiers, err := d.notifierLister.List("", labels.NewSelector())
if err != nil {
return false, err
}
if len(notifiers) == 0 {
return false, err
}
clusterAlerts, err := d.clusterAlertLister.List("", labels.NewSelector())
if err != nil {
return false, err
}
for _, alert := range clusterAlerts {
if len(alert.Spec.Recipients) > 0 {
return true, nil
}
}
projectAlerts, err := d.projectAlertLister.List("", labels.NewSelector())
if err != nil {
return false, nil
}
for _, alert := range projectAlerts {
if controller.ObjectInCluster(d.clusterName, alert) {
if len(alert.Spec.Recipients) > 0 {
return true, nil
}
}
}
return false, nil
}
func (d *Deployer) cleanup() error {
err := d.nsClient.Delete("cattle-alerting", &metav1.DeleteOptions{})
if err != nil && !apierrors.IsNotFound(err) {
return err
}
d.alertManager.IsDeploy = false
return nil
}
func (d *Deployer) deploy() error {
//TODO: cleanup resources while there is not any alert configured
ns := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "cattle-alerting",
},
}
if _, err := d.nsClient.Create(ns); err != nil && !apierrors.IsAlreadyExists(err) {
return errors.Wrapf(err, "Creating ns")
}
secret := d.getSecret()
if _, err := d.secretClient.Create(secret); err != nil && !apierrors.IsAlreadyExists(err) {
return errors.Wrapf(err, "Creating secret")
}
deployment := GetDeployment()
if _, err := d.appsClient.Create(deployment); err != nil && !apierrors.IsAlreadyExists(err) {
return errors.Wrapf(err, "Creating deployment")
}
service := getService()
if _, err := d.svcClient.Create(service); err != nil && !apierrors.IsAlreadyExists(err) {
return errors.Wrapf(err, "Creating service")
}
d.alertManager.IsDeploy = true
return nil
}
func (d *Deployer) getSecret() *corev1.Secret {
cfg := d.alertManager.GetDefaultConfig()
data, err := yaml.Marshal(cfg)
if err != nil {
return nil
}
return &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Namespace: "cattle-alerting",
Name: "alertmanager",
},
Data: map[string][]byte{
"config.yml": data,
"notification.tmpl": []byte(NotificationTmpl),
},
}
}
func getService() *corev1.Service {
return &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Namespace: "cattle-alerting",
Name: "alertmanager-svc",
},
Spec: corev1.ServiceSpec{
Selector: map[string]string{
"app": "alertmanager",
},
Ports: []corev1.ServicePort{
{
Name: "alertmanager",
Port: 9093,
},
},
},
}
}
func GetDeployment() *appsv1beta2.Deployment {
replicas := int32(1)
return &appsv1beta2.Deployment{
ObjectMeta: metav1.ObjectMeta{
Namespace: "cattle-alerting",
Name: "alertmanager",
},
Spec: appsv1beta2.DeploymentSpec{
Replicas: &replicas,
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{"app": "alertmanager"},
},
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{"app": "alertmanager"},
Name: "alertmanager",
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "alertmanager",
Image: image.Resolve(v3.ToolsSystemImages.AlertSystemImages.AlertManager),
Args: []string{"--config.file=/etc/alertmanager/config.yml", "--storage.path=/alertmanager"},
Ports: []corev1.ContainerPort{
{
Name: "alertmanager",
ContainerPort: 9093,
},
},
VolumeMounts: []corev1.VolumeMount{
{
Name: "alertmanager",
MountPath: "/alertmanager",
},
{
Name: "config-volume",
MountPath: "/etc/alertmanager",
},
},
},
{
Name: "alertmanager-helper",
Image: image.Resolve(v3.ToolsSystemImages.AlertSystemImages.AlertManagerHelper),
Command: []string{"alertmanager-helper"},
Args: []string{"--watched-file-list", "/etc/alertmanager"},
VolumeMounts: []corev1.VolumeMount{
{
Name: "config-volume",
MountPath: "/etc/alertmanager",
},
},
},
},
Volumes: []corev1.Volume{
{
Name: "alertmanager",
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{},
},
},
{
Name: "config-volume",
VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: "alertmanager",
},
},
},
},
},
},
},
}
}