@grafana/create-plugin 7.10.1 → 7.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +20 -0
- package/dist/codemods/additions/additions.js +5 -0
- package/dist/codemods/additions/scripts/experimental-app-sdk.js +275 -0
- package/dist/codemods/context.js +13 -1
- package/dist/codemods/runner.js +2 -1
- package/dist/codemods/utils.goMod.js +45 -0
- package/dist/codemods/utils.js +25 -1
- package/dist/commands/add.command.js +9 -4
- package/dist/commands/generate/print-success-message.js +1 -1
- package/package.json +3 -3
- package/src/codemods/additions/additions.ts +5 -0
- package/src/codemods/additions/scripts/experimental-app-sdk.test.ts +655 -0
- package/src/codemods/additions/scripts/experimental-app-sdk.ts +431 -0
- package/src/codemods/context.test.ts +7 -1
- package/src/codemods/context.ts +23 -1
- package/src/codemods/runner.ts +3 -1
- package/src/codemods/utils.goMod.test.ts +86 -0
- package/src/codemods/utils.goMod.ts +70 -0
- package/src/codemods/utils.test.ts +49 -0
- package/src/codemods/utils.ts +39 -0
- package/src/commands/add.command.ts +10 -5
- package/src/commands/generate/print-success-message.ts +1 -1
- package/templates/app-sdk/.config/AGENTS/app-sdk.md +87 -0
- package/templates/app-sdk/.config/app-sdk/README.md +60 -0
- package/templates/app-sdk/.config/app-sdk/generate-kinds.mjs +138 -0
- package/templates/app-sdk/.github/workflows/generate-kinds-drift.yml +84 -0
- package/templates/app-sdk/kinds/README.md +3 -0
- package/templates/app-sdk/kinds/config.cue +39 -0
- package/templates/app-sdk/kinds/cue.mod/module.cue +4 -0
- package/templates/app-sdk/kinds/example.cue +29 -0
- package/templates/app-sdk/kinds/manifest.cue +29 -0
- package/templates/app-sdk/pkg/generated/example/v1alpha1/doc.go +11 -0
- package/templates/app-sdk/pkg/generated/manifestdata/doc.go +12 -0
- package/templates/app-sdk/pkg/provider/provider.go +30 -0
- package/templates/common/_package.json +1 -1
- package/vitest.setup.ts +2 -2
|
@@ -8,13 +8,25 @@ import {
|
|
|
8
8
|
readJsonFile,
|
|
9
9
|
isVersionGreater,
|
|
10
10
|
printChanges,
|
|
11
|
+
runGoModTidy,
|
|
11
12
|
} from './utils.js';
|
|
12
13
|
import { join } from 'node:path';
|
|
13
14
|
import { mkdir, rm, writeFile } from 'node:fs/promises';
|
|
14
15
|
import { readFileSync } from 'node:fs';
|
|
16
|
+
import { execSync } from 'node:child_process';
|
|
17
|
+
import which from 'which';
|
|
15
18
|
import { output } from '../utils/utils.console.js';
|
|
16
19
|
import { vi } from 'vitest';
|
|
17
20
|
|
|
21
|
+
vi.mock(import('node:child_process'), async (importOriginal) => ({
|
|
22
|
+
...(await importOriginal()),
|
|
23
|
+
execSync: vi.fn(),
|
|
24
|
+
}));
|
|
25
|
+
|
|
26
|
+
vi.mock('which', () => ({
|
|
27
|
+
default: { sync: vi.fn() },
|
|
28
|
+
}));
|
|
29
|
+
|
|
18
30
|
describe('utils', () => {
|
|
19
31
|
const tmpObj = dirSync({ unsafeCleanup: true });
|
|
20
32
|
const tmpDir = join(tmpObj.name, 'cp-test-migration');
|
|
@@ -121,6 +133,43 @@ describe('utils', () => {
|
|
|
121
133
|
});
|
|
122
134
|
});
|
|
123
135
|
|
|
136
|
+
describe('runGoModTidy', () => {
|
|
137
|
+
beforeEach(() => {
|
|
138
|
+
vi.mocked(execSync).mockClear();
|
|
139
|
+
vi.mocked(which.sync).mockReset().mockReturnValue('/usr/local/bin/go');
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it('runs `go mod tidy` when go.mod was updated', async () => {
|
|
143
|
+
await writeFile(join(tmpDir, 'go.mod'), 'module example.com/foo\n');
|
|
144
|
+
const context = new Context(tmpDir);
|
|
145
|
+
context.updateFile('go.mod', 'module example.com/foo\n\nrequire example.com/bar v1.0.0\n');
|
|
146
|
+
|
|
147
|
+
runGoModTidy(context);
|
|
148
|
+
|
|
149
|
+
expect(execSync).toHaveBeenCalledWith('go mod tidy', { cwd: tmpDir, stdio: 'inherit' });
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it('does nothing when go.mod was not changed', () => {
|
|
153
|
+
const context = new Context(tmpDir);
|
|
154
|
+
context.addFile('other.txt', 'content');
|
|
155
|
+
|
|
156
|
+
runGoModTidy(context);
|
|
157
|
+
|
|
158
|
+
expect(execSync).not.toHaveBeenCalled();
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it('does nothing when there is no `go` binary on PATH', async () => {
|
|
162
|
+
vi.mocked(which.sync).mockReturnValue(null as unknown as string);
|
|
163
|
+
await writeFile(join(tmpDir, 'go.mod'), 'module example.com/foo\n');
|
|
164
|
+
const context = new Context(tmpDir);
|
|
165
|
+
context.updateFile('go.mod', 'module example.com/foo\n\nrequire example.com/baz v2.0.0\n');
|
|
166
|
+
|
|
167
|
+
runGoModTidy(context);
|
|
168
|
+
|
|
169
|
+
expect(execSync).not.toHaveBeenCalled();
|
|
170
|
+
});
|
|
171
|
+
});
|
|
172
|
+
|
|
124
173
|
describe('readJsonFile', () => {
|
|
125
174
|
it('should read a json file', () => {
|
|
126
175
|
const context = new Context(tmpDir);
|
package/src/codemods/utils.ts
CHANGED
|
@@ -6,6 +6,7 @@ import { styleText } from 'node:util';
|
|
|
6
6
|
import { output } from '../utils/utils.console.js';
|
|
7
7
|
import { getPackageManagerSilentInstallCmd, getPackageManagerWithFallback } from '../utils/utils.packageManager.js';
|
|
8
8
|
import { execSync } from 'node:child_process';
|
|
9
|
+
import which from 'which';
|
|
9
10
|
import { clean, coerce, gt, gte } from 'semver';
|
|
10
11
|
import { debug } from '../utils/utils.cli.js';
|
|
11
12
|
import { renderHandlebarsTemplate } from '../utils/utils.handlebars.js';
|
|
@@ -133,6 +134,44 @@ export function installNPMDependencies(context: Context) {
|
|
|
133
134
|
}
|
|
134
135
|
}
|
|
135
136
|
|
|
137
|
+
// Cache the go.mod contents to avoid re-running `go mod tidy` if go.mod hasn't changed
|
|
138
|
+
// (This runs for each codemod used in an update)
|
|
139
|
+
let goModTidyCache: string;
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Runs `go mod tidy` when a codemod has updated go.mod, mirroring installNPMDependencies. Skipped
|
|
143
|
+
* when there is no `go` binary on PATH, since a Go backend is optional for plugins that use this repo's
|
|
144
|
+
* tooling from a machine without a Go toolchain.
|
|
145
|
+
*/
|
|
146
|
+
export function runGoModTidy(context: Context) {
|
|
147
|
+
const hasGoModChanges = Object.entries(context.listChanges()).some(
|
|
148
|
+
([filePath, { changeType }]) => filePath === 'go.mod' && changeType === 'update'
|
|
149
|
+
);
|
|
150
|
+
|
|
151
|
+
if (!hasGoModChanges) {
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const goModContents = context.getFile('go.mod');
|
|
156
|
+
|
|
157
|
+
if (!goModContents) {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
if (goModContents === goModTidyCache) {
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (!which.sync('go', { nothrow: true })) {
|
|
166
|
+
additionsDebug('No `go` binary found on PATH. Skipping `go mod tidy`.');
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
goModTidyCache = goModContents;
|
|
171
|
+
output.logSingleLine('Running `go mod tidy`...');
|
|
172
|
+
execSync('go mod tidy', { cwd: context.basePath, stdio: 'inherit' });
|
|
173
|
+
}
|
|
174
|
+
|
|
136
175
|
export function readJsonFile<T extends object = any>(context: Context, path: string): T {
|
|
137
176
|
if (!context.doesFileExist(path)) {
|
|
138
177
|
throw new Error(`Cannot find ${path}`);
|
|
@@ -24,11 +24,16 @@ export const add = async (argv: minimist.ParsedArgs) => {
|
|
|
24
24
|
|
|
25
25
|
// filter out minimist internal properties (_ and $0) before passing to codemod
|
|
26
26
|
const { _, $0, ...codemodOptions } = argv;
|
|
27
|
-
await runCodemod(addition, codemodOptions);
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
27
|
+
const context = await runCodemod(addition, codemodOptions);
|
|
28
|
+
|
|
29
|
+
const message = context.getMessage();
|
|
30
|
+
if (message) {
|
|
31
|
+
output[message.level]({ title: message.title, body: message.body });
|
|
32
|
+
} else {
|
|
33
|
+
output.success({
|
|
34
|
+
title: `Successfully added ${addition.name} to your plugin.`,
|
|
35
|
+
});
|
|
36
|
+
}
|
|
32
37
|
} catch (error) {
|
|
33
38
|
if (error instanceof Error) {
|
|
34
39
|
output.error({
|
|
@@ -39,7 +39,7 @@ export function printGenerateSuccessMessage(answers: TemplateData) {
|
|
|
39
39
|
});
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
-
function getBackendCmd() {
|
|
42
|
+
export function getBackendCmd() {
|
|
43
43
|
const platform = machine();
|
|
44
44
|
if (platform === 'arm64') {
|
|
45
45
|
return output.formatCode('mage -v build:linuxARM64');
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: grafana-app-sdk kind instructions for a grafana plugin
|
|
3
|
+
description: Guides how to work with CUE kinds and generated code in a plugin that uses the grafana-app-sdk
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# grafana-app-sdk kinds
|
|
7
|
+
|
|
8
|
+
This plugin defines its own API resources ("kinds") as [CUE](https://cuelang.org/) schemas using the
|
|
9
|
+
[grafana-app-sdk](https://github.com/grafana/grafana-app-sdk). Code generation turns those schemas into
|
|
10
|
+
TypeScript types and an app manifest that Grafana reads from the plugin bundle.
|
|
11
|
+
|
|
12
|
+
## Critical rules
|
|
13
|
+
|
|
14
|
+
- **`kinds/*.cue` is the source of truth for the API.** To change a resource's shape, edit the CUE and
|
|
15
|
+
regenerate. Never work the other way around.
|
|
16
|
+
- **Never hand-edit generated code.** Everything under the generated directories listed below is
|
|
17
|
+
overwritten on the next run, so edits there are silently lost. They carry a
|
|
18
|
+
`Code generated - EDITING IS FUTILE. DO NOT EDIT.` header.
|
|
19
|
+
- **Regenerate after every change under `kinds/`:**
|
|
20
|
+
```bash
|
|
21
|
+
{{ packageManagerName }} run generate:kinds
|
|
22
|
+
```
|
|
23
|
+
- **Generated code is committed.** Commit the regenerated files alongside the CUE change so schema
|
|
24
|
+
changes are reviewable and a fresh clone builds without running code generation.
|
|
25
|
+
- **This plugin generates no Go code.** `kinds/config.cue` sets `codegen: goEnabled: false`, so
|
|
26
|
+
generation emits only TypeScript and the JSON definitions. Do not add Go output paths or a Go
|
|
27
|
+
backend to work around a generation problem.
|
|
28
|
+
- **Do not set `GRAFANA_APP_SDK_BIN`.** It overrides the pinned CLI with a local build, and is meant
|
|
29
|
+
for people working on the app-sdk itself. Code generated with it can differ from what `VERSION` in
|
|
30
|
+
`.config/app-sdk/generate-kinds.mjs` produces, so committing it would put the repository out of step. Run
|
|
31
|
+
`generate:kinds` without it.
|
|
32
|
+
- **Do not add code generation to the build.** It is a schema-change-time step, not a build step. The
|
|
33
|
+
frontend build must keep working without a Go toolchain.
|
|
34
|
+
|
|
35
|
+
## Layout
|
|
36
|
+
|
|
37
|
+
| Path | What it is |
|
|
38
|
+
| ---- | ---------- |
|
|
39
|
+
| `kinds/manifest.cue` | The app manifest: app name, and the versions and kinds your app serves. |
|
|
40
|
+
| `kinds/*.cue` | One file per kind. Edit these to change a resource's schema. |
|
|
41
|
+
| `kinds/config.cue` | Code generation settings (output paths). Rarely needs changing. |
|
|
42
|
+
| `src/generated/` | Generated TypeScript types. **Do not edit.** Import from here in frontend code. |
|
|
43
|
+
| `src/app-sdk-manifest.json` | Generated app manifest JSON. **Do not edit.** The frontend build's existing JSON copy pattern carries it into the plugin bundle. |
|
|
44
|
+
|
|
45
|
+
## Changing a schema
|
|
46
|
+
|
|
47
|
+
1. Edit the relevant kind in `kinds/`. Add a new kind by creating a file and listing it in the version's
|
|
48
|
+
`kinds` array in `kinds/manifest.cue`.
|
|
49
|
+
2. Run `{{ packageManagerName }} run generate:kinds`.
|
|
50
|
+
3. Update the frontend to match. Because the TypeScript types are generated from the CUE, a schema
|
|
51
|
+
change surfaces as a type error wherever the code is now wrong — fix those rather than casting.
|
|
52
|
+
4. Commit the CUE and the regenerated files together.
|
|
53
|
+
|
|
54
|
+
Common CUE constructs, for reference:
|
|
55
|
+
|
|
56
|
+
```cue
|
|
57
|
+
spec: {
|
|
58
|
+
title: string // required
|
|
59
|
+
owner?: string // optional
|
|
60
|
+
tier: *"gold" | "silver" | "bronze" // enum, defaults to "gold"
|
|
61
|
+
labels: [string]: string // map
|
|
62
|
+
tags: [...string] // list
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
For anything beyond this — subtypes, time types, constraints, multiple versions — consult the app-sdk's
|
|
67
|
+
kind authoring reference at
|
|
68
|
+
https://github.com/grafana/grafana-app-sdk/blob/main/docs/custom-kinds/writing-kinds.md rather than
|
|
69
|
+
guessing at CUE syntax.
|
|
70
|
+
|
|
71
|
+
## Serving the kinds
|
|
72
|
+
|
|
73
|
+
Grafana serves storage and CRUD for the kinds from the manifest bundled in the plugin, behind the
|
|
74
|
+
`appplugins.loadAppManifest` and `appplugins.registerAPIServer` feature toggles. The development server
|
|
75
|
+
enables both. Grafana only reads plugin manifests at startup, so restart it after rebuilding.
|
|
76
|
+
|
|
77
|
+
Resources are served under a Kubernetes-style path:
|
|
78
|
+
|
|
79
|
+
```
|
|
80
|
+
/apis/<group>/<version>/namespaces/<namespace>/<plural>
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
The namespace is deployment-dependent — `default` on single-tenant Grafana, `stacks-<id>` on Grafana
|
|
84
|
+
Cloud. Read it from `config.namespace` in `@grafana/runtime`; never hardcode it.
|
|
85
|
+
|
|
86
|
+
This plugin has no Go backend, and does not need one for storage or CRUD. Admission (validation and
|
|
87
|
+
mutation), conversion between versions, and custom routes would require adding one.
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# Kinds (grafana-app-sdk)
|
|
2
|
+
|
|
3
|
+
The [kinds/](../../kinds) directory declares your app's API as [CUE](https://cuelang.org/) "kinds", which
|
|
4
|
+
[grafana-app-sdk](https://github.com/grafana/grafana-app-sdk) turns into TypeScript types and an app
|
|
5
|
+
manifest.
|
|
6
|
+
|
|
7
|
+
| File | Purpose |
|
|
8
|
+
| ---- | ------- |
|
|
9
|
+
| `manifest.cue` | The app manifest: app name, and the versions/kinds your app serves. |
|
|
10
|
+
| `example.cue` | An example kind. Rename it and edit its `spec` to model your own resource. |
|
|
11
|
+
| `config.cue` | Code generation settings (output paths). You rarely need to change this. |
|
|
12
|
+
| `cue.mod/module.cue` | The CUE module definition. |
|
|
13
|
+
|
|
14
|
+
## Generating code
|
|
15
|
+
|
|
16
|
+
After every change under this directory:
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
{{ packageManagerName }} run generate:kinds
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
That script always runs the `grafana-app-sdk` version set as `VERSION` at the top of
|
|
23
|
+
`.config/app-sdk/generate-kinds.mjs`, reusing a copy in `node_modules/.cache/` or downloading and checksum-verifying one for
|
|
24
|
+
your platform. A `grafana-app-sdk` on your `PATH` is ignored, so everyone on the project generates with
|
|
25
|
+
the same version. To run a local build instead, set `GRAFANA_APP_SDK_BIN` to its path. It writes:
|
|
26
|
+
|
|
27
|
+
| Output | Path |
|
|
28
|
+
| ------ | ---- |
|
|
29
|
+
| TypeScript types | `src/generated/<kind>/<version>/` |
|
|
30
|
+
| App manifest (JSON) | `src/app-sdk-manifest.json` |
|
|
31
|
+
| Go types (backend only) | `pkg/generated/` |
|
|
32
|
+
|
|
33
|
+
Generated code is meant to be committed, so schema changes show up in review and a fresh clone builds
|
|
34
|
+
without running code generation.
|
|
35
|
+
|
|
36
|
+
> **Note:** No Go toolchain is needed to run generate:kinds unless generating Go code.**
|
|
37
|
+
|
|
38
|
+
## How the manifest reaches Grafana
|
|
39
|
+
|
|
40
|
+
The generator writes the manifest straight into `src/app-sdk-manifest.json`, so the frontend build's
|
|
41
|
+
existing `**/*.json` copy pattern (`.config/bundler/copyFiles.ts`) carries it into the plugin bundle as
|
|
42
|
+
`dist/app-sdk-manifest.json` with no dedicated copy step. Grafana reads it when the
|
|
43
|
+
`appplugins.loadAppManifest` and `appplugins.registerAPIServer` feature toggles are enabled — the Docker
|
|
44
|
+
dev server in this repo enables both for you. Note the toggles are experimental and off by default in
|
|
45
|
+
Grafana.
|
|
46
|
+
|
|
47
|
+
With the manifest in place, Grafana serves storage and CRUD for your kinds through its aggregated API
|
|
48
|
+
server, and users can also manage the objects with `kubectl`.
|
|
49
|
+
|
|
50
|
+
## Adding a backend
|
|
51
|
+
|
|
52
|
+
Storage and CRUD come from the manifest alone, so a plugin with no backend still gets them. A backend will
|
|
53
|
+
be required for:
|
|
54
|
+
|
|
55
|
+
- Admission logic (i.e. validation/mutation)
|
|
56
|
+
- Conversion logic (i.e. between API versions)
|
|
57
|
+
- Custom routes and subresource routes
|
|
58
|
+
- Controller logic (informers, watchers, or any other background work)
|
|
59
|
+
|
|
60
|
+
If this plugin has a backend, `pkg/provider/provider.go` is where you wire those up as your app grows.
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
// Runs grafana-app-sdk kind code generation from the CUE kinds in ./kinds.
|
|
2
|
+
//
|
|
3
|
+
// Always runs the CLI at the version pinned in VERSION below, resolved as:
|
|
4
|
+
// 1. a previously obtained copy in node_modules/.cache/grafana-app-sdk/<version>/
|
|
5
|
+
// 2. a fresh copy, downloaded from the official release and verified against its checksum
|
|
6
|
+
//
|
|
7
|
+
// A `grafana-app-sdk` on your PATH is deliberately ignored: generated code has to match the library
|
|
8
|
+
// version it is compiled against, and silently generating with whatever happens to be installed makes
|
|
9
|
+
// that mismatch invisible. To use your own build, set GRAFANA_APP_SDK_BIN to the binary — an explicit,
|
|
10
|
+
// per-invocation override for local development.
|
|
11
|
+
//
|
|
12
|
+
// Downloading the CLI needs no Go toolchain. Generation itself only needs Go when codegen.goEnabled
|
|
13
|
+
// is set in kinds/config.cue: the generator formats Go output with golang.org/x/tools, which shells
|
|
14
|
+
// out to `go`.
|
|
15
|
+
//
|
|
16
|
+
// Output paths are configured in kinds/config.cue. Generated code is intended to be committed.
|
|
17
|
+
|
|
18
|
+
import { createHash } from 'node:crypto';
|
|
19
|
+
import { spawnSync } from 'node:child_process';
|
|
20
|
+
import { chmodSync, existsSync, mkdirSync, rmSync, writeFileSync } from 'node:fs';
|
|
21
|
+
import { join, resolve } from 'node:path';
|
|
22
|
+
import { arch, platform, tmpdir } from 'node:os';
|
|
23
|
+
|
|
24
|
+
const VERSION = 'v0.60.0';
|
|
25
|
+
const REPO = 'grafana/grafana-app-sdk';
|
|
26
|
+
const BIN = 'grafana-app-sdk';
|
|
27
|
+
|
|
28
|
+
// Escape hatch for developing against a local CLI build, e.g. one you are changing yourself. Unlike a
|
|
29
|
+
// binary that merely happens to be on PATH, setting this is a deliberate act, so it is honoured.
|
|
30
|
+
const BIN_OVERRIDE = 'GRAFANA_APP_SDK_BIN';
|
|
31
|
+
|
|
32
|
+
// node_modules is already gitignored, so nothing extra needs ignoring for this cache.
|
|
33
|
+
const CACHE_DIR = resolve('node_modules', '.cache', 'grafana-app-sdk', VERSION);
|
|
34
|
+
|
|
35
|
+
function run(command, args, options = {}) {
|
|
36
|
+
return spawnSync(command, args, { stdio: 'inherit', ...options });
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const EXE = platform() === 'win32' ? '.exe' : '';
|
|
40
|
+
|
|
41
|
+
/** Returns the release asset's platform pair, or exits if this platform has no published build. */
|
|
42
|
+
function target() {
|
|
43
|
+
const goos = { linux: 'linux', darwin: 'darwin', win32: 'windows' }[platform()];
|
|
44
|
+
const goarch = { x64: 'amd64', arm64: 'arm64' }[arch()];
|
|
45
|
+
|
|
46
|
+
if (!goos || !goarch) {
|
|
47
|
+
console.error(
|
|
48
|
+
`No grafana-app-sdk release build for ${platform()}/${arch()}.\n` +
|
|
49
|
+
`Install the CLI yourself and re-run: https://github.com/${REPO}/releases`
|
|
50
|
+
);
|
|
51
|
+
process.exit(1);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return { goos, goarch };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async function fetchOrDie(url, what) {
|
|
58
|
+
const response = await fetch(url);
|
|
59
|
+
if (!response.ok) {
|
|
60
|
+
console.error(`Failed to download ${what} (${response.status} ${response.statusText})\n ${url}`);
|
|
61
|
+
process.exit(1);
|
|
62
|
+
}
|
|
63
|
+
return Buffer.from(await response.arrayBuffer());
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Downloads the release archive, verifies it against checksums.txt, and unpacks it into CACHE_DIR. */
|
|
67
|
+
async function download({ goos, goarch }) {
|
|
68
|
+
// Release assets drop the leading "v" from the tag.
|
|
69
|
+
const archive = `${BIN}_${VERSION.replace(/^v/, '')}_${goos}_${goarch}.tar.gz`;
|
|
70
|
+
const base = `https://github.com/${REPO}/releases/download/${VERSION}`;
|
|
71
|
+
|
|
72
|
+
console.log(`Downloading ${BIN} ${VERSION} for ${goos}/${goarch}...`);
|
|
73
|
+
const [tarball, checksums] = await Promise.all([
|
|
74
|
+
fetchOrDie(`${base}/${archive}`, archive),
|
|
75
|
+
fetchOrDie(`${base}/checksums.txt`, 'checksums.txt'),
|
|
76
|
+
]);
|
|
77
|
+
|
|
78
|
+
const expected = checksums
|
|
79
|
+
.toString('utf8')
|
|
80
|
+
.split('\n')
|
|
81
|
+
.map((line) => line.trim().split(/\s+/))
|
|
82
|
+
.find(([, name]) => name === archive)?.[0];
|
|
83
|
+
|
|
84
|
+
if (!expected) {
|
|
85
|
+
console.error(`${archive} is not listed in checksums.txt; refusing to run an unverified binary.`);
|
|
86
|
+
process.exit(1);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const actual = createHash('sha256').update(tarball).digest('hex');
|
|
90
|
+
if (actual !== expected) {
|
|
91
|
+
console.error(`Checksum mismatch for ${archive}.\n expected ${expected}\n actual ${actual}`);
|
|
92
|
+
process.exit(1);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Unpack via tar(1): available on macOS/Linux and shipped with Windows 10+. The archive lays the
|
|
96
|
+
// binary flat at its root (alongside LICENSE and README.md), so extract straight into CACHE_DIR.
|
|
97
|
+
const tarPath = join(tmpdir(), `${archive}-${process.pid}`);
|
|
98
|
+
writeFileSync(tarPath, tarball);
|
|
99
|
+
mkdirSync(CACHE_DIR, { recursive: true });
|
|
100
|
+
|
|
101
|
+
const untar = run('tar', ['-xzf', tarPath, '-C', CACHE_DIR, `${BIN}${EXE}`]);
|
|
102
|
+
rmSync(tarPath, { force: true });
|
|
103
|
+
if (untar.status !== 0) {
|
|
104
|
+
console.error(`Could not unpack ${archive}. Is tar available on your PATH?`);
|
|
105
|
+
process.exit(1);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const binary = join(CACHE_DIR, `${BIN}${EXE}`);
|
|
109
|
+
chmodSync(binary, 0o755);
|
|
110
|
+
|
|
111
|
+
return binary;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
async function resolveBinary() {
|
|
115
|
+
const override = process.env[BIN_OVERRIDE];
|
|
116
|
+
if (override) {
|
|
117
|
+
// Exit rather than falling back to the pinned version: you asked for a specific binary, so
|
|
118
|
+
// silently generating with a different one is the surprise worth avoiding.
|
|
119
|
+
if (!existsSync(override)) {
|
|
120
|
+
console.error(`${BIN_OVERRIDE} is set to ${override}, but no such file exists.`);
|
|
121
|
+
process.exit(1);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
console.log(`Using ${BIN} from ${BIN_OVERRIDE}: ${override}`);
|
|
125
|
+
return resolve(override);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const cached = join(CACHE_DIR, `${BIN}${EXE}`);
|
|
129
|
+
if (existsSync(cached)) {
|
|
130
|
+
return cached;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return download(target());
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const binary = await resolveBinary();
|
|
137
|
+
const result = run(binary, ['generate', '--source', 'kinds']);
|
|
138
|
+
process.exit(result.status ?? 1);
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
name: Kinds drift check
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches:
|
|
6
|
+
- master
|
|
7
|
+
- main
|
|
8
|
+
paths:
|
|
9
|
+
- 'kinds/**'
|
|
10
|
+
- 'pkg/generated/**'
|
|
11
|
+
- 'src/generated/**'
|
|
12
|
+
- 'src/app-sdk-manifest.json'
|
|
13
|
+
- '.github/workflows/generate-kinds-drift.yml'
|
|
14
|
+
pull_request:
|
|
15
|
+
branches:
|
|
16
|
+
- master
|
|
17
|
+
- main
|
|
18
|
+
paths:
|
|
19
|
+
- 'kinds/**'
|
|
20
|
+
- 'pkg/generated/**'
|
|
21
|
+
- 'src/generated/**'
|
|
22
|
+
- 'src/app-sdk-manifest.json'
|
|
23
|
+
- '.github/workflows/generate-kinds-drift.yml'
|
|
24
|
+
|
|
25
|
+
permissions:
|
|
26
|
+
contents: read
|
|
27
|
+
|
|
28
|
+
jobs:
|
|
29
|
+
check-drift:
|
|
30
|
+
name: Check generated kinds are up to date
|
|
31
|
+
runs-on: ubuntu-latest
|
|
32
|
+
steps:
|
|
33
|
+
- uses: actions/checkout@v6
|
|
34
|
+
with:
|
|
35
|
+
persist-credentials: false
|
|
36
|
+
{{#if_eq packageManagerName "pnpm"}}
|
|
37
|
+
# pnpm action uses the packageManager field in package.json to
|
|
38
|
+
# understand which version to install.
|
|
39
|
+
- uses: pnpm/action-setup@0e279bb959325dab635dd2c09392533439d90093 # v6.0.8
|
|
40
|
+
{{/if_eq}}
|
|
41
|
+
- name: Setup Node.js environment
|
|
42
|
+
uses: actions/setup-node@v6
|
|
43
|
+
with:
|
|
44
|
+
node-version: '22'
|
|
45
|
+
cache: '{{ packageManagerName }}'
|
|
46
|
+
|
|
47
|
+
- name: Check for backend
|
|
48
|
+
id: check-for-backend
|
|
49
|
+
run: |
|
|
50
|
+
if [ -f "Magefile.go" ]
|
|
51
|
+
then
|
|
52
|
+
echo "has-backend=true" >> $GITHUB_OUTPUT
|
|
53
|
+
fi
|
|
54
|
+
|
|
55
|
+
- name: Setup Go environment
|
|
56
|
+
if: steps.check-for-backend.outputs.has-backend == 'true'
|
|
57
|
+
uses: actions/setup-go@v6
|
|
58
|
+
with:
|
|
59
|
+
go-version-file: go.mod
|
|
60
|
+
|
|
61
|
+
- name: Install dependencies
|
|
62
|
+
run: {{ packageManagerInstallCmd }}
|
|
63
|
+
|
|
64
|
+
- name: Cache grafana-app-sdk CLI
|
|
65
|
+
uses: actions/cache@v4
|
|
66
|
+
with:
|
|
67
|
+
path: node_modules/.cache/grafana-app-sdk
|
|
68
|
+
key: grafana-app-sdk-cli-${{ '{{' }} hashFiles('.config/app-sdk/generate-kinds.mjs') {{ '}}' }}
|
|
69
|
+
|
|
70
|
+
- name: Generate kinds
|
|
71
|
+
run: {{ packageManagerName }} run generate:kinds
|
|
72
|
+
|
|
73
|
+
- name: Fail if generated code is out of date
|
|
74
|
+
run: |
|
|
75
|
+
# Scoped to kinds/ and the generated output: installing dependencies above can itself touch
|
|
76
|
+
# files (e.g. package-lock.json, if CI's Node version differs from the one used to generate
|
|
77
|
+
# the committed lockfile), which is not kind drift and shouldn't fail this check.
|
|
78
|
+
paths="kinds src/generated pkg/generated src/app-sdk-manifest.json"
|
|
79
|
+
if [ -n "$(git status --porcelain -- $paths)" ]; then
|
|
80
|
+
echo "::error::Generated code is out of date. Run '{{ packageManagerName }} run generate:kinds' and commit the result."
|
|
81
|
+
git status --porcelain -- $paths
|
|
82
|
+
git diff --stat -- $paths
|
|
83
|
+
exit 1
|
|
84
|
+
fi
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
package kinds
|
|
2
|
+
|
|
3
|
+
// config holds the grafana-app-sdk code generation settings, and is read by
|
|
4
|
+
// `grafana-app-sdk generate` (via the default `-c config` selector) when you run
|
|
5
|
+
// `{{ packageManagerName }} run generate:kinds`.
|
|
6
|
+
//
|
|
7
|
+
// Paths are relative to the plugin root and match the layout scaffolded by @grafana/create-plugin:
|
|
8
|
+
// the frontend lives in src/.
|
|
9
|
+
config: {
|
|
10
|
+
definitions: {
|
|
11
|
+
manifestVersion: "v1alpha2"
|
|
12
|
+
|
|
13
|
+
// Do not edit this section; the generated manifest must be named exactly as below.
|
|
14
|
+
// The manifest tells Grafana which kinds and capabilities your app serves. Write it
|
|
15
|
+
// straight into src/ so the frontend build picks it up as-is.
|
|
16
|
+
manifestSchemas: true
|
|
17
|
+
path: "src"
|
|
18
|
+
manifestFileName: "app-sdk-manifest.json"
|
|
19
|
+
encoding: "json"
|
|
20
|
+
// Do not generate separate files per-CRD, they are not used.
|
|
21
|
+
genCRDs: false
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
codegen: {
|
|
25
|
+
{{#if hasBackend}}
|
|
26
|
+
// Generated Go types land alongside the plugin backend.
|
|
27
|
+
goEnabled: true
|
|
28
|
+
goGenPath: "pkg/generated/"
|
|
29
|
+
{{else}}
|
|
30
|
+
// This plugin has no Go backend, so skip Go code generation entirely: only TypeScript and
|
|
31
|
+
// the definitions below are emitted, and no Go toolchain is needed to generate them.
|
|
32
|
+
goEnabled: false
|
|
33
|
+
{{/if}}
|
|
34
|
+
// Generated TypeScript types land in the frontend source dir.
|
|
35
|
+
tsGenPath: "src/generated/"
|
|
36
|
+
enableK8sPostProcessing: false
|
|
37
|
+
enableOperatorStatusGeneration: false
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
package kinds
|
|
2
|
+
|
|
3
|
+
// exampleKind holds the information about the Example kind that does not change between versions.
|
|
4
|
+
// Rename this kind (and its file) to model your own resource.
|
|
5
|
+
exampleKind: {
|
|
6
|
+
kind: "Example"
|
|
7
|
+
pluralName: "Examples"
|
|
8
|
+
// Namespaced resources are created per-tenant. Use "Cluster" for global resources.
|
|
9
|
+
scope: "Namespaced"
|
|
10
|
+
codegen: {
|
|
11
|
+
ts: {
|
|
12
|
+
enabled: true
|
|
13
|
+
}
|
|
14
|
+
go: {
|
|
15
|
+
enabled: true
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// examplev1alpha1 is the v1alpha1 version of the Example kind: the common kind information plus
|
|
21
|
+
// this version's schema. Edit the spec to model your resource, then re-run code generation.
|
|
22
|
+
examplev1alpha1: exampleKind & {
|
|
23
|
+
schema: {
|
|
24
|
+
spec: {
|
|
25
|
+
title: string
|
|
26
|
+
description: string
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
package kinds
|
|
2
|
+
|
|
3
|
+
manifest: {
|
|
4
|
+
// appName is the unique name of your app. It is used to reference the app from other config
|
|
5
|
+
// objects, and to derive the API group your app serves (by default,
|
|
6
|
+
// LOWER(strip dashes)+".ext.grafana.app"). Set `groupOverride` if you need to pin a shorter or
|
|
7
|
+
// different group.
|
|
8
|
+
appName: "{{ pluginId }}"
|
|
9
|
+
|
|
10
|
+
// versions maps each version your app serves to the kinds it exposes. Version names follow the
|
|
11
|
+
// format "v<integer>" or "v<integer>(alpha|beta)<integer>".
|
|
12
|
+
versions: {
|
|
13
|
+
"v1alpha1": v1alpha1
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// extraPermissions declares any additional permissions your app needs, e.g. access to kinds
|
|
17
|
+
// owned by other apps.
|
|
18
|
+
extraPermissions: {
|
|
19
|
+
accessKinds: []
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// v1alpha1 is the v1alpha1 version of the app's API.
|
|
24
|
+
v1alpha1: {
|
|
25
|
+
// kinds lists the version-specific kind values served by this version.
|
|
26
|
+
kinds: [examplev1alpha1]
|
|
27
|
+
// served indicates whether this version is served by the API server. Defaults to true.
|
|
28
|
+
served: true
|
|
29
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// Package v1alpha1 is a stub that exists purely so `go mod tidy` resolves its dependencies before
|
|
2
|
+
// code generation has run. `generate:kinds` fills this package with its own generated files, without
|
|
3
|
+
// removing this one — it's left behind, harmless, since it exports nothing.
|
|
4
|
+
package v1alpha1
|
|
5
|
+
|
|
6
|
+
import (
|
|
7
|
+
_ "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
8
|
+
_ "k8s.io/apimachinery/pkg/runtime"
|
|
9
|
+
_ "k8s.io/apimachinery/pkg/runtime/schema"
|
|
10
|
+
_ "k8s.io/apimachinery/pkg/types"
|
|
11
|
+
)
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// Package manifestdata is a stub that exists purely so `go mod tidy` resolves its dependencies before
|
|
2
|
+
// code generation has run. `generate:kinds` fills this package with its own generated files, without
|
|
3
|
+
// removing this one — it's left behind, harmless, since it exports nothing.
|
|
4
|
+
package manifestdata
|
|
5
|
+
|
|
6
|
+
import (
|
|
7
|
+
_ "github.com/grafana/grafana-app-sdk/app"
|
|
8
|
+
_ "github.com/grafana/grafana-app-sdk/resource"
|
|
9
|
+
_ "k8s.io/apimachinery/pkg/runtime"
|
|
10
|
+
_ "k8s.io/kube-openapi/pkg/spec3"
|
|
11
|
+
_ "k8s.io/kube-openapi/pkg/validation/spec"
|
|
12
|
+
)
|