Skip to content
22 changes: 13 additions & 9 deletions cmd/lk/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ On LiveKit Cloud: "create" and "deploy" ship it, then "status", "logs",
},
}
subdomainPattern = regexp.MustCompile(`^(?:https?|wss?)://([^.]+)\.`)
agentsClient *cloudagents.Client
agentsClient agentAPI
ignoredSecrets = []string{
"LIVEKIT_API_KEY",
"LIVEKIT_API_SECRET",
Expand All @@ -552,7 +552,7 @@ func createAgentClient(ctx context.Context, cmd *cli.Command) (context.Context,
if _, err := requireProjectForSDK(ctx, cmd); err != nil {
return ctx, err
}
return createAgentClientForDir(ctx)
return createAgentClientForDir(ctx, cmd)
}

// createInitAgentClient is the Before of `agent init`, whose argument names
Expand All @@ -568,12 +568,13 @@ func createInitAgentClient(ctx context.Context, cmd *cli.Command) (context.Conte
workingDir = cmd.Args().First()
lkConfig = nil
}
return createAgentClientForDir(ctx)
return createAgentClientForDir(ctx, cmd)
}

// createAgentClientForDir creates agentsClient for the resolved project,
// once it's checked against the livekit.toml in workingDir, if any.
func createAgentClientForDir(ctx context.Context) (context.Context, error) {
// createAgentClientForDir creates agentsClient and privateLinksClient for the
// resolved project, once it's checked against the livekit.toml in workingDir,
// if any. cmd picks their backend (see newAgentAPI).
func createAgentClientForDir(ctx context.Context, cmd *cli.Command) (context.Context, error) {
// If a project has been manually selected that conflicts with the agent's config,
// or if the config file is malformed, this is an error. If the config does not exist,
// we assume it gets created later.
Expand All @@ -591,7 +592,7 @@ func createAgentClientForDir(ctx context.Context) (context.Context, error) {
}
}

agentsClient, err = newAgentsClient(project)
agentsClient, privateLinksClient, err = newAgentAPI(cmd, project)
if err != nil {
return ctx, err
}
Expand Down Expand Up @@ -709,7 +710,7 @@ func createAgent(ctx context.Context, cmd *cli.Command) error {
(&resolvedProject{project: project, source: sourceSelected}).announce()
var err error
// Recreate the client with the new project
agentsClient, err = newAgentsClient(project)
agentsClient, privateLinksClient, err = newAgentAPI(cmd, project)
if err != nil {
return err
}
Expand Down Expand Up @@ -852,6 +853,9 @@ func createAgent(ctx context.Context, cmd *cli.Command) error {
).Run(); err != nil {
return err
} else if viewLogs {
if len(resp.ServerRegions) == 0 {
return fmt.Errorf("agent [%s] was created, but no region was returned to stream its logs from; view them with `lk agent logs`", resp.AgentId)
}
out.Status("Tailing runtime logs...safe to exit at any time")
return agentsClient.StreamLogs(ctx, "deploy", lkConfig.Agent.ID, "", os.Stdout, resp.ServerRegions[0])
}
Expand Down Expand Up @@ -1357,7 +1361,7 @@ func getLogs(ctx context.Context, cmd *cli.Command) error {
return err
}

if len(response.Agents) == 0 {
if len(response.Agents) == 0 || len(response.Agents[0].AgentDeployments) == 0 {
return fmt.Errorf("no agent deployments found")
}

Expand Down
13 changes: 8 additions & 5 deletions cmd/lk/agent_private_link.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import (
"github.com/urfave/cli/v3"
)

// privateLinksClient is set, with agentsClient, by createAgentClient.
var privateLinksClient privateLinkAPI

var privateLinkCommands = &cli.Command{
Name: "private-link",
Usage: "Manage private links for agents",
Expand Down Expand Up @@ -197,7 +200,7 @@ func createPrivateLink(ctx context.Context, cmd *cli.Command) error {
}

req := buildCreatePrivateLinkRequest(cmd.String("name"), region, uint32(cmd.Uint("port")), cmd.String("endpoint"), cmd.String("cloud-region"))
resp, err := agentsClient.CreatePrivateLink(ctx, req)
resp, err := privateLinksClient.CreatePrivateLink(ctx, req)
if err != nil {
return formatPrivateLinkClientError("create", err)
}
Expand Down Expand Up @@ -226,7 +229,7 @@ func createPrivateLink(ctx context.Context, cmd *cli.Command) error {
}

func listPrivateLinks(ctx context.Context, cmd *cli.Command) error {
resp, err := agentsClient.ListPrivateLinks(ctx, &lkproto.ListPrivateLinksRequest{})
resp, err := privateLinksClient.ListPrivateLinks(ctx, &lkproto.ListPrivateLinksRequest{})
if err != nil {
return formatPrivateLinkClientError("list", err)
}
Expand All @@ -237,7 +240,7 @@ func listPrivateLinks(ctx context.Context, cmd *cli.Command) error {
if link == nil || link.PrivateLinkId == "" {
continue
}
health, healthErr := agentsClient.GetPrivateLinkStatus(ctx, &lkproto.GetPrivateLinkStatusRequest{
health, healthErr := privateLinksClient.GetPrivateLinkStatus(ctx, &lkproto.GetPrivateLinkStatusRequest{
PrivateLinkId: link.PrivateLinkId,
})
if healthErr != nil {
Expand Down Expand Up @@ -286,7 +289,7 @@ func listPrivateLinks(ctx context.Context, cmd *cli.Command) error {

func deletePrivateLink(ctx context.Context, cmd *cli.Command) error {
privateLinkID := cmd.String("id")
resp, err := agentsClient.DestroyPrivateLink(ctx, &lkproto.DestroyPrivateLinkRequest{
resp, err := privateLinksClient.DestroyPrivateLink(ctx, &lkproto.DestroyPrivateLinkRequest{
PrivateLinkId: privateLinkID,
})
if err != nil {
Expand All @@ -303,7 +306,7 @@ func deletePrivateLink(ctx context.Context, cmd *cli.Command) error {

func getPrivateLinkHealthStatus(ctx context.Context, cmd *cli.Command) error {
privateLinkID := cmd.String("id")
resp, err := agentsClient.GetPrivateLinkStatus(ctx, &lkproto.GetPrivateLinkStatusRequest{
resp, err := privateLinksClient.GetPrivateLinkStatus(ctx, &lkproto.GetPrivateLinkStatusRequest{
PrivateLinkId: privateLinkID,
})
if err != nil {
Expand Down
57 changes: 57 additions & 0 deletions cmd/lk/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -648,3 +648,60 @@ func TestAgentDeployRegistersIDFlag(t *testing.T) {
}
require.Contains(t, names, "id", "'agent deploy' must register the --id flag")
}

// fakeAgentLogs answers the calls `lk agent logs` makes.
type fakeAgentLogs struct {
agentAPI
agents []*lkproto.AgentInfo
streamed []string // the server region of each StreamLogs call
}

func (f *fakeAgentLogs) ListAgents(context.Context, *lkproto.ListAgentsRequest) (*lkproto.ListAgentsResponse, error) {
return &lkproto.ListAgentsResponse{Agents: f.agents}, nil
}

func (f *fakeAgentLogs) StreamLogs(_ context.Context, _, _, _ string, _ io.Writer, serverRegion string) error {
f.streamed = append(f.streamed, serverRegion)
return nil
}

// `lk agent logs` streams from the region of the agent's first deployment. An
// agent with no deployment, which has no region to stream from, is an error,
// as no agent is.
func TestGetLogsNeedsADeployment(t *testing.T) {
for _, tc := range []struct {
name string
agents []*lkproto.AgentInfo
wantErr string
want []string
}{
{name: "no agent", wantErr: "no agent deployments found"},
{name: "no deployment", agents: []*lkproto.AgentInfo{{AgentId: "CA_1"}}, wantErr: "no agent deployments found"},
{name: "deployed", agents: []*lkproto.AgentInfo{{AgentId: "CA_1", AgentDeployments: []*lkproto.AgentDeployment{{ServerRegion: "osbxash1a"}}}},
want: []string{"osbxash1a"}},
} {
t.Run(tc.name, func(t *testing.T) {
fake := &fakeAgentLogs{agents: tc.agents}
prev := agentsClient
t.Cleanup(func() { agentsClient = prev })
agentsClient = fake

app := &cli.Command{
Name: "logs",
Flags: []cli.Flag{
&cli.StringFlag{Name: "id"},
&cli.StringFlag{Name: "log-type"},
&cli.StringFlag{Name: "deployment"},
},
Action: getLogs,
}
err := app.Run(context.Background(), []string{"logs", "--id", "CA_1"})
if tc.wantErr != "" {
require.EqualError(t, err, tc.wantErr)
} else {
require.NoError(t, err)
}
require.Equal(t, tc.want, fake.streamed)
})
}
}
Loading
Loading