From 9cd207904ca7db8d9acd670bbb6e367e8d471e00 Mon Sep 17 00:00:00 2001 From: Oleg Sucharevich Date: Tue, 13 Feb 2018 16:51:44 +0200 Subject: [PATCH] Add cmd to generate image pull secret This requires kubernetes cluster and docker registry integrated with codefresh --- docs/content/more/_index.md | 7 +++ .../commands/imagePullSecret/generate.cmd.js | 50 +++++++++++++++++++ .../cli/commands/root/generate.cmd.js | 14 ++++++ lib/logic/api/kubernetes.js | 25 ++++++++++ package.json | 2 +- 5 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 docs/content/more/_index.md create mode 100644 lib/interface/cli/commands/imagePullSecret/generate.cmd.js create mode 100644 lib/interface/cli/commands/root/generate.cmd.js create mode 100644 lib/logic/api/kubernetes.js diff --git a/docs/content/more/_index.md b/docs/content/more/_index.md new file mode 100644 index 000000000..5384c6b6a --- /dev/null +++ b/docs/content/more/_index.md @@ -0,0 +1,7 @@ ++++ +title = "More" +weight = 40 ++++ + + +{{COMMANDS}} diff --git a/lib/interface/cli/commands/imagePullSecret/generate.cmd.js b/lib/interface/cli/commands/imagePullSecret/generate.cmd.js new file mode 100644 index 000000000..cab78eacb --- /dev/null +++ b/lib/interface/cli/commands/imagePullSecret/generate.cmd.js @@ -0,0 +1,50 @@ +const debug = require('debug')('codefresh:cli:generate:imagePullSecret'); +const Command = require('../../Command'); +const genCmd = require('../root/generate.cmd'); +const { + generateImagePullSecret, +} = require('./../../../../logic/api/kubernetes'); + +const command = new Command({ + command: 'image-pull-secret', + parent: genCmd, + description: 'Generate image pull secret on Kubernetes cluster from integrated Docker registry', + usage: ` + For Kuberentes cluster to pull an image from your private registry it needs special secret typed as \`kubernetes.io/dockercfg\`. + After this secret been created you can use them in pod template that lives in the same namespace. + You can generate this secret from any integrated Docker registry on your account. + More information about image pull secret can be found here: https://kubernetes.io/docs/tasks/configure-pod-container/pull-image-private-registry/`, + webDocs: { + category: 'More', + title: 'Image Pull Secret', + }, + builder: (yargs) => { + return yargs + .option('cluster', { + describe: 'cluster name', + required: true, + }) + .option('namespace', { + describe: 'namespace name', + default: 'default', + }) + .option('registry', { + describe: 'name of Docker registry to generate pull secret from', + required: true, + }) + .example('codefresh generate image-pull-secret --cluster cluster --registry cfcr', 'Generate image pull secret'); + }, + handler: async (argv) => { + const res = await generateImagePullSecret({ + cluster: argv.cluster, + namespace: argv.namespace, + registry: argv.registry, + }); + console.log(`Image Pull Secret created with name: ${res.name}`); + console.log('Avaliable via kubectl:'); + console.log(`kubectl get secret --context ${argv.cluster} --namespace ${argv.namespace} ${res.name}`); + }, +}); + +module.exports = command; + diff --git a/lib/interface/cli/commands/root/generate.cmd.js b/lib/interface/cli/commands/root/generate.cmd.js new file mode 100644 index 000000000..3277fdbd6 --- /dev/null +++ b/lib/interface/cli/commands/root/generate.cmd.js @@ -0,0 +1,14 @@ +const Command = require('../../Command'); +const yargs = require('yargs'); + +const get = new Command({ + root: true, + command: 'generate', + description: 'Generate resources as Kubernetes image pull secret and Codefresh Registry token', + usage: 'Codefresh generate --help', + handler: async () => { + yargs.showHelp(); + }, +}); + +module.exports = get; diff --git a/lib/logic/api/kubernetes.js b/lib/logic/api/kubernetes.js new file mode 100644 index 000000000..cd80fd116 --- /dev/null +++ b/lib/logic/api/kubernetes.js @@ -0,0 +1,25 @@ +const CFError = require('cf-errors'); // eslint-disable-line +const { sendHttpRequest } = require('./helper'); + + +const generateImagePullSecret = async (data) => { + const body = { + registry: data.registry, + }; + + const options = { + url: '/api/kubernetes/secrets/imagePullSecret', + method: 'POST', + qs: { + namespace: data.namespace, + selector: data.cluster, + }, + body, + }; + return sendHttpRequest(options); +}; + + +module.exports = { + generateImagePullSecret, +}; diff --git a/package.json b/package.json index 5d13386f6..cd321b401 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "codefresh", - "version": "0.8.19", + "version": "0.8.20", "description": "Codefresh command line utility", "main": "index.js", "preferGlobal": true,