Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: feat: add withServiceMonitor #237

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -448,6 +448,25 @@ spec:
apiVersion: apps/v1
kind: Deployment
---
metadata:
labels:
app: www
name: www-monitor
namespace: sample-kosko
spec:
endpoints:
- path: /path/to/metrics
port: http
interval: 30s
namespaceSelector:
matchNames:
- sample-kosko
selector:
matchLabels:
app: www
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
---
metadata:
annotations:
kubernetes.io/ingress.class: nginx
Expand Down
1 change: 1 addition & 0 deletions package.json
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
20 changes: 20 additions & 0 deletions src/components/app/index.ts
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,18 @@ export const create = (
addWaitForPostgres(deployment);
}

// add a service monitor in production
if (env.env === "prod" && 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
@@ -0,0 +1,45 @@
import { ServiceMonitor } from "@kubernetes-models/prometheus-operator/monitoring.coreos.com/v1/ServiceMonitor";

export interface 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
Expand Up @@ -16,6 +16,10 @@ const manifests = create("www", {
},
},
containerPort: 8080,
withServiceMonitor: {
path: "/path/to/metrics",
interval: "30s",
},
withPostgres: true,
withRedirections: {
destination: "www.website.fr",
Expand Down
10 changes: 10 additions & 0 deletions yarn.lock
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