-
Notifications
You must be signed in to change notification settings - Fork 4
/
volume.ts
173 lines (151 loc) · 4.89 KB
/
volume.ts
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
import {Logger} from 'winston';
import Icinga from '../icinga';
import KubeNode from './node';
import Resource from './abstract.resource';
interface VolumeOptions {
discover?: boolean;
applyServices?: boolean;
attachToNodes?: boolean;
hostName?: string;
hostDefinition?: object;
serviceDefinition?: object;
hostTemplates?: string[];
serviceTemplates?: string[];
}
/**
* kubernetes ingresses
*/
export default class Volume extends Resource {
protected logger: Logger;
protected icinga: Icinga;
protected kubeNode: KubeNode;
protected options = {
applyServices: true,
attachToNodes: false,
hostName: 'kubernetes-volumes',
hostDefinition: {},
serviceDefinition: {},
hostTemplates: [],
serviceTemplates: [],
};
/**
* kubernetes hosts
*/
constructor(logger: Logger, kubeNode: KubeNode, icinga: Icinga, options: VolumeOptions) {
super();
this.logger = logger;
this.icinga = icinga;
this.kubeNode = kubeNode;
this.options = Object.assign(this.options, options);
}
/**
* Apply host
*/
protected async applyHost(name: string, address: string, metadata, templates: string[]): Promise<boolean> {
let definition = {
'display_name': name,
'address': address,
'check_command': 'dummy',
'vars.dummy_state': 0,
'vars._kubernetes': true,
'vars.kubernetes': metadata,
};
Object.assign(definition, this.options.hostDefinition);
return this.icinga.applyHost(name, definition, this.options.hostTemplates);
}
/**
* Apply service
*/
protected async applyService(host: string, name: string, definition, templates: string[]) {
if (this.options.attachToNodes) {
for (const node of this.kubeNode.getWorkerNodes()) {
definition.host_name = node;
this.icinga.applyService(node, name, definition, templates);
}
} else {
definition.host_name = host;
this.icinga.applyService(host, name, definition, templates);
}
}
/**
* Preapre icinga object and apply
*/
public async prepareObject(definition: any): Promise<any> {
let hostname = this.getHostname(definition);
if (!this.options.attachToNodes) {
await this.applyHost(hostname, hostname, definition, this.options.hostTemplates);
}
if (this.options.applyServices) {
let groups = [];
if (definition.spec.claimRef.namespace) {
groups.push(definition.spec.claimRef.namespace);
await this.icinga.applyServiceGroup(definition.spec.claimRef.namespace);
}
let templates = this.options.serviceTemplates;
templates = templates.concat(this.prepareTemplates(definition));
let service = this.options.serviceDefinition;
let name = this.escapeName(definition.metadata.name);
let addition = {
'check_command': 'dummy',
'display_name': `${definition.metadata.name}:volume`,
'vars._kubernetes': true,
'vars.kubernetes': definition,
'groups': groups,
};
Object.assign(addition, service);
Object.assign(addition, this.prepareResource(definition));
this.applyService(hostname, name, addition, templates);
}
}
/**
* Get hostname
*/
protected getHostname(definition: any): string {
if (definition.metadata.annotations['kube-icinga/host']) {
return definition.metadata.annotations['kube-icinga/host'];
} else if (this.options.hostName === null) {
return this.escapeName(['volume', definition.metadata.name].join('-'));
}
return this.options.hostName;
}
/**
* Delete object
*/
protected deleteObject(definition: any): Promise<boolean> {
if (this.options.hostName === null) {
let hostname = this.getHostname(definition);
return this.icinga.deleteHost(hostname);
}
return this.icinga.deleteServicesByFilter('service.vars.kubernetes.metadata.uid=="'+definition.metadata.uid+'"');
}
/**
* Start kube listener
*/
public async kubeListener(provider) {
try {
let stream = provider();
stream.on('data', async (object) => {
this.logger.debug('received kubernetes persistent volume resource', {object});
if (object.object.kind !== 'PersistentVolume') {
this.logger.error('skip invalid object', {object: object});
return;
}
if (object.type == 'MODIFIED' || object.type == 'DELETED') {
await this.deleteObject(object.object).catch((err) => {
this.logger.error('failed to remove objects', {error: err});
});
}
if (object.type == 'ADDED' || object.type == 'MODIFIED') {
this.prepareObject(object.object).catch((err) => {
this.logger.error('failed to handle resource', {error: err});
});
}
});
stream.on('finish', () => {
this.kubeListener(provider);
});
} catch (err) {
this.logger.error('failed start ingresses listener', {error: err});
}
}
}