Introduce Lighthouse Backend with Scheduling of Audits
Signed-off-by: Dominik Pfaffenbauer <dominik@pfaffenbauer.at>
This commit is contained in:
@@ -0,0 +1 @@
|
||||
module.exports = require('@backstage/cli/config/eslint-factory')(__dirname);
|
||||
@@ -0,0 +1,3 @@
|
||||
# @backstage/plugin-lighthouse-common
|
||||
|
||||
Common types and functionalities for lighthouse, to be shared between lighthouse and lighthouse-backend.
|
||||
@@ -0,0 +1,286 @@
|
||||
## API Report File for "@backstage/plugin-kubernetes-common"
|
||||
|
||||
> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/).
|
||||
|
||||
```ts
|
||||
import { Entity } from '@backstage/catalog-model';
|
||||
import type { JsonObject } from '@backstage/types';
|
||||
import { PodStatus } from '@kubernetes/client-node';
|
||||
import { V1ConfigMap } from '@kubernetes/client-node';
|
||||
import { V1CronJob } from '@kubernetes/client-node';
|
||||
import { V1DaemonSet } from '@kubernetes/client-node';
|
||||
import { V1Deployment } from '@kubernetes/client-node';
|
||||
import { V1HorizontalPodAutoscaler } from '@kubernetes/client-node';
|
||||
import { V1Ingress } from '@kubernetes/client-node';
|
||||
import { V1Job } from '@kubernetes/client-node';
|
||||
import { V1LimitRange } from '@kubernetes/client-node';
|
||||
import { V1Pod } from '@kubernetes/client-node';
|
||||
import { V1ReplicaSet } from '@kubernetes/client-node';
|
||||
import { V1Service } from '@kubernetes/client-node';
|
||||
import { V1StatefulSet } from '@kubernetes/client-node';
|
||||
|
||||
// @public (undocumented)
|
||||
export type AuthProviderType = 'google' | 'serviceAccount' | 'aws' | 'azure';
|
||||
|
||||
// @public (undocumented)
|
||||
export interface ClientContainerStatus {
|
||||
// (undocumented)
|
||||
container: string;
|
||||
// (undocumented)
|
||||
cpuUsage: ClientCurrentResourceUsage;
|
||||
// (undocumented)
|
||||
memoryUsage: ClientCurrentResourceUsage;
|
||||
}
|
||||
|
||||
// @public (undocumented)
|
||||
export interface ClientCurrentResourceUsage {
|
||||
// (undocumented)
|
||||
currentUsage: number | string;
|
||||
// (undocumented)
|
||||
limitTotal: number | string;
|
||||
// (undocumented)
|
||||
requestTotal: number | string;
|
||||
}
|
||||
|
||||
// @public (undocumented)
|
||||
export interface ClientPodStatus {
|
||||
// (undocumented)
|
||||
containers: ClientContainerStatus[];
|
||||
// (undocumented)
|
||||
cpu: ClientCurrentResourceUsage;
|
||||
// (undocumented)
|
||||
memory: ClientCurrentResourceUsage;
|
||||
// (undocumented)
|
||||
pod: V1Pod;
|
||||
}
|
||||
|
||||
// @public (undocumented)
|
||||
export interface ClusterAttributes {
|
||||
dashboardApp?: string;
|
||||
dashboardParameters?: JsonObject;
|
||||
dashboardUrl?: string;
|
||||
name: string;
|
||||
}
|
||||
|
||||
// @public (undocumented)
|
||||
export interface ClusterObjects {
|
||||
// (undocumented)
|
||||
cluster: ClusterAttributes;
|
||||
// (undocumented)
|
||||
errors: KubernetesFetchError[];
|
||||
// (undocumented)
|
||||
podMetrics: ClientPodStatus[];
|
||||
// (undocumented)
|
||||
resources: FetchResponse[];
|
||||
}
|
||||
|
||||
// @public (undocumented)
|
||||
export interface ConfigMapFetchResponse {
|
||||
// (undocumented)
|
||||
resources: Array<V1ConfigMap>;
|
||||
// (undocumented)
|
||||
type: 'configmaps';
|
||||
}
|
||||
|
||||
// @public (undocumented)
|
||||
export interface CronJobsFetchResponse {
|
||||
// (undocumented)
|
||||
resources: Array<V1CronJob>;
|
||||
// (undocumented)
|
||||
type: 'cronjobs';
|
||||
}
|
||||
|
||||
// @public (undocumented)
|
||||
export interface CustomObjectsByEntityRequest {
|
||||
// (undocumented)
|
||||
auth: KubernetesRequestAuth;
|
||||
// (undocumented)
|
||||
customResources: CustomResourceMatcher[];
|
||||
// (undocumented)
|
||||
entity: Entity;
|
||||
}
|
||||
|
||||
// @public (undocumented)
|
||||
export interface CustomResourceFetchResponse {
|
||||
// (undocumented)
|
||||
resources: Array<any>;
|
||||
// (undocumented)
|
||||
type: 'customresources';
|
||||
}
|
||||
|
||||
// @public (undocumented)
|
||||
export interface CustomResourceMatcher {
|
||||
// (undocumented)
|
||||
apiVersion: string;
|
||||
// (undocumented)
|
||||
group: string;
|
||||
// (undocumented)
|
||||
plural: string;
|
||||
}
|
||||
|
||||
// @public (undocumented)
|
||||
export interface DaemonSetsFetchResponse {
|
||||
// (undocumented)
|
||||
resources: Array<V1DaemonSet>;
|
||||
// (undocumented)
|
||||
type: 'daemonsets';
|
||||
}
|
||||
|
||||
// @public (undocumented)
|
||||
export interface DeploymentFetchResponse {
|
||||
// (undocumented)
|
||||
resources: Array<V1Deployment>;
|
||||
// (undocumented)
|
||||
type: 'deployments';
|
||||
}
|
||||
|
||||
// @public (undocumented)
|
||||
export type FetchResponse =
|
||||
| PodFetchResponse
|
||||
| ServiceFetchResponse
|
||||
| ConfigMapFetchResponse
|
||||
| DeploymentFetchResponse
|
||||
| LimitRangeFetchReponse
|
||||
| ReplicaSetsFetchResponse
|
||||
| HorizontalPodAutoscalersFetchResponse
|
||||
| JobsFetchResponse
|
||||
| CronJobsFetchResponse
|
||||
| IngressesFetchResponse
|
||||
| CustomResourceFetchResponse
|
||||
| StatefulSetsFetchResponse
|
||||
| DaemonSetsFetchResponse
|
||||
| PodStatusFetchResponse;
|
||||
|
||||
// @public (undocumented)
|
||||
export interface HorizontalPodAutoscalersFetchResponse {
|
||||
// (undocumented)
|
||||
resources: Array<V1HorizontalPodAutoscaler>;
|
||||
// (undocumented)
|
||||
type: 'horizontalpodautoscalers';
|
||||
}
|
||||
|
||||
// @public (undocumented)
|
||||
export interface IngressesFetchResponse {
|
||||
// (undocumented)
|
||||
resources: Array<V1Ingress>;
|
||||
// (undocumented)
|
||||
type: 'ingresses';
|
||||
}
|
||||
|
||||
// @public (undocumented)
|
||||
export interface JobsFetchResponse {
|
||||
// (undocumented)
|
||||
resources: Array<V1Job>;
|
||||
// (undocumented)
|
||||
type: 'jobs';
|
||||
}
|
||||
|
||||
// @public (undocumented)
|
||||
export type KubernetesErrorTypes =
|
||||
| 'BAD_REQUEST'
|
||||
| 'UNAUTHORIZED_ERROR'
|
||||
| 'NOT_FOUND'
|
||||
| 'SYSTEM_ERROR'
|
||||
| 'UNKNOWN_ERROR';
|
||||
|
||||
// @public (undocumented)
|
||||
export type KubernetesFetchError = StatusError | RawFetchError;
|
||||
|
||||
// @public (undocumented)
|
||||
export interface KubernetesRequestAuth {
|
||||
// (undocumented)
|
||||
google?: string;
|
||||
// (undocumented)
|
||||
oidc?: {
|
||||
[key: string]: string;
|
||||
};
|
||||
}
|
||||
|
||||
// @public (undocumented)
|
||||
export interface KubernetesRequestBody {
|
||||
// (undocumented)
|
||||
auth?: KubernetesRequestAuth;
|
||||
// (undocumented)
|
||||
entity: Entity;
|
||||
}
|
||||
|
||||
// @public (undocumented)
|
||||
export interface LimitRangeFetchReponse {
|
||||
// (undocumented)
|
||||
resources: Array<V1LimitRange>;
|
||||
// (undocumented)
|
||||
type: 'limitranges';
|
||||
}
|
||||
|
||||
// @public (undocumented)
|
||||
export interface ObjectsByEntityResponse {
|
||||
// (undocumented)
|
||||
items: ClusterObjects[];
|
||||
}
|
||||
|
||||
// @public (undocumented)
|
||||
export interface PodFetchResponse {
|
||||
// (undocumented)
|
||||
resources: Array<V1Pod>;
|
||||
// (undocumented)
|
||||
type: 'pods';
|
||||
}
|
||||
|
||||
// @public (undocumented)
|
||||
export interface PodStatusFetchResponse {
|
||||
// (undocumented)
|
||||
resources: Array<PodStatus>;
|
||||
// (undocumented)
|
||||
type: 'podstatus';
|
||||
}
|
||||
|
||||
// @public (undocumented)
|
||||
export interface RawFetchError {
|
||||
// (undocumented)
|
||||
errorType: 'FETCH_ERROR';
|
||||
// (undocumented)
|
||||
message: string;
|
||||
}
|
||||
|
||||
// @public (undocumented)
|
||||
export interface ReplicaSetsFetchResponse {
|
||||
// (undocumented)
|
||||
resources: Array<V1ReplicaSet>;
|
||||
// (undocumented)
|
||||
type: 'replicasets';
|
||||
}
|
||||
|
||||
// @public (undocumented)
|
||||
export interface ServiceFetchResponse {
|
||||
// (undocumented)
|
||||
resources: Array<V1Service>;
|
||||
// (undocumented)
|
||||
type: 'services';
|
||||
}
|
||||
|
||||
// @public (undocumented)
|
||||
export interface StatefulSetsFetchResponse {
|
||||
// (undocumented)
|
||||
resources: Array<V1StatefulSet>;
|
||||
// (undocumented)
|
||||
type: 'statefulsets';
|
||||
}
|
||||
|
||||
// @public (undocumented)
|
||||
export interface StatusError {
|
||||
// (undocumented)
|
||||
errorType: KubernetesErrorTypes;
|
||||
// (undocumented)
|
||||
resourcePath?: string;
|
||||
// (undocumented)
|
||||
statusCode?: number;
|
||||
}
|
||||
|
||||
// @public (undocumented)
|
||||
export interface WorkloadsByEntityRequest {
|
||||
// (undocumented)
|
||||
auth: KubernetesRequestAuth;
|
||||
// (undocumented)
|
||||
entity: Entity;
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,51 @@
|
||||
{
|
||||
"name": "@backstage/plugin-lighthouse-common",
|
||||
"description": "Common functionalities for lighthouse, to be shared between lighthouse and lighthouse-backend plugin",
|
||||
"version": "0.0.0",
|
||||
"main": "src/index.ts",
|
||||
"types": "src/index.ts",
|
||||
"license": "Apache-2.0",
|
||||
"publishConfig": {
|
||||
"access": "public",
|
||||
"main": "dist/index.cjs.js",
|
||||
"module": "dist/index.esm.js",
|
||||
"types": "dist/index.d.ts"
|
||||
},
|
||||
"backstage": {
|
||||
"role": "common-library"
|
||||
},
|
||||
"homepage": "https://backstage.io",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/backstage/backstage",
|
||||
"directory": "plugins/lighthouse-common"
|
||||
},
|
||||
"keywords": [
|
||||
"lighthouse"
|
||||
],
|
||||
"files": [
|
||||
"dist"
|
||||
],
|
||||
"scripts": {
|
||||
"build": "backstage-cli package build",
|
||||
"lint": "backstage-cli package lint",
|
||||
"test": "backstage-cli package test",
|
||||
"prepack": "backstage-cli package prepack",
|
||||
"postpack": "backstage-cli package postpack",
|
||||
"clean": "backstage-cli package clean"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/backstage/backstage/issues"
|
||||
},
|
||||
"dependencies": {
|
||||
"@backstage/config": "workspace:^"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@backstage/cli": "workspace:^"
|
||||
},
|
||||
"jest": {
|
||||
"roots": [
|
||||
".."
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,188 @@
|
||||
/*
|
||||
* Copyright 2020 The Backstage Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { Config } from '@backstage/config';
|
||||
|
||||
/** @public */
|
||||
export type LighthouseCategoryId =
|
||||
| 'pwa'
|
||||
| 'seo'
|
||||
| 'performance'
|
||||
| 'accessibility'
|
||||
| 'best-practices';
|
||||
|
||||
/** @public */
|
||||
export interface LighthouseCategoryAbbr {
|
||||
id: LighthouseCategoryId;
|
||||
score: number;
|
||||
title: string;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export interface LASListRequest {
|
||||
offset?: number;
|
||||
limit?: number;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export interface LASListResponse<Item> {
|
||||
items: Item[];
|
||||
total: number;
|
||||
offset: number;
|
||||
limit: number;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export interface AuditBase {
|
||||
id: string;
|
||||
url: string;
|
||||
timeCreated: string;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export interface AuditRunning extends AuditBase {
|
||||
status: 'RUNNING';
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export interface AuditFailed extends AuditBase {
|
||||
status: 'FAILED';
|
||||
timeCompleted: string;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export interface AuditCompleted extends AuditBase {
|
||||
status: 'COMPLETED';
|
||||
timeCompleted: string;
|
||||
report: Object;
|
||||
categories: Record<LighthouseCategoryId, LighthouseCategoryAbbr>;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export type Audit = AuditRunning | AuditFailed | AuditCompleted;
|
||||
|
||||
/** @public */
|
||||
export interface Website {
|
||||
url: string;
|
||||
audits: Audit[];
|
||||
lastAudit: Audit;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export type WebsiteListResponse = LASListResponse<Website>;
|
||||
|
||||
/** @public */
|
||||
export type FormFactor = 'mobile' | 'desktop';
|
||||
|
||||
/** @public */
|
||||
export type LighthouseConfigSettings = {
|
||||
// For lighthouse 7+
|
||||
formFactor: FormFactor;
|
||||
screenEmulation:
|
||||
| {
|
||||
mobile: boolean;
|
||||
width: number;
|
||||
height: number;
|
||||
deviceScaleFactor: number;
|
||||
disabled: boolean;
|
||||
}
|
||||
| undefined;
|
||||
// For lighthouse before 7
|
||||
emulatedFormFactor: FormFactor;
|
||||
};
|
||||
|
||||
/** @public */
|
||||
export interface TriggerAuditPayload {
|
||||
url: string;
|
||||
options: {
|
||||
lighthouseConfig: {
|
||||
settings: LighthouseConfigSettings;
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export class FetchError extends Error {
|
||||
get name(): string {
|
||||
return this.constructor.name;
|
||||
}
|
||||
|
||||
static async forResponse(resp: Response): Promise<FetchError> {
|
||||
return new FetchError(
|
||||
`Request failed with status code ${
|
||||
resp.status
|
||||
}.\nReason: ${await resp.text()}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export type LighthouseApi = {
|
||||
url: string;
|
||||
getWebsiteList: (listOptions: LASListRequest) => Promise<WebsiteListResponse>;
|
||||
getWebsiteForAuditId: (auditId: string) => Promise<Website>;
|
||||
triggerAudit: (payload: TriggerAuditPayload) => Promise<Audit>;
|
||||
getWebsiteByUrl: (websiteUrl: string) => Promise<Website>;
|
||||
};
|
||||
|
||||
/** @public */
|
||||
export class LighthouseRestApi implements LighthouseApi {
|
||||
static fromConfig(config: Config) {
|
||||
return new LighthouseRestApi(config.getString('lighthouse.baseUrl'));
|
||||
}
|
||||
|
||||
constructor(public url: string) {}
|
||||
|
||||
private async fetch<T = any>(input: string, init?: RequestInit): Promise<T> {
|
||||
const resp = await fetch(`${this.url}${input}`, init);
|
||||
if (!resp.ok) throw await FetchError.forResponse(resp);
|
||||
return await resp.json();
|
||||
}
|
||||
|
||||
async getWebsiteList(
|
||||
options: LASListRequest = {},
|
||||
): Promise<WebsiteListResponse> {
|
||||
const { limit, offset } = options;
|
||||
const params = new URLSearchParams();
|
||||
if (typeof limit === 'number') params.append('limit', limit.toString());
|
||||
if (typeof offset === 'number') params.append('offset', offset.toString());
|
||||
return await this.fetch<WebsiteListResponse>(
|
||||
`/v1/websites?${params.toString()}`,
|
||||
);
|
||||
}
|
||||
|
||||
async getWebsiteForAuditId(auditId: string): Promise<Website> {
|
||||
return await this.fetch<Website>(
|
||||
`/v1/audits/${encodeURIComponent(auditId)}/website`,
|
||||
);
|
||||
}
|
||||
|
||||
async triggerAudit(payload: TriggerAuditPayload): Promise<Audit> {
|
||||
return await this.fetch<Audit>('/v1/audits', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(payload),
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async getWebsiteByUrl(websiteUrl: string): Promise<Website> {
|
||||
return this.fetch<Website>(
|
||||
`/v1/websites/${encodeURIComponent(websiteUrl)}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* Copyright 2020 The Backstage Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Common functionalities for Lighthouse, to be shared between the `lighthouse` and `lighthouse-backend` plugins
|
||||
*
|
||||
* @packageDocumentation
|
||||
*/
|
||||
|
||||
export * from './api';
|
||||
Reference in New Issue
Block a user