-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update to using default scrapeclass for tls config (#517)
## Description add pod monitors to uds-core operator automation and UDS package CR monitor spec. update to using default scrapeClass for tls config in prometheus and "exempt" class to override default tls config update core components existing pod and service monitor implementations to fit with the new default scrapeClass implementation migrate pepr over to using the generated helm based implementation to facilitate ability to override and align zarf.yaml composition organization with the other packages. add authorization to the endpoint configuration options for monitors ## Related Issue Fixes #417 ## Type of change - [ ] Bug fix (non-breaking change which fixes an issue) - [x] New feature (non-breaking change which adds functionality) - [ ] Other (security config, docs update, etc) ## Checklist before merging - [x] Test, docs, adr added or updated as needed - [x] [Contributor Guide Steps](https://github.com/defenseunicorns/uds-template-capability/blob/main/CONTRIBUTING.md)(https://github.com/defenseunicorns/uds-template-capability/blob/main/CONTRIBUTING.md#submitting-a-pull-request) followed --------- Co-authored-by: Wayne Starr <[email protected]> Co-authored-by: Micah Nagel <[email protected]>
- Loading branch information
1 parent
1d4df64
commit 258bb6b
Showing
18 changed files
with
1,799 additions
and
187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Monitor } from "../../crd"; | ||
import { sanitizeResourceName } from "../utils"; | ||
|
||
export function generateMonitorName(pkgName: string, monitor: Monitor) { | ||
const { selector, portName, description } = monitor; | ||
|
||
// Ensure the resource name is valid | ||
const nameSuffix = description || `${Object.values(selector)}-${portName}`; | ||
const name = sanitizeResourceName(`${pkgName}-${nameSuffix}`); | ||
|
||
return name; | ||
} |
41 changes: 41 additions & 0 deletions
41
src/pepr/operator/controllers/monitoring/pod-monitor.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { describe, expect, it } from "@jest/globals"; | ||
import { Monitor } from "../../crd"; | ||
import { generatePodMonitor } from "./pod-monitor"; | ||
|
||
describe("test generate Pod monitor", () => { | ||
it("should return a valid Pod Monitor object", () => { | ||
const ownerRefs = [ | ||
{ | ||
apiVersion: "uds.dev/v1alpha1", | ||
kind: "Package", | ||
name: "test", | ||
uid: "f50120aa-2713-4502-9496-566b102b1174", | ||
}, | ||
]; | ||
const portName = "http-metrics"; | ||
const metricsPath = "/test"; | ||
const selectorApp = "test"; | ||
const monitor: Monitor = { | ||
portName: portName, | ||
path: metricsPath, | ||
targetPort: 1234, | ||
selector: { | ||
app: selectorApp, | ||
}, | ||
}; | ||
const namespace = "test"; | ||
const pkgName = "test"; | ||
const generation = "1"; | ||
const payload = generatePodMonitor(monitor, namespace, pkgName, generation, ownerRefs); | ||
|
||
expect(payload).toBeDefined(); | ||
expect(payload.metadata?.name).toEqual(`${pkgName}-${selectorApp}-${portName}`); | ||
expect(payload.metadata?.namespace).toEqual(namespace); | ||
expect(payload.spec?.podMetricsEndpoints).toBeDefined(); | ||
if (payload.spec?.podMetricsEndpoints) { | ||
expect(payload.spec.podMetricsEndpoints[0].port).toEqual(portName); | ||
expect(payload.spec.podMetricsEndpoints[0].path).toEqual(metricsPath); | ||
} | ||
expect(payload.spec?.selector.matchLabels).toHaveProperty("app", "test"); | ||
}); | ||
}); |
103 changes: 103 additions & 0 deletions
103
src/pepr/operator/controllers/monitoring/pod-monitor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import { V1OwnerReference } from "@kubernetes/client-node"; | ||
import { K8s } from "pepr"; | ||
import { Component, setupLogger } from "../../../logger"; | ||
import { Monitor, PrometheusPodMonitor, UDSPackage } from "../../crd"; | ||
import { Kind } from "../../crd/generated/package-v1alpha1"; | ||
import { getOwnerRef } from "../utils"; | ||
import { generateMonitorName } from "./common"; | ||
|
||
// configure subproject logger | ||
const log = setupLogger(Component.OPERATOR_MONITORING); | ||
|
||
/** | ||
* Generate a pod monitor for a pod | ||
* | ||
* @param pkg UDS Package | ||
* @param namespace | ||
*/ | ||
export async function podMonitor(pkg: UDSPackage, namespace: string) { | ||
const pkgName = pkg.metadata!.name!; | ||
const generation = (pkg.metadata?.generation ?? 0).toString(); | ||
const ownerRefs = getOwnerRef(pkg); | ||
|
||
log.debug(`Reconciling PodMonitors for ${pkgName}`); | ||
|
||
// Get the list of monitored services | ||
const monitorList = pkg.spec?.monitor ?? []; | ||
|
||
// Create a list of generated PodMonitors | ||
const payloads: PrometheusPodMonitor[] = []; | ||
|
||
try { | ||
for (const monitor of monitorList) { | ||
if (monitor.kind === Kind.PodMonitor) { | ||
const payload = generatePodMonitor(monitor, namespace, pkgName, generation, ownerRefs); | ||
|
||
log.debug(payload, `Applying PodMonitor ${payload.metadata?.name}`); | ||
|
||
// Apply the PodMonitor and force overwrite any existing policy | ||
await K8s(PrometheusPodMonitor).Apply(payload, { force: true }); | ||
|
||
payloads.push(payload); | ||
} | ||
} | ||
|
||
// Get all related PodMonitors in the namespace | ||
const podMonitors = await K8s(PrometheusPodMonitor) | ||
.InNamespace(namespace) | ||
.WithLabel("uds/package", pkgName) | ||
.Get(); | ||
|
||
// Find any orphaned PodMonitors (not matching the current generation) | ||
const orphanedMonitor = podMonitors.items.filter( | ||
m => m.metadata?.labels?.["uds/generation"] !== generation, | ||
); | ||
|
||
// Delete any orphaned PodMonitors | ||
for (const m of orphanedMonitor) { | ||
log.debug(m, `Deleting orphaned PodMonitor ${m.metadata!.name}`); | ||
await K8s(PrometheusPodMonitor).Delete(m); | ||
} | ||
} catch (err) { | ||
throw new Error(`Failed to process PodMonitors for ${pkgName}, cause: ${JSON.stringify(err)}`); | ||
} | ||
|
||
// Return the list of monitor names | ||
return [...payloads.map(m => m.metadata!.name!)]; | ||
} | ||
|
||
export function generatePodMonitor( | ||
monitor: Monitor, | ||
namespace: string, | ||
pkgName: string, | ||
generation: string, | ||
ownerRefs: V1OwnerReference[], | ||
) { | ||
const { selector, portName } = monitor; | ||
const name = generateMonitorName(pkgName, monitor); | ||
const payload: PrometheusPodMonitor = { | ||
metadata: { | ||
name, | ||
namespace, | ||
labels: { | ||
"uds/package": pkgName, | ||
"uds/generation": generation, | ||
}, | ||
ownerReferences: ownerRefs, | ||
}, | ||
spec: { | ||
podMetricsEndpoints: [ | ||
{ | ||
port: portName, | ||
path: monitor.path || "/metrics", | ||
authorization: monitor.authorization, | ||
}, | ||
], | ||
selector: { | ||
matchLabels: selector, | ||
}, | ||
}, | ||
}; | ||
|
||
return payload; | ||
} |
2 changes: 1 addition & 1 deletion
2
src/pepr/operator/controllers/monitoring/service-monitor.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.