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

fix: Check if a local engine is running before switching to a remote context and let the user know what to do #1011

Merged
merged 6 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 15 additions & 9 deletions cli/cli/commands/kurtosis_context/context_switch/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"github.com/kurtosis-tech/kurtosis-portal/api/golang/constructors"
"github.com/kurtosis-tech/kurtosis/api/golang/engine/lib/kurtosis_context"
"github.com/kurtosis-tech/kurtosis/cli/cli/command_framework/highlevel/context_id_arg"
"github.com/kurtosis-tech/kurtosis/cli/cli/command_framework/lowlevel"
"github.com/kurtosis-tech/kurtosis/cli/cli/command_framework/lowlevel/args"
Expand Down Expand Up @@ -57,6 +56,21 @@ func run(ctx context.Context, _ *flags.ParsedFlags, args *args.ParsedArgs) error
return stacktrace.NewError("An error occurred retrieving current context prior to switching to the new one '%s'", contextIdentifier)
}

if !store.IsRemote(contextPriorToSwitch) {
engineManager, err := engine_manager.NewEngineManager(ctx)
if err != nil {
return stacktrace.Propagate(err, "An error occurred creating an engine manager.")
}
engineStatus, _, _, err := engineManager.GetEngineStatus(ctx)
if err != nil {
return stacktrace.Propagate(err, "An error occurred retrieving the engine status.")
}
if engineStatus == engine_manager.EngineStatus_Running {
logrus.Infof("An engine is running locally. Stop the engine before switching context so the remote engine port can be forwarded locally.")
laurentluce marked this conversation as resolved.
Show resolved Hide resolved
return nil
}
}

contextsMatchingIdentifiers, err := context_id_arg.GetContextUuidForContextIdentifier(contextsConfigStore, []string{contextIdentifier})
if err != nil {
return stacktrace.Propagate(err, "Error searching for context matching context identifier: '%s'", contextIdentifier)
Expand Down Expand Up @@ -101,14 +115,6 @@ func run(ctx context.Context, _ *flags.ParsedFlags, args *args.ParsedArgs) error
if _, err = portalDaemonClient.SwitchContext(ctx, switchContextArg); err != nil {
return stacktrace.Propagate(err, "Error switching Kurtosis portal context")
}
if store.IsRemote(currentContext) {
// Forward the remote engine port to the local machine
portalClient := portalManager.GetClient()
forwardEnginePortArgs := constructors.NewForwardPortArgs(uint32(kurtosis_context.DefaultGrpcEngineServerPortNum), uint32(kurtosis_context.DefaultGrpcEngineServerPortNum), &kurtosis_context.EnginePortTransportProtocol)
if _, err := portalClient.ForwardPort(ctx, forwardEnginePortArgs); err != nil {
return stacktrace.Propagate(err, "Unable to forward the remote engine port to the local machine")
}
}
}
} else {
if store.IsRemote(currentContext) {
Expand Down
20 changes: 20 additions & 0 deletions cli/cli/helpers/engine_manager/engine_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package engine_manager

import (
"context"
portal_constructors "github.com/kurtosis-tech/kurtosis-portal/api/golang/constructors"
"github.com/kurtosis-tech/kurtosis/api/golang/engine/kurtosis_engine_rpc_api_bindings"
"github.com/kurtosis-tech/kurtosis/api/golang/engine/lib/kurtosis_context"
"github.com/kurtosis-tech/kurtosis/cli/cli/command_str_consts"
"github.com/kurtosis-tech/kurtosis/cli/cli/helpers/kurtosis_config_getter"
"github.com/kurtosis-tech/kurtosis/cli/cli/helpers/portal_manager"
"github.com/kurtosis-tech/kurtosis/cli/cli/kurtosis_cluster_setting"
"github.com/kurtosis-tech/kurtosis/cli/cli/kurtosis_config"
"github.com/kurtosis-tech/kurtosis/cli/cli/kurtosis_config/resolved_config"
Expand Down Expand Up @@ -338,6 +341,23 @@ func (manager *EngineManager) startEngineWithGuarantor(ctx context.Context, curr
return nil, nil, stacktrace.Propagate(err, "An error occurred connecting to the running engine; this is very strange and likely indicates a bug in the engine itself")
}

currentContext, err := store.GetContextsConfigStore().GetCurrentContext()
if err == nil {
if store.IsRemote(currentContext) {
portalManager := portal_manager.NewPortalManager()
if portalManager.IsReachable() {
// Forward the remote engine port to the local machine
portalClient := portalManager.GetClient()
forwardEnginePortArgs := portal_constructors.NewForwardPortArgs(uint32(hostMachinePortBinding.portNum), uint32(hostMachinePortBinding.portNum), &kurtosis_context.EnginePortTransportProtocol)
if _, err := portalClient.ForwardPort(ctx, forwardEnginePortArgs); err != nil {
return nil, nil, stacktrace.Propagate(err, "Unable to forward the remote engine port to the local machine.")
}
}
}
} else {
logrus.Warnf("Unable to retrieve current Kurtosis context. This is not critical, it will assume using Kurtosis default context for now.")
}

clusterType := manager.clusterConfig.GetClusterType()
// If we're in docker, we can make a health check
// In the kubernetes case, this health check will fail if the gateway isn't running
Expand Down