Skip to content

Commit

Permalink
feat: add withServiceMonitor
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Bouquillon committed Oct 16, 2020
1 parent b710bc4 commit 0842837
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"author": "Fabrique numérique des Ministères Sociaux <dsi-incubateur@sg.social.gouv.fr> (https://incubateur.social.gouv.fr)",
"bugs": "https://github.com/SocialGouv/kosko-charts/issues",
"dependencies": {
"@kubernetes-models/prometheus-operator": "^1.0.0",
"@kubernetes-models/sealed-secrets": "^1.0.0",
"@sindresorhus/is": "^4.0.0",
"fp-ts": "^2.8.4",
Expand Down
19 changes: 19 additions & 0 deletions src/components/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ import { updateMetadata } from "../../utils/updateMetadata";
import { merge } from "../../utils/merge";
import { addPostgresUserSecret } from "../../utils/addPostgresUserSecret";
import { addWaitForPostgres } from "../../utils/addWaitForPostgres";
import {
ServiceMonitorParams,
getServiceMonitor,
} from "../../utils/getServiceMonitor";

type AliasParams = {
hosts: string[];
Expand All @@ -36,8 +40,12 @@ export type AppConfig = DeploymentParams &
labels: Record<string, string>;
ingress: boolean;
withPostgres: boolean;
withServiceMonitor:
| boolean
| Omit<ServiceMonitorParams, "appName" | "namespace">;
withRedirections?: AliasParams;
};

export const create = (
name: string,
{
Expand Down Expand Up @@ -85,6 +93,17 @@ export const create = (
addWaitForPostgres(deployment);
}

if (envParams.withServiceMonitor) {
const monitor = getServiceMonitor({
namespace: envParams.namespace.name,
appName: name,
...(typeof envParams.withServiceMonitor === "object"
? envParams.withServiceMonitor
: {}),
});
manifests.push(monitor);
}

// add a redirection ingresses, production only
if (env.env === "prod" && envParams.withRedirections) {
const { hosts, destination } = envParams.withRedirections;
Expand Down
45 changes: 45 additions & 0 deletions src/utils/getServiceMonitor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { ServiceMonitor } from "@kubernetes-models/prometheus-operator/monitoring.coreos.com/v1/ServiceMonitor";

export type ServiceMonitorParams = {
path?: string;
interval?: string;
port?: string;
namespace: string;
appName: string;
};

export const getServiceMonitor = ({
path = "/metrics",
interval = "1h",
port = "http",
namespace,
appName,
}: ServiceMonitorParams): ServiceMonitor => {
const monitor = new ServiceMonitor({
metadata: {
labels: {
app: appName,
},
name: `${appName}-monitor`,
namespace,
},
spec: {
endpoints: [
{
path,
port,
interval,
},
],
namespaceSelector: {
matchNames: [namespace],
},
selector: {
matchLabels: {
app: appName,
},
},
},
});
return monitor;
};
4 changes: 4 additions & 0 deletions templates/simple/components/www.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ const manifests = create("www", {
},
},
containerPort: 8080,
withMonitor: {
path: "/path/to/metrics",
interval: "30s",
},
withPostgres: true,
withRedirections: {
destination: "www.website.fr",
Expand Down
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1186,6 +1186,16 @@
is-plain-object "^5.0.0"
tslib "^2.0.3"

"@kubernetes-models/prometheus-operator@^1.0.0":
version "1.0.0"
resolved "https://registry.yarnpkg.com/@kubernetes-models/prometheus-operator/-/prometheus-operator-1.0.0.tgz#0a0b9797afee5c4e27380efc6bd9a8038958deb7"
integrity sha512-52XOweZ/UPxMRXYgoRbZR9llTY3IIJEl6i4pAXzi/KUox0ico2p4QDJwdJCoCuRPG/45hFhnskDNHV7eAwpDkg==
dependencies:
"@kubernetes-models/base" "^1.0.0"
"@kubernetes-models/validate" "^1.0.0"
kubernetes-models "^1.0.1"
tslib "^1.10.0"

"@kubernetes-models/sealed-secrets@^1.0.0":
version "1.0.0"
resolved "https://registry.yarnpkg.com/@kubernetes-models/sealed-secrets/-/sealed-secrets-1.0.0.tgz#12f48d70d4c140fb235e19a8c9193dfef1881a65"
Expand Down

0 comments on commit 0842837

Please sign in to comment.