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

fix(core): upstream health checker optional field schema #169

Merged
merged 6 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions apps/cli/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@
"targets": {
"build": {
"executor": "@nx/webpack:webpack",
"outputs": ["{options.outputPath}"],
"outputs": [
"{options.outputPath}"
],
"defaultConfiguration": "production",
"options": {
"target": "node",
"compiler": "tsc",
"outputPath": "dist/apps/cli",
"main": "apps/cli/src/main.ts",
"tsConfig": "apps/cli/tsconfig.app.json",
"assets": ["apps/cli/src/assets"],
"assets": [
"apps/cli/src/assets"
],
"webpackConfig": "apps/cli/webpack.config.js"
},
"configurations": {
Expand All @@ -39,14 +43,24 @@
},
"lint": {
"executor": "@nx/eslint:lint",
"outputs": ["{options.outputFile}"]
"outputs": [
"{options.outputFile}"
]
},
"test": {
"executor": "@nx/jest:jest",
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
"outputs": [
"{workspaceRoot}/coverage/{projectRoot}"
],
"options": {
"jestConfig": "apps/cli/jest.config.ts"
}
},
"export-schema": {
"executor": "nx:run-commands",
"options": {
"command": "ts-node apps/cli/src/linter/exporter.ts"
}
}
},
"tags": []
Expand Down
17 changes: 9 additions & 8 deletions apps/cli/src/linter/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const timeoutSchema = z.object({
send: z.number().gt(0),
read: z.number().gt(0),
});
const hostSchema = z.string().min(1);
const portSchema = z.number().int().min(1).max(65535);
const secretRefSchema = z.string().regex(/^\$(secret|env):\/\//);
const certificateSchema = z.union([
Expand Down Expand Up @@ -73,8 +74,8 @@ const upstreamSchema = z
type: upstreamHealthCheckType.optional(),
timeout: z.number().default(1).optional(),
concurrency: z.number().default(10).optional(),
host: z.string(),
port: portSchema,
host: hostSchema.optional(),
port: portSchema.optional(),
http_path: z.string().default('/').optional(),
https_verify_cert: z.boolean().default(true).optional(),
http_request_headers: z.array(z.string()).min(1).optional(),
Expand Down Expand Up @@ -113,7 +114,7 @@ const upstreamSchema = z
nodes: z
.array(
z.object({
host: z.string(),
host: hostSchema,
port: portSchema.optional(),
weight: z.number().int().min(0),
priority: z.number().default(0).optional(),
Expand Down Expand Up @@ -149,7 +150,7 @@ const upstreamSchema = z
})
.optional(),
pass_host: z.enum(['pass', 'node', 'rewrite']).default('pass').optional(),
upstream_host: z.string().optional(),
upstream_host: hostSchema.optional(),

service_name: z.string().optional(),
discovery_type: z.string().optional(),
Expand All @@ -172,7 +173,7 @@ const routeSchema = z
description: descriptionSchema.optional(),
labels: labelsSchema.optional(),

hosts: z.array(z.string()).optional(),
hosts: z.array(hostSchema).optional(),
uris: z.array(z.string()).min(1),
priority: z.number().int().optional(),
timeout: timeoutSchema.optional(),
Expand Down Expand Up @@ -212,7 +213,7 @@ const streamRouteSchema = z
remote_addr: z.string().optional(),
server_addr: z.string().optional(),
server_port: portSchema.optional(),
sni: z.string().optional(),
sni: hostSchema.optional(),
})
.strict();

Expand All @@ -226,7 +227,7 @@ const serviceSchema = z
plugins: pluginsSchema.optional(),
path_prefix: z.string().optional(),
strip_path_prefix: z.boolean().optional(),
hosts: z.array(z.string()).optional(),
hosts: z.array(hostSchema).optional(),

routes: z.array(routeSchema).optional(),
stream_routes: z.array(streamRouteSchema).optional(),
Expand Down Expand Up @@ -254,7 +255,7 @@ const sslSchema = z
labels: labelsSchema.optional(),

type: z.enum(['server', 'client']).default('server').optional(),
snis: z.array(z.string().min(1)).min(1),
snis: z.array(hostSchema).min(1),
certificates: z
.array(
z
Expand Down
13 changes: 13 additions & 0 deletions apps/cli/src/linter/specs/schema-json.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { readFileSync } from 'node:fs';
import { join } from 'node:path';
import zodToJsonSchema from 'zod-to-json-schema';

import { ConfigurationSchema } from '../schema';

describe('Schema JSON Check', () => {
it('should check schema.json is consistent with git HEAD', () => {
const currentSchema = zodToJsonSchema(ConfigurationSchema);
const file = readFileSync(join('./schema.json'));
expect(JSON.parse(file.toString())).toEqual(currentSchema);
});
});
34 changes: 34 additions & 0 deletions apps/cli/src/linter/specs/upstream.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,40 @@ describe('Upstream Linter', () => {
} as ADCSDK.Configuration,
expect: true,
},
{
name: 'should check active health checker optional field',
input: {
services: [
{
name: 'No_HealthChecker_HostPort',
upstream: {
nodes: [
{
host: '1.1.1.1',
port: 443,
weight: 100,
},
],
checks: {
active: {
type: 'http',
http_path: '/',
healthy: {
interval: 2,
successes: 1,
},
unhealthy: {
interval: 1,
timeouts: 3,
},
},
},
},
},
],
} as ADCSDK.Configuration,
expect: true,
},
];

// test cases runner
Expand Down
22 changes: 9 additions & 13 deletions schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@
"default": 10
},
"host": {
"type": "string"
"type": "string",
"minLength": 1
},
"port": {
"type": "integer",
Expand Down Expand Up @@ -184,10 +185,6 @@
"additionalProperties": false
}
},
"required": [
"host",
"port"
],
"additionalProperties": false
},
"passive": {
Expand Down Expand Up @@ -241,7 +238,7 @@
"type": "object",
"properties": {
"host": {
"type": "string"
"$ref": "#/properties/services/items/properties/upstream/properties/checks/properties/active/properties/host"
},
"port": {
"$ref": "#/properties/services/items/properties/upstream/properties/checks/properties/active/properties/port"
Expand Down Expand Up @@ -367,7 +364,7 @@
"default": "pass"
},
"upstream_host": {
"type": "string"
"$ref": "#/properties/services/items/properties/upstream/properties/checks/properties/active/properties/host"
},
"service_name": {
"type": "string"
Expand Down Expand Up @@ -398,7 +395,7 @@
"hosts": {
"type": "array",
"items": {
"type": "string"
"$ref": "#/properties/services/items/properties/upstream/properties/checks/properties/active/properties/host"
}
},
"routes": {
Expand All @@ -418,7 +415,7 @@
"hosts": {
"type": "array",
"items": {
"type": "string"
"$ref": "#/properties/services/items/properties/upstream/properties/checks/properties/active/properties/host"
}
},
"uris": {
Expand Down Expand Up @@ -514,7 +511,7 @@
"$ref": "#/properties/services/items/properties/upstream/properties/checks/properties/active/properties/port"
},
"sni": {
"type": "string"
"$ref": "#/properties/services/items/properties/upstream/properties/checks/properties/active/properties/host"
}
},
"required": [
Expand Down Expand Up @@ -549,8 +546,7 @@
"snis": {
"type": "array",
"items": {
"type": "string",
"minLength": 1
"$ref": "#/properties/services/items/properties/upstream/properties/checks/properties/active/properties/host"
},
"minItems": 1
},
Expand Down Expand Up @@ -700,4 +696,4 @@
},
"additionalProperties": false,
"$schema": "http://json-schema.org/draft-07/schema#"
}
}
Loading