@kustodian/registry 1.0.0 → 1.1.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/README.md +78 -0
- package/package.json +4 -3
- package/src/helm.ts +179 -0
- package/src/index.ts +1 -0
package/README.md
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
# @kustodian/registry
|
|
2
|
+
|
|
3
|
+
Container registry client for fetching image tags from Docker Hub and GitHub Container Registry (GHCR).
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
bun add @kustodian/registry
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## API
|
|
12
|
+
|
|
13
|
+
### Client Functions
|
|
14
|
+
|
|
15
|
+
- **`parse_image_reference(image: string)`** - Parse an image string into its components (registry, namespace, repository, tag)
|
|
16
|
+
- **`detect_registry_type(image)`** - Auto-detect registry type from an image reference
|
|
17
|
+
- **`create_registry_client(type, config?)`** - Create a client for a specific registry type
|
|
18
|
+
- **`create_client_for_image(image)`** - Create a client with auto-detected type and authentication
|
|
19
|
+
- **`create_dockerhub_client(config?)`** - Create a Docker Hub client
|
|
20
|
+
- **`create_ghcr_client(config?)`** - Create a GitHub Container Registry client
|
|
21
|
+
|
|
22
|
+
### Authentication
|
|
23
|
+
|
|
24
|
+
- **`get_dockerhub_auth()`** - Get Docker Hub auth from `DOCKER_USERNAME` and `DOCKER_PASSWORD`
|
|
25
|
+
- **`get_ghcr_auth()`** - Get GHCR auth from `GITHUB_TOKEN` or `GH_TOKEN`
|
|
26
|
+
- **`get_auth_for_registry(registry)`** - Get auth for a registry by hostname
|
|
27
|
+
|
|
28
|
+
### Version Utilities
|
|
29
|
+
|
|
30
|
+
- **`filter_semver_tags(tags, options?)`** - Filter tags to semver-valid versions
|
|
31
|
+
- **`find_latest_matching(versions, constraint?)`** - Find the latest version matching a constraint
|
|
32
|
+
- **`check_version_update(current, available, constraint?)`** - Check if a newer version is available
|
|
33
|
+
- **`DEFAULT_SEMVER_PATTERN`** - Default regex for semver-like tags
|
|
34
|
+
|
|
35
|
+
### Types
|
|
36
|
+
|
|
37
|
+
- `ImageReferenceType` - Parsed image reference components
|
|
38
|
+
- `RegistryAuthType` - Authentication configuration
|
|
39
|
+
- `RegistryClientType` - Registry client interface
|
|
40
|
+
- `RegistryClientConfigType` - Client configuration options
|
|
41
|
+
- `TagInfoType` - Tag information from registry
|
|
42
|
+
- `VersionCheckResultType` - Version check result
|
|
43
|
+
|
|
44
|
+
## Usage
|
|
45
|
+
|
|
46
|
+
```typescript
|
|
47
|
+
import {
|
|
48
|
+
parse_image_reference,
|
|
49
|
+
create_client_for_image,
|
|
50
|
+
filter_semver_tags,
|
|
51
|
+
check_version_update,
|
|
52
|
+
} from '@kustodian/registry';
|
|
53
|
+
|
|
54
|
+
// Parse image reference
|
|
55
|
+
const image = parse_image_reference('ghcr.io/org/app:v1.0.0');
|
|
56
|
+
|
|
57
|
+
// Create client and fetch tags
|
|
58
|
+
const client = create_client_for_image(image);
|
|
59
|
+
const result = await client.list_tags(image);
|
|
60
|
+
|
|
61
|
+
if (result.success) {
|
|
62
|
+
// Filter to semver tags and check for updates
|
|
63
|
+
const versions = filter_semver_tags(result.value);
|
|
64
|
+
const update = check_version_update('1.0.0', versions);
|
|
65
|
+
|
|
66
|
+
if (update.has_update) {
|
|
67
|
+
console.log(`Update available: ${update.latest_version}`);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## License
|
|
73
|
+
|
|
74
|
+
MIT
|
|
75
|
+
|
|
76
|
+
## Links
|
|
77
|
+
|
|
78
|
+
- [Repository](https://github.com/lucasilverentand/kustodian)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kustodian/registry",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "Container registry client for fetching image tags from Docker Hub and GHCR",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.ts",
|
|
@@ -37,8 +37,9 @@
|
|
|
37
37
|
"registry": "https://npm.pkg.github.com"
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"@kustodian/core": "
|
|
41
|
-
"semver": "^7.6.0"
|
|
40
|
+
"@kustodian/core": "1.1.0",
|
|
41
|
+
"semver": "^7.6.0",
|
|
42
|
+
"yaml": "^2.8.2"
|
|
42
43
|
},
|
|
43
44
|
"devDependencies": {
|
|
44
45
|
"@types/semver": "^7.5.0"
|
package/src/helm.ts
ADDED
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import { type ResultType, failure, success } from '@kustodian/core';
|
|
2
|
+
import type { KustodianErrorType } from '@kustodian/core';
|
|
3
|
+
import { parse } from 'yaml';
|
|
4
|
+
import { create_ghcr_client } from './ghcr.js';
|
|
5
|
+
import type {
|
|
6
|
+
ImageReferenceType,
|
|
7
|
+
RegistryClientConfigType,
|
|
8
|
+
RegistryClientType,
|
|
9
|
+
TagInfoType,
|
|
10
|
+
} from './types.js';
|
|
11
|
+
|
|
12
|
+
const DEFAULT_TIMEOUT = 30000;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Helm index.yaml entry structure.
|
|
16
|
+
*/
|
|
17
|
+
interface HelmChartEntry {
|
|
18
|
+
name: string;
|
|
19
|
+
version: string;
|
|
20
|
+
created?: string;
|
|
21
|
+
description?: string;
|
|
22
|
+
digest?: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Helm index.yaml structure.
|
|
27
|
+
*/
|
|
28
|
+
interface HelmIndexType {
|
|
29
|
+
apiVersion: string;
|
|
30
|
+
entries: Record<string, HelmChartEntry[]>;
|
|
31
|
+
generated?: string;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Creates an abort signal with timeout.
|
|
36
|
+
*/
|
|
37
|
+
function create_abort_signal(timeout_ms: number): AbortSignal {
|
|
38
|
+
const controller = new AbortController();
|
|
39
|
+
setTimeout(() => controller.abort(), timeout_ms);
|
|
40
|
+
return controller.signal;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Fetches and parses a Helm repository index.yaml file.
|
|
45
|
+
*/
|
|
46
|
+
async function fetch_helm_index(
|
|
47
|
+
repository_url: string,
|
|
48
|
+
config?: RegistryClientConfigType,
|
|
49
|
+
): Promise<ResultType<HelmIndexType, KustodianErrorType>> {
|
|
50
|
+
// Ensure URL doesn't have trailing slash
|
|
51
|
+
const base_url = repository_url.replace(/\/$/, '');
|
|
52
|
+
const index_url = `${base_url}/index.yaml`;
|
|
53
|
+
|
|
54
|
+
try {
|
|
55
|
+
const response = await fetch(index_url, {
|
|
56
|
+
signal: create_abort_signal(config?.timeout ?? DEFAULT_TIMEOUT),
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
if (!response.ok) {
|
|
60
|
+
return failure({
|
|
61
|
+
code: 'HELM_REPO_ERROR',
|
|
62
|
+
message: `Failed to fetch Helm index: ${response.status} ${response.statusText}`,
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const content = await response.text();
|
|
67
|
+
const index = parse(content) as HelmIndexType;
|
|
68
|
+
|
|
69
|
+
if (!index.entries) {
|
|
70
|
+
return failure({
|
|
71
|
+
code: 'HELM_REPO_ERROR',
|
|
72
|
+
message: 'Invalid Helm index: missing entries',
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return success(index);
|
|
77
|
+
} catch (error) {
|
|
78
|
+
return failure({
|
|
79
|
+
code: 'HELM_REPO_ERROR',
|
|
80
|
+
message: `Failed to fetch Helm index: ${error instanceof Error ? error.message : String(error)}`,
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Parses an OCI Helm reference into components.
|
|
87
|
+
* Example: oci://ghcr.io/traefik/helm/traefik -> {registry: ghcr.io, namespace: traefik/helm, repository: traefik}
|
|
88
|
+
*/
|
|
89
|
+
function parse_oci_helm_reference(oci_url: string, chart_name: string): ImageReferenceType {
|
|
90
|
+
// Remove oci:// prefix
|
|
91
|
+
const url_without_prefix = oci_url.replace(/^oci:\/\//, '');
|
|
92
|
+
|
|
93
|
+
// Split into parts
|
|
94
|
+
const parts = url_without_prefix.split('/');
|
|
95
|
+
|
|
96
|
+
if (parts.length < 2) {
|
|
97
|
+
// Fallback for invalid format
|
|
98
|
+
return {
|
|
99
|
+
registry: parts[0] || 'ghcr.io',
|
|
100
|
+
namespace: 'library',
|
|
101
|
+
repository: chart_name,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// First part is registry
|
|
106
|
+
const registry = parts[0] || 'ghcr.io';
|
|
107
|
+
|
|
108
|
+
// Everything except first part and last part (if it matches chart name) is namespace
|
|
109
|
+
const last_part = parts[parts.length - 1];
|
|
110
|
+
let namespace: string;
|
|
111
|
+
|
|
112
|
+
if (last_part === chart_name && parts.length > 2) {
|
|
113
|
+
// If last part is the chart name, use everything in between as namespace
|
|
114
|
+
const intermediate_namespace = parts.slice(1, -1).join('/');
|
|
115
|
+
namespace = intermediate_namespace || 'library';
|
|
116
|
+
} else {
|
|
117
|
+
// Otherwise, use everything after registry as namespace
|
|
118
|
+
const full_namespace = parts.slice(1).join('/');
|
|
119
|
+
namespace = full_namespace || 'library';
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return {
|
|
123
|
+
registry,
|
|
124
|
+
namespace,
|
|
125
|
+
repository: chart_name,
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Creates a Helm repository client.
|
|
131
|
+
* Supports both traditional Helm repositories and OCI registries.
|
|
132
|
+
*/
|
|
133
|
+
export function create_helm_client(
|
|
134
|
+
helm_config: { repository?: string; oci?: string; chart: string },
|
|
135
|
+
config?: RegistryClientConfigType,
|
|
136
|
+
): RegistryClientType {
|
|
137
|
+
// OCI registry
|
|
138
|
+
if (helm_config.oci) {
|
|
139
|
+
const image_ref = parse_oci_helm_reference(helm_config.oci, helm_config.chart);
|
|
140
|
+
// Use GHCR client for OCI registries (they all use the same OCI Distribution spec)
|
|
141
|
+
const oci_client = create_ghcr_client(config);
|
|
142
|
+
return {
|
|
143
|
+
list_tags: () => oci_client.list_tags(image_ref),
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// Traditional Helm repository
|
|
148
|
+
return {
|
|
149
|
+
async list_tags(
|
|
150
|
+
_image?: ImageReferenceType,
|
|
151
|
+
): Promise<ResultType<TagInfoType[], KustodianErrorType>> {
|
|
152
|
+
if (!helm_config.repository) {
|
|
153
|
+
return failure({
|
|
154
|
+
code: 'HELM_REPO_ERROR',
|
|
155
|
+
message: 'No repository URL provided for Helm chart',
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const index_result = await fetch_helm_index(helm_config.repository, config);
|
|
160
|
+
if (!index_result.success) {
|
|
161
|
+
return index_result;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const chart_entries = index_result.value.entries[helm_config.chart];
|
|
165
|
+
if (!chart_entries || chart_entries.length === 0) {
|
|
166
|
+
return failure({
|
|
167
|
+
code: 'HELM_CHART_NOT_FOUND',
|
|
168
|
+
message: `Chart "${helm_config.chart}" not found in Helm repository`,
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
const tags: TagInfoType[] = chart_entries.map((entry) =>
|
|
173
|
+
entry.digest ? { name: entry.version, digest: entry.digest } : { name: entry.version },
|
|
174
|
+
);
|
|
175
|
+
|
|
176
|
+
return success(tags);
|
|
177
|
+
},
|
|
178
|
+
};
|
|
179
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -19,6 +19,7 @@ export {
|
|
|
19
19
|
// Registry implementations
|
|
20
20
|
export { create_dockerhub_client } from './dockerhub.js';
|
|
21
21
|
export { create_ghcr_client } from './ghcr.js';
|
|
22
|
+
export { create_helm_client } from './helm.js';
|
|
22
23
|
|
|
23
24
|
// Auth utilities
|
|
24
25
|
export { get_dockerhub_auth, get_ghcr_auth, get_auth_for_registry } from './auth.js';
|