├── .gitignore ├── .travis.yml ├── .vscode └── launch.json ├── CODE_OF_CONDUCT.md ├── LICENSE ├── docs └── debug.png ├── generators ├── app │ ├── base.js │ └── index.js ├── deployment │ ├── base.js │ └── index.js ├── ingress │ ├── base.js │ └── index.js ├── lego │ ├── base.js │ └── index.js ├── replication-controller │ ├── base.js │ └── index.js ├── service │ ├── base.js │ └── index.js └── validations.js ├── package.json ├── readme.md └── tests ├── app.js └── validations.js /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | *.pid.lock 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # nyc test coverage 21 | .nyc_output 22 | 23 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 24 | .grunt 25 | 26 | # Bower dependency directory (https://bower.io/) 27 | bower_components 28 | 29 | # node-waf configuration 30 | .lock-wscript 31 | 32 | # Compiled binary addons (http://nodejs.org/api/addons.html) 33 | build/Release 34 | 35 | # Dependency directories 36 | node_modules/ 37 | jspm_packages/ 38 | 39 | # Typescript v1 declaration files 40 | typings/ 41 | 42 | # Optional npm cache directory 43 | .npm 44 | 45 | # Optional eslint cache 46 | .eslintcache 47 | 48 | # Optional REPL history 49 | .node_repl_history 50 | 51 | # Output of 'npm pack' 52 | *.tgz 53 | 54 | # Yarn Integrity file 55 | .yarn-integrity 56 | 57 | # dotenv environment variables file 58 | .env 59 | 60 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | node_js: 4 | - '7' 5 | after_success: 6 | - npm run codacy-cover 7 | - npm run coveralls 8 | deploy: 9 | on: 10 | tags: true 11 | provider: npm 12 | email: sesispla@outlook.com 13 | api_key: 14 | secure: ybZN7BMC8LnAiu6pA9xPEyJekX61Km2UAikmc0+MAL4vpp3QnyUfFuh0FP0C4gBgiwIUeOkJ42X3d4d7pADoPQIZPdGZQTeoMObBhVckVd4eS+3uQzWrfi96X1Nt5wfEk3uTqo8wHYfbO4L69969+pC7RBZx0Ie7MOWjY1hSZrbJJt5a8eq2tIidtgA0ngIJ8eVfn09quS8LUsbkTN2dT9/4kA99m/vuLRSaHuNPwz9KZXDienBwV8mpgfDtpuj8MJVrHYW6dZywhBcpqE+nvdlD8xErU2HDHBrPfX3S/naDqFq4cbW8CmA0cmKKbToqGJPmmTnw5GKEqIegIF7UIxM958rY3xfKiXBFgFMo14Qq2Q/iMs3uN3egKmQeOkgWTsd/HYD3mAKEUcQl3/NaQ+G6EhZRVKg7DOMR01TX9fOWJkzLCc1kNZHaXzhEWrQXfTmBeWw0m0WvWKSOLKJ3y8gNyVYfyD92L9EBkI+d5SNYofGMM7N/VLayNDhlTCkTrhb0C0/XgJnMvbRpILeHmcuN3iKP24mtT5wdibaP2rtCkKfge27U6/6UWx+Zu83gNMGPb3KDdl/f5k7ZakS5Q3SbnycUBOQUqAbMNEEG0rsPSuzzdoXSipg1H4tEYxlPGzd1wUUBBqNLOAfeLYu6uHNJzpZ643tnhwMcTochj8Y= 15 | -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | // Use IntelliSense to learn about possible Node.js debug attributes. 3 | // Hover to view descriptions of existing attributes. 4 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 5 | "version": "0.2.0", 6 | "configurations": [ 7 | { 8 | "type": "node", 9 | "request": "launch", 10 | "name": "Yeoman generator", 11 | "program": "${workspaceRoot}/node_modules/yo/lib/cli.js", 12 | "args": [ 13 | "kubegen" 14 | ], 15 | "console": "integratedTerminal", 16 | "internalConsoleOptions": "neverOpen" 17 | },{ 18 | "type": "node", 19 | "request": "launch", 20 | "name": "Mocha test", 21 | "program": "${workspaceRoot}/node_modules/mocha/bin/_mocha", 22 | "args": [ 23 | "./tests/*.js" 24 | ], 25 | "console": "integratedTerminal", 26 | "internalConsoleOptions": "neverOpen" 27 | }] 28 | } -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation. 6 | 7 | ## Our Standards 8 | 9 | Examples of behavior that contributes to creating a positive environment include: 10 | 11 | * Using welcoming and inclusive language 12 | * Being respectful of differing viewpoints and experiences 13 | * Gracefully accepting constructive criticism 14 | * Focusing on what is best for the community 15 | * Showing empathy towards other community members 16 | 17 | Examples of unacceptable behavior by participants include: 18 | 19 | * The use of sexualized language or imagery and unwelcome sexual attention or advances 20 | * Trolling, insulting/derogatory comments, and personal or political attacks 21 | * Public or private harassment 22 | * Publishing others' private information, such as a physical or electronic address, without explicit permission 23 | * Other conduct which could reasonably be considered inappropriate in a professional setting 24 | 25 | ## Our Responsibilities 26 | 27 | Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior. 28 | 29 | Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful. 30 | 31 | ## Scope 32 | 33 | This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers. 34 | 35 | ## Enforcement 36 | 37 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at sesispla@outlook.com. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately. 38 | 39 | Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership. 40 | 41 | ## Attribution 42 | 43 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version] 44 | 45 | [homepage]: http://contributor-covenant.org 46 | [version]: http://contributor-covenant.org/version/1/4/ 47 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Sergio Sisternes 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /docs/debug.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sesispla/generator-kubegen/3592423cc58b851d17c0a6530f7ff3180daa384a/docs/debug.png -------------------------------------------------------------------------------- /generators/app/base.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | const fs = require("fs"); 4 | const val = require("../validations.js"); 5 | 6 | module.exports = { 7 | 8 | initializing(generator) { 9 | generator.log(" "); 10 | generator.log(" | |/ / | | | | | _ \\ | ____| / _____|| ____|| \\ | | "); 11 | generator.log(" | ' / | | | | | |_) | | |__ | | __ | |__ | \\| | "); 12 | generator.log(" | < | | | | | _ < | __| | | |_ | | __| | . ` | "); 13 | generator.log(" | . \\ | `--' | | |_) | | |____ | |__| | | |____ | |\\ | "); 14 | generator.log(" |__|\\__\\ \\______/ |______/ |_______| \\______| |_______||__| \\__| "); 15 | generator.log(" "); 16 | generator.log("Welcome to Kubernetes Generator (kubegen)!"); 17 | }, 18 | getPrompts() { 19 | var prompts = [{ 20 | type: "input", 21 | name: "name", 22 | message: "How the service should be named?", 23 | default: "service1", 24 | validate: val.isString 25 | }, { 26 | type: "input", 27 | name: "namespace", 28 | message: "In which Namespace should be deployed?", 29 | default: "default", 30 | validate: val.isString 31 | }]; 32 | 33 | return prompts; 34 | }, 35 | spawnKubectlCommand(generator, fileOrFolder, command) { 36 | generator.spawnCommandSync("kubectl",[command, "-f", fileOrFolder]); 37 | } 38 | }; -------------------------------------------------------------------------------- /generators/app/index.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var Generator = require("yeoman-generator"); 4 | var common = require("./base.js"); 5 | var ingress = require("../ingress/base.js"); 6 | var deployment = require("../deployment/base.js"); 7 | var rc = require("../replication-controller/base.js"); 8 | var service = require("../service/base.js"); 9 | 10 | module.exports = class extends Generator { 11 | 12 | constructor(args, opts) { 13 | super(args, opts); 14 | this.argument("apply", { required: false }); 15 | } 16 | 17 | initializing() { 18 | common.initializing(this); 19 | } 20 | 21 | prompting() { 22 | 23 | var prompts = common.getPrompts() 24 | .concat([{ 25 | name: "podControllerType", 26 | type: "list", 27 | message: "Which type of Pod controller mechanism whould you like to use?", 28 | choices: ["Deployment", "Replication Controller", "Other"] 29 | }]) 30 | .concat(deployment.getPrompts()) 31 | .concat(rc.getPrompts()) 32 | .concat(service.getPrompts()) 33 | .concat(ingress.getPrompts()); 34 | 35 | return this.prompt(prompts).then((answers) => { 36 | this.answers = answers; 37 | answers.shouldExpose = answers.shouldExpose === "yes"; 38 | }); 39 | } 40 | 41 | configuring() {} 42 | 43 | default () {} 44 | 45 | writing() { 46 | this.destinationRoot("./" + this.answers.name); 47 | switch (this.answers.podControllerType) { 48 | case "Deployment": 49 | deployment.write(this.fs, this.answers); 50 | break; 51 | case "Replication Controller": 52 | rc.write(this.fs, this.answers); 53 | break; 54 | default: 55 | this.log("Not supported yet!"); 56 | return; 57 | } 58 | 59 | service.write(this.fs, this.answers); 60 | if (this.answers.shouldExpose) { 61 | ingress.write(this.fs, this.answers); 62 | } 63 | } 64 | 65 | conflicts() {} 66 | 67 | install() { 68 | if (this.options.apply) { 69 | common.spawnKubectlCommand(this, this.destinationRoot(), "apply"); 70 | } 71 | } 72 | 73 | end() {} 74 | }; -------------------------------------------------------------------------------- /generators/deployment/base.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var yaml = require("yamljs"); 4 | var val = require("../validations.js"); 5 | 6 | module.exports = { 7 | write: function (fs, answers, inline = 10) { 8 | var deployment = { 9 | apiVersion: "extensions/v1beta1", 10 | kind: "Deployment", 11 | metadata: { 12 | labels: { 13 | name: answers.name 14 | }, 15 | name: answers.name, 16 | namespace: answers.namespace 17 | }, 18 | spec: { 19 | replicas: answers.replicas, 20 | template: { 21 | metadata: { 22 | labels: { 23 | app: answers.name 24 | } 25 | }, 26 | spec:  { 27 | containers: [{ 28 | name: answers.name, 29 | image: answers.image 30 | }] 31 | } 32 | } 33 | } 34 | }; 35 | 36 | var yamlContent = yaml.stringify(deployment, inline); 37 | fs.write("deployment.yml", yamlContent); 38 | }, 39 | getPrompts: function () { 40 | var prompts = [{ 41 | type: "input", 42 | name: "image", 43 | message: "(Deployment) Which Docker image should the Deployment use?", 44 | when: this.when, 45 | validate: val.isString 46 | }, { 47 | type: "input", 48 | name: "replicas", 49 | message: "(Deployment) How much container replicas should be created?", 50 | default: 1, 51 | validate: val.isNumber, 52 | when: this.when, 53 | filter: val.parseInteger 54 | }]; 55 | 56 | return prompts; 57 | }, 58 | when: function (answers) { 59 | return answers.podControllerType === "Deployment" || !answers.podControllerType; 60 | } 61 | } -------------------------------------------------------------------------------- /generators/deployment/index.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var Generator = require("yeoman-generator"); 4 | var common = require("../app/base.js"); 5 | var deployment = require("./base.js"); 6 | 7 | 8 | module.exports = class extends Generator { 9 | 10 | constructor(args, opts) { 11 | super(args, opts); 12 | } 13 | 14 | initializing() { 15 | common.initializing(this); 16 | } 17 | 18 | prompting() { 19 | var prompts = common.getPrompts() 20 | .concat(deployment.getPrompts()); 21 | 22 | return this.prompt(prompts).then((answers) => { 23 | this.answers = answers; 24 | }); 25 | } 26 | 27 | configuring() {} 28 | 29 | default () {} 30 | 31 | writing() { 32 | deployment.write(this.fs, this.answers); 33 | } 34 | 35 | conflicts() {} 36 | 37 | install() {} 38 | 39 | end() {} 40 | }; -------------------------------------------------------------------------------- /generators/ingress/base.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var yaml = require("yamljs"); 4 | const val = require("../validations.js"); 5 | 6 | module.exports = { 7 | write: function (fs, answers, inline = 10) { 8 | var ingress = { 9 | apiVersion: "extensions/v1beta1", 10 | kind: "Ingress", 11 | metadata: { 12 | name: answers.name, 13 | namespace: answers.namespace, 14 | annotations: { 15 | 16 | } 17 | }, 18 | spec: { 19 | rules: [{ 20 | http: { 21 | paths: [{ 22 | path: answers.path, 23 | backend: { 24 | serviceName: answers.name, 25 | servicePort: answers.ingressPort 26 | } 27 | }] 28 | } 29 | }] 30 | } 31 | }; 32 | 33 | if (answers.host) { 34 | ingress.spec.rules[0].host = answers.host; 35 | } 36 | 37 | switch (answers.ingressType) { 38 | case "tls-lego": 39 | ingress.metadata.annotations["kubernetes.io/tls-acme"] = "true"; 40 | ingress.spec.tls = [ 41 | { 42 | "hosts": [ 43 | answers.host 44 | ], 45 | "secretName": answers.legoSecretName 46 | }]; 47 | break; 48 | default: 49 | ingress.metadata.annotations["kubernetes.io/ingress.class"] = answers.ingressType; 50 | break; 51 | } 52 | 53 | var yamlContent = yaml.stringify(ingress, inline); 54 | fs.write("ing.yml", yamlContent); 55 | }, 56 | getPrompts: function () { 57 | 58 | var prompts = [{ 59 | name: "shouldExpose", 60 | type: "list", 61 | message: "(Ingress) Would like to expose the service out of the cluster?", 62 | choices: ["yes", "no"] 63 | }, { 64 | name: "ingressType", 65 | type: "list", 66 | message: "(Ingress) Which class of expose would you like?", 67 | choices: ["external", "internal", "nginx", "tls-lego"], 68 | default: "external" 69 | }, { 70 | name: "host", 71 | type: "input", 72 | message: "(Ingress) Does the service have a hostname?", 73 | when: this.when.shouldExpose 74 | }, { 75 | name: "legoSecretName", 76 | type: "input", 77 | message: "(Ingress) A name for the kube-lego TLS storage secret?", 78 | validate: val.isString, 79 | when: this.when.shouldExposeTls 80 | }, { 81 | name: "path", 82 | type: "input", 83 | message: "(Ingress) Ingress root path?", 84 | default: "/", 85 | when: this.when.shouldExpose 86 | }, { 87 | name: "ingressPort", 88 | type: "input", 89 | message: "(Ingress) In which port should the Ingress listen?", 90 | default: 80, 91 | when: this.when.shouldExpose, 92 | validate: val.isNumber, 93 | filter: val.parseInteger 94 | }]; 95 | 96 | return prompts; 97 | }, 98 | when: { 99 | shouldExpose(answers) { 100 | return answers.shouldExpose === "yes"; 101 | }, 102 | shouldExposeTls(answers) { 103 | return answers.shouldExpose === "yes" && answers.ingressType === "tls-lego"; 104 | } 105 | }, 106 | } -------------------------------------------------------------------------------- /generators/ingress/index.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var Generator = require("yeoman-generator"); 4 | var common = require("../app/base.js"); 5 | var ing = require("./base.js"); 6 | 7 | 8 | module.exports = class extends Generator { 9 | 10 | constructor(args, opts) { 11 | super(args, opts); 12 | } 13 | 14 | initializing() { 15 | common.initializing(this); 16 | } 17 | 18 | prompting() { 19 | var prompts = common.getPrompts() 20 | .concat(ing.getPrompts()); 21 | 22 | return this.prompt(prompts).then((answers) => { 23 | this.answers = answers; 24 | }); 25 | } 26 | 27 | configuring() {} 28 | 29 | default () {} 30 | 31 | writing() { 32 | ing.write(this.fs, this.answers); 33 | } 34 | 35 | conflicts() {} 36 | 37 | install() {} 38 | 39 | end() {} 40 | }; -------------------------------------------------------------------------------- /generators/lego/base.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var yaml = require("yamljs"); 4 | const val = require("../validations.js"); 5 | 6 | module.exports = { 7 | write: function (fs, answers, inline = 10) { 8 | var deployment = { 9 | "apiVersion": "extensions/v1beta1", 10 | "kind": "Deployment", 11 | "metadata": { 12 | "name": answers.name, 13 | "namespace": answers.namespace 14 | }, 15 | "spec": { 16 | "replicas": 1, 17 | "template": { 18 | "metadata": { 19 | "labels": { 20 | "app": answers.name 21 | } 22 | }, 23 | "spec": { 24 | "containers": [{ 25 | "name": answers.name, 26 | "image": answers.image, 27 | "imagePullPolicy": "Always", 28 | "ports": [{ 29 | "containerPort": 8080 30 | }], 31 | "env": [{ 32 | "name": "LEGO_EMAIL", 33 | "valueFrom": { 34 | "configMapKeyRef": { 35 | "name": "kube-lego", 36 | "key": "lego.email" 37 | } 38 | } 39 | }, 40 | { 41 | "name": "LEGO_URL", 42 | "valueFrom": { 43 | "configMapKeyRef": { 44 | "name": "kube-lego", 45 | "key": "lego.url" 46 | } 47 | } 48 | }, 49 | { 50 | "name": "LEGO_NAMESPACE", 51 | "valueFrom": { 52 | "fieldRef": { 53 | "fieldPath": "metadata.namespace" 54 | } 55 | } 56 | }, 57 | { 58 | "name": "LEGO_POD_IP", 59 | "valueFrom": { 60 | "fieldRef": { 61 | "fieldPath": "status.podIP" 62 | } 63 | } 64 | } 65 | ], 66 | "readinessProbe": { 67 | "httpGet": { 68 | "path": "/healthz", 69 | "port": 8080 70 | }, 71 | "initialDelaySeconds": 5, 72 | "timeoutSeconds": 1 73 | } 74 | }] 75 | } 76 | } 77 | } 78 | }; 79 | 80 | var configMap = { 81 | "apiVersion": "v1", 82 | "kind": "ConfigMap", 83 | "metadata": { 84 | "name": answers.name, 85 | "namespace": answers.namespace 86 | }, 87 | "data": { 88 | "lego.email": answers.lego.mail, 89 | "lego.url": answers.lego.url 90 | } 91 | }; 92 | 93 | var deploymentYml = yaml.stringify(deployment, inline); 94 | fs.write("deployment.yml", deploymentYml); 95 | var configMapYml = yaml.stringify(configMap, inline); 96 | fs.write("configmap.yml", configMapYml); 97 | }, 98 | getPrompts: function () { 99 | var prompts = [{ 100 | type: "input", 101 | name: "image", 102 | message: "(Let\"s Encrypt) Which Docker image should the Deployment use?", 103 | default: "jetstack/kube-lego:0.1.4", 104 | validate: val.isString 105 | }, { 106 | type: "input", 107 | name: "lego.mail", 108 | message: "(Let\"s Encrypt) Your Let\"s Encrypt e-mail?", 109 | validate: val.isString 110 | }, { 111 | type: "input", 112 | name: "lego.url", 113 | message: "(Let\"s Encrypt) Is the Let\"s Encrypt service URL correct?", 114 | default: "https://acme-v01.api.letsencrypt.org/directory" 115 | }]; 116 | 117 | return prompts; 118 | } 119 | } -------------------------------------------------------------------------------- /generators/lego/index.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var Generator = require("yeoman-generator"); 4 | var common = require("../app/base.js"); 5 | var svc = require("./base.js"); 6 | 7 | 8 | module.exports = class extends Generator { 9 | 10 | constructor(args, opts) { 11 | super(args, opts); 12 | } 13 | 14 | initializing() { 15 | common.initializing(this); 16 | } 17 | 18 | prompting() { 19 | var prompts = common.getPrompts() 20 | .concat(svc.getPrompts()); 21 | 22 | return this.prompt(prompts).then((answers) => { 23 | this.answers = answers; 24 | }); 25 | } 26 | 27 | configuring() {} 28 | 29 | default () {} 30 | 31 | writing() { 32 | this.destinationRoot("./kube-lego"); 33 | svc.write(this.fs, this.answers); 34 | } 35 | 36 | conflicts() {} 37 | 38 | install() {} 39 | 40 | end() {} 41 | }; -------------------------------------------------------------------------------- /generators/replication-controller/base.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var yaml = require("yamljs"); 4 | var val = require("../validations.js"); 5 | 6 | module.exports = { 7 | write: function (fs, answers, inline = 10) { 8 | var rc = { 9 | apiVersion: "v1", 10 | kind: "ReplicationController", 11 | metadata: { 12 | name: answers.name, 13 | namespace: answers.namespace 14 | }, 15 | spec: { 16 | replicas: answers.replicas, 17 | selector: { 18 | app: answers.name 19 | }, 20 | template: { 21 | metadata: { 22 | labels: { 23 | app: answers.name 24 | } 25 | }, 26 | spec: { 27 | containers: [{ 28 | name: answers.name, 29 | image: answers.image, 30 | imagePullPolicy: "Always" 31 | }] 32 | // ,imagePullSecrets: "registryKey" 33 | } 34 | } 35 | } 36 | }; 37 | 38 | var yamlContent = yaml.stringify(rc, inline); 39 | fs.write("rc.yml", yamlContent); 40 | }, 41 | getPrompts: function () { 42 | var prompts = [{ 43 | type: "input", 44 | name: "image", 45 | message: "(Replication Controller) Which Docker image should the Deployment use?", 46 | when: this.when, 47 | validate: val.isString 48 | }, { 49 | type: "input", 50 | name: "replicas", 51 | message: "(Replication Controller) How much container replicas should be created?", 52 | default: 1, 53 | validate: val.isNumber, 54 | when: this.when, 55 | filter: val.parseInteger 56 | }]; 57 | 58 | return prompts; 59 | }, 60 | when: function(answers) { 61 | return answers.podControllerType === "Replication Controller" || !answers.podControllerType; 62 | } 63 | } -------------------------------------------------------------------------------- /generators/replication-controller/index.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var Generator = require("yeoman-generator"); 4 | var common = require("../app/base.js"); 5 | var rc = require("./base.js"); 6 | 7 | 8 | module.exports = class extends Generator { 9 | 10 | constructor(args, opts) { 11 | super(args, opts); 12 | } 13 | 14 | initializing() { 15 | common.initializing(this); 16 | } 17 | 18 | prompting() { 19 | var prompts = common.getPrompts() 20 | .concat(rc.getPrompts()); 21 | 22 | return this.prompt(prompts).then((answers) => { 23 | this.answers = answers; 24 | }); 25 | } 26 | 27 | configuring() {} 28 | 29 | default () {} 30 | 31 | writing() { 32 | rc.write(this.fs, this.answers); 33 | } 34 | 35 | conflicts() {} 36 | 37 | install() {} 38 | 39 | end() {} 40 | }; -------------------------------------------------------------------------------- /generators/service/base.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var yaml = require("yamljs"); 4 | const val = require("../validations.js"); 5 | 6 | module.exports = { 7 | write: function (fs, answers, inline = 10) { 8 | 9 | var service = { 10 | apiVersion: "v1", 11 | kind: "Service", 12 | metadata: { 13 | name: answers.name, 14 | namespace: answers.namespace 15 | }, 16 | spec: { 17 | ports: [{ 18 | port: answers.servicePort, 19 | targetPort: answers.containerPort 20 | }], 21 | selector: { 22 | app: answers.name 23 | } 24 | } 25 | }; 26 | 27 | var yamlContent = yaml.stringify(service, inline); 28 | fs.write("svc.yml", yamlContent); 29 | }, 30 | getPrompts: function () { 31 | var prompts = [{ 32 | name: "containerPort", 33 | type: "input", 34 | message: "(Service) In which port is the Container listening?", 35 | default: 80, 36 | validate: val.isNumber, 37 | filter: val.parseInteger 38 | }, { 39 | name: "servicePort", 40 | type: "input", 41 | message: "(Service) In which port should the Service listen?", 42 | default: 80, 43 | validate: val.isNumber, 44 | filter: val.parseInteger 45 | }]; 46 | 47 | return prompts; 48 | } 49 | } -------------------------------------------------------------------------------- /generators/service/index.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var Generator = require("yeoman-generator"); 4 | var common = require("../app/base.js"); 5 | var svc = require("./base.js"); 6 | 7 | 8 | module.exports = class extends Generator { 9 | 10 | constructor(args, opts) { 11 | super(args, opts); 12 | } 13 | 14 | initializing() { 15 | common.initializing(this); 16 | } 17 | 18 | prompting() { 19 | var prompts = common.getPrompts() 20 | .concat(svc.getPrompts()); 21 | 22 | return this.prompt(prompts).then((answers) => { 23 | this.answers = answers; 24 | }); 25 | } 26 | 27 | configuring() {} 28 | 29 | default () {} 30 | 31 | writing() { 32 | svc.write(this.fs, this.answers); 33 | } 34 | 35 | conflicts() {} 36 | 37 | install() {} 38 | 39 | end() {} 40 | }; -------------------------------------------------------------------------------- /generators/validations.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | module.exports = { 4 | isString(str) { 5 | return str ? true : false; 6 | }, 7 | isNumber (str) { 8 | return str && !Number.isNaN(str) && Number.isInteger(str) ? true : false; 9 | }, 10 | parseInteger(str) { 11 | return parseInt(str); 12 | } 13 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "generator-kubegen", 3 | "version": "0.1.18", 4 | "description": "A Kubernetes code generation tool that deals with boilerplate code and let you focus on the important details", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "mocha ./tests/*.js", 8 | "cover": "istanbul cover _mocha ./tests/*.js", 9 | "codacy-cover": "npm run cover -- --report lcovonly && cat ./coverage/lcov.info | ./node_modules/.bin/codacy-coverage", 10 | "coveralls": "npm run cover -- --report lcovonly && cat ./coverage/lcov.info | coveralls" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "git+https://github.com/sesispla/generator-kubegen.git" 15 | }, 16 | "keywords": [ 17 | "kubernetes", 18 | "yeoman", 19 | "generator", 20 | "yaml" 21 | ], 22 | "author": "Sergio Sisternes ", 23 | "license": "MIT", 24 | "bugs": { 25 | "url": "https://github.com/sesispla/generator-kubegen/issues" 26 | }, 27 | "homepage": "https://github.com/sesispla/generator-kubegen#readme", 28 | "dependencies": { 29 | "codacy-coverage": "^2.0.2", 30 | "mocha-lcov-reporter": "^1.3.0", 31 | "yamljs": "^0.3.0", 32 | "yeoman-generator": "^1.1.1" 33 | }, 34 | "devDependencies": { 35 | "coveralls": "^2.13.1", 36 | "expect": "^1.20.2", 37 | "istanbul": "^0.4.5", 38 | "mocha": "^3.4.2", 39 | "mocha-lcov-reporter": "^1.3.0", 40 | "touch": "^3.1.0", 41 | "yeoman-assert": "^3.0.0", 42 | "yeoman-generator": "^1.1.1", 43 | "yeoman-test": "^1.6.0", 44 | "yo": "^2.0.0" 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Yeoman generator for you Kubernetes boilerplate code 2 | 3 | [![Build Status](https://travis-ci.org/sesispla/generator-kubegen.svg?branch=master)](https://travis-ci.org/sesispla/generator-kubegen) 4 | [![Coveralls](https://img.shields.io/coveralls/sesispla/generator-kubegen.svg)](https://coveralls.io/github/sesispla/generator-kubegen) 5 | ![Version](https://img.shields.io/npm/v/generator-kubegen.svg) 6 | [![npm](https://img.shields.io/npm/dm/generator-kubegen.svg)](https://www.npmjs.com/package/generator-kubegen) 7 | 8 | kubegen is a tool that aims to simplify your Kubernetes day-to-day, writing the boilerplate code for you and letting you focus on the important things. 9 | 10 | kubegen is a [Yeoman](http://yeoman.io) generator, so you'll need to have [NodeJS](https://nodejs.org/) installed. 11 | 12 | # Installation 13 | 14 | To install kubegen, you need to execute the following command: 15 | 16 | ```bash 17 | npm install -g generator-kubegen 18 | ``` 19 | 20 | # Usage 21 | 22 | Quite easy! The follwing commands are available at the moment: 23 | 24 | | Command | Description | Arguments | 25 | | ---------------------- | ---------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------ | 26 | | yo kubegen | Starts a full Kubernetes file generation wizard. All generated files are stored in a new folder. | --apply: Spawns a "kubectl apply -f " to all files generated immediately | 27 | | yo kubegen:deployment | Starts the Deployment subgenerator. 'deployment.yml' file is created, in the current folder. | | 28 | | yo kubegen:rc | Starts the Replication Controller subgenerator. 'rc.yml' file is created, in the current folder. | | 29 | | yo kubegen:service | Starts the Service subgenerator. 'service.yml' file is created, in the current folder. | | 30 | | yo kubegen:ingress | Starts the Replication Controller subgenerator. 'deployment.yml' file is created, in the current folder. | | 31 | | yo kubegen:lego | Starts the Let's Encrypt subgenerator. All the internal components required by a TLS Ingress with kube-lego is created by this subgen | | 32 | 33 | ```bash 34 | yo kubegen 35 | 36 | | |/ / | | | | | _ \ | ____| / _____|| ____|| \ | | 37 | | ' / | | | | | |_) | | |__ | | __ | |__ | \| | 38 | | < | | | | | _ < | __| | | |_ | | __| | . ` | 39 | | . \ | `--' | | |_) | | |____ | |__| | | |____ | |\ | 40 | |__|\__\ \______/ |______/ |_______| \______| |_______||__| \__| 41 | 42 | Welcome to Kubernetes Generator (kubegen)! 43 | ? How the service should be named? nginx 44 | ? In which Namespace should be deployed? default 45 | ? Which type of Pod controller mechanism whould you like to use? Replication Controller 46 | ? (Replication Controller) Which Docker image should the Deployment use? nginx 47 | ? (Replication Controller) How much container replicas should be created? 1 48 | ? (Service) In which port is the Container listening? 80 49 | ? (Service) In which port should the Service listen? 80 50 | ? (Ingress) Would like to expose the service out of the cluster? yes 51 | ? (Ingress) Which class of expose would you like? 52 | ❯ external 53 | internal 54 | nginx 55 | tls-lego 56 | ``` 57 | 58 | # Contributing 59 | 60 | Contribution is welcome! Just: 61 | 62 | - Fork this project 63 | - install the NPM dependencies: 64 | 65 | ```bash 66 | npm install 67 | ``` 68 | 69 | - Link this folder to npm 70 | 71 | ```bash 72 | npm link 73 | /usr/local/lib/node_modules/generator-kubegen -> /Users/User/generator-kubegen 74 | ``` 75 | 76 | - Edit the project with your favorite text editor or tool 77 | 78 | # Debugging 79 | 80 | For your conveniente, this project comes with a pre-built configuration to debug the solution with [Visual Studio Code](https://code.visualstudio.com). Just go to the "Debug" menu on the left, select "Yeoman generator" in the dropdown (beside "play button") and then click "play" to start debugging. 81 | 82 | ![debug with VSCode](docs/debug.png) -------------------------------------------------------------------------------- /tests/app.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var yeoman = require("yeoman-generator"); 4 | var helpers = require("yeoman-test"); 5 | var assert = require("yeoman-assert"); 6 | var path = require("path"); 7 | var yaml = require("yamljs"); 8 | var expect = require("expect"); 9 | var touch = require("touch"); 10 | 11 | describe("Replication Controller without Ingress scenarios", function () { 12 | 13 | beforeEach(function () { 14 | return helpers.run(path.join(__dirname, "../generators/app")) 15 | .withPrompts({ 16 | name: "nginx", 17 | namespace: "default", 18 | podControllerType: "Replication Controller", 19 | image: "nginx", 20 | replicas: 1, 21 | containerPort: 80, 22 | servicePort: 8080, 23 | shouldExpose: false 24 | }); 25 | }); 26 | 27 | it("File rc.yml is generated and filled in", function () { 28 | assert.file(["rc.yml"]); 29 | var rc = yaml.load("rc.yml"); 30 | expect(rc.apiVersion).toBe("v1"); 31 | expect(rc.kind).toBe("ReplicationController"); 32 | expect(rc.metadata.name).toBe("nginx"); 33 | expect(rc.metadata).toExist("Missing 'metadata' section"); 34 | expect(rc.metadata.namespace).toBe("default"); 35 | expect(rc.spec).toExist("Missing 'spec' section"); 36 | expect(rc.spec.replicas).toBe(1); 37 | expect(rc.spec.selector).toExist("Missing 'selector' section"); 38 | expect(rc.spec.selector.app).toBe("nginx"); 39 | expect(rc.spec.template.metadata.labels.app).toBe("nginx"); 40 | expect(rc.spec.template.spec.containers[0].name).toBe("nginx"); 41 | expect(rc.spec.template.spec.containers[0].image).toBe("nginx"); 42 | }); 43 | 44 | it("File svc.yml is generated and filled in", function () { 45 | assert.file(["svc.yml"]); 46 | var svc = yaml.load("svc.yml"); 47 | expect(svc.apiVersion).toBe("v1"); 48 | expect(svc.kind).toBe("Service"); 49 | expect(svc.metadata.name).toBe("nginx"); 50 | expect(svc.metadata.namespace).toBe("default"); 51 | expect(svc.spec.ports[0].port).toBe(8080); 52 | expect(svc.spec.ports[0].targetPort).toBe(80); 53 | expect(svc.spec.selector.app).toBe("nginx"); 54 | }); 55 | 56 | it("File ing.yml is not generated", function () { 57 | assert.noFile(["ing.yml"]); 58 | }); 59 | 60 | it("File deployment.yml is not generated", function () { 61 | assert.noFile(["deployment.yml"]); 62 | }); 63 | 64 | }); 65 | 66 | describe("Replication Controller with Ingress scenarios", function () { 67 | 68 | beforeEach(function () { 69 | return helpers.run(path.join(__dirname, "../generators/app")) 70 | .withPrompts({ 71 | name: "nginx", 72 | namespace: "default", 73 | podControllerType: "Replication Controller", 74 | image: "nginx", 75 | replicas: 1, 76 | containerPort: 80, 77 | servicePort: 80, 78 | shouldExpose: "yes", 79 | ingressType: "external", 80 | host: "nginx.ingress.com", 81 | path: "/", 82 | ingressPort: 80 83 | }); 84 | }); 85 | 86 | it("File rc.yml is generated and filled in", function () { 87 | assert.file(["rc.yml"]); 88 | var rc = yaml.load("rc.yml"); 89 | expect(rc.apiVersion).toBe("v1"); 90 | expect(rc.kind).toBe("ReplicationController"); 91 | expect(rc.metadata.name).toBe("nginx"); 92 | expect(rc.metadata.namespace).toBe("default"); 93 | expect(rc.spec.replicas).toBe(1); 94 | expect(rc.spec.selector.app).toBe("nginx"); 95 | expect(rc.spec.template.metadata.labels.app).toBe("nginx"); 96 | expect(rc.spec.template.spec.containers[0].name).toBe("nginx"); 97 | expect(rc.spec.template.spec.containers[0].image).toBe("nginx"); 98 | }); 99 | 100 | it("File svc.yml is generated and filled in", function () { 101 | assert.file(["svc.yml"]); 102 | var svc = yaml.load("svc.yml"); 103 | expect(svc.apiVersion).toBe("v1"); 104 | expect(svc.kind).toBe("Service"); 105 | expect(svc.metadata.name).toBe("nginx"); 106 | expect(svc.metadata.namespace).toBe("default"); 107 | expect(svc.spec.ports[0].port).toBe(80); 108 | expect(svc.spec.ports[0].targetPort).toBe(80); 109 | expect(svc.spec.selector.app).toBe("nginx"); 110 | }); 111 | 112 | it("File ing.yml is generated and filled in", function () { 113 | assert.file(["ing.yml"]); 114 | var ing = yaml.load("ing.yml"); 115 | expect(ing.apiVersion).toBe("extensions/v1beta1"); 116 | expect(ing.kind).toBe("Ingress"); 117 | expect(ing.metadata.annotations).toExist("No annotations found for ing.yml"); 118 | expect(ing.metadata.annotations["kubernetes.io/ingress.class"]).toExist("No kubernetes.io/ingress class found"); 119 | expect(ing.metadata.annotations["kubernetes.io/ingress.class"]).toBe("external", "kubernetes.io/ingress class should be 'external'"); 120 | expect(ing.metadata.name).toBe("nginx"); 121 | expect(ing.metadata.namespace).toBe("default"); 122 | expect(ing.spec.rules).toExist("Expected rules to exist"); 123 | expect(ing.spec.rules[0].host).toBe("nginx.ingress.com"); 124 | expect(ing.spec.rules[0].http.paths[0].path).toBe("/"); 125 | expect(ing.spec.rules[0].http.paths[0].backend.serviceName).toBe("nginx"); 126 | expect(ing.spec.rules[0].http.paths[0].backend.servicePort).toBe(80); 127 | }); 128 | 129 | it("File deployment.yml is not generated", function () { 130 | assert.noFile(["deployment.yml"]); 131 | }); 132 | 133 | }); 134 | 135 | 136 | describe("Deployment without Ingress scenarios", function () { 137 | 138 | beforeEach(function () { 139 | return helpers.run(path.join(__dirname, "../generators/app")) 140 | .withPrompts({ 141 | name: "nginx", 142 | namespace: "default", 143 | podControllerType: "Deployment", 144 | image: "nginx", 145 | replicas: 1, 146 | containerPort: 80, 147 | servicePort: 8080, 148 | shouldExpose: false 149 | }); 150 | }); 151 | 152 | it("File rc.yml is not generated", function () { 153 | assert.noFile(["rc.yml"]); 154 | }); 155 | 156 | it("File svc.yml is generated and filled in", function () { 157 | assert.file(["svc.yml"]); 158 | var svc = yaml.load("svc.yml"); 159 | expect(svc.apiVersion).toBe("v1"); 160 | expect(svc.kind).toBe("Service"); 161 | expect(svc.metadata.name).toBe("nginx"); 162 | expect(svc.metadata.namespace).toBe("default"); 163 | expect(svc.spec.ports[0].port).toBe(8080); 164 | expect(svc.spec.ports[0].targetPort).toBe(80); 165 | expect(svc.spec.selector.app).toBe("nginx"); 166 | }); 167 | 168 | it("File ing.yml is not generated", function () { 169 | assert.noFile(["ing.yml"]); 170 | }); 171 | 172 | it("File deployment.yml is generated and filled in", function () { 173 | assert.file(["deployment.yml"]); 174 | var deployment = yaml.load("deployment.yml"); 175 | expect(deployment.apiVersion).toBe("extensions/v1beta1"); 176 | expect(deployment.kind).toBe("Deployment"); 177 | expect(deployment.metadata.labels.name).toBe("nginx"); 178 | expect(deployment.metadata.name).toBe("nginx"); 179 | expect(deployment.metadata.namespace).toBe("default"); 180 | expect(deployment.spec.replicas).toBe(1); 181 | expect(deployment.spec.template.metadata.labels.app).toBe("nginx"); 182 | expect(deployment.spec.template.spec.containers[0].name).toBe("nginx"); 183 | expect(deployment.spec.template.spec.containers[0].image).toBe("nginx"); 184 | }); 185 | 186 | }); 187 | 188 | describe("Deployment with Ingress scenarios", function () { 189 | 190 | beforeEach(function () { 191 | return helpers.run(path.join(__dirname, "../generators/app")) 192 | .withPrompts({ 193 | name: "nginx", 194 | namespace: "default", 195 | podControllerType: "Deployment", 196 | image: "nginx", 197 | replicas: 1, 198 | containerPort: 80, 199 | servicePort: 80, 200 | shouldExpose: "yes", 201 | ingressType: "internal", 202 | host: "nginx.ingress.com", 203 | path: "/", 204 | ingressPort: 80 205 | }); 206 | }); 207 | 208 | it("File rc.yml is not generated", function () { 209 | assert.noFile(["rc.yml"]); 210 | }); 211 | 212 | it("File svc.yml is generated and filled in", function () { 213 | assert.file(["svc.yml"]); 214 | var svc = yaml.load("svc.yml"); 215 | expect(svc.apiVersion).toBe("v1"); 216 | expect(svc.kind).toBe("Service"); 217 | expect(svc.metadata.name).toBe("nginx"); 218 | expect(svc.metadata.namespace).toBe("default"); 219 | expect(svc.spec.ports[0].port).toBe(80); 220 | expect(svc.spec.ports[0].targetPort).toBe(80); 221 | expect(svc.spec.selector.app).toBe("nginx"); 222 | }); 223 | 224 | it("File ing.yml is generated and filled in", function () { 225 | assert.file(["ing.yml"]); 226 | var ing = yaml.load("ing.yml"); 227 | expect(ing.apiVersion).toBe("extensions/v1beta1"); 228 | expect(ing.kind).toBe("Ingress"); 229 | expect(ing.metadata.name).toBe("nginx"); 230 | expect(ing.metadata.namespace).toBe("default"); 231 | expect(ing.metadata.annotations).toExist("No annotations found for ing.yml"); 232 | expect(ing.metadata.annotations["kubernetes.io/ingress.class"]).toExist("No kubernetes.io/ingress class found"); 233 | expect(ing.metadata.annotations["kubernetes.io/ingress.class"]).toBe("internal", "kubernetes.io/ingress class should be 'internal'"); 234 | expect(ing.spec.rules).toExist("Expected rules to exist"); 235 | expect(ing.spec.rules[0].host).toBe("nginx.ingress.com"); 236 | expect(ing.spec.rules[0].http.paths[0].path).toBe("/"); 237 | expect(ing.spec.rules[0].http.paths[0].backend.serviceName).toBe("nginx"); 238 | expect(ing.spec.rules[0].http.paths[0].backend.servicePort).toBe(80); 239 | }); 240 | 241 | it("File deployment.yml is generated and filled in", function () { 242 | assert.file(["deployment.yml"]); 243 | var deployment = yaml.load("deployment.yml"); 244 | expect(deployment.apiVersion).toBe("extensions/v1beta1"); 245 | expect(deployment.kind).toBe("Deployment"); 246 | expect(deployment.metadata.labels.name).toBe("nginx"); 247 | expect(deployment.metadata.name).toBe("nginx"); 248 | expect(deployment.metadata.namespace).toBe("default"); 249 | expect(deployment.spec.replicas).toBe(1); 250 | expect(deployment.spec.template.metadata.labels.app).toBe("nginx"); 251 | expect(deployment.spec.template.spec.containers[0].name).toBe("nginx"); 252 | expect(deployment.spec.template.spec.containers[0].image).toBe("nginx"); 253 | }); 254 | }); 255 | 256 | describe("Deployment with TLS-Ingress", function () { 257 | 258 | beforeEach(function () { 259 | return helpers.run(path.join(__dirname, "../generators/app")) 260 | .withPrompts({ 261 | name: "nginx", 262 | namespace: "default", 263 | podControllerType: "Deployment", 264 | image: "nginx", 265 | replicas: 1, 266 | containerPort: 80, 267 | servicePort: 80, 268 | shouldExpose: "yes", 269 | ingressType: "tls-lego", 270 | legoSecretName: "nginx-secret", 271 | host: "nginx.ingress.com", 272 | path: "/", 273 | ingressPort: 80 274 | }); 275 | }); 276 | 277 | it("File rc.yml is not generated", function () { 278 | assert.noFile(["rc.yml"]); 279 | }); 280 | 281 | it("File svc.yml is generated and filled in", function () { 282 | assert.file(["svc.yml"]); 283 | var svc = yaml.load("svc.yml"); 284 | expect(svc.apiVersion).toBe("v1"); 285 | expect(svc.kind).toBe("Service"); 286 | expect(svc.metadata.name).toBe("nginx"); 287 | expect(svc.metadata.namespace).toBe("default"); 288 | expect(svc.spec.ports[0].port).toBe(80); 289 | expect(svc.spec.ports[0].targetPort).toBe(80); 290 | expect(svc.spec.selector.app).toBe("nginx"); 291 | }); 292 | 293 | it("File ing.yml is generated and filled in", function () { 294 | assert.file(["ing.yml"]); 295 | var ing = yaml.load("ing.yml"); 296 | expect(ing.apiVersion).toBe("extensions/v1beta1"); 297 | expect(ing.kind).toBe("Ingress"); 298 | expect(ing.metadata.name).toBe("nginx"); 299 | expect(ing.metadata.namespace).toBe("default"); 300 | expect(ing.metadata.annotations).toExist("No annotations found for ing.yml"); 301 | expect(ing.metadata.annotations["kubernetes.io/ingress.class"]).toNotExist("ingress.class should not be present in TLS scenarios"); 302 | expect(ing.metadata.annotations["kubernetes.io/tls-acme"]).toExist("No kubernetes.io/tls-acme class found"); 303 | expect(ing.metadata.annotations["kubernetes.io/tls-acme"]).toBe("true", "kubernetes.io/tls-acme class should be 'true'"); 304 | expect(ing.spec.rules).toExist("Expected rules to exist"); 305 | expect(ing.spec.rules[0].host).toBe("nginx.ingress.com"); 306 | expect(ing.spec.rules[0].http.paths[0].path).toBe("/"); 307 | expect(ing.spec.rules[0].http.paths[0].backend.serviceName).toBe("nginx"); 308 | expect(ing.spec.rules[0].http.paths[0].backend.servicePort).toBe(80); 309 | expect(ing.spec.tls).toExist("Expected TLS to exist"); 310 | expect(ing.spec.tls[0].hosts[0]).toBe("nginx.ingress.com"); 311 | expect(ing.spec.tls[0].secretName).toBe("nginx-secret"); 312 | }); 313 | 314 | it("File deployment.yml is generated and filled in", function () { 315 | assert.file(["deployment.yml"]); 316 | var deployment = yaml.load("deployment.yml"); 317 | expect(deployment.apiVersion).toBe("extensions/v1beta1"); 318 | expect(deployment.kind).toBe("Deployment"); 319 | expect(deployment.metadata.labels.name).toBe("nginx"); 320 | expect(deployment.metadata.name).toBe("nginx"); 321 | expect(deployment.metadata.namespace).toBe("default"); 322 | expect(deployment.spec.replicas).toBe(1); 323 | expect(deployment.spec.template.metadata.labels.app).toBe("nginx"); 324 | expect(deployment.spec.template.spec.containers[0].name).toBe("nginx"); 325 | expect(deployment.spec.template.spec.containers[0].image).toBe("nginx"); 326 | }); 327 | }); 328 | 329 | describe("Ingress with no host", function () { 330 | 331 | beforeEach(function () { 332 | return helpers.run(path.join(__dirname, "../generators/app")) 333 | .withPrompts({ 334 | name: "nginx", 335 | namespace: "default", 336 | podControllerType: "Deployment", 337 | image: "nginx", 338 | replicas: 1, 339 | containerPort: 80, 340 | servicePort: 80, 341 | shouldExpose: "yes", 342 | ingressType: "nginx", 343 | path: "/", 344 | ingressPort: 80 345 | }); 346 | }); 347 | 348 | it("File rc.yml is not generated", function () { 349 | assert.noFile(["rc.yml"]); 350 | }); 351 | 352 | it("File svc.yml is generated and filled in", function () { 353 | assert.file(["svc.yml"]); 354 | var svc = yaml.load("svc.yml"); 355 | expect(svc.apiVersion).toBe("v1"); 356 | expect(svc.kind).toBe("Service"); 357 | expect(svc.metadata.name).toBe("nginx"); 358 | expect(svc.metadata.namespace).toBe("default"); 359 | expect(svc.spec.ports[0].port).toBe(80); 360 | expect(svc.spec.ports[0].targetPort).toBe(80); 361 | expect(svc.spec.selector.app).toBe("nginx"); 362 | }); 363 | 364 | it("File ing.yml is generated and filled in", function () { 365 | assert.file(["ing.yml"]); 366 | var ing = yaml.load("ing.yml"); 367 | expect(ing.apiVersion).toBe("extensions/v1beta1"); 368 | expect(ing.kind).toBe("Ingress"); 369 | expect(ing.metadata.name).toBe("nginx"); 370 | expect(ing.metadata.namespace).toBe("default"); 371 | expect(ing.metadata.annotations).toExist("No annotations found for ing.yml"); 372 | expect(ing.metadata.annotations["kubernetes.io/ingress.class"]).toExist("No kubernetes.io/ingress class found"); 373 | expect(ing.metadata.annotations["kubernetes.io/ingress.class"]).toBe("nginx", "kubernetes.io/ingress class should be 'nginx'"); 374 | expect(ing.spec.rules).toExist("Expected rules to exist"); 375 | expect(ing.spec.rules[0].host).toNotExist(); 376 | expect(ing.spec.rules[0].http.paths[0].path).toBe("/"); 377 | expect(ing.spec.rules[0].http.paths[0].backend.serviceName).toBe("nginx"); 378 | expect(ing.spec.rules[0].http.paths[0].backend.servicePort).toBe(80); 379 | }); 380 | 381 | it("File deployment.yml is generated and filled in", function () { 382 | assert.file(["deployment.yml"]); 383 | var deployment = yaml.load("deployment.yml"); 384 | expect(deployment.apiVersion).toBe("extensions/v1beta1"); 385 | expect(deployment.kind).toBe("Deployment"); 386 | expect(deployment.metadata.labels.name).toBe("nginx"); 387 | expect(deployment.metadata.name).toBe("nginx"); 388 | expect(deployment.metadata.namespace).toBe("default"); 389 | expect(deployment.spec.replicas).toBe(1); 390 | expect(deployment.spec.template.metadata.labels.app).toBe("nginx"); 391 | expect(deployment.spec.template.spec.containers[0].name).toBe("nginx"); 392 | expect(deployment.spec.template.spec.containers[0].image).toBe("nginx"); 393 | }); 394 | }); 395 | 396 | describe("Invalid podControllerType stops the writing process", function () { 397 | 398 | beforeEach(function () { 399 | return helpers.run(path.join(__dirname, "../generators/app")) 400 | .withArguments(["--create"]) 401 | .withPrompts({ 402 | name: "nginx", 403 | namespace: "default", 404 | podControllerType: "InvalidType", 405 | image: "nginx", 406 | replicas: 1, 407 | containerPort: 80, 408 | servicePort: 80, 409 | shouldExpose: "yes", 410 | host: "nginx.ingress.com", 411 | path: "/", 412 | ingressPort: 80 413 | }); 414 | }); 415 | 416 | it("File rc.yml is not generated", function () { 417 | assert.noFile(["rc.yml"]); 418 | }); 419 | 420 | it("File svc.yml is not generated", function () { 421 | assert.noFile(["svc.yml"]); 422 | }); 423 | 424 | it("File ing.yml is not generated", function () { 425 | assert.noFile(["ing.yml"]); 426 | }); 427 | 428 | it("File deployment.yml is not generated", function () { 429 | assert.noFile(["deployment.yml"]); 430 | }); 431 | }); 432 | 433 | describe("Spawn create command with Deployment", function () { 434 | 435 | beforeEach(function () { 436 | return helpers.run(path.join(__dirname, "../generators/app")) 437 | .withArguments(["--create"]) 438 | .withPrompts({ 439 | name: "nginx", 440 | namespace: "default", 441 | podControllerType: "Deployment", 442 | image: "nginx", 443 | replicas: 1, 444 | containerPort: 80, 445 | servicePort: 80, 446 | shouldExpose: "yes", 447 | host: "nginx.ingress.com", 448 | path: "/", 449 | ingressPort: 80 450 | }); 451 | }); 452 | 453 | it("File rc.yml is not generated", function () { 454 | assert.noFile(["rc.yml"]); 455 | }); 456 | 457 | it("File svc.yml is generated", function () { 458 | assert.file(["svc.yml"]); 459 | }); 460 | 461 | it("File ing.yml is generated", function () { 462 | assert.file(["ing.yml"]); 463 | }); 464 | 465 | it("File deployment.yml is generated", function () { 466 | assert.file(["deployment.yml"]); 467 | }); 468 | }); 469 | 470 | describe("Spawn apply command with Deployment", function () { 471 | 472 | beforeEach(function () { 473 | return helpers.run(path.join(__dirname, "../generators/app")) 474 | .withArguments(["--apply"]) 475 | .withPrompts({ 476 | name: "nginx", 477 | namespace: "default", 478 | podControllerType: "Deployment", 479 | image: "nginx", 480 | replicas: 1, 481 | containerPort: 80, 482 | servicePort: 80, 483 | shouldExpose: "yes", 484 | ingressType: "external", 485 | host: "nginx.ingress.com", 486 | path: "/", 487 | ingressPort: 80 488 | }); 489 | }); 490 | 491 | it("File rc.yml is not generated", function () { 492 | assert.noFile(["rc.yml"]); 493 | }); 494 | 495 | it("File svc.yml is generated", function () { 496 | assert.file(["svc.yml"]); 497 | }); 498 | 499 | it("File ing.yml is generated", function () { 500 | assert.file(["ing.yml"]); 501 | }); 502 | 503 | it("File deployment.yml is generated", function () { 504 | assert.file(["deployment.yml"]); 505 | }); 506 | }); -------------------------------------------------------------------------------- /tests/validations.js: -------------------------------------------------------------------------------- 1 | var expect = require("expect"); 2 | var val = require("../generators/validations.js"); 3 | 4 | describe("String validation tests", function () { 5 | it("Is string", function () { 6 | expect(val.isString("string")).toBe(true); 7 | }); 8 | it("A number is not string", function () { 9 | expect(val.isString(0)).toBe(false); 10 | }); 11 | it("Null is not string", function () { 12 | expect(val.isString(null)).toBe(false); 13 | }); 14 | }); 15 | 16 | describe("Integer validation tests", function () { 17 | it("Is integer", function () { 18 | expect(val.isNumber(124)).toBe(true); 19 | }); 20 | it("String is not integer", function () { 21 | expect(val.isNumber("string")).toBe(false); 22 | }); 23 | it("Null is not integer", function () { 24 | expect(val.isNumber(null)).toBe(false); 25 | }); 26 | it("Decimal is not integer", function () { 27 | expect(val.isNumber(3.1416)).toBe(false); 28 | }); 29 | it("Can parse a string to integer", function () { 30 | expect(val.parseInteger("40")).toBe(40); 31 | }); 32 | }); --------------------------------------------------------------------------------