Skip to content
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
1 change: 1 addition & 0 deletions apps/cli/src/command/diff.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export const DiffCommand = new BackendCommand<DiffOptions>(
opts.labelSelector,
opts.includeResourceType,
opts.excludeResourceType,
opts.managedByLabel,
),
opts.lint ? LintTask() : { task: () => undefined },
!opts.remoteStateFile
Expand Down
6 changes: 6 additions & 0 deletions apps/cli/src/command/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,12 @@ export class BackendCommand<
)
.env('ADC_TLS_SKIP_VERIFY')
.default(false),
)
.addOption(
new Option(
'--no-managed-by-label',
'disable injecting the "managed-by=adc" label into synced resources',
),
);
}
}
Expand Down
1 change: 1 addition & 0 deletions apps/cli/src/command/ingress-sync.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const IngressSyncCommand = new BackendCommand<SyncOption>('sync')
opts.labelSelector,
opts.includeResourceType,
opts.excludeResourceType,
opts.managedByLabel,
),
LintTask(),
!opts.remoteStateFile
Expand Down
5 changes: 4 additions & 1 deletion apps/cli/src/command/lint.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ export const LintCommand = new BaseCommand('lint')
const opts = LintCommand.optsWithGlobals();

const tasks = new Listr(
[LoadLocalConfigurationTask(opts.file, {}), LintTask()],
[
LoadLocalConfigurationTask(opts.file, {}, undefined, undefined, false),
LintTask(),
],
{
renderer: SignaleRenderer,
rendererOptions: { verbose: opts.verbose },
Expand Down
1 change: 1 addition & 0 deletions apps/cli/src/command/sync.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export const SyncCommand = new BackendCommand<SyncOption>(
opts.labelSelector,
opts.includeResourceType,
opts.excludeResourceType,
opts.managedByLabel,
),
opts.lint ? LintTask() : { task: () => undefined },
!opts.remoteStateFile
Expand Down
1 change: 1 addition & 0 deletions apps/cli/src/command/typing.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export type BackendOptions = {
labelSelector?: Record<string, string>;
includeResourceType?: Array<ADCSDK.ResourceType>;
excludeResourceType?: Array<ADCSDK.ResourceType>;
managedByLabel?: boolean;
} & GlobalOptions;

export interface KVConfiguration {
Expand Down
116 changes: 116 additions & 0 deletions apps/cli/src/command/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import * as ADCSDK from '@api7/adc-sdk';

import {
MANAGED_BY_LABEL_KEY,
MANAGED_BY_LABEL_VALUE,
fillLabels,
recursiveRemoveIdField,
recursiveReplaceEnvVars,
Expand Down Expand Up @@ -254,6 +256,120 @@ describe('CLI utils', () => {
});
});

it('should label local resources as managed by ADC, the same way fillLabels does for --label-selector', () => {
expect(
fillLabels(
{
services: [
{
name: 'Test Service',
routes: [
{
name: 'Test Nested Route',
uris: ['/test-nested'],
},
],
stream_routes: [
{
name: 'Test Nested Stream Route',
},
],
},
],
consumers: [{ username: 'alice', labels: { team: 'a' } }],
} as ADCSDK.Configuration,
{ [MANAGED_BY_LABEL_KEY]: MANAGED_BY_LABEL_VALUE },
),
).toEqual({
services: [
{
name: 'Test Service',
labels: { [MANAGED_BY_LABEL_KEY]: MANAGED_BY_LABEL_VALUE },
routes: [
{
name: 'Test Nested Route',
uris: ['/test-nested'],
labels: { [MANAGED_BY_LABEL_KEY]: MANAGED_BY_LABEL_VALUE },
},
],
stream_routes: [
{
name: 'Test Nested Stream Route',
labels: { [MANAGED_BY_LABEL_KEY]: MANAGED_BY_LABEL_VALUE },
},
],
},
],
consumers: [
{
username: 'alice',
labels: { team: 'a', [MANAGED_BY_LABEL_KEY]: MANAGED_BY_LABEL_VALUE },
},
],
});
});

it('should overwrite an existing managed-by label with a different value, keeping other labels', () => {
expect(
fillLabels(
{
services: [
{
name: 'Test Service',
labels: { team: 'a', [MANAGED_BY_LABEL_KEY]: 'something-else' },
routes: [
{
name: 'Test Nested Route',
uris: ['/test-nested'],
labels: { [MANAGED_BY_LABEL_KEY]: 'something-else' },
},
],
stream_routes: [
{
name: 'Test Nested Stream Route',
labels: { [MANAGED_BY_LABEL_KEY]: 'something-else' },
},
],
},
],
consumers: [
{
username: 'alice',
labels: { [MANAGED_BY_LABEL_KEY]: 'something-else' },
},
],
} as ADCSDK.Configuration,
{ [MANAGED_BY_LABEL_KEY]: MANAGED_BY_LABEL_VALUE },
),
).toEqual({
services: [
{
name: 'Test Service',
labels: { team: 'a', [MANAGED_BY_LABEL_KEY]: MANAGED_BY_LABEL_VALUE },
routes: [
{
name: 'Test Nested Route',
uris: ['/test-nested'],
labels: { [MANAGED_BY_LABEL_KEY]: MANAGED_BY_LABEL_VALUE },
},
],
stream_routes: [
{
name: 'Test Nested Stream Route',
labels: { [MANAGED_BY_LABEL_KEY]: MANAGED_BY_LABEL_VALUE },
},
],
},
],
consumers: [
{
username: 'alice',
labels: { [MANAGED_BY_LABEL_KEY]: MANAGED_BY_LABEL_VALUE },
},
],
});
});

describe('Environment Variables', () => {
it('mock config', () => {
const config: ADCSDK.Configuration = {
Expand Down
6 changes: 6 additions & 0 deletions apps/cli/src/command/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,12 @@ export const fillLabels = (
return configuration;
};

// Injected into local resources the same way --label-selector's fillLabels()
// works, so it flows into every CREATE/UPDATE payload naturally without any
// special-casing in the differ or the backend.
export const MANAGED_BY_LABEL_KEY = 'managed-by';
export const MANAGED_BY_LABEL_VALUE = 'adc';

export const filterResourceType = (
config: ADCSDK.Configuration = {},
includes?: Array<string>,
Expand Down
1 change: 1 addition & 0 deletions apps/cli/src/command/validate.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export const ValidateCommand = new BackendCommand<ValidateOptions>(
opts.labelSelector,
opts.includeResourceType,
opts.excludeResourceType,
opts.managedByLabel,
),
opts.lint ? LintTask() : { task: () => undefined },
DiffResourceTask(),
Expand Down
15 changes: 13 additions & 2 deletions apps/cli/src/tasks/load_local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import { ListrTask } from 'listr2';
import path from 'node:path';

import type { TaskContext } from '../command/diff.command';

import {
MANAGED_BY_LABEL_KEY,
MANAGED_BY_LABEL_VALUE,
fillLabels,
filterResourceType,
mergeKVConfigurations,
Expand All @@ -22,6 +23,7 @@ export const LoadLocalConfigurationTask = (
labelSelector?: ADCSDK.BackendOptions['labelSelector'],
includeResourceType?: Array<ADCSDK.ResourceType>,
excludeResourceType?: Array<ADCSDK.ResourceType>,
managedByLabel = true,
): ListrTask<TaskContext> => ({
title: `Load local configuration`,
task: async (ctx, task) => {
Expand Down Expand Up @@ -87,7 +89,8 @@ export const LoadLocalConfigurationTask = (
{
title: 'Filter configuration resource type',
enabled: () =>
(includeResourceType?.length ?? 0) > 0 || (excludeResourceType?.length ?? 0) > 0,
(includeResourceType?.length ?? 0) > 0 ||
(excludeResourceType?.length ?? 0) > 0,
task: () => {
ctx.local = filterResourceType(
ctx.local,
Expand All @@ -104,6 +107,14 @@ export const LoadLocalConfigurationTask = (
fillLabels(ctx.local, labelSelector);
},
},
{
enabled: managedByLabel,
task: async () => {
fillLabels(ctx.local, {
[MANAGED_BY_LABEL_KEY]: MANAGED_BY_LABEL_VALUE,
});
},
},
],
{
ctx: subCtx,
Expand Down
9 changes: 8 additions & 1 deletion libs/sdk/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,12 @@
"extends": "../../tsconfig.base.json",
"files": [],
"include": [],
"references": []
"references": [
{
"path": "./tsconfig.lib.json"
},
{
"path": "./tsconfig.spec.json"
}
]
}
6 changes: 5 additions & 1 deletion libs/sdk/tsconfig.spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,9 @@
"src/**/*.spec.jsx",
"src/**/*.d.ts"
],
"references": []
"references": [
{
"path": "./tsconfig.lib.json"
}
]
}
Loading