Skip to content

Commit 2bec62a

Browse files
feat(cli): cdk8s init template for java (#245)
Signed-off-by: campionfellin <campionfellin@gmail.com> *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 2ae890a commit 2bec62a

6 files changed

Lines changed: 131 additions & 2 deletions

File tree

packages/cdk8s-cli/bin/cmds/init.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as yargs from 'yargs';
22
import * as fs from 'fs-extra';
33
import * as path from 'path';
44
import { sscaff } from 'sscaff';
5+
const constructsVersion = require('../../package.json').dependencies.constructs;
56

67
const templatesDir = path.join(__dirname, '..', '..', 'templates');
78
const availableTemplates = fs.readdirSync(templatesDir).filter(x => !x.startsWith('.'));
@@ -41,7 +42,8 @@ async function determineDeps(version: string, dist?: string): Promise<Deps> {
4142
const ret = {
4243
'npm_cdk8s': path.resolve(dist, 'js', `cdk8s@${version}.jsii.tgz`),
4344
'npm_cdk8s_cli': path.resolve(dist, 'js', `cdk8s-cli-${version}.tgz`),
44-
'pypi_cdk8s': path.resolve(dist, 'python', `cdk8s-${version.replace(/-/g, '_')}-py3-none-any.whl`)
45+
'pypi_cdk8s': path.resolve(dist, 'python', `cdk8s-${version.replace(/-/g, '_')}-py3-none-any.whl`),
46+
'mvn_cdk8s': path.resolve(dist, 'java', `org/cdk8s/cdk8s/${version}/cdk8s-${version}.jar`),
4547
};
4648

4749
for (const file of Object.values(ret)) {
@@ -50,7 +52,15 @@ async function determineDeps(version: string, dist?: string): Promise<Deps> {
5052
}
5153
}
5254

53-
return ret;
55+
const versions = {
56+
'cdk8s_version': version,
57+
'constructs_version': constructsVersion,
58+
}
59+
60+
return {
61+
...ret,
62+
...versions,
63+
};
5464
}
5565

5666
if (version === '0.0.0') {
@@ -67,13 +77,19 @@ async function determineDeps(version: string, dist?: string): Promise<Deps> {
6777
'npm_cdk8s': `cdk8s@${ver}`,
6878
'npm_cdk8s_cli': `cdk8s-cli@${ver}`,
6979
'pypi_cdk8s': `cdk8s~=${version}`, // no support for pre-release
80+
'mvn_cdk8s': version,
81+
'cdk8s_version': version,
82+
'constructs_version': constructsVersion,
7083
};
7184
}
7285

7386
interface Deps {
7487
npm_cdk8s: string;
7588
npm_cdk8s_cli: string;
7689
pypi_cdk8s: string;
90+
mvn_cdk8s: string;
91+
cdk8s_version: string;
92+
constructs_version: string;
7793
}
7894

7995
module.exports = new Command();
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const { execSync } = require('child_process');
2+
const { readFileSync } = require('fs');
3+
4+
const cli = require.resolve('../../bin/cdk8s');
5+
6+
exports.pre = (variables) => {
7+
try {
8+
execSync('which mvn');
9+
} catch {
10+
console.error(`Unable to find "mvn". Install from https://maven.apache.org/install.html`);
11+
process.exit(1);
12+
}
13+
variables.constructs_version = variables.constructs_version.replace('^', '').replace('~', '');
14+
};
15+
16+
exports.post = options => {
17+
const { mvn_cdk8s, cdk8s_version } = options;
18+
if (!mvn_cdk8s) {
19+
throw new Error(`missing context "mvn_cdk8s"`);
20+
}
21+
22+
// This is used for installing artifacts that are local (not from Maven)
23+
// https://maven.apache.org/plugins/maven-install-plugin/usage.html
24+
if (mvn_cdk8s.endsWith('.jar')) {
25+
execSync(`mvn install:install-file -Dfile=${mvn_cdk8s} -DgroupId=org.cdk8s -DartifactId=cdk8s -Dversion=${cdk8s_version} -Dpackaging=jar`)
26+
}
27+
28+
execSync(`mvn install`);
29+
execSync(`${cli} import k8s -l java`);
30+
execSync(`mvn compile`);
31+
execSync(`mvn exec:java -Dexec.mainClass="com.mycompany.app.Main"`);
32+
33+
console.log(readFileSync('./help', 'utf-8'));
34+
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
language: java
2+
app: mvn exec:java -Dexec.mainClass="com.mycompany.app.HelloKube"
3+
imports:
4+
- k8s
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
========================================================================================================
2+
3+
Your cdk8s Java project is ready!
4+
5+
cat help Prints this message
6+
cdk8s synth Synthesize k8s manifests to dist/
7+
cdk8s import Imports k8s API objects to "src/main/java/imports/"
8+
mvn compile Compiles your java packages
9+
10+
Deploy:
11+
kubectl apply -f dist/*.k8s.yaml
12+
13+
========================================================================================================
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.mycompany.app</groupId>
5+
<artifactId>my-app</artifactId>
6+
<packaging>jar</packaging>
7+
<version>1.0-SNAPSHOT</version>
8+
<name>my-app</name>
9+
<url>http://maven.apache.org</url>
10+
<dependencies>
11+
<dependency>
12+
<groupId>org.cdk8s</groupId>
13+
<artifactId>cdk8s</artifactId>
14+
<version>{{ cdk8s_version }}</version>
15+
</dependency>
16+
<dependency>
17+
<groupId>software.constructs</groupId>
18+
<artifactId>constructs</artifactId>
19+
<version>{{ constructs_version }}</version>
20+
</dependency>
21+
<dependency>
22+
<groupId>com.google.guava</groupId>
23+
<artifactId>guava</artifactId>
24+
<version>29.0-jre</version>
25+
</dependency>
26+
</dependencies>
27+
<properties>
28+
<maven.compiler.source>1.8</maven.compiler.source>
29+
<maven.compiler.target>1.8</maven.compiler.target>
30+
</properties>
31+
<build>
32+
<sourceDirectory>src/main/java</sourceDirectory>
33+
<testSourceDirectory>src/test/java</testSourceDirectory>
34+
</build>
35+
</project>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.mycompany.app;
2+
3+
import software.constructs.Construct;
4+
5+
import org.cdk8s.App;
6+
import org.cdk8s.Chart;
7+
import org.cdk8s.ChartOptions;
8+
9+
public class Main extends Chart
10+
{
11+
12+
public Main(final Construct scope, final String id) {
13+
this(scope, id, null);
14+
}
15+
16+
public Main(final Construct scope, final String id, final ChartOptions options) {
17+
super(scope, id, options);
18+
19+
// define resources here
20+
}
21+
22+
public static void main(String[] args) {
23+
final App app = new App();
24+
new Main(app, "{{ $base }}");
25+
app.synth();
26+
}
27+
}

0 commit comments

Comments
 (0)