Skip to content

Commit

Permalink
Merge pull request #2 from laurci/feat/init-cmd
Browse files Browse the repository at this point in the history
feat: kubernate init with templates
  • Loading branch information
laurci authored Sep 5, 2021
2 parents 9382aa8 + 1326b1c commit a86ccd8
Show file tree
Hide file tree
Showing 36 changed files with 602 additions and 5 deletions.
4 changes: 3 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
node_modules
src/__generated__/
src/__generated__/
src/assets
dist/
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"compile": "tsc",
"compile:watch": "tsc -w",
"build": "tsc && ts-node src/scripts/build.ts '0.0.1' && chmod +x ./dist/cli/bin.js",
"build:assets": "rimraf dist/assets && cp -R src/assets dist/assets",
"clean": "rimraf dist",
"clean:incremental": "rimraf dist/tsconfig.tsbuildinfo",
"format": "prettier --write .",
Expand All @@ -36,6 +37,7 @@
"cosmiconfig": "^7.0.1",
"deepmerge": "^4.2.2",
"glob": "^7.1.7",
"handlebars": "^4.7.7",
"minimatch": "^3.0.4",
"semver": "^7.3.5",
"ts-morph": "^11.0.3",
Expand All @@ -49,6 +51,7 @@
"devDependencies": {
"@octokit/rest": "^18.9.1",
"@types/deepmerge": "^2.2.0",
"@types/handlebars": "^4.1.0",
"@types/minimatch": "^3.0.5",
"@types/node": "^16.4.1",
"@types/package-json": "^5.0.1",
Expand Down
2 changes: 2 additions & 0 deletions src/assets/init-templates/basic/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
dist
3 changes: 3 additions & 0 deletions src/assets/init-templates/basic/.kubernaterc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
targetVersion: "v1"
scripts:
hello-world: ./src/hello-world.ts
2 changes: 2 additions & 0 deletions src/assets/init-templates/basic/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
output/
6 changes: 6 additions & 0 deletions src/assets/init-templates/basic/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"tabWidth": 4,
"singleQuote": false,
"bracketSpacing": false,
"printWidth": 140
}
3 changes: 3 additions & 0 deletions src/assets/init-templates/basic/.vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"recommendations": ["redhat.vscode-yaml", "esbenp.prettier-vscode"]
}
5 changes: 5 additions & 0 deletions src/assets/init-templates/basic/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"yaml.schemas": {
"node_modules/kubernate/schemas/config.json": ".kubernaterc.yaml"
},
}
5 changes: 5 additions & 0 deletions src/assets/init-templates/basic/README.md.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## {{projectName}}

Run `kubernate hello-world`.

This example uses Kubernate to create a namespace, a pod template and a deployment. The outputs are written to a single yaml file in `output/hello-world.yaml`.
Empty file.
21 changes: 21 additions & 0 deletions src/assets/init-templates/basic/package.json.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "{{projectName}}",
"version": "1.0.0",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"dist/**/*"
],
"scripts": {
"format": "prettier --write .",
"build": "tsc"
},
"dependencies": {
"kubernate": "~1.22"
},
"devDependencies": {
"@types/node": "^16.7.9",
"prettier": "^2.3.2",
"typescript": "^4.3.5"
}
}
68 changes: 68 additions & 0 deletions src/assets/init-templates/basic/src/hello-world.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import kube, {output} from "kubernate";

/**
* Kubernate comes with some pre-built resource helpers for most common resources.
* check imports from "kubernate/resources/*"
*/
import namespace from "kubernate/resources/namespace";

import * as path from "path";

const outputPath = (fileName: string) => path.join(__dirname, "../output", fileName);

/**
* This is the main entry point and will be called by Kubernate.
*/
export default async () => {
// carete a new namespace
const ns = namespace("hello-world");

// create and reuse this selector
const selector = {
app: "hello-world",
};

// create a pod template for out deployment
const pod = kube.core.v1.PodTemplate({
template: {
metadata: {
namespace: ns,
name: "hello-world",
labels: selector,
},
spec: {
containers: [
{
name: "hello-world-http",
image: "kornkitti/express-hello-world:latest",
imagePullPolicy: "Always",
ports: [
{
containerPort: 8080,
name: "http",
},
],
},
],
},
},
});

// create a deployment using our pod template
kube.apps.v1.Deployment({
metadata: {
namespace: ns,
name: "hello-world",
},
spec: {
replicas: 1,
selector: {
matchLabels: selector,
},
template: pod.template!,
},
});

// write the output to disk
await output.bundleToDisk(outputPath("hello-world.yaml"));
};
18 changes: 18 additions & 0 deletions src/assets/init-templates/basic/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"compilerOptions": {
"incremental": true,
"target": "ES2019",
"module": "commonjs",
"declaration": true,
"declarationMap": true,
"sourceMap": true,
"outDir": "./dist",
"strict": true,
"strictFunctionTypes": false,
"moduleResolution": "node",
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},
"include": ["src"]
}
2 changes: 2 additions & 0 deletions src/assets/init-templates/with-resources/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
dist
12 changes: 12 additions & 0 deletions src/assets/init-templates/with-resources/.kubernaterc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
targetVersion: "v1"
scripts:
hello-world: ./src/hello-world.ts
resources:
entry: ./src/resources/index.ts
entryTypeName: Resources # this is optional
exclude:
- output/*
include: input/*.yaml
output:
code: ./src/generated
schemas: ./schemas
3 changes: 3 additions & 0 deletions src/assets/init-templates/with-resources/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
output/
src/generated
6 changes: 6 additions & 0 deletions src/assets/init-templates/with-resources/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"tabWidth": 4,
"singleQuote": false,
"bracketSpacing": false,
"printWidth": 140
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"recommendations": ["redhat.vscode-yaml", "esbenp.prettier-vscode"]
}
14 changes: 14 additions & 0 deletions src/assets/init-templates/with-resources/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"yaml.schemas": {
"node_modules/kubernate/schemas/config.json": ".kubernaterc.yaml",
"schemas/resources.json": "[!output]**/[!.kubernaterc]*.yaml"
},
"files.exclude": {
"**/.git": true,
"**/.svn": true,
"**/.hg": true,
"**/CVS": true,
"**/.DS_Store": true,
".kubernate": true
}
}
7 changes: 7 additions & 0 deletions src/assets/init-templates/with-resources/README.md.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## {{projectName}}

Run `kubernate hello-world`.

This example uses Kubernate to create a namespace, a pod template and a deployment for every demo/v1/HelloWorld you create. The outputs are written to a single yaml file in `output/hello-world.yaml`.

If you update the spec of the resource, make sure to run `kubernate generate` to update the generated code.
14 changes: 14 additions & 0 deletions src/assets/init-templates/with-resources/input/hello.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# you have intellisense here. give it a try :)
apiVersion: demo/v1
kind: HelloWorld
metadata:
name: hello-test1
spec:
who: world1
---
apiVersion: demo/v1
kind: HelloWorld
metadata:
name: hello-test2
spec:
who: world2
Empty file.
21 changes: 21 additions & 0 deletions src/assets/init-templates/with-resources/package.json.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "{{projectName}}",
"version": "1.0.0",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"dist/**/*"
],
"scripts": {
"format": "prettier --write .",
"build": "tsc"
},
"dependencies": {
"kubernate": "~1.22"
},
"devDependencies": {
"@types/node": "^16.7.9",
"prettier": "^2.3.2",
"typescript": "^4.3.5"
}
}
75 changes: 75 additions & 0 deletions src/assets/init-templates/with-resources/schemas/objects.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
{
"description": "This is how you can declare a resource",
"type": "object",
"properties": {
"apiVersion": {
"type": "string",
"enum": [
"demo/v1"
]
},
"kind": {
"type": "string",
"enum": [
"HelloWorld"
]
},
"metadata": {
"type": "object",
"properties": {
"namespace": {
"type": "string"
},
"name": {
"type": "string"
},
"annotations": {
"type": "object",
"additionalProperties": {},
"propertyOrder": []
}
},
"propertyOrder": [
"namespace",
"name",
"annotations"
],
"required": [
"name"
]
},
"spec": {
"$ref": "#/definitions/HelloWorldServiceSpec"
}
},
"propertyOrder": [
"apiVersion",
"kind",
"metadata",
"spec"
],
"required": [
"apiVersion",
"kind",
"metadata",
"spec"
],
"definitions": {
"HelloWorldServiceSpec": {
"description": "This is the spec of our resource.",
"type": "object",
"properties": {
"who": {
"type": "string"
}
},
"propertyOrder": [
"who"
],
"required": [
"who"
]
}
},
"$schema": "http://json-schema.org/draft-07/schema#"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"oneOf": [
{
"$ref": "objects.json"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

/**
* This file was generated by kubernate.
* Do not edit this file manually. Any changes will be overwritten.
*/
import { HelloWorldService } from "../resources/hello-world-service";


export type DeclaredServicesMap = {
"demo/v1/HelloWorld": HelloWorldService
};
11 changes: 11 additions & 0 deletions src/assets/init-templates/with-resources/src/generated/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

/**
* This file was generated by kubernate.
* Do not edit this file manually. Any changes will be overwritten.
*/
import {makeResourcesBrowser} from "kubernate/internal/api";
import {join as pathJoin} from "path";
import {DeclaredServicesMap} from "./declarations";

const resources = makeResourcesBrowser<DeclaredServicesMap>(pathJoin(__dirname, "../.."));
export default resources;
Loading

0 comments on commit a86ccd8

Please sign in to comment.