Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added port print functionality in cli for users to quickly chec…
…k how to access port. (#778) Using this command: `kurtosis port print enclave-id service-id port-id` users can access the information about a port. An example is show below HERE Is the sample output: <img width="1443" alt="Screen Shot 2023-06-26 at 5 56 04 PM" src="https://github.com/kurtosis-tech/kurtosis/assets/15133250/bc6125f2-cc1d-4d66-83aa-8403139bfa20">
- Loading branch information
Showing
5 changed files
with
163 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,19 @@ | ||
package port | ||
|
||
import ( | ||
"github.com/kurtosis-tech/kurtosis/cli/cli/command_str_consts" | ||
"github.com/kurtosis-tech/kurtosis/cli/cli/commands/port/print" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// PortCmd Suppressing exhaustruct requirement because this struct has ~40 properties | ||
// nolint: exhaustruct | ||
var PortCmd = &cobra.Command{ | ||
Use: command_str_consts.PortCmdStr, | ||
Short: "Manage ports", | ||
RunE: nil, | ||
} | ||
|
||
func init() { | ||
PortCmd.AddCommand(print.PortPrintCmd.MustGetCobraCommand()) | ||
} |
This file contains 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,128 @@ | ||
package print | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"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_framework/highlevel/enclave_id_arg" | ||
"github.com/kurtosis-tech/kurtosis/cli/cli/command_framework/highlevel/engine_consuming_kurtosis_command" | ||
"github.com/kurtosis-tech/kurtosis/cli/cli/command_framework/highlevel/service_identifier_arg" | ||
"github.com/kurtosis-tech/kurtosis/cli/cli/command_framework/lowlevel/args" | ||
"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/out" | ||
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface" | ||
metrics_client "github.com/kurtosis-tech/metrics-library/golang/lib/client" | ||
"github.com/kurtosis-tech/stacktrace" | ||
) | ||
|
||
const ( | ||
kurtosisBackendCtxKey = "kurtosis-backend" | ||
engineClientCtxKey = "engine-client" | ||
|
||
enclaveIdentifierArgKey = "enclave" | ||
isEnclaveIdArgOptional = false | ||
isEnclaveIdArgGreedy = false | ||
|
||
serviceIdentifierArgKey = "service" | ||
isServiceIdentifierArgOptional = false | ||
isServiceIdentifierArgGreedy = false | ||
|
||
portIdentifierArgKey = "port_id" | ||
isPortIdentifierArgOptional = false | ||
isPortIdentifierArgGreedy = false | ||
|
||
ipAddress = "127.0.0.1" | ||
) | ||
|
||
var PortPrintCmd = &engine_consuming_kurtosis_command.EngineConsumingKurtosisCommand{ | ||
CommandStr: command_str_consts.PortPrintCmdStr, | ||
ShortDescription: "Get information about port", | ||
LongDescription: "Get information for port using port id", | ||
KurtosisBackendContextKey: kurtosisBackendCtxKey, | ||
EngineClientContextKey: engineClientCtxKey, | ||
Flags: []*flags.FlagConfig{}, | ||
Args: []*args.ArgConfig{ | ||
enclave_id_arg.NewHistoricalEnclaveIdentifiersArgWithValidationDisabled( | ||
enclaveIdentifierArgKey, | ||
isEnclaveIdArgOptional, | ||
isEnclaveIdArgGreedy, | ||
), | ||
// TODO use the `NewServiceIdentifierArg` instead when we start storing identifiers in DB | ||
// TODO we should fix this after https://github.com/kurtosis-tech/kurtosis/issues/879 | ||
service_identifier_arg.NewHistoricalServiceIdentifierArgWithValidationDisabled( | ||
serviceIdentifierArgKey, | ||
isServiceIdentifierArgOptional, | ||
isServiceIdentifierArgGreedy, | ||
), | ||
{ | ||
Key: portIdentifierArgKey, | ||
IsOptional: isPortIdentifierArgOptional, | ||
IsGreedy: isPortIdentifierArgGreedy, | ||
}, | ||
}, | ||
RunFunc: run, | ||
} | ||
|
||
func run( | ||
ctx context.Context, | ||
kurtosisBackend backend_interface.KurtosisBackend, | ||
_ kurtosis_engine_rpc_api_bindings.EngineServiceClient, | ||
_ metrics_client.MetricsClient, | ||
flags *flags.ParsedFlags, | ||
args *args.ParsedArgs, | ||
) error { | ||
enclaveIdentifier, err := args.GetNonGreedyArg(enclaveIdentifierArgKey) | ||
if err != nil { | ||
return stacktrace.Propagate(err, "An error occurred getting the enclave identifier using arg key '%v'", enclaveIdentifierArgKey) | ||
} | ||
|
||
serviceIdentifier, err := args.GetNonGreedyArg(serviceIdentifierArgKey) | ||
if err != nil { | ||
return stacktrace.Propagate(err, "An error occurred getting the service identifier using arg key '%v'", serviceIdentifierArgKey) | ||
} | ||
|
||
portIdentifier, err := args.GetNonGreedyArg(portIdentifierArgKey) | ||
if err != nil { | ||
return stacktrace.Propagate(err, "An error occurred getting the port identifier using arg key '%v'", portIdentifier) | ||
} | ||
|
||
kurtosisCtx, err := kurtosis_context.NewKurtosisContextFromLocalEngine() | ||
if err != nil { | ||
return stacktrace.Propagate(err, "An error occurred connecting to the local Kurtosis engine") | ||
} | ||
|
||
enclaveCtx, err := kurtosisCtx.GetEnclaveContext(ctx, enclaveIdentifier) | ||
if err != nil { | ||
return stacktrace.Propagate(err, "An error occurred while getting enclave context for enclave with identifier '%v' exists", enclaveIdentifier) | ||
} | ||
|
||
serviceCtx, err := enclaveCtx.GetServiceContext(serviceIdentifier) | ||
if err != nil { | ||
return stacktrace.Propagate(err, "An error occurred while getting service context for service with identifier '%v'", serviceIdentifier) | ||
} | ||
|
||
publicPorts := serviceCtx.GetPublicPorts() | ||
|
||
publicPort, found := publicPorts[portIdentifier] | ||
if !found { | ||
return stacktrace.NewError( | ||
fmt.Sprintf("Port Identifier: %v is not found for service: %v in enclave %v", portIdentifier, serviceIdentifier, enclaveIdentifier), | ||
) | ||
} | ||
|
||
fullUrl := fmt.Sprintf("%v:%v", ipAddress, publicPort.GetNumber()) | ||
maybeApplicationProtocol := publicPort.GetMaybeApplicationProtocol() | ||
|
||
if maybeApplicationProtocol != "" { | ||
fullUrl = fmt.Sprintf("%v://%v", maybeApplicationProtocol, fullUrl) | ||
} | ||
|
||
outputString := fmt.Sprintf("Here is the port information for port: %v for %v in %v", | ||
portIdentifier, serviceIdentifier, enclaveIdentifier) | ||
|
||
out.PrintOutLn(outputString) | ||
out.PrintOutLn(fmt.Sprintf("The url is: %v", fullUrl)) | ||
return nil | ||
} |
This file contains 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 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,12 @@ | ||
--- | ||
title: port print | ||
sidebar_label: port print | ||
slug: /port-print | ||
--- | ||
|
||
To print information about the PortSpec for Service, run: | ||
|
||
```bash | ||
kurtosis port print $THE_ENCLAVE_IDENTIFIER $THE_SERVICE_IDENTIFIER $PORT_ID | ||
``` | ||
where `$THE_ENCLAVE_IDENTIFIER` and the `$THE_SERVICE_IDENTIFIER` are [resource identifiers](../concepts-reference/resource-identifier.md) for the enclave and service, respectively. The `$PORT_ID` is the unique port identifier assigned to the port using [`ServiceConfig`](../starlark-reference/service-config.md) on starlark. |