Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Migrate start actions from CLI to agent (#3378)
* wip * chore: starting to decouple agent and cli * fix agent tests * update starter
- Loading branch information
1 parent
54bef84
commit d0e3702
Showing
17 changed files
with
547 additions
and
339 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,18 @@ | ||
package config | ||
|
||
type Mode string | ||
|
||
const ( | ||
Mode_Desktop Mode = "desktop" | ||
Mode_Verbose Mode = "verbose" | ||
) | ||
|
||
type Flags struct { | ||
Endpoint string | ||
OrganizationID string | ||
EnvironmentID string | ||
CI bool | ||
AgentApiKey string | ||
Token string | ||
Mode Mode | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,48 @@ | ||
package runner | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/kubeshop/tracetest/cli/config" | ||
"github.com/kubeshop/tracetest/cli/pkg/resourcemanager" | ||
) | ||
|
||
type environment struct { | ||
ID string `json:"id"` | ||
Name string `json:"name"` | ||
AgentApiKey string `json:"agentApiKey"` | ||
OrganizationID string `json:"organizationID"` | ||
} | ||
|
||
func (s *Runner) getEnvironment(ctx context.Context, cfg config.Config) (environment, error) { | ||
resource, err := s.resources.Get("env") | ||
if err != nil { | ||
return environment{}, err | ||
} | ||
|
||
resource = resource. | ||
WithHttpClient(config.SetupHttpClient(cfg)). | ||
WithOptions(resourcemanager.WithPrefixGetter(func() string { | ||
return fmt.Sprintf("/organizations/%s/", cfg.OrganizationID) | ||
})) | ||
|
||
resultFormat, err := resourcemanager.Formats.GetWithFallback("json", "json") | ||
if err != nil { | ||
return environment{}, err | ||
} | ||
|
||
raw, err := resource.Get(ctx, cfg.EnvironmentID, resultFormat) | ||
if err != nil { | ||
return environment{}, err | ||
} | ||
|
||
var env environment | ||
err = json.Unmarshal([]byte(raw), &env) | ||
if err != nil { | ||
return environment{}, err | ||
} | ||
|
||
return env, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package runner | ||
|
||
import ( | ||
"context" | ||
|
||
agentConfig "github.com/kubeshop/tracetest/agent/config" | ||
"github.com/kubeshop/tracetest/agent/ui" | ||
|
||
"github.com/kubeshop/tracetest/cli/config" | ||
"github.com/kubeshop/tracetest/cli/pkg/resourcemanager" | ||
) | ||
|
||
type Runner struct { | ||
configurator config.Configurator | ||
resources *resourcemanager.Registry | ||
ui ui.ConsoleUI | ||
mode agentConfig.Mode | ||
} | ||
|
||
func NewRunner(configurator config.Configurator, resources *resourcemanager.Registry, ui ui.ConsoleUI) *Runner { | ||
return &Runner{ | ||
configurator: configurator, | ||
resources: resources, | ||
ui: ui, | ||
mode: agentConfig.Mode_Desktop, | ||
} | ||
} | ||
|
||
func (s *Runner) Run(ctx context.Context, cfg config.Config, flags agentConfig.Flags) error { | ||
s.ui.Banner(config.Version) | ||
s.ui.Println(`Tracetest start launches a lightweight agent. It enables you to run tests and collect traces with Tracetest. | ||
Once started, Tracetest Agent exposes OTLP ports 4317 and 4318 to ingest traces via gRCP and HTTP.`) | ||
|
||
if flags.Token == "" || flags.AgentApiKey != "" { | ||
s.configurator = s.configurator.WithOnFinish(s.onStartAgent) | ||
} | ||
|
||
s.ui.Infof("Running in %s mode...", s.mode) | ||
|
||
s.mode = flags.Mode | ||
|
||
return s.configurator.Start(ctx, cfg, flags) | ||
} | ||
|
||
func (s *Runner) onStartAgent(ctx context.Context, cfg config.Config) { | ||
if cfg.AgentApiKey != "" { | ||
err := s.StartAgent(ctx, cfg.AgentEndpoint, cfg.AgentApiKey, cfg.UIEndpoint) | ||
if err != nil { | ||
s.ui.Error(err.Error()) | ||
} | ||
|
||
return | ||
} | ||
|
||
env, err := s.getEnvironment(ctx, cfg) | ||
if err != nil { | ||
s.ui.Error(err.Error()) | ||
} | ||
|
||
err = s.StartAgent(ctx, cfg.AgentEndpoint, env.AgentApiKey, cfg.UIEndpoint) | ||
if err != nil { | ||
s.ui.Error(err.Error()) | ||
} | ||
} | ||
|
||
func (s *Runner) StartAgent(ctx context.Context, endpoint, agentApiKey, uiEndpoint string) error { | ||
cfg, err := agentConfig.LoadConfig() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if endpoint != "" { | ||
cfg.ServerURL = endpoint | ||
} | ||
|
||
if agentApiKey != "" { | ||
cfg.APIKey = agentApiKey | ||
} | ||
|
||
if s.mode == agentConfig.Mode_Desktop { | ||
return RunDesktopStrategy(ctx, cfg, s.ui, uiEndpoint) | ||
} | ||
|
||
// TODO: Add verbose strategy | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package runner | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
|
||
agentConfig "github.com/kubeshop/tracetest/agent/config" | ||
"github.com/kubeshop/tracetest/cli/config" | ||
|
||
consoleUI "github.com/kubeshop/tracetest/agent/ui" | ||
) | ||
|
||
func RunDesktopStrategy(ctx context.Context, cfg agentConfig.Config, ui consoleUI.ConsoleUI, uiEndpoint string) error { | ||
ui.Infof("Starting Agent with name %s...", cfg.Name) | ||
|
||
isStarted := false | ||
session := &Session{} | ||
|
||
var err error | ||
|
||
for !isStarted { | ||
session, err = StartSession(ctx, cfg) | ||
if err != nil && errors.Is(err, ErrOtlpServerStart) { | ||
ui.Error("Tracetest Agent binds to the OpenTelemetry ports 4317 and 4318 which are used to receive trace information from your system. The agent tried to bind to these ports, but failed.") | ||
shouldRetry := ui.Enter("Please stop the process currently listening on these ports and press enter to try again.") | ||
|
||
if !shouldRetry { | ||
ui.Finish() | ||
return err | ||
} | ||
|
||
continue | ||
} | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
isStarted = true | ||
} | ||
|
||
claims, err := config.GetTokenClaims(session.Token) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
isOpen := true | ||
message := `Agent is started! Leave the terminal open so tests can be run and traces gathered from this environment. | ||
You can` | ||
options := []consoleUI.Option{{ | ||
Text: "Open Tracetest in a browser to this environment", | ||
Fn: func(_ consoleUI.ConsoleUI) { | ||
ui.OpenBrowser(fmt.Sprintf("%sorganizations/%s/environments/%s/dashboard", uiEndpoint, claims["organization_id"], claims["environment_id"])) | ||
}, | ||
}, { | ||
Text: "Stop this agent", | ||
Fn: func(_ consoleUI.ConsoleUI) { | ||
isOpen = false | ||
session.Close() | ||
ui.Finish() | ||
}, | ||
}} | ||
|
||
for isOpen { | ||
selected := ui.Select(message, options, 0) | ||
selected.Fn(ui) | ||
} | ||
return nil | ||
} |
Oops, something went wrong.