Skip to content

Commit

Permalink
feat: added port print functionality in cli for users to quickly chec…
Browse files Browse the repository at this point in the history
…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
Peeeekay committed Jun 27, 2023
1 parent 23b121f commit 477510b
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cli/cli/command_str_consts/command_str_consts.go
Expand Up @@ -65,6 +65,8 @@ const (
PathCmdStr = "path"
VersionCmdStr = "version"
GatewayCmdStr = "gateway"
PortCmdStr = "port"
PortPrintCmdStr = "print"
)

// TODO: added constant error message here, can we move to another file later.
Expand Down
19 changes: 19 additions & 0 deletions cli/cli/commands/port/port.go
@@ -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())
}
128 changes: 128 additions & 0 deletions cli/cli/commands/port/print/print.go
@@ -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
}
2 changes: 2 additions & 0 deletions cli/cli/commands/root.go
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/kurtosis-tech/kurtosis/cli/cli/commands/gateway"
"github.com/kurtosis-tech/kurtosis/cli/cli/commands/kurtosis_context"
"github.com/kurtosis-tech/kurtosis/cli/cli/commands/lsp"
"github.com/kurtosis-tech/kurtosis/cli/cli/commands/port"
"github.com/kurtosis-tech/kurtosis/cli/cli/commands/portal"
"github.com/kurtosis-tech/kurtosis/cli/cli/commands/run"
"github.com/kurtosis-tech/kurtosis/cli/cli/commands/service"
Expand Down Expand Up @@ -121,6 +122,7 @@ func init() {
RootCmd.AddCommand(twitter.TwitterCmd.MustGetCobraCommand())
RootCmd.AddCommand(version.VersionCmd)
RootCmd.AddCommand(lsp.NewLspCommand())
RootCmd.AddCommand(port.PortCmd)
}

// ====================================================================================================
Expand Down
12 changes: 12 additions & 0 deletions docs/docs/cli-reference/port-print.md
@@ -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.

0 comments on commit 477510b

Please sign in to comment.