Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): implement cluster create and delete commands #446

Merged
merged 16 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat(cli): initial implementation of cluster commands
Signed-off-by: Lenin Mehedy <lenin.mehedy@swirldslabs.com>
  • Loading branch information
leninmehedy committed Oct 24, 2023
commit 19a9e4bc0ed300de108711a93fd16e9c21c0dd13
7 changes: 7 additions & 0 deletions fullstack-network-manager/resources/dev-cluster.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
name: fst # this is overridden if CLUSTER_NAME env var is set. Check .env file
nodes:
- role: control-plane
labels:
fullstack-scheduling.io/role: network
11 changes: 10 additions & 1 deletion fullstack-network-manager/src/commands/base.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use strict"
import {exec} from "child_process";
import * as core from "../core/index.mjs"
import * as util from "util";

export const BaseCommand = class BaseCommand {
/**
Expand Down Expand Up @@ -93,16 +94,24 @@ export const BaseCommand = class BaseCommand {
*/
runExec(cmd) {
return new Promise((resolve, reject) => {
exec(cmd, (error, stdout, stderr) => {
exec(cmd, (error, stderr, stdout) => {
if (error) {
reject(error)
}

console.log(stderr)
console.log(stdout)

resolve(stdout)
})
})
}

showUser(msg, ...args) {
console.log(util.format(msg, ...args))
this.logger.debug(msg, ...args)
}

constructor(opts) {
if (opts.logger === undefined) throw new Error("logger cannot be null")

Expand Down
37 changes: 33 additions & 4 deletions fullstack-network-manager/src/commands/cluster.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,48 @@ export const ClusterCommand = class extends BaseCommand {
/**
* Create a cluster
* @param argv
* @returns {Promise<void>}
* @returns {Promise<boolean>}
*/
async create(argv) {
this.logger.info("creating cluster '%s'", argv.name)
let cmd = `kind create cluster -n ${argv.name} --config ${core.constants.RESOURCES_DIR}/dev-cluster.yaml`

try {
this.showUser(`Invoking '${cmd}'...`)
let output = await this.runExec(cmd)
this.logger.debug(output)

this.showUser("Created cluster '%s'\n", argv.name)
output = await this.runExec(`kubectl cluster-info --context kind-${argv.name}`)
this.showUser(output)

return true
} catch (e) {
this.logger.error("%s", e)
this.showUser(e.message)
}

return false
}

/**
* Delete a cluster
* @param argv
* @returns {Promise<void>}
* @returns {Promise<boolean>}
*/
async delete(argv) {
this.logger.info("deleting cluster '%s'", argv.name, {name: argv.name})
let cmd = `kind delete cluster -n ${argv.name}`
try {
this.showUser(`Invoking '${cmd}'...`)
let output = await this.runExec(cmd)
this.showUser(output)
this.showUser("Deleted cluster '%s' (if it existed)", argv.name)
return true
} catch (e) {
this.logger.error("%s", e.stack)
this.showUser(e.message)
}

return false
}

/**
Expand Down
3 changes: 2 additions & 1 deletion fullstack-network-manager/src/core/logging.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ export function NewLogger(level = 'debug') {
// - Write all logs with importance level of `info` or less to `combined.log`
//
new winston.transports.File({filename: constants.TMP_DIR + "/logs/combined.log"}),
new winston.transports.File({filename: constants.TMP_DIR + "/logs/error.log", level: 'error'}),
// new winston.transports.File({filename: constants.TMP_DIR + "/logs/error.log", level: 'error'}),
// new winston.transports.Console({format: customFormat})
],
});

Expand Down