-
Notifications
You must be signed in to change notification settings - Fork 15
feat: Implement solo context connect #863
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
Merged
jeromy-cannon
merged 8 commits into
main
from
00754-implement-solo-context-connect-part-1-of-2-updates-the-localconfig-by-connecting-a-deployment-to-a-k8s-context
Dec 2, 2024
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
598efeb
feat: solo context use
Ivo-Yankov e8c7549
Merge branch 'main' into 00754-implement-solo-context-connect-part-1-…
Ivo-Yankov e5db03f
chore: update context connect command
Ivo-Yankov 74bd85b
feat: add descriptive error messages for LocalConfig
Ivo-Yankov bd41b5d
chore: format
Ivo-Yankov 35cb512
Merge branch 'main' into 00754-implement-solo-context-connect-part-1-…
Ivo-Yankov 1083862
chore: addressing comments
Ivo-Yankov d48164c
feat: cache context results in k8
Ivo-Yankov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* Copyright (C) 2024 Hedera Hashgraph, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the ""License""); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an ""AS IS"" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
import * as flags from '../flags.js' | ||
|
||
export const DEFAULT_FLAGS = { | ||
requiredFlags: [], | ||
requiredFlagsWithDisabledPrompt: [flags.namespace, flags.cacheDir, flags.releaseTag], | ||
optionalFlags: [flags.devMode] | ||
} | ||
|
||
export const USE_FLAGS = { | ||
requiredFlags: [], | ||
requiredFlagsWithDisabledPrompt: [], | ||
optionalFlags: [flags.devMode, flags.quiet, flags.clusterName, flags.context, flags.force, flags.namespace] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* Copyright (C) 2024 Hedera Hashgraph, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the ""License""); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an ""AS IS"" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
import { type BaseCommand } from '../base.js' | ||
import { type ContextCommandTasks } from './tasks.js' | ||
import * as helpers from '../../core/helpers.js' | ||
import { constants } from '../../core/index.js' | ||
import { type CommandHandlers } from '../../types/index.js' | ||
import * as ContextFlags from './flags.js' | ||
|
||
export class ContextCommandHandlers implements CommandHandlers { | ||
readonly parent: BaseCommand | ||
readonly tasks: ContextCommandTasks | ||
|
||
constructor (parent: BaseCommand, tasks: ContextCommandTasks) { | ||
this.parent = parent | ||
this.tasks = tasks | ||
} | ||
|
||
async connect (argv: any) { | ||
argv = helpers.addFlagsToArgv(argv, ContextFlags.USE_FLAGS) | ||
|
||
const action = helpers.commandActionBuilder([ | ||
this.tasks.initialize(argv), | ||
this.parent.getLocalConfig().promptLocalConfigTask(this.parent.getK8(), argv), | ||
this.tasks.updateLocalConfig(argv), | ||
], { | ||
concurrent: false, | ||
rendererOptions: constants.LISTR_DEFAULT_RENDERER_OPTION | ||
}, 'context use', null) | ||
|
||
await action(argv, this) | ||
return true | ||
} | ||
|
||
} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/** | ||
* Copyright (C) 2024 Hedera Hashgraph, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the ""License""); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an ""AS IS"" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
import { YargsCommand } from '../../core/index.js' | ||
import { BaseCommand } from './../base.js' | ||
import type { Opts } from '../../types/index.js' | ||
import { ContextCommandTasks } from './tasks.js' | ||
import { ContextCommandHandlers } from './handlers.js' | ||
import * as ContextFlags from './flags.js' | ||
import { getPromptMap } from '../prompts.js' | ||
|
||
/** | ||
* Defines the core functionalities of 'node' command | ||
*/ | ||
export class ContextCommand extends BaseCommand { | ||
private handlers: ContextCommandHandlers | ||
|
||
constructor (opts: Opts) { | ||
super(opts) | ||
|
||
this.handlers = new ContextCommandHandlers(this, new ContextCommandTasks(this, getPromptMap())) | ||
} | ||
|
||
getCommandDefinition () { | ||
return { | ||
command: 'context', | ||
desc: 'Manage local and remote configurations', | ||
builder: (yargs: any) => { | ||
return yargs | ||
.command(new YargsCommand({ | ||
command: 'connect', | ||
description: 'updates the local configuration by connecting a deployment to a k8s context', | ||
commandDef: this, | ||
handler: 'connect' | ||
}, ContextFlags.USE_FLAGS)) | ||
.demandCommand(1, 'Select a context command') | ||
} | ||
} | ||
} | ||
} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/** | ||
* Copyright (C) 2024 Hedera Hashgraph, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the ""License""); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an ""AS IS"" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
import { | ||
Task, Templates | ||
} from '../../core/index.js' | ||
import * as flags from '../flags.js' | ||
import type { ListrTaskWrapper } from 'listr2' | ||
import { type BaseCommand } from '../base.js' | ||
|
||
export class ContextCommandTasks { | ||
|
||
private readonly parent: BaseCommand | ||
private readonly promptMap: Map<string, Function> | ||
|
||
constructor (parent, promptMap) { | ||
this.parent = parent | ||
this.promptMap = promptMap | ||
} | ||
|
||
updateLocalConfig (argv) { | ||
return new Task('Update local configuration', async (ctx: any, task: ListrTaskWrapper<any, any, any>) => { | ||
this.parent.logger.info('Updating local configuration...') | ||
|
||
const isQuiet = !!argv[flags.quiet.name] | ||
|
||
let currentDeploymentName = argv[flags.namespace.name] | ||
let clusterAliases = Templates.parseClusterAliases(argv[flags.clusterName.name]) | ||
let contextName = argv[flags.context.name] | ||
|
||
const kubeContexts = await this.parent.getK8().getKubeConfig().getContexts() | ||
|
||
if (isQuiet) { | ||
const currentCluster = (await this.parent.getK8().getKubeConfig().getCurrentCluster()) | ||
if (!clusterAliases.length) clusterAliases = [currentCluster.name] | ||
if (!contextName) contextName = await this.parent.getK8().getKubeConfig().getCurrentContext() | ||
|
||
if (!currentDeploymentName) { | ||
const selectedContext = kubeContexts.find(e => e.name === contextName) | ||
currentDeploymentName = selectedContext && selectedContext.namespace ? selectedContext.namespace : 'default' | ||
} | ||
} | ||
else { | ||
if (!clusterAliases.length) { | ||
const prompt = this.promptMap.get(flags.clusterName.name) | ||
const unparsedClusterAliases = await prompt(task, clusterAliases) | ||
clusterAliases = Templates.parseClusterAliases(unparsedClusterAliases) | ||
} | ||
if (!contextName) { | ||
const prompt = this.promptMap.get(flags.context.name) | ||
contextName = await prompt(task, kubeContexts.map(c => c.name), contextName) | ||
} | ||
if (!currentDeploymentName) { | ||
const prompt = this.promptMap.get(flags.namespace.name) | ||
currentDeploymentName = await prompt(task, currentDeploymentName) | ||
} | ||
} | ||
|
||
// Select current deployment | ||
this.parent.getLocalConfig().setCurrentDeployment(currentDeploymentName) | ||
|
||
// Set clusters for active deployment | ||
const deployments = this.parent.getLocalConfig().deployments | ||
deployments[currentDeploymentName].clusterAliases = clusterAliases | ||
this.parent.getLocalConfig().setDeployments(deployments) | ||
|
||
this.parent.getK8().getKubeConfig().setCurrentContext(contextName) | ||
|
||
this.parent.logger.info(`currentDeploymentName: ${currentDeploymentName}`) | ||
this.parent.logger.info(`contextName: ${contextName}`) | ||
this.parent.logger.info(`clusterAliases: ${clusterAliases.join(' ')}`) | ||
this.parent.logger.info('Save LocalConfig file') | ||
jeromy-cannon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
await this.parent.getLocalConfig().write() | ||
}) | ||
} | ||
|
||
initialize (argv: any) { | ||
const { requiredFlags, optionalFlags } = argv | ||
|
||
argv.flags = [ | ||
...requiredFlags, | ||
...optionalFlags | ||
] | ||
|
||
return new Task('Initialize', async (ctx: any, task: ListrTaskWrapper<any, any, any>) => { | ||
if (argv[flags.devMode.name]) { | ||
this.parent.logger.setDevMode(true) | ||
} | ||
}) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,8 +22,8 @@ | |
|
||
/** | ||
* Set flag from the flag option | ||
* @param y instance of yargs | ||
Check warning on line 25 in src/commands/flags.ts
|
||
* @param commandFlags a set of command flags | ||
Check warning on line 26 in src/commands/flags.ts
|
||
* | ||
*/ | ||
export function setCommandFlags (y: any, ...commandFlags: CommandFlag[]) { | ||
|
@@ -748,6 +748,16 @@ | |
} | ||
} | ||
|
||
export const context: CommandFlag = { | ||
constName: 'contextName', | ||
name: 'context', | ||
definition: { | ||
describe: 'The kind context name to be used', | ||
jeromy-cannon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
defaultValue: '', | ||
type: 'string' | ||
} | ||
} | ||
|
||
export const deploymentName: CommandFlag = { | ||
constName: 'deploymentName', | ||
name: 'deployment-name', | ||
|
@@ -775,9 +785,9 @@ | |
name: 'grpc-tls-cert', | ||
definition: { | ||
describe: | ||
'TLS Certificate path for the gRPC ' + | ||
'(e.g. "node1=/Users/username/node1-grpc.cert" ' + | ||
'with multiple nodes comma seperated)', | ||
'TLS Certificate path for the gRPC ' + | ||
'(e.g. "node1=/Users/username/node1-grpc.cert" ' + | ||
'with multiple nodes comma seperated)', | ||
defaultValue: '', | ||
type: 'string' | ||
} | ||
|
@@ -788,9 +798,9 @@ | |
name: 'grpc-web-tls-cert', | ||
definition: { | ||
describe: | ||
'TLS Certificate path for gRPC Web ' + | ||
'(e.g. "node1=/Users/username/node1-grpc-web.cert" ' + | ||
'with multiple nodes comma seperated)', | ||
'TLS Certificate path for gRPC Web ' + | ||
'(e.g. "node1=/Users/username/node1-grpc-web.cert" ' + | ||
'with multiple nodes comma seperated)', | ||
defaultValue: '', | ||
type: 'string' | ||
} | ||
|
@@ -801,9 +811,9 @@ | |
name: 'grpc-tls-key', | ||
definition: { | ||
describe: | ||
'TLS Certificate key path for the gRPC ' + | ||
'(e.g. "node1=/Users/username/node1-grpc.key" ' + | ||
'with multiple nodes comma seperated)', | ||
'TLS Certificate key path for the gRPC ' + | ||
'(e.g. "node1=/Users/username/node1-grpc.key" ' + | ||
'with multiple nodes comma seperated)', | ||
defaultValue: '', | ||
type: 'string' | ||
} | ||
|
@@ -814,9 +824,9 @@ | |
name: 'grpc-web-tls-key', | ||
definition: { | ||
describe: | ||
'TLC Certificate key path for gRPC Web ' + | ||
'(e.g. "node1=/Users/username/node1-grpc-web.key" ' + | ||
'with multiple nodes comma seperated)', | ||
'TLC Certificate key path for gRPC Web ' + | ||
'(e.g. "node1=/Users/username/node1-grpc-web.key" ' + | ||
'with multiple nodes comma seperated)', | ||
defaultValue: '', | ||
type: 'string' | ||
} | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.