@jttc/projen-project-types 1.0.0-beta.1 → 1.0.0-beta.3
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/.cz-config.js +8 -1
- package/.jsii +440 -5
- package/API.md +9621 -252
- package/README.md +186 -29
- package/docs/components/cdk8s.md +724 -0
- package/docs/index.md +74 -13
- package/docs/project-types/cdk-app.md +481 -0
- package/docs/project-types/cdk-library.md +16 -108
- package/docs/project-types/cdk8s-library.md +442 -0
- package/lib/cdk/cdk-app-project.d.ts +9 -0
- package/lib/cdk/cdk-app-project.js +27 -0
- package/lib/cdk/cdk-library-project.js +1 -1
- package/lib/cdk/cdk8s-library-project.d.ts +12 -0
- package/lib/cdk/cdk8s-library-project.js +37 -0
- package/lib/common/common-options.d.ts +1 -0
- package/lib/common/common-options.js +10 -3
- package/lib/components/cdk8s/cdk8s.d.ts +11 -0
- package/lib/components/cdk8s/cdk8s.js +98 -0
- package/lib/components/cdk8s/index.d.ts +2 -0
- package/lib/components/cdk8s/index.js +19 -0
- package/lib/components/cdk8s/interfaces/Cdk8s.d.ts +39 -0
- package/lib/components/cdk8s/interfaces/Cdk8s.js +12 -0
- package/lib/components/index.d.ts +1 -0
- package/lib/components/index.js +18 -0
- package/lib/index.d.ts +3 -0
- package/lib/index.js +4 -1
- package/mkdocs.yml +4 -0
- package/package.json +6 -6
|
@@ -0,0 +1,724 @@
|
|
|
1
|
+
# CDK8s Component
|
|
2
|
+
|
|
3
|
+
The CDK8s Component allows you to integrate **CDK for Kubernetes (CDK8s)** into any AWS CDK TypeScript application, enabling you to define Kubernetes manifests using familiar TypeScript constructs alongside your AWS infrastructure.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
CDK8s is a software development framework for defining Kubernetes applications and reusable abstractions using familiar programming languages. This component seamlessly integrates CDK8s into your existing CDK projects, allowing you to:
|
|
8
|
+
|
|
9
|
+
- **Define Kubernetes manifests** in TypeScript alongside your AWS resources
|
|
10
|
+
- **Share code and constructs** between your AWS and Kubernetes infrastructure
|
|
11
|
+
- **Synthesize Kubernetes YAML** from TypeScript code
|
|
12
|
+
- **Import existing Kubernetes resources** and CRDs
|
|
13
|
+
- **Leverage the power of programming** for Kubernetes configuration
|
|
14
|
+
|
|
15
|
+
!!! info "Universal Compatibility"
|
|
16
|
+
This component works with **any** AWS CDK TypeScript application, not just those created with our project types. You can add it to existing CDK apps, apps created with standard projen, or any other CDK project setup.
|
|
17
|
+
|
|
18
|
+
## Key Features
|
|
19
|
+
|
|
20
|
+
### 🔧 **Automatic Setup**
|
|
21
|
+
- Installs required CDK8s dependencies (`cdk8s`, `cdk8s-cli`, `cdk8s-plus-XX`)
|
|
22
|
+
- Configures TypeScript compilation for Kubernetes manifests
|
|
23
|
+
- Sets up proper project structure for CDK8s development
|
|
24
|
+
|
|
25
|
+
### ⚙️ **Task Automation**
|
|
26
|
+
- **`cdk8s:import`**: Import Kubernetes APIs and CRDs
|
|
27
|
+
- **`cdk8s:synth`**: Synthesize Kubernetes manifests from TypeScript
|
|
28
|
+
- **`cdk8s`**: Run any CDK8s command with arguments
|
|
29
|
+
|
|
30
|
+
### 🎯 **Flexible Configuration**
|
|
31
|
+
- Support for multiple Kubernetes versions (1.29 to 1.33)
|
|
32
|
+
- Custom import paths for external resources
|
|
33
|
+
- Configurable output and source directories
|
|
34
|
+
- Integration with existing project structure
|
|
35
|
+
|
|
36
|
+
### 📁 **Project Structure Integration**
|
|
37
|
+
- Generates sample application code
|
|
38
|
+
- Creates proper `cdk8s.yaml` configuration
|
|
39
|
+
- Maintains separation between AWS and Kubernetes code
|
|
40
|
+
- Supports both library and application patterns
|
|
41
|
+
|
|
42
|
+
## Quick Start
|
|
43
|
+
|
|
44
|
+
### Adding to an Existing CDK App
|
|
45
|
+
|
|
46
|
+
To add the CDK8s component to any existing AWS CDK TypeScript application:
|
|
47
|
+
|
|
48
|
+
```typescript linenums="1" title=".projenrc.ts" hl_lines="2 12"
|
|
49
|
+
import { AwsCdkTypeScriptApp } from 'projen/lib/awscdk';
|
|
50
|
+
import { Cdk8sComponent } from '@jttc/projen-project-types';
|
|
51
|
+
|
|
52
|
+
const project = new AwsCdkTypeScriptApp({
|
|
53
|
+
name: 'my-cdk-app',
|
|
54
|
+
cdkVersion: '2.1.0',
|
|
55
|
+
// ... other CDK app options
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
// Add CDK8s component to your existing CDK app
|
|
59
|
+
new Cdk8sComponent(project, 'cdk8s');
|
|
60
|
+
|
|
61
|
+
project.synth();
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
**Highlighted lines explanation:**
|
|
65
|
+
|
|
66
|
+
- **Line 2**: Import the CDK8s component
|
|
67
|
+
- **Line 12**: Add the component to your project with default settings
|
|
68
|
+
|
|
69
|
+
### Adding to Our CDK App Projects
|
|
70
|
+
|
|
71
|
+
For projects created with our CDK App project type:
|
|
72
|
+
|
|
73
|
+
```typescript linenums="1" title=".projenrc.ts" hl_lines="2 11"
|
|
74
|
+
import { CdkApp } from '@jttc/projen-project-types';
|
|
75
|
+
import { Cdk8sComponent } from '@jttc/projen-project-types';
|
|
76
|
+
|
|
77
|
+
const project = new CdkApp({
|
|
78
|
+
name: 'my-hybrid-app',
|
|
79
|
+
cdkVersion: '2.1.0',
|
|
80
|
+
defaultReleaseBranch: 'main',
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
// Add Kubernetes capabilities
|
|
84
|
+
new Cdk8sComponent(project, 'cdk8s');
|
|
85
|
+
|
|
86
|
+
project.synth();
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
**Highlighted lines explanation:**
|
|
90
|
+
|
|
91
|
+
- **Line 2**: Import both the CDK App project and CDK8s component
|
|
92
|
+
- **Line 11**: Add CDK8s functionality to your CDK app
|
|
93
|
+
|
|
94
|
+
### First Steps After Setup
|
|
95
|
+
|
|
96
|
+
After adding the component and running `yarn projen`:
|
|
97
|
+
|
|
98
|
+
```bash linenums="1"
|
|
99
|
+
# Install dependencies
|
|
100
|
+
yarn install
|
|
101
|
+
|
|
102
|
+
# Import Kubernetes APIs (automatically runs on first setup)
|
|
103
|
+
yarn cdk8s:import
|
|
104
|
+
|
|
105
|
+
# Build and synthesize Kubernetes manifests
|
|
106
|
+
yarn build
|
|
107
|
+
yarn cdk8s:synth
|
|
108
|
+
|
|
109
|
+
# Check generated manifests
|
|
110
|
+
ls kubernetes/
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
## Configuration Options
|
|
114
|
+
|
|
115
|
+
### Basic Configuration
|
|
116
|
+
|
|
117
|
+
The CDK8s component accepts the following configuration options:
|
|
118
|
+
|
|
119
|
+
```typescript linenums="1" title="Basic CDK8s Component Configuration"
|
|
120
|
+
import { K8sVersion } from '@jttc/projen-project-types';
|
|
121
|
+
|
|
122
|
+
new Cdk8sComponent(project, 'cdk8s', {
|
|
123
|
+
// Kubernetes version to target
|
|
124
|
+
k8sVersion: K8sVersion.V1_30, // Default
|
|
125
|
+
|
|
126
|
+
// Source directory for CDK8s code
|
|
127
|
+
appPath: 'src', // Default
|
|
128
|
+
|
|
129
|
+
// Main CDK8s application file
|
|
130
|
+
appFile: 'main.ts', // Default
|
|
131
|
+
|
|
132
|
+
// Output directory for synthesized manifests
|
|
133
|
+
outputPath: 'kubernetes', // Default
|
|
134
|
+
|
|
135
|
+
// Additional imports (CRDs, operators, etc.)
|
|
136
|
+
imports: [
|
|
137
|
+
'prometheus-operator@1.0.0',
|
|
138
|
+
'istio@3.0.0',
|
|
139
|
+
],
|
|
140
|
+
});
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
### Kubernetes Version Support
|
|
144
|
+
|
|
145
|
+
Choose from supported Kubernetes versions:
|
|
146
|
+
|
|
147
|
+
=== "Latest Stable (Recommended)"
|
|
148
|
+
|
|
149
|
+
```typescript linenums="1" title="Use Latest K8s Version" hl_lines="3"
|
|
150
|
+
new Cdk8sComponent(project, 'cdk8s', {
|
|
151
|
+
// Use the latest stable Kubernetes version
|
|
152
|
+
k8sVersion: K8sVersion.V1_33,
|
|
153
|
+
});
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
=== "LTS Version"
|
|
157
|
+
|
|
158
|
+
```typescript linenums="1" title="Use LTS K8s Version" hl_lines="3"
|
|
159
|
+
new Cdk8sComponent(project, 'cdk8s', {
|
|
160
|
+
// Use a Long Term Support version
|
|
161
|
+
k8sVersion: K8sVersion.V1_31,
|
|
162
|
+
});
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
=== "Specific Version"
|
|
166
|
+
|
|
167
|
+
```typescript linenums="1" title="Pin to Specific Version" hl_lines="3"
|
|
168
|
+
new Cdk8sComponent(project, 'cdk8s', {
|
|
169
|
+
// Pin to specific version for consistency
|
|
170
|
+
k8sVersion: K8sVersion.V1_30,
|
|
171
|
+
});
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
### Custom Paths and Structure
|
|
175
|
+
|
|
176
|
+
Organize your CDK8s code with custom paths:
|
|
177
|
+
|
|
178
|
+
```typescript linenums="1" title="Custom Project Structure" hl_lines="3 4 5"
|
|
179
|
+
new Cdk8sComponent(project, 'cdk8s', {
|
|
180
|
+
// Separate CDK8s code in its own directory
|
|
181
|
+
appPath: 'k8s',
|
|
182
|
+
appFile: 'cluster.ts',
|
|
183
|
+
outputPath: 'manifests',
|
|
184
|
+
|
|
185
|
+
// Import additional Kubernetes resources
|
|
186
|
+
imports: [
|
|
187
|
+
'cert-manager@1.0.0',
|
|
188
|
+
'nginx-ingress@2.0.0',
|
|
189
|
+
],
|
|
190
|
+
});
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
**Highlighted lines explanation:**
|
|
194
|
+
|
|
195
|
+
- **Line 3**: Put CDK8s code in `k8s/` directory instead of `src/`
|
|
196
|
+
- **Line 4**: Use `cluster.ts` as the main application file
|
|
197
|
+
- **Line 5**: Output manifests to `manifests/` directory
|
|
198
|
+
|
|
199
|
+
## Usage Examples
|
|
200
|
+
|
|
201
|
+
### Example 1: Basic Kubernetes Application
|
|
202
|
+
|
|
203
|
+
After adding the component, create a simple Kubernetes application:
|
|
204
|
+
|
|
205
|
+
```typescript linenums="1" title="src/main.ts (CDK8s Application)" hl_lines="8 9 10 11 12 13"
|
|
206
|
+
import { App, Chart, ChartProps } from 'cdk8s';
|
|
207
|
+
import { KubeDeployment, KubeService, KubeNamespace } from 'cdk8s-plus-30/lib/imports/k8s';
|
|
208
|
+
|
|
209
|
+
export class MyChart extends Chart {
|
|
210
|
+
constructor(scope: Construct, id: string, props: ChartProps = { }) {
|
|
211
|
+
super(scope, id, props);
|
|
212
|
+
|
|
213
|
+
// Create a namespace
|
|
214
|
+
new KubeNamespace(this, 'my-namespace', {
|
|
215
|
+
metadata: { name: 'my-app' }
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
// Create a deployment
|
|
219
|
+
new KubeDeployment(this, 'deployment', {
|
|
220
|
+
metadata: {
|
|
221
|
+
namespace: 'my-app',
|
|
222
|
+
name: 'my-app-deployment'
|
|
223
|
+
},
|
|
224
|
+
spec: {
|
|
225
|
+
replicas: 3,
|
|
226
|
+
selector: {
|
|
227
|
+
matchLabels: { app: 'my-app' }
|
|
228
|
+
},
|
|
229
|
+
template: {
|
|
230
|
+
metadata: { labels: { app: 'my-app' } },
|
|
231
|
+
spec: {
|
|
232
|
+
containers: [{
|
|
233
|
+
name: 'app',
|
|
234
|
+
image: 'nginx:latest',
|
|
235
|
+
ports: [{ containerPort: 80 }]
|
|
236
|
+
}]
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
// Create a service
|
|
243
|
+
new KubeService(this, 'service', {
|
|
244
|
+
metadata: {
|
|
245
|
+
namespace: 'my-app',
|
|
246
|
+
name: 'my-app-service'
|
|
247
|
+
},
|
|
248
|
+
spec: {
|
|
249
|
+
selector: { app: 'my-app' },
|
|
250
|
+
ports: [{ port: 80, targetPort: 80 }]
|
|
251
|
+
}
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
const app = new App();
|
|
257
|
+
new MyChart(app, 'MyChart');
|
|
258
|
+
app.synth();
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
**Highlighted lines explanation:**
|
|
262
|
+
|
|
263
|
+
- **Lines 8-13**: Define Kubernetes resources using imported constructs
|
|
264
|
+
- Resources are defined in TypeScript with full IDE support and type safety
|
|
265
|
+
|
|
266
|
+
### Example 2: AWS + Kubernetes Hybrid Application
|
|
267
|
+
|
|
268
|
+
Combine AWS resources with Kubernetes manifests:
|
|
269
|
+
|
|
270
|
+
```typescript linenums="1" title="AWS CDK Stack with Kubernetes" hl_lines="15 16 17 18"
|
|
271
|
+
import * as aws from 'aws-cdk-lib';
|
|
272
|
+
import * as eks from 'aws-cdk-lib/aws-eks';
|
|
273
|
+
import * as ec2 from 'aws-cdk-lib/aws-ec2';
|
|
274
|
+
import { Construct } from 'constructs';
|
|
275
|
+
|
|
276
|
+
export class HybridStack extends aws.Stack {
|
|
277
|
+
constructor(scope: Construct, id: string, props?: aws.StackProps) {
|
|
278
|
+
super(scope, id, props);
|
|
279
|
+
|
|
280
|
+
// Create VPC for EKS cluster
|
|
281
|
+
const vpc = new ec2.Vpc(this, 'VPC', {
|
|
282
|
+
maxAzs: 2
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
// Create EKS cluster
|
|
286
|
+
const cluster = new eks.Cluster(this, 'Cluster', {
|
|
287
|
+
vpc,
|
|
288
|
+
defaultCapacity: 2,
|
|
289
|
+
version: eks.KubernetesVersion.V1_30,
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
// Output cluster details for kubectl configuration
|
|
293
|
+
new aws.CfnOutput(this, 'ClusterName', {
|
|
294
|
+
value: cluster.clusterName,
|
|
295
|
+
description: 'EKS Cluster Name',
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
**Highlighted lines explanation:**
|
|
302
|
+
|
|
303
|
+
- **Lines 15-18**: Create an EKS cluster that can run the Kubernetes manifests generated by CDK8s
|
|
304
|
+
|
|
305
|
+
Then deploy your CDK8s manifests to the cluster:
|
|
306
|
+
|
|
307
|
+
```bash linenums="1"
|
|
308
|
+
# Deploy AWS infrastructure
|
|
309
|
+
npx cdk deploy
|
|
310
|
+
|
|
311
|
+
# Configure kubectl for the cluster
|
|
312
|
+
aws eks update-kubeconfig --name <cluster-name> --region <region>
|
|
313
|
+
|
|
314
|
+
# Deploy Kubernetes manifests
|
|
315
|
+
kubectl apply -f kubernetes/
|
|
316
|
+
```
|
|
317
|
+
|
|
318
|
+
### Example 3: Using External Kubernetes Resources
|
|
319
|
+
|
|
320
|
+
Import and use external Kubernetes resources like operators:
|
|
321
|
+
|
|
322
|
+
```typescript linenums="1" title="Using External Resources" hl_lines="4 5 6 7 8"
|
|
323
|
+
new Cdk8sComponent(project, 'cdk8s', {
|
|
324
|
+
k8sVersion: K8sVersion.V1_31,
|
|
325
|
+
|
|
326
|
+
// Import external Kubernetes resources
|
|
327
|
+
imports: [
|
|
328
|
+
'prometheus-operator@0.75.0',
|
|
329
|
+
'cert-manager@1.14.0',
|
|
330
|
+
'istio@1.20.0'
|
|
331
|
+
],
|
|
332
|
+
});
|
|
333
|
+
```
|
|
334
|
+
|
|
335
|
+
**Highlighted lines explanation:**
|
|
336
|
+
|
|
337
|
+
- **Lines 5-7**: Import specific versions of popular Kubernetes operators and tools
|
|
338
|
+
|
|
339
|
+
After running `yarn cdk8s:import`, you can use these resources:
|
|
340
|
+
|
|
341
|
+
```typescript linenums="1" title="Using Imported Operators"
|
|
342
|
+
import { Prometheus } from './imports/prometheus-operator';
|
|
343
|
+
import { Certificate } from './imports/cert-manager';
|
|
344
|
+
|
|
345
|
+
// Use imported CRDs in your CDK8s code
|
|
346
|
+
new Prometheus(this, 'monitoring', {
|
|
347
|
+
// Prometheus configuration
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
new Certificate(this, 'tls-cert', {
|
|
351
|
+
// Certificate configuration
|
|
352
|
+
});
|
|
353
|
+
```
|
|
354
|
+
|
|
355
|
+
## Project Structure
|
|
356
|
+
|
|
357
|
+
After adding the CDK8s component, your project structure will look like:
|
|
358
|
+
|
|
359
|
+
```text title="Project Structure with CDK8s"
|
|
360
|
+
my-cdk-app/
|
|
361
|
+
├── lib/ # AWS CDK stacks
|
|
362
|
+
│ ├── my-stack.ts
|
|
363
|
+
│ └── ...
|
|
364
|
+
├── src/ # CDK8s application (configurable)
|
|
365
|
+
│ ├── main.ts # CDK8s main app
|
|
366
|
+
│ └── imports/ # Auto-generated Kubernetes imports
|
|
367
|
+
│ ├── k8s.ts # Core Kubernetes API
|
|
368
|
+
│ ├── prometheus-operator.ts # External imports
|
|
369
|
+
│ └── ...
|
|
370
|
+
├── kubernetes/ # Generated manifests (configurable)
|
|
371
|
+
│ ├── my-chart.k8s.yaml
|
|
372
|
+
│ └── ...
|
|
373
|
+
├── test/
|
|
374
|
+
│ ├── *.test.ts # AWS CDK tests
|
|
375
|
+
│ └── main.test.ts # CDK8s tests (for cdk8s projects)
|
|
376
|
+
├── cdk8s.yaml # CDK8s configuration
|
|
377
|
+
├── package.json # Updated with CDK8s dependencies
|
|
378
|
+
└── ...
|
|
379
|
+
```
|
|
380
|
+
|
|
381
|
+
!!! tip "Separation of Concerns"
|
|
382
|
+
Keep your AWS CDK stacks in `lib/` and your Kubernetes definitions in `src/` (or your configured `appPath`). This maintains clear separation between cloud infrastructure and Kubernetes workloads.
|
|
383
|
+
|
|
384
|
+
## Available Tasks
|
|
385
|
+
|
|
386
|
+
The component automatically adds these tasks to your project:
|
|
387
|
+
|
|
388
|
+
### `yarn cdk8s:import`
|
|
389
|
+
|
|
390
|
+
Import Kubernetes APIs and external resources:
|
|
391
|
+
|
|
392
|
+
```bash linenums="1"
|
|
393
|
+
# Import default Kubernetes API
|
|
394
|
+
yarn cdk8s:import
|
|
395
|
+
|
|
396
|
+
# Import with additional options
|
|
397
|
+
yarn cdk8s:import -- --help
|
|
398
|
+
```
|
|
399
|
+
|
|
400
|
+
### `yarn cdk8s:synth`
|
|
401
|
+
|
|
402
|
+
Synthesize Kubernetes manifests from TypeScript:
|
|
403
|
+
|
|
404
|
+
```bash linenums="1"
|
|
405
|
+
# Generate manifests
|
|
406
|
+
yarn cdk8s:synth
|
|
407
|
+
|
|
408
|
+
# Output to specific directory
|
|
409
|
+
yarn cdk8s:synth -- --output ./my-manifests
|
|
410
|
+
```
|
|
411
|
+
|
|
412
|
+
### `yarn cdk8s`
|
|
413
|
+
|
|
414
|
+
Run any CDK8s command:
|
|
415
|
+
|
|
416
|
+
```bash linenums="1"
|
|
417
|
+
# General CDK8s help
|
|
418
|
+
yarn cdk8s -- --help
|
|
419
|
+
|
|
420
|
+
# Initialize new chart
|
|
421
|
+
yarn cdk8s -- init typescript-app
|
|
422
|
+
|
|
423
|
+
# List available commands
|
|
424
|
+
yarn cdk8s -- list
|
|
425
|
+
```
|
|
426
|
+
|
|
427
|
+
## Best Practices
|
|
428
|
+
|
|
429
|
+
### 1. **Version Consistency**
|
|
430
|
+
|
|
431
|
+
Keep Kubernetes versions consistent between your EKS cluster and CDK8s configuration:
|
|
432
|
+
|
|
433
|
+
```typescript
|
|
434
|
+
// In your CDK stack
|
|
435
|
+
const cluster = new eks.Cluster(this, 'Cluster', {
|
|
436
|
+
version: eks.KubernetesVersion.V1_30, // Match this version...
|
|
437
|
+
});
|
|
438
|
+
|
|
439
|
+
// In your CDK8s component
|
|
440
|
+
new Cdk8sComponent(project, 'cdk8s', {
|
|
441
|
+
k8sVersion: K8sVersion.V1_30, // ...with this version
|
|
442
|
+
});
|
|
443
|
+
```
|
|
444
|
+
|
|
445
|
+
### 2. **Namespace Organization**
|
|
446
|
+
|
|
447
|
+
Use namespaces to organize your Kubernetes resources:
|
|
448
|
+
|
|
449
|
+
```typescript
|
|
450
|
+
// Create environment-specific namespaces
|
|
451
|
+
new KubeNamespace(this, 'production', {
|
|
452
|
+
metadata: { name: 'production' }
|
|
453
|
+
});
|
|
454
|
+
|
|
455
|
+
new KubeNamespace(this, 'staging', {
|
|
456
|
+
metadata: { name: 'staging' }
|
|
457
|
+
});
|
|
458
|
+
```
|
|
459
|
+
|
|
460
|
+
### 3. **Resource Management**
|
|
461
|
+
|
|
462
|
+
Define resource requests and limits:
|
|
463
|
+
|
|
464
|
+
```typescript
|
|
465
|
+
containers: [{
|
|
466
|
+
name: 'app',
|
|
467
|
+
image: 'my-app:latest',
|
|
468
|
+
resources: {
|
|
469
|
+
requests: { memory: '128Mi', cpu: '100m' },
|
|
470
|
+
limits: { memory: '256Mi', cpu: '200m' }
|
|
471
|
+
}
|
|
472
|
+
}]
|
|
473
|
+
```
|
|
474
|
+
|
|
475
|
+
### 4. **Configuration Management**
|
|
476
|
+
|
|
477
|
+
Use ConfigMaps and Secrets for application configuration:
|
|
478
|
+
|
|
479
|
+
```typescript
|
|
480
|
+
// Configuration
|
|
481
|
+
new KubeConfigMap(this, 'app-config', {
|
|
482
|
+
metadata: { name: 'app-config' },
|
|
483
|
+
data: {
|
|
484
|
+
'database-url': 'postgres://...',
|
|
485
|
+
'api-endpoint': 'https://api.example.com'
|
|
486
|
+
}
|
|
487
|
+
});
|
|
488
|
+
|
|
489
|
+
// Secrets (values should be base64 encoded)
|
|
490
|
+
new KubeSecret(this, 'app-secrets', {
|
|
491
|
+
metadata: { name: 'app-secrets' },
|
|
492
|
+
data: {
|
|
493
|
+
'database-password': Buffer.from('secret').toString('base64')
|
|
494
|
+
}
|
|
495
|
+
});
|
|
496
|
+
```
|
|
497
|
+
|
|
498
|
+
### 5. **CI/CD Integration**
|
|
499
|
+
|
|
500
|
+
Integrate CDK8s synthesis into your CI/CD pipeline:
|
|
501
|
+
|
|
502
|
+
```yaml title=".github/workflows/deploy.yml"
|
|
503
|
+
name: Deploy
|
|
504
|
+
on:
|
|
505
|
+
push:
|
|
506
|
+
branches: [main]
|
|
507
|
+
|
|
508
|
+
jobs:
|
|
509
|
+
deploy:
|
|
510
|
+
runs-on: ubuntu-latest
|
|
511
|
+
steps:
|
|
512
|
+
- uses: actions/checkout@v4
|
|
513
|
+
- uses: actions/setup-node@v4
|
|
514
|
+
with:
|
|
515
|
+
node-version: '18'
|
|
516
|
+
|
|
517
|
+
# Build and deploy AWS resources
|
|
518
|
+
- run: yarn install
|
|
519
|
+
- run: yarn build
|
|
520
|
+
- run: npx cdk deploy --require-approval never
|
|
521
|
+
|
|
522
|
+
# Generate and apply Kubernetes manifests
|
|
523
|
+
- run: yarn cdk8s:synth
|
|
524
|
+
- name: Apply manifests
|
|
525
|
+
run: |
|
|
526
|
+
aws eks update-kubeconfig --name $CLUSTER_NAME
|
|
527
|
+
kubectl apply -f kubernetes/
|
|
528
|
+
```
|
|
529
|
+
|
|
530
|
+
## Deployment Workflows
|
|
531
|
+
|
|
532
|
+
### Local Development
|
|
533
|
+
|
|
534
|
+
1. **Setup**: Add component and run `yarn projen`
|
|
535
|
+
2. **Import**: Run `yarn cdk8s:import` for Kubernetes APIs
|
|
536
|
+
3. **Develop**: Write CDK8s code in TypeScript
|
|
537
|
+
4. **Synthesize**: Run `yarn cdk8s:synth` to generate manifests
|
|
538
|
+
5. **Deploy**: Apply manifests with `kubectl apply -f kubernetes/`
|
|
539
|
+
|
|
540
|
+
### Production Deployment
|
|
541
|
+
|
|
542
|
+
1. **AWS Infrastructure**: Deploy CDK stacks first (`npx cdk deploy`)
|
|
543
|
+
2. **Cluster Access**: Configure kubectl for your EKS cluster
|
|
544
|
+
3. **Manifest Generation**: Run `yarn cdk8s:synth` in CI/CD
|
|
545
|
+
4. **Kubernetes Deployment**: Apply manifests automatically or via GitOps
|
|
546
|
+
|
|
547
|
+
### GitOps Integration
|
|
548
|
+
|
|
549
|
+
For GitOps workflows with tools like ArgoCD or Flux:
|
|
550
|
+
|
|
551
|
+
```yaml title="argocd-application.yaml"
|
|
552
|
+
apiVersion: argoproj.io/v1alpha1
|
|
553
|
+
kind: Application
|
|
554
|
+
metadata:
|
|
555
|
+
name: my-app
|
|
556
|
+
spec:
|
|
557
|
+
source:
|
|
558
|
+
repoURL: https://github.com/myorg/my-app
|
|
559
|
+
path: kubernetes/ # CDK8s output directory
|
|
560
|
+
targetRevision: HEAD
|
|
561
|
+
destination:
|
|
562
|
+
server: https://kubernetes.default.svc
|
|
563
|
+
namespace: default
|
|
564
|
+
```
|
|
565
|
+
|
|
566
|
+
## Troubleshooting
|
|
567
|
+
|
|
568
|
+
### Import Issues
|
|
569
|
+
|
|
570
|
+
If `cdk8s:import` fails:
|
|
571
|
+
|
|
572
|
+
```bash
|
|
573
|
+
# Clear import cache
|
|
574
|
+
rm -rf src/imports
|
|
575
|
+
yarn cdk8s:import
|
|
576
|
+
|
|
577
|
+
# Check CDK8s version compatibility
|
|
578
|
+
yarn list cdk8s cdk8s-cli
|
|
579
|
+
```
|
|
580
|
+
|
|
581
|
+
### Synthesis Errors
|
|
582
|
+
|
|
583
|
+
For TypeScript compilation errors:
|
|
584
|
+
|
|
585
|
+
```bash
|
|
586
|
+
# Check TypeScript configuration
|
|
587
|
+
npx tsc --noEmit
|
|
588
|
+
|
|
589
|
+
# Verify CDK8s configuration
|
|
590
|
+
cat cdk8s.yaml
|
|
591
|
+
|
|
592
|
+
# Run synthesis with verbose output
|
|
593
|
+
yarn cdk8s:synth -- --verbose
|
|
594
|
+
```
|
|
595
|
+
|
|
596
|
+
### Deployment Issues
|
|
597
|
+
|
|
598
|
+
For kubectl deployment problems:
|
|
599
|
+
|
|
600
|
+
```bash
|
|
601
|
+
# Validate generated manifests
|
|
602
|
+
kubectl apply --dry-run=client -f kubernetes/
|
|
603
|
+
|
|
604
|
+
# Check cluster connectivity
|
|
605
|
+
kubectl cluster-info
|
|
606
|
+
|
|
607
|
+
# Verify namespace existence
|
|
608
|
+
kubectl get namespaces
|
|
609
|
+
```
|
|
610
|
+
|
|
611
|
+
### Version Mismatches
|
|
612
|
+
|
|
613
|
+
For compatibility issues:
|
|
614
|
+
|
|
615
|
+
```bash
|
|
616
|
+
# Check your cluster's Kubernetes version
|
|
617
|
+
kubectl version
|
|
618
|
+
|
|
619
|
+
# Update CDK8s component to match
|
|
620
|
+
# Edit .projenrc.ts and set matching k8sVersion
|
|
621
|
+
yarn projen
|
|
622
|
+
```
|
|
623
|
+
|
|
624
|
+
## Advanced Configuration
|
|
625
|
+
|
|
626
|
+
### Custom CDK8s Configuration
|
|
627
|
+
|
|
628
|
+
Override the generated `cdk8s.yaml`:
|
|
629
|
+
|
|
630
|
+
```typescript linenums="1" title="Advanced cdk8s.yaml Configuration"
|
|
631
|
+
new Cdk8sComponent(project, 'cdk8s', {
|
|
632
|
+
// Component automatically creates cdk8s.yaml
|
|
633
|
+
// Manual overrides can be done post-synth
|
|
634
|
+
});
|
|
635
|
+
|
|
636
|
+
// Add custom post-synth configuration if needed
|
|
637
|
+
project.addTask('cdk8s:configure', {
|
|
638
|
+
exec: 'echo "Custom CDK8s configuration"'
|
|
639
|
+
});
|
|
640
|
+
```
|
|
641
|
+
|
|
642
|
+
### Multiple Chart Applications
|
|
643
|
+
|
|
644
|
+
Structure for multiple Kubernetes applications:
|
|
645
|
+
|
|
646
|
+
```typescript linenums="1" title="Multi-Chart Structure"
|
|
647
|
+
new Cdk8sComponent(project, 'cdk8s', {
|
|
648
|
+
appPath: 'k8s',
|
|
649
|
+
appFile: 'apps.ts', // Main file that imports all charts
|
|
650
|
+
});
|
|
651
|
+
```
|
|
652
|
+
|
|
653
|
+
```typescript linenums="1" title="k8s/apps.ts"
|
|
654
|
+
import { App } from 'cdk8s';
|
|
655
|
+
import { WebAppChart } from './charts/web-app';
|
|
656
|
+
import { DatabaseChart } from './charts/database';
|
|
657
|
+
import { MonitoringChart } from './charts/monitoring';
|
|
658
|
+
|
|
659
|
+
const app = new App();
|
|
660
|
+
|
|
661
|
+
new WebAppChart(app, 'web-app');
|
|
662
|
+
new DatabaseChart(app, 'database');
|
|
663
|
+
new MonitoringChart(app, 'monitoring');
|
|
664
|
+
|
|
665
|
+
app.synth();
|
|
666
|
+
```
|
|
667
|
+
|
|
668
|
+
### Environment-Specific Configuration
|
|
669
|
+
|
|
670
|
+
Use CDK context for environment-specific Kubernetes configuration:
|
|
671
|
+
|
|
672
|
+
```typescript linenums="1" title="Environment Configuration"
|
|
673
|
+
const environment = app.node.tryGetContext('environment') || 'dev';
|
|
674
|
+
const config = app.node.tryGetContext(environment);
|
|
675
|
+
|
|
676
|
+
new KubeDeployment(this, 'app', {
|
|
677
|
+
spec: {
|
|
678
|
+
replicas: config.replicas || 1,
|
|
679
|
+
// Environment-specific configuration
|
|
680
|
+
}
|
|
681
|
+
});
|
|
682
|
+
```
|
|
683
|
+
|
|
684
|
+
## Migration Guide
|
|
685
|
+
|
|
686
|
+
### From Helm Charts
|
|
687
|
+
|
|
688
|
+
To migrate from Helm charts to CDK8s:
|
|
689
|
+
|
|
690
|
+
1. **Analyze** existing Helm templates and values
|
|
691
|
+
2. **Convert** YAML resources to CDK8s constructs
|
|
692
|
+
3. **Replace** Helm values with TypeScript variables
|
|
693
|
+
4. **Test** generated manifests match original output
|
|
694
|
+
|
|
695
|
+
### From Raw YAML
|
|
696
|
+
|
|
697
|
+
To convert existing Kubernetes YAML:
|
|
698
|
+
|
|
699
|
+
1. **Import** existing resources: `kubectl get -o yaml > existing.yaml`
|
|
700
|
+
2. **Convert** manually or use tools like `cdk8s import`
|
|
701
|
+
3. **Refactor** into reusable constructs
|
|
702
|
+
4. **Validate** output matches original resources
|
|
703
|
+
|
|
704
|
+
## Contributing
|
|
705
|
+
|
|
706
|
+
To contribute improvements to the CDK8s component:
|
|
707
|
+
|
|
708
|
+
1. **Fork** the repository
|
|
709
|
+
2. **Add** tests for new functionality
|
|
710
|
+
3. **Update** documentation
|
|
711
|
+
4. **Submit** a pull request
|
|
712
|
+
|
|
713
|
+
## Support
|
|
714
|
+
|
|
715
|
+
For help with the CDK8s component:
|
|
716
|
+
|
|
717
|
+
- **Documentation**: Check this guide and [CDK8s docs](https://cdk8s.io/)
|
|
718
|
+
- **Issues**: Create a GitHub issue with detailed information
|
|
719
|
+
- **Community**: Join CDK8s community discussions
|
|
720
|
+
- **Examples**: Check the project's example directory
|
|
721
|
+
|
|
722
|
+
---
|
|
723
|
+
|
|
724
|
+
*The CDK8s component is maintained by [Jump to the Cloud](https://jumptothecloud.tech) team and leverages the open-source [CDK8s project](https://cdk8s.io/).*
|