Skip to content

Commit

Permalink
feat: rename kurtosis context "switch" to "set" (#1537)
Browse files Browse the repository at this point in the history
## Description:
Changes the command `kurtosis context switch` to `kurtosis context set`,
to match the similar command `kurtosis cluster set`.

I've changed the cli command itself, plus the context config store.
There's also the portal manager, which I've left as switch for now, as
it extends into the kurtosis-portal project; I think we'll also need to
do that?

We're missing documentation on contexts and this cli command. I don't
know what to write for it yet so I've added
#1536

## Is this change user facing?
YES
<!-- If yes, please add the "user facing" label to the PR -->
<!-- If yes, don't forget to include docs changes where relevant -->

## References (if applicable):
#1508

---------

Co-authored-by: Omar <omar@omar>
  • Loading branch information
omar711 and Omar committed Oct 13, 2023
1 parent 446d56f commit ccff275
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 52 deletions.
2 changes: 1 addition & 1 deletion cli/cli/command_str_consts/command_str_consts.go
Expand Up @@ -24,7 +24,7 @@ const (
ContextAddCmdStr = "add"
ContextLsCmdStr = "ls"
ContextRmCmdStr = "rm"
ContextSwitchCmdStr = "switch"
ContextSetCmdStr = "set"
DiscordCmdStr = "discord"
DocsCmdStr = "docs"
EnclaveCmdStr = "enclave"
Expand Down
4 changes: 2 additions & 2 deletions cli/cli/commands/cloud/load/load.go
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/kurtosis-tech/kurtosis/cli/cli/command_framework/lowlevel/flags"
"github.com/kurtosis-tech/kurtosis/cli/cli/command_str_consts"
"github.com/kurtosis-tech/kurtosis/cli/cli/commands/kurtosis_context/add"
"github.com/kurtosis-tech/kurtosis/cli/cli/commands/kurtosis_context/context_switch"
"github.com/kurtosis-tech/kurtosis/cli/cli/commands/kurtosis_context/set"
cloudhelper "github.com/kurtosis-tech/kurtosis/cli/cli/helpers/cloud"
api "github.com/kurtosis-tech/kurtosis/cloud/api/golang/kurtosis_backend_server_rpc_api_bindings"
"github.com/kurtosis-tech/kurtosis/contexts-config-store/store"
Expand Down Expand Up @@ -105,7 +105,7 @@ func run(ctx context.Context, _ *flags.ParsedFlags, args *args.ParsedArgs) error
return stacktrace.Propagate(err, "Unable to add context to context store")
}
contextIdentifier := parsedContext.GetName()
return context_switch.SwitchContext(ctx, contextIdentifier)
return set.SetContext(ctx, contextIdentifier)
}

func assembleEnvVars(cloudInstanceConfig *api.GetCloudInstanceConfigResponse) *string {
Expand Down
2 changes: 1 addition & 1 deletion cli/cli/commands/cluster/set/set.go
Expand Up @@ -57,7 +57,7 @@ func run(ctx context.Context, flags *flags.ParsedFlags, args *args.ParsedArgs) e
return stacktrace.Propagate(err, "tried fetching the current Kurtosis context but failed, we can't switch clusters without this information. This is a bug in Kurtosis")
}
if store.IsRemote(currentKurtosisContext) {
return stacktrace.NewError("Switching clusters on a remote context is not a permitted operation, please switch to the local context using `kurtosis %s %s default` before switching clusters", command_str_consts.ContextCmdStr, command_str_consts.ContextSwitchCmdStr)
return stacktrace.NewError("Switching clusters on a remote context is not a permitted operation, please switch to the local context using `kurtosis %s %s default` before switching clusters", command_str_consts.ContextCmdStr, command_str_consts.ContextSetCmdStr)
}

clusterSettingStore := kurtosis_cluster_setting.GetKurtosisClusterSettingStore()
Expand Down
4 changes: 2 additions & 2 deletions cli/cli/commands/kurtosis_context/kurtosis_context.go
Expand Up @@ -3,9 +3,9 @@ package kurtosis_context
import (
"github.com/kurtosis-tech/kurtosis/cli/cli/command_str_consts"
"github.com/kurtosis-tech/kurtosis/cli/cli/commands/kurtosis_context/add"
"github.com/kurtosis-tech/kurtosis/cli/cli/commands/kurtosis_context/context_switch"
"github.com/kurtosis-tech/kurtosis/cli/cli/commands/kurtosis_context/ls"
"github.com/kurtosis-tech/kurtosis/cli/cli/commands/kurtosis_context/rm"
"github.com/kurtosis-tech/kurtosis/cli/cli/commands/kurtosis_context/set"
"github.com/spf13/cobra"
)

Expand All @@ -21,5 +21,5 @@ func init() {
ContextCmd.AddCommand(add.ContextAddCmd.MustGetCobraCommand())
ContextCmd.AddCommand(ls.ContextLsCmd.MustGetCobraCommand())
ContextCmd.AddCommand(rm.ContextRmCmd.MustGetCobraCommand())
ContextCmd.AddCommand(context_switch.ContextSwitchCmd.MustGetCobraCommand())
ContextCmd.AddCommand(set.ContextSetCmd.MustGetCobraCommand())
}
@@ -1,4 +1,4 @@
package context_switch
package set

import (
"context"
Expand All @@ -23,11 +23,11 @@ const (
contextIdentifierArgIsGreedy = false
)

var ContextSwitchCmd = &lowlevel.LowlevelKurtosisCommand{
CommandStr: command_str_consts.ContextSwitchCmdStr,
ShortDescription: "Switches to a different Kurtosis context",
LongDescription: fmt.Sprintf("Switches to a different Kurtosis context. The context needs to be added "+
"first using the `%s` command. When switching to a remote context, the connection will be established with "+
var ContextSetCmd = &lowlevel.LowlevelKurtosisCommand{
CommandStr: command_str_consts.ContextSetCmdStr,
ShortDescription: "Sets the active Kurtosis context",
LongDescription: fmt.Sprintf("Sets the active Kurtosis context. The context needs to be added "+
"first using the `%s` command. When setting a remote context, the connection will be established with "+
"the remote Kurtosis server. Kurtosis Portal needs to be running for this. If the remote server can't be "+
"reached, the context will remain unchanged.", command_str_consts.ContextAddCmdStr),
Flags: []*flags.FlagConfig{},
Expand All @@ -45,30 +45,30 @@ func run(ctx context.Context, _ *flags.ParsedFlags, args *args.ParsedArgs) error
return stacktrace.Propagate(err, "Expected a value for context identifier arg '%v' but none was found; this is a bug in the Kurtosis CLI!", contextIdentifierArgKey)
}

return SwitchContext(ctx, contextIdentifier)
return SetContext(ctx, contextIdentifier)
}

func SwitchContext(
func SetContext(
ctx context.Context,
contextIdentifier string,
) error {
isContextSwitchSuccessful := false
isContextSetSuccessful := false
logrus.Info("Switching context...")

contextsConfigStore := store.GetContextsConfigStore()
contextPriorToSwitch, err := contextsConfigStore.GetCurrentContext()
contextPriorToSet, err := contextsConfigStore.GetCurrentContext()
if err != nil {
return stacktrace.NewError("An error occurred retrieving current context prior to switching to the new one '%s'", contextIdentifier)
return stacktrace.NewError("An error occurred retrieving current context prior to setting to the new one '%s'", contextIdentifier)
}

if !store.IsRemote(contextPriorToSwitch) {
if !store.IsRemote(contextPriorToSet) {
engineManager, err := engine_manager.NewEngineManager(ctx)
if err != nil {
return stacktrace.Propagate(err, "An error occurred creating an engine manager.")
}
if err := engineManager.StopEngineIdempotently(ctx); err != nil {
return stacktrace.Propagate(err, "An error occurred stopping the local engine. The local engine"+
"needs to be stopped before the context can be switched. The engine status can be obtained running"+
"needs to be stopped before the context can be set. The engine status can be obtained running"+
"kurtosis %s %s and it can be stopped manually by running kurtosis %s %s.",
command_str_consts.EngineCmdStr, command_str_consts.EngineStatusCmdStr,
command_str_consts.EngineCmdStr, command_str_consts.EngineStopCmdStr)
Expand All @@ -79,36 +79,36 @@ func SwitchContext(
if err != nil {
return stacktrace.Propagate(err, "Error searching for context matching context identifier: '%s'", contextIdentifier)
}
contextUuidToSwitchTo, found := contextsMatchingIdentifiers[contextIdentifier]
contextUuidToSet, found := contextsMatchingIdentifiers[contextIdentifier]
if !found {
return stacktrace.NewError("No context matching identifier '%s' could be found", contextIdentifier)
}

if contextUuidToSwitchTo.GetValue() == contextPriorToSwitch.GetUuid().GetValue() {
logrus.Infof("Already on context '%s'", contextPriorToSwitch.GetName())
if contextUuidToSet.GetValue() == contextPriorToSet.GetUuid().GetValue() {
logrus.Infof("Context '%s' already set", contextPriorToSet.GetName())
return nil
}

if err = contextsConfigStore.SwitchContext(contextUuidToSwitchTo); err != nil {
return stacktrace.Propagate(err, "An error occurred switching to context '%s' with UUID '%s'", contextIdentifier, contextUuidToSwitchTo.GetValue())
if err = contextsConfigStore.SetContext(contextUuidToSet); err != nil {
return stacktrace.Propagate(err, "An error occurred setting context '%s' with UUID '%s'", contextIdentifier, contextUuidToSet.GetValue())
}
defer func() {
if isContextSwitchSuccessful {
if isContextSetSuccessful {
return
}
if err = contextsConfigStore.SwitchContext(contextPriorToSwitch.GetUuid()); err != nil {
logrus.Errorf("An unexpected error occurred switching to context '%s' with UUID "+
if err = contextsConfigStore.SetContext(contextPriorToSet.GetUuid()); err != nil {
logrus.Errorf("An unexpected error occurred setting context '%s' with UUID "+
"'%s'. Kurtosis tried to roll back to previous context '%s' with UUID '%s' but the roll back "+
"failed. It is likely that the current context is still set to '%s' but it is not fully functional. "+
"Try manually switching back to '%s' to get back to a working state. Error was:\n%v",
contextIdentifier, contextUuidToSwitchTo.GetValue(), contextPriorToSwitch.GetName(),
contextPriorToSwitch.GetUuid(), contextIdentifier, contextPriorToSwitch.GetName(), err.Error())
"Try manually reverting to context '%s' to get back to a working state. Error was:\n%v",
contextIdentifier, contextUuidToSet.GetValue(), contextPriorToSet.GetName(),
contextPriorToSet.GetUuid(), contextIdentifier, contextPriorToSet.GetName(), err.Error())
}
}()

currentContext, err := contextsConfigStore.GetCurrentContext()
if err != nil {
return stacktrace.Propagate(err, "Error retrieving context info for context '%s' after switching to it", contextIdentifier)
return stacktrace.Propagate(err, "Error retrieving context info for context '%s' after setting it", contextIdentifier)
}

portalManager := portal_manager.NewPortalManager()
Expand All @@ -133,7 +133,7 @@ func SwitchContext(
}
}

logrus.Infof("Context switched to '%s', Kurtosis engine will now be restarted", contextIdentifier)
logrus.Infof("Context set to '%s', Kurtosis engine will now be restarted", contextIdentifier)

// Instantiate the engine manager after storing the new context so the manager can read it.
engineManager, err := engine_manager.NewEngineManager(ctx)
Expand All @@ -143,7 +143,7 @@ func SwitchContext(

_, engineClientCloseFunc, startEngineErr := engineManager.StartEngineIdempotentlyWithDefaultVersion(ctx, logrus.InfoLevel, defaults.DefaultEngineEnclavePoolSize)
if startEngineErr != nil {
logrus.Warnf("The context was successfully switched to '%s' but Kurtosis failed to start an engine in "+
logrus.Warnf("The context was successfully set to '%s' but Kurtosis failed to start an engine in "+
"this new context. A new engine should be started manually with '%s %s %s'. The error was:\n%v",
contextIdentifier, command_str_consts.KurtosisCmdStr, command_str_consts.EngineCmdStr, command_str_consts.EngineRestartCmdStr, startEngineErr)
} else {
Expand All @@ -153,9 +153,9 @@ func SwitchContext(
contextIdentifier, err)
}
}()
logrus.Info("Successfully switched context")
logrus.Info("Successfully set context")
}

isContextSwitchSuccessful = true
isContextSetSuccessful = true
return nil
}
4 changes: 2 additions & 2 deletions contexts-config-store/store/contexts_config_store.go
Expand Up @@ -19,9 +19,9 @@ type ContextsConfigStore interface {
// GetCurrentContext returns the current context information.
GetCurrentContext() (*generated.KurtosisContext, error)

// SwitchContext switches to the context passed as an argument.
// SetContext switches to the context passed as an argument.
// It throws an error if the contextUuid does not point to any known context.
SwitchContext(contextUuid *generated.ContextUuid) error
SetContext(contextUuid *generated.ContextUuid) error

// AddNewContext adds a new context to the store.
// It throws an error if a context with the same UUID already exists
Expand Down
2 changes: 1 addition & 1 deletion contexts-config-store/store/contexts_config_store_impl.go
Expand Up @@ -60,7 +60,7 @@ func (store *contextConfigStoreImpl) GetCurrentContext() (*generated.KurtosisCon
strings.Join(contextUuidsInStore, contextUuidsSeparator))
}

func (store *contextConfigStoreImpl) SwitchContext(contextUuid *generated.ContextUuid) error {
func (store *contextConfigStoreImpl) SetContext(contextUuid *generated.ContextUuid) error {
store.Lock()
defer store.Unlock()

Expand Down
Expand Up @@ -68,7 +68,7 @@ func TestGetCurrentContext_failureInconsistentContextConfig(t *testing.T) {
require.Nil(t, result)
}

func TestSwitchContext(t *testing.T) {
func TestSetContext(t *testing.T) {
// Setup storage mock
storage := persistence.NewMockConfigPersistence(t)
contextsConfig := api.NewKurtosisContextsConfig(contextUuid, localContext, otherLocalContext)
Expand All @@ -79,19 +79,19 @@ func TestSwitchContext(t *testing.T) {

// Run test
testContextConfigStore := NewContextConfigStore(storage)
err := testContextConfigStore.SwitchContext(otherContextUuid)
err := testContextConfigStore.SetContext(otherContextUuid)
require.NoError(t, err)
}

func TestSwitchContext_NonExistingContextFailure(t *testing.T) {
func TestSetContext_NonExistingContextFailure(t *testing.T) {
// Setup storage mock
storage := persistence.NewMockConfigPersistence(t)
contextsConfig := api.NewKurtosisContextsConfig(contextUuid, localContext)
storage.EXPECT().LoadContextsConfig().Return(contextsConfig, nil)

// Run test
testContextConfigStore := NewContextConfigStore(storage)
err := testContextConfigStore.SwitchContext(otherContextUuid)
err := testContextConfigStore.SetContext(otherContextUuid)
require.Error(t, err)
expectedErr := fmt.Sprintf("Context with UUID '%s' does not exist in store. Known contexts are: '%s'",
otherContextUuid.GetValue(), contextUuid.GetValue())
Expand Down
20 changes: 10 additions & 10 deletions contexts-config-store/store/mock_contexts_config_store.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ccff275

Please sign in to comment.