Skip to content

Commit 4306349

Browse files
authored
feat(agents): support int and float cpu cfg values (#548)
1 parent 4775f23 commit 4306349

File tree

3 files changed

+41
-12
lines changed

3 files changed

+41
-12
lines changed

cmd/lk/agent.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ var (
110110
Before: createAgentClient,
111111
Action: createAgentConfig,
112112
Flags: []cli.Flag{
113-
nameFlag(true),
113+
nameFlag(false),
114114
tomlFlag,
115115
},
116116
ArgsUsage: "[working-dir]",
@@ -388,7 +388,7 @@ func createAgent(ctx context.Context, cmd *cli.Command) error {
388388
Secrets: secrets,
389389
Replicas: int32(agentConfig.Replicas),
390390
MaxReplicas: int32(agentConfig.MaxReplicas),
391-
CpuReq: agentConfig.CPU,
391+
CpuReq: string(agentConfig.CPU),
392392
}
393393

394394
resp, err := agentsClient.CreateAgent(ctx, req)
@@ -455,8 +455,21 @@ func createAgentConfig(ctx context.Context, cmd *cli.Command) error {
455455
}
456456
}
457457

458+
name := cmd.String("name")
459+
if name == "" {
460+
if err := huh.NewInput().
461+
Title("Agent name").
462+
Value(&name).
463+
WithTheme(util.Theme).
464+
Run(); err != nil {
465+
return err
466+
} else if name == "" {
467+
return fmt.Errorf("name is required")
468+
}
469+
}
470+
458471
response, err := agentsClient.ListAgents(ctx, &lkproto.ListAgentsRequest{
459-
AgentName: cmd.String("name"),
472+
AgentName: name,
460473
})
461474
if err != nil {
462475
if twerr, ok := err.(twirp.Error); ok {
@@ -481,7 +494,7 @@ func createAgentConfig(ctx context.Context, cmd *cli.Command) error {
481494
agentConfig := &agentfs.AgentTOML{
482495
ProjectSubdomain: matches[1],
483496
Name: agent.AgentName,
484-
CPU: regionAgent.CpuReq,
497+
CPU: agentfs.CPUString(regionAgent.CpuReq),
485498
Replicas: int(regionAgent.Replicas),
486499
MaxReplicas: int(regionAgent.MaxReplicas),
487500
}
@@ -518,7 +531,7 @@ func deployAgent(ctx context.Context, cmd *cli.Command) error {
518531
AgentName: agentConfig.Name,
519532
Secrets: secrets,
520533
Replicas: int32(agentConfig.Replicas),
521-
CpuReq: agentConfig.CPU,
534+
CpuReq: string(agentConfig.CPU),
522535
MaxReplicas: int32(agentConfig.MaxReplicas),
523536
}
524537

@@ -623,7 +636,7 @@ func updateAgent(ctx context.Context, cmd *cli.Command) error {
623636
req := &lkproto.UpdateAgentRequest{
624637
AgentName: agentConfig.Name,
625638
Replicas: int32(agentConfig.Replicas),
626-
CpuReq: agentConfig.CPU,
639+
CpuReq: string(agentConfig.CPU),
627640
MaxReplicas: int32(agentConfig.MaxReplicas),
628641
}
629642

cmd/lk/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ func main() {
5858
}
5959

6060
app.Commands = append(app.Commands, AppCommands...)
61+
app.Commands = append(app.Commands, AgentCommands...)
6162
app.Commands = append(app.Commands, CloudCommands...)
6263
app.Commands = append(app.Commands, ProjectCommands...)
6364
app.Commands = append(app.Commands, RoomCommands...)
@@ -69,7 +70,6 @@ func main() {
6970
app.Commands = append(app.Commands, SIPCommands...)
7071
app.Commands = append(app.Commands, ReplayCommands...)
7172
app.Commands = append(app.Commands, LoadTestCommands...)
72-
app.Commands = append(app.Commands, AgentCommands...)
7373

7474
// Register cleanup hook for SIGINT, SIGTERM, SIGQUIT
7575
ctx, stop := signal.NotifyContext(

pkg/agentfs/config-file.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,31 @@ import (
2525
)
2626

2727
type AgentTOML struct {
28-
ProjectSubdomain string `toml:"project_subdomain"`
29-
Name string `toml:"name"`
30-
CPU string `toml:"cpu"`
31-
Replicas int `toml:"replicas"`
32-
MaxReplicas int `toml:"max_replicas"`
28+
ProjectSubdomain string `toml:"project_subdomain"`
29+
Name string `toml:"name"`
30+
CPU CPUString `toml:"cpu"`
31+
Replicas int `toml:"replicas"`
32+
MaxReplicas int `toml:"max_replicas"`
3333

3434
Regions []string `toml:"regions"`
3535
}
3636

37+
type CPUString string
38+
39+
func (c *CPUString) UnmarshalTOML(v interface{}) error {
40+
switch value := v.(type) {
41+
case int64:
42+
*c = CPUString(fmt.Sprintf("%d", value))
43+
case float64:
44+
*c = CPUString(fmt.Sprintf("%g", value))
45+
case string:
46+
*c = CPUString(value)
47+
default:
48+
return fmt.Errorf("invalid type for cpu: %T", v)
49+
}
50+
return nil
51+
}
52+
3753
const (
3854
AgentTOMLFile = "livekit.toml"
3955
)

0 commit comments

Comments
 (0)