Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
raffis committed Feb 14, 2019
1 parent 77f224a commit 8dcfb39
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 109 deletions.
2 changes: 1 addition & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
},
"services": {
"ClusterIP": {
"discover": true,
"discover": false,
"hostDefinition": {},
"serviceTemplates": ["generic-service"],
"hostTemplates": ["generic-host"],
Expand Down
6 changes: 3 additions & 3 deletions src/client/kube.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const KubeApi = require('kubernetes-client').Client;
const KubeConfig = require('kubernetes-client').config;

var config;
if(process.env.KUBERNETES_SERVICE_HOST && process.env.KUBERNETES_SERVICE_PORT) {
let config;
if (process.env.KUBERNETES_SERVICE_HOST && process.env.KUBERNETES_SERVICE_PORT) {
config = KubeConfig.getInCluster();
} else {
config = KubeConfig.fromKubeconfig();
}

const kubeClient = new KubeApi({config: config, version: '1.9'});
export default kubeClient;
20 changes: 10 additions & 10 deletions src/icinga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ export default class Icinga {
this.icingaClient.createHostGroup(name, name, [], (err, result) => {
if (err) {
this.logger.error(`failed create host group ${name}`, {error: err});
reject(err);
resolve(false);
} else {
this.logger.info(`host group ${name} was created successfully`, {result: result});
resolve(true);
resolve(true);
}
});
} else {
Expand Down Expand Up @@ -81,10 +81,10 @@ export default class Icinga {
this.icingaClient.createServiceGroup(name, name, [], (err, result) => {
if (err) {
this.logger.error(`failed create service group ${name}`, {error: err});
reject(err);
resolve(false);
} else {
this.logger.info(`service group ${name} was created successfully`, {result: result});
resolve(true);
resolve(true);
}
});
} else {
Expand Down Expand Up @@ -117,7 +117,7 @@ export default class Icinga {
this.icingaClient.createHostCustom(JSON.stringify(host), name, (err, result) => {
if (err) {
this.logger.error(`failed create host ${name}`, {error: err});
reject(err);
resolve(false);
} else {
this.logger.info(`host ${name} was created successfully`, {result: result});
resolve(true);
Expand Down Expand Up @@ -153,7 +153,7 @@ export default class Icinga {
this.icingaClient.createServiceCustom(JSON.stringify(service), host, name, (err, result) => {
if (err) {
this.logger.error(`failed create service ${name} on host ${host}`, {error: err});
reject(err);
resolve(false);
} else {
this.logger.info(`service ${name} on host ${host} was created successfully`, {result: result});
resolve(true);
Expand Down Expand Up @@ -215,11 +215,11 @@ export default class Icinga {

return new Promise((resolve, reject) => {
this.icingaClient.getServiceFiltered({filter: 'service.vars._kubernetes == true'}, async (err, result) => {
if(err) {
if (err) {
return reject(err);
}

var handlers = [];
let handlers = [];
for (const service of result) {
handlers.push(this.deleteService(service.attrs.host_name, service.attrs.name));
}
Expand All @@ -229,11 +229,11 @@ export default class Icinga {
});

this.icingaClient.getHostFiltered({filter: 'host.vars._kubernetes == true'}, async (err, result) => {
if(err) {
if (err) {
return reject(err);
}

var handlers = [];
let handlers = [];
for (const host of result) {
handlers.push(this.deleteHost(host.attrs.name));
}
Expand Down
32 changes: 16 additions & 16 deletions src/kube/abstract.resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,37 @@ export default abstract class Resource {
* Prepare icinga object with kube annotations
*/
protected prepareResource(resource: any): any {
var definition: any = {};
let definition: any = {};

if(!resource.metadata.annotations) {
return definition;
if (!resource.metadata.annotations) {
return definition;
}

var annotations: any = resource.metadata.annotations;
let annotations: any = resource.metadata.annotations;

if(annotations['kube-icinga/check_command']) {
definition.check_command = annotations['kube-icinga/check_command'];
if (annotations['kube-icinga/check_command']) {
definition.check_command = annotations['kube-icinga/check_command'];
}
if(annotations['kube-icinga/definition']) {
Object.assign(definition, JSON.parse(annotations['kube-icinga/definition']));

if (annotations['kube-icinga/definition']) {
Object.assign(definition, JSON.parse(annotations['kube-icinga/definition']));
}

return definition;
}

/**
* Prepare icinga object with kube annotations
*/
protected prepareTemplates(resource: any): string[] {
if(!resource.metadata.annotations) {
return [];
if (!resource.metadata.annotations) {
return [];
}

var annotations: any = resource.metadata.annotations;
if(annotations['kube-icinga/templates']) {
return annotations['kube-icinga/templates'].split(',');
let annotations: any = resource.metadata.annotations;

if (annotations['kube-icinga/templates']) {
return annotations['kube-icinga/templates'].split(',');
}

return [];
Expand Down
16 changes: 8 additions & 8 deletions src/kube/ingress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default class Ingress extends Resource {
this.jsonStream = jsonStream;
this.kubeNode = kubeNode;
this.options = Object.assign(this.options, options);
}
}

/**
* Apply host
Expand Down Expand Up @@ -85,7 +85,7 @@ export default class Ingress extends Resource {
}

let service = this.prepareResource(definition);
var templates = this.options.serviceTemplates;
let templates = this.options.serviceTemplates;
templates = templates.concat(this.prepareTemplates(definition));

if (this.options.applyServices) {
Expand All @@ -94,7 +94,7 @@ export default class Ingress extends Resource {
for (const spec of definition.spec.rules) {
for (const path of spec.http.paths) {
let base = path.path || '/';
let name = this.escapeName([spec.host, 'http', base].join('-'));
let name = this.escapeName([spec.host, 'http', base].join('-'));
let addition = {
'check_command': 'http',
'display_name': `${spec.host}${base}:http`,
Expand All @@ -113,7 +113,7 @@ export default class Ingress extends Resource {

// tls secret set, also apply https service
if (definition.spec.tls) {
name = this.escapeName([spec.host, 'https', base].join('-'));
name = this.escapeName([spec.host, 'https', base].join('-'));
addition.display_name += 's';
addition['vars.http_ssl'] = true;
this.applyService(hostname, name, addition, templates);
Expand All @@ -128,12 +128,12 @@ export default class Ingress extends Resource {
*/
public async kubeListener(provider) {
try {
var stream = provider();
let stream = provider();
stream.pipe(this.jsonStream);
this.jsonStream.on('data', async (object) => {
this.logger.debug('received kubernetes ingress resource', {object});

if(object.object.kind !== 'Ingress') {
if (object.object.kind !== 'Ingress') {
this.logger.error('skip invalid ingress object', {object: object});
return;
}
Expand All @@ -143,8 +143,8 @@ export default class Ingress extends Resource {
}

if (object.type == 'ADDED' || object.type == 'MODIFIED') {
this.prepareObject(object.object).catch(err => {
this.logger.error('failed to handle resource', {error: err})
this.prepareObject(object.object).catch((err) => {
this.logger.error('failed to handle resource', {error: err});
});
}
});
Expand Down
8 changes: 4 additions & 4 deletions src/kube/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ export default class Node extends Resource {
'address': definition.metadata.name,
'vars._kubernetes': true,
'vars.kubernetes': definition,
'check_command': 'ping',
'check_command': 'ping',
};

if (!definition.spec.unschedulable) {
this.logger.debug('skip kube worker node '+definition.metadata.name+' since it is flagged as unschedulable');
this.nodes.push(definition.metadata.name);
}

Object.assign(host, this.options.hostDefinition);
return this.icinga.applyHost(definition.metadata.name, host, this.options.hostTemplates);
}
Expand All @@ -76,7 +76,7 @@ export default class Node extends Resource {
}

this.logger.debug('received kubernetes host resource', {object});
if(object.object.kind !== 'Node') {
if (object.object.kind !== 'Node') {
this.logger.error('skip invalid node object', {object: object});
return;
}
Expand All @@ -86,7 +86,7 @@ export default class Node extends Resource {
}

if (object.type == 'ADDED') {
this.prepareObject(object.object).catch(err => {
this.prepareObject(object.object).catch((err) => {
this.logger.error('failed to handle resource', {error: err});
});
}
Expand Down
72 changes: 36 additions & 36 deletions src/kube/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,30 @@ interface ServiceOptions {
}

const defaults: ServiceOptions = {
ClusterIP: {
discover: false,
applyServices: true,
hostDefinition: {},
serviceDefinition: {},
hostTemplates: [],
serviceTemplates: [],
},
NodePort: {
discover: true,
applyServices: true,
hostDefinition: {},
serviceDefinition: {},
hostTemplates: [],
serviceTemplates: [],
},
LoadBalancer: {
discover: true,
applyServices: true,
hostDefinition: {},
serviceDefinition: {},
hostTemplates: [],
serviceTemplates: [],
},
ClusterIP: {
discover: false,
applyServices: true,
hostDefinition: {},
serviceDefinition: {},
hostTemplates: [],
serviceTemplates: [],
},
NodePort: {
discover: true,
applyServices: true,
hostDefinition: {},
serviceDefinition: {},
hostTemplates: [],
serviceTemplates: [],
},
LoadBalancer: {
discover: true,
applyServices: true,
hostDefinition: {},
serviceDefinition: {},
hostTemplates: [],
serviceTemplates: [],
},
};

/**
Expand All @@ -68,7 +68,7 @@ export default class Service extends Resource {
this.logger = logger;
this.icinga = icinga;
this.jsonStream = jsonStream;
var clone = JSON.parse(JSON.stringify(defaults));
let clone = JSON.parse(JSON.stringify(defaults));
Object.assign(clone.ClusterIP, options.ClusterIP);
Object.assign(clone.NodePort, options.NodePort);
Object.assign(clone.LoadBalancer, options.LoadBalancer);
Expand Down Expand Up @@ -97,7 +97,7 @@ export default class Service extends Resource {
* Apply service
*/
protected async applyService(host: string, name: string, type: string, definition, templates: string[]) {
if (type === Service.TYPE_NODEPORT) {
if (type === Service.TYPE_NODEPORT) {
for (const node of this.kubeNode.getWorkerNodes()) {
this.icinga.applyService(node, name, definition, templates);
}
Expand All @@ -113,17 +113,17 @@ export default class Service extends Resource {
public async prepareObject(definition): Promise<any> {
let serviceType = definition.spec.type;

if(!this.options[serviceType]) {
if (!this.options[serviceType]) {
throw new Error('unknown service type provided');
}

var options = this.options[serviceType];
var service = JSON.parse(JSON.stringify(options.serviceDefinition));
let options = this.options[serviceType];
let service = JSON.parse(JSON.stringify(options.serviceDefinition));
service['groups'] = [definition.metadata.namespace];
Object.assign(service, this.prepareResource(definition));

let hostname = this.escapeName(['service', definition.metadata.namespace, definition.metadata.name].join('-'));
var templates = options.serviceTemplates;
let templates = options.serviceTemplates;
templates = templates.concat(this.prepareTemplates(definition));
if (serviceType !== Service.TYPE_NODEPORT) {
await this.applyHost(hostname, definition.spec.clusterIP, serviceType, definition, options.hostTemplates);
Expand All @@ -140,11 +140,11 @@ export default class Service extends Resource {
this.logger.debug('service can be checked via check command '+port.check_command);
port['vars.'+port.check_command+'_port'] = servicePort.nodePort || servicePort.port;
} else {
delete port.check_command;
delete port.check_command;
this.logger.warn('service can not be checked via check command '+port.check_command+', icinga check command does not exists, fallback to service protocol '+servicePort.protocol);
}
}

let protocol = servicePort.protocol.toLowerCase();
let port_name = servicePort.name || protocol+':'+servicePort.port;

Expand All @@ -168,12 +168,12 @@ export default class Service extends Resource {
*/
public async kubeListener(provider) {
try {
var stream = provider();
let stream = provider();
stream.pipe(this.jsonStream);
this.jsonStream.on('data', async (object) => {
this.logger.debug('received kubernetes service resource', {object});

if(object.object.kind !== 'Service') {
if (object.object.kind !== 'Service') {
this.logger.error('skip invalid service object', {object: object});
return;
}
Expand All @@ -188,8 +188,8 @@ export default class Service extends Resource {
}

if (object.type == 'ADDED' || object.type == 'MODIFIED') {
this.prepareObject(object.object).catch(err => {
this.logger.error('failed to handle resource', {error: err})
this.prepareObject(object.object).catch((err) => {
this.logger.error('failed to handle resource', {error: err});
});
}
});
Expand Down
Loading

0 comments on commit 8dcfb39

Please sign in to comment.