Skip to content

Commit

Permalink
typescript strict
Browse files Browse the repository at this point in the history
  • Loading branch information
raffis committed Aug 29, 2019
1 parent 77e82d5 commit 90cf6b9
Show file tree
Hide file tree
Showing 15 changed files with 114 additions and 523 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* [CHANGE] Upgrade to typescript 3.x / jest 24.x #22
* [FIX] ClusterIP service check for UDP fails #20
* [FIX] LoadBalancers services using node port instead of target port #21
* [CHANGE] Enabled best pratcicts typescript compiler flags (strict, notnull, ..)


## 2.0.1
Expand Down
17 changes: 1 addition & 16 deletions docker-compose-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,9 @@ services:
hostname: icinga2
environment:
- MYSQL_ROOT_PASSWORD=<password>
- ICINGA2_FEATURE_GRAPHITE=1
# Important:
# keep the hostname graphite the same as
# the name of the graphite docker-container
- ICINGA2_FEATURE_GRAPHITE_HOST=graphite
- ICINGA2_FEATURE_GRAPHITE_PORT=2003
- ICINGA2_FEATURE_GRAPHITE_URL=https://graphite
#- ICINGAWEB2_ADMIN_USER=icingaadmin
#- ICINGAWEB2_ADMIN_PASS=icinga
#- ICINGA2_USER_FULLNAME=Icinga2 Docker Monitoring Instance
- DEFAULT_MYSQL_HOST=mysql
Expand All @@ -39,19 +34,9 @@ services:
#- ./ssmtp/revaliases:/etc/ssmtp/revaliases:ro
#- ./ssmtp/ssmtp.conf:/etc/ssmtp/ssmtp.conf:ro
ports:
- "80:80"
- "81:80"
- "443:443"
- "5665:5665"
graphite:
image: graphiteapp/graphite-statsd:latest
container_name: graphite
restart: on-failure:5
hostname: graphite
volumes:
- ./data/graphite/conf:/opt/graphite/conf
- ./data/graphite/storage:/opt/graphite/storage
- ./data/graphite/log/graphite:/var/log/graphite
- ./data/graphite/log/carbon:/var/log/carbon
mysql:
image: mariadb:10.1
environment:
Expand Down
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
declare module 'icinga2-api';
declare module 'json-stream';
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kube-icinga",
"version": "2.0.1",
"version": "2.1.0",
"description": "Icinga2 autodiscovery service for kubernetes",
"main": "main.js",
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions src/icinga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export class Icinga {
/**
* Create or update a new icinga host object
*/
public applyHost(name: string, definition: IcingaObject, templates: string[]=[]): Promise<boolean> {
public applyHost(name: string, definition: IcingaObject={}, templates: string[]=[]): Promise<boolean> {
let host = {
attrs: definition,
templates: templates,
Expand Down Expand Up @@ -171,7 +171,7 @@ export class Icinga {
/**
* Create or update an icinga service object
*/
public applyService(host: string, name: string, definition: IcingaObject, templates: string[]=[]): Promise<boolean> {
public applyService(host: string, name: string, definition: IcingaObject={}, templates: string[]=[]): Promise<boolean> {
let service = {
attrs: definition,
templates: templates,
Expand Down
7 changes: 5 additions & 2 deletions src/kube/abstract.resource.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import {Logger} from 'winston';
import {Service as KubeService} from 'kubernetes-types/core/v1';

/**
* kubernetes hosts
*/
export default abstract class Resource {
protected logger: Logger;

/**
* Initialize kube handler with logger
*/
constructor(logger: Logger) {
this.logger = logger;
}
Expand Down Expand Up @@ -61,7 +63,7 @@ export default abstract class Resource {
/**
* Get annotations
*/
protected getAnnotations(definition: KubeService): { [key: string]: string } {
protected getAnnotations(definition: any): { [key: string]: string } {
if (definition.metadata && definition.metadata.annotations) {
return definition.metadata.annotations;
}
Expand Down Expand Up @@ -109,6 +111,7 @@ export default abstract class Resource {

if (object.type == 'ADDED' || object.type == 'MODIFIED') {
this.prepareObject(object.object).catch((err) => {
console.log(err);
this.logger.error('failed to handle resource', {error: err});
});
}
Expand Down
12 changes: 7 additions & 5 deletions src/kube/ingress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ const DefaultOptions: IngressOptions = {
export default class Ingress extends AbstractResource {
protected icinga: Icinga;
protected kubeNode: KubeNode;
protected options = DefaultOptions;
protected options: IngressOptions = DefaultOptions;

/**
* kubernetes hosts
*/
constructor(logger: Logger, kubeNode: KubeNode, icinga: Icinga, options: IngressOptions = DefaultOptions) {
constructor(logger: Logger, kubeNode: KubeNode, icinga: Icinga, options: any = DefaultOptions) {
super(logger);
this.logger = logger;
this.icinga = icinga;
this.kubeNode = kubeNode;
this.options = Object.assign(this.options, options);
this.options = Object.assign({}, this.options, options);
}

/**
Expand Down Expand Up @@ -138,8 +138,10 @@ export default class Ingress extends AbstractResource {
* Get hostname
*/
protected getHostname(definition: KubeIngress): string {
if (definition.metadata!.annotations!['kube-icinga/host']) {
return definition.metadata!.annotations!['kube-icinga/host'];
let annotations = this.getAnnotations(definition);

if (annotations['kube-icinga/host']) {
return annotations['kube-icinga/host'];
} else if (this.options.hostName === null) {
return this.escapeName(['ingress', definition.metadata!.namespace, definition.metadata!.name].join('-'));
}
Expand Down
6 changes: 3 additions & 3 deletions src/kube/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ const DefaultOptions: NodeOptions = {
export default class Node extends Resource {
protected icinga: Icinga;
protected nodes: string[] = [];
protected options = DefaultOptions;
protected options: NodeOptions = DefaultOptions;

/**
* kubernetes hosts
*/
constructor(logger: Logger, icinga: Icinga, options: NodeOptions = DefaultOptions) {
constructor(logger: Logger, icinga: Icinga, options: any = DefaultOptions) {
super(logger);
this.icinga = icinga;
this.options = Object.assign(this.options, options);
this.options = Object.assign({}, this.options, options);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/kube/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ const HEADLESS_ADDRESS = 'None';
export default class Service extends AbstractResource {
protected icinga: Icinga;
protected kubeNode: KubeNode;
protected options = DefaultOptions;
protected options: ServiceOptions = DefaultOptions;

/**
* kubernetes services
*/
constructor(logger: Logger, kubeNode: KubeNode, icinga: Icinga, options: ServiceOptions=DefaultOptions) {
constructor(logger: Logger, kubeNode: KubeNode, icinga: Icinga, options: any =DefaultOptions) {
super(logger);
this.icinga = icinga;
let clone = JSON.parse(JSON.stringify(DefaultOptions));
Expand Down Expand Up @@ -289,7 +289,7 @@ export default class Service extends AbstractResource {
stream.on('data', async (object: any) => {
this.logger.debug('received kubernetes service resource', {object});

if (this.getServiceAddress(object.obect) === HEADLESS_ADDRESS) {
if (this.getServiceAddress(object.object) === HEADLESS_ADDRESS) {
this.logger.info('skip headless service object (use pod provisioning instead)', {object: object});
return false;
}
Expand Down
12 changes: 7 additions & 5 deletions src/kube/volume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ const DefaultOptions: VolumeOptions = {
export default class Volume extends AbstractResource {
protected icinga: Icinga;
protected kubeNode: KubeNode;
protected options = DefaultOptions;
protected options: VolumeOptions = DefaultOptions;

/**
* kubernetes hosts
*/
constructor(logger: Logger, kubeNode: KubeNode, icinga: Icinga, options: VolumeOptions = DefaultOptions) {
constructor(logger: Logger, kubeNode: KubeNode, icinga: Icinga, options: any = DefaultOptions) {
super(logger);
this.logger = logger;
this.icinga = icinga;
this.kubeNode = kubeNode;
this.options = Object.assign(this.options, options);
this.options = Object.assign({}, this.options, options);
}

/**
Expand Down Expand Up @@ -125,8 +125,10 @@ export default class Volume extends AbstractResource {
* Get hostname
*/
protected getHostname(definition: PersistentVolume): string {
if (definition.metadata!.annotations!['kube-icinga/host']) {
return definition.metadata!.annotations!['kube-icinga/host'];
let annotations = this.getAnnotations(definition);

if (annotations['kube-icinga/host']) {
return annotations['kube-icinga/host'];
} else if (this.options.hostName === null) {
return this.escapeName(['volume', definition.metadata!.name].join('-'));
}
Expand Down
10 changes: 5 additions & 5 deletions tests/icinga.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import * as IcingaApi from 'icinga2-api';
import IcingaClient from '../src/icinga';
import {Icinga} from '../src/icinga';
import Logger from '../src/logger';
jest.mock('icinga2-api');
jest.mock('../src/logger');
var icinga;
var icinga: any;

beforeEach(() => {
icinga = new IcingaClient(Logger, IcingaApi);
icinga = new Icinga(Logger, IcingaApi);
});

describe('icinga', () => {
Expand Down Expand Up @@ -35,7 +35,7 @@ describe('icinga', () => {
IcingaApi.getCheckCommand = jest.fn()
.mockImplementation((command, cb) => cb({Statuscode: 500}, null));

var result = await expect(icinga.hasCheckCommand('foobar')).rejects.toEqual({
await expect(icinga.hasCheckCommand('foobar')).rejects.toEqual({
Statuscode: 500
});

Expand Down Expand Up @@ -246,7 +246,7 @@ describe('icinga', () => {
.mockImplementation((cb) => cb(null, {foo: "bar"}));

jest.useFakeTimers();
icinga = new IcingaClient(Logger, IcingaApi);
icinga = new Icinga(Logger, IcingaApi);

IcingaApi.createHostCustom = jest.fn()
.mockImplementation((data, name, cb) => cb(null, {foo: "bar"}));
Expand Down
Loading

0 comments on commit 90cf6b9

Please sign in to comment.