Skip to content

Commit

Permalink
chore: move lib/ code related to data collection to lib/telemetry (
Browse files Browse the repository at this point in the history
…#1522)

## Description

This PR moves code related to collecting, transmitting, and analyzing
data into a single directory to provide more clarity to the structure of
`lib/` contents.

## Related Issue

None, this was an emergent PR focused around an organizational change to
Pepr.

## Type of change

- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [x] Other (security config, docs update, etc)

## Checklist before merging
- [x] Unit,
[Journey](https://github.com/defenseunicorns/pepr/tree/main/journey),
[E2E Tests](https://github.com/defenseunicorns/pepr-excellent-examples),
[docs](https://github.com/defenseunicorns/pepr/tree/main/docs),
[adr](https://github.com/defenseunicorns/pepr/tree/main/adr) added or
updated as needed
- [x] [Contributor Guide
Steps](https://docs.pepr.dev/main/contribute/#submitting-a-pull-request)
followed
  • Loading branch information
samayer12 authored Dec 5, 2024
1 parent 244747d commit c9d7227
Show file tree
Hide file tree
Showing 23 changed files with 27 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { K8s, RegisterKind, kind as a, fetch, fetchStatus, kind } from "kubernet
import * as R from "ramda";

import { Capability } from "./lib/capability";
import Log from "./lib/logger";
import Log from "./lib/telemetry/logger";
import { PeprModule } from "./lib/module";
import { PeprMutateRequest } from "./lib/mutate-request";
import * as PeprUtils from "./lib/utils";
Expand Down
2 changes: 1 addition & 1 deletion src/lib/assets/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { K8s, kind } from "kubernetes-fluent-client";
import { V1PolicyRule as PolicyRule } from "@kubernetes/client-node";

import { Assets } from ".";
import Log from "../logger";
import Log from "../telemetry/logger";
import { apiTokenSecret, service, tlsSecret, watcherService } from "./networking";
import { deployment, moduleSecret, namespace, watcher } from "./pods";
import { clusterRole, clusterRoleBinding, serviceAccount, storeRole, storeRoleBinding } from "./rbac";
Expand Down
2 changes: 1 addition & 1 deletion src/lib/assets/destroy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import { K8s, kind } from "kubernetes-fluent-client";

import Log from "../logger";
import Log from "../telemetry/logger";
import { peprStoreCRD } from "./store";

export async function destroyModule(name: string) {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/capability.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Capability } from "./capability";
import Log from "./logger";
import Log from "./telemetry/logger";
import { CapabilityCfg, FinalizeAction, MutateAction, ValidateAction, WatchLogAction } from "./types";
import { a } from "../lib";
import { V1Pod } from "@kubernetes/client-node";
Expand All @@ -21,7 +21,7 @@ jest.mock("./module", () => ({
}));

// Mock logger globally
jest.mock("./logger", () => ({
jest.mock("./telemetry/logger", () => ({
__esModule: true,
default: {
info: jest.fn(),
Expand Down
2 changes: 1 addition & 1 deletion src/lib/capability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import { GenericClass, GroupVersionKind, modelToGroupVersionKind } from "kubernetes-fluent-client";
import { pickBy } from "ramda";
import Log from "./logger";
import Log from "./telemetry/logger";
import { isBuildMode, isDevMode, isWatchMode } from "./module";
import { PeprStore, Storage } from "./storage";
import { OnSchedule, Schedule } from "./schedule";
Expand Down
4 changes: 2 additions & 2 deletions src/lib/controller/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import https from "https";

import { Capability } from "../capability";
import { MutateResponse, ValidateResponse } from "../k8s";
import Log from "../logger";
import { metricsCollector, MetricsCollector } from "../metrics";
import Log from "../telemetry/logger";
import { metricsCollector, MetricsCollector } from "../telemetry/metrics";
import { ModuleConfig, isWatchMode } from "../module";
import { mutateProcessor } from "../mutate-processor";
import { validateProcessor } from "../validate-processor";
Expand Down
2 changes: 1 addition & 1 deletion src/lib/controller/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { startsWith } from "ramda";

import { Capability } from "../capability";
import { Store } from "../k8s";
import Log, { redactedPatch, redactedStore } from "../logger";
import Log, { redactedPatch, redactedStore } from "../telemetry/logger";
import { DataOp, DataSender, DataStore, Storage } from "../storage";
import { fillStoreCache, sendUpdatesAndFlushCache } from "./storeCache";

Expand Down
2 changes: 1 addition & 1 deletion src/lib/controller/storeCache.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DataOp } from "../storage";
import Log from "../logger";
import Log from "../telemetry/logger";
import { K8s } from "kubernetes-fluent-client";
import { Store } from "../k8s";
import { StatusCodes } from "http-status-codes";
Expand Down
2 changes: 1 addition & 1 deletion src/lib/deploymentChecks.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// check to see if all replicas are ready for all deployments in the pepr-system namespace

import { K8s, kind } from "kubernetes-fluent-client";
import Log from "./logger";
import Log from "./telemetry/logger";

// returns true if all deployments are ready, false otherwise
export async function checkDeploymentStatus(namespace: string) {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/finalizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-FileCopyrightText: 2023-Present The Pepr Authors

import { K8s, KubernetesObject, RegisterKind } from "kubernetes-fluent-client";
import Log from "./logger";
import Log from "./telemetry/logger";
import { Binding, DeepPartial } from "./types";
import { Operation } from "./enums";
import { PeprMutateRequest } from "./mutate-request";
Expand Down
2 changes: 1 addition & 1 deletion src/lib/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-FileCopyrightText: 2023-Present The Pepr Authors

import { KubernetesObject } from "kubernetes-fluent-client";
import Log from "./logger";
import Log from "./telemetry/logger";
import { Binding, CapabilityExport } from "./types";
import { sanitizeResourceName } from "../sdk/sdk";
import {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/mutate-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Errors } from "./errors";
import { shouldSkipRequest } from "./filter/filter";
import { MutateResponse } from "./k8s";
import { AdmissionRequest } from "./types";
import Log from "./logger";
import Log from "./telemetry/logger";
import { ModuleConfig } from "./module";
import { PeprMutateRequest } from "./mutate-request";
import { base64Encode, convertFromBase64Map, convertToBase64Map } from "./utils";
Expand Down
4 changes: 2 additions & 2 deletions src/lib/queue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { afterEach, describe, expect, jest, it } from "@jest/globals";
import { WatchPhase } from "kubernetes-fluent-client/dist/fluent/types";
import { Queue } from "./queue";

import Log from "./logger";
jest.mock("./logger");
import Log from "./telemetry/logger";
jest.mock("./telemetry/logger");

describe("Queue", () => {
afterEach(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { KubernetesObject } from "@kubernetes/client-node";
import { WatchPhase } from "kubernetes-fluent-client/dist/fluent/types";
import { randomBytes } from "node:crypto";
import Log from "./logger";
import Log from "./telemetry/logger";

type WatchCallback = (obj: KubernetesObject, phase: WatchPhase) => Promise<void>;

Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/lib/logger.ts → src/lib/telemetry/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import { Operation } from "fast-json-patch";
import { pino, stdTimeFunctions } from "pino";
import { Store } from "./k8s";
import { Store } from "../k8s";

const isPrettyLog = process.env.PEPR_PRETTY_LOGS === "true";
const redactedValue = "**redacted**";
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023-Present The Pepr Authors

import Log from "./logger";
import Log from "./telemetry/logger";

/** Test if a string is ascii or not */
export const isAscii = /^[\s\x20-\x7E]*$/;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/validate-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Capability } from "./capability";
import { shouldSkipRequest } from "./filter/filter";
import { ValidateResponse } from "./k8s";
import { AdmissionRequest } from "./types";
import Log from "./logger";
import Log from "./telemetry/logger";
import { convertFromBase64Map } from "./utils";
import { PeprValidateRequest } from "./validate-request";
import { ModuleConfig } from "./module";
Expand Down
8 changes: 4 additions & 4 deletions src/lib/watch-processor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ import { K8sInit, WatchPhase } from "kubernetes-fluent-client/dist/fluent/types"
import { WatchCfg, WatchEvent, Watcher } from "kubernetes-fluent-client/dist/fluent/watch";
import { Capability } from "./capability";
import { setupWatch, logEvent, queueKey, getOrCreateQueue } from "./watch-processor";
import Log from "./logger";
import { metricsCollector } from "./metrics";
import Log from "./telemetry/logger";
import { metricsCollector } from "./telemetry/metrics";

type onCallback = (eventName: string | symbol, listener: (msg: string) => void) => void;

// Mock the dependencies
jest.mock("kubernetes-fluent-client");

jest.mock("./logger", () => ({
jest.mock("./telemetry/logger", () => ({
debug: jest.fn(),
error: jest.fn(),
}));

jest.mock("./metrics", () => ({
jest.mock("./telemetry/metrics", () => ({
metricsCollector: {
initCacheMissWindow: jest.fn(),
incCacheMiss: jest.fn(),
Expand Down
4 changes: 2 additions & 2 deletions src/lib/watch-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import { WatchPhase } from "kubernetes-fluent-client/dist/fluent/types";
import { Capability } from "./capability";
import { filterNoMatchReason } from "./helpers";
import { removeFinalizer } from "./finalizer";
import Log from "./logger";
import Log from "./telemetry/logger";
import { Queue } from "./queue";
import { Binding } from "./types";
import { Event } from "./enums";
import { metricsCollector } from "./metrics";
import { metricsCollector } from "./telemetry/metrics";

// stores Queue instances
const queues: Record<string, Queue<KubernetesObject>> = {};
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import crypto from "crypto";
import fs from "fs";
import { gunzipSync } from "zlib";
import { K8s, kind } from "kubernetes-fluent-client";
import Log from "../lib/logger";
import Log from "../lib/telemetry/logger";
import { packageJSON } from "../templates/data.json";
import { peprStoreCRD } from "../lib/assets/store";
import { validateHash } from "../lib/helpers";
Expand Down

0 comments on commit c9d7227

Please sign in to comment.