Skip to content

Commit d6d3660

Browse files
fix(version): print out version regardless of port-forwarding; pass through contexts (#980)
This PR prints out the `kagent` version even if a port-forwarding to the `kagent` server fails. In case the port-forwarding does not succeed, the unavailable versions will be marked as `<unknown>`: Fixes: #868 Related to: #879 ```sh > go run cli/cmd/kagent/main.go version Error starting port-forward: failed to establish connection to kagent-controller. error connecting to server. Please run 'install' command first {"backend_version":"unknown","build_date":"unknown","git_commit":"none","kagent_version":"dev"} ``` ~~**Note:** We might can consider removing the error message as it breaks the `json` output ...~~ Furthermore: - This PR uses `cmd.Context()` consistently throughout all `kagent` subcommands. - It adds a `help` Makefile target for a neat list of available Makefile targets ```sh > make controller-gen Download controller-gen locally if necessary. envtest Download setup-envtest locally if necessary. fmt Run go fmt against code. generate Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations. golangci-lint Download golangci-lint locally if necessary. help list makefile targets lint-config Verify golangci-lint linter configuration lint-fix Run golangci-lint linter and perform fixes lint Run golangci-lint linter manifests Generate ClusterRole and CustomResourceDefinition objects. run Run a controller from your host. setup-envtest Download the binaries required for ENVTEST in the local bin directory. vet Run go vet against code. ``` Signed-off-by: Tom Morelly <[email protected]>
1 parent 9a74b73 commit d6d3660

File tree

4 files changed

+31
-30
lines changed

4 files changed

+31
-30
lines changed

go/Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ else
1313
GOBIN=$(shell go env GOBIN)
1414
endif
1515

16+
default: help
17+
18+
.PHONY: help
19+
help: ## list makefile targets
20+
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
21+
1622
##@ Development
1723

1824
.PHONY: manifests

go/cli/cmd/kagent/main.go

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ func main() {
3838
Use: "kagent",
3939
Short: "kagent is a CLI and TUI for kagent",
4040
Long: "kagent is a CLI and TUI for kagent",
41-
Run: func(cmd *cobra.Command, args []string) {
42-
runInteractive()
43-
},
41+
Run: runInteractive,
4442
}
4543

4644
rootCmd.PersistentFlags().StringVar(&cfg.KAgentURL, "kagent-url", "http://localhost:8083", "KAgent URL")
@@ -101,8 +99,8 @@ func main() {
10199
Short: "Generate a bug report",
102100
Long: `Generate a bug report`,
103101
Run: func(cmd *cobra.Command, args []string) {
104-
if err := cli.CheckServerConnection(cfg.Client()); err != nil {
105-
pf, err := cli.NewPortForward(ctx, cfg)
102+
if err := cli.CheckServerConnection(cmd.Context(), cfg.Client()); err != nil {
103+
pf, err := cli.NewPortForward(cmd.Context(), cfg)
106104
if err != nil {
107105
fmt.Fprintf(os.Stderr, "Error starting port-forward: %v\n", err)
108106
return
@@ -118,15 +116,15 @@ func main() {
118116
Short: "Print the kagent version",
119117
Long: `Print the kagent version`,
120118
Run: func(cmd *cobra.Command, args []string) {
121-
if err := cli.CheckServerConnection(cfg.Client()); err != nil {
122-
pf, err := cli.NewPortForward(ctx, cfg)
123-
if err != nil {
124-
fmt.Fprintf(os.Stderr, "Error starting port-forward: %v\n", err)
125-
return
126-
}
119+
// print out kagent CLI version regardless if a port-forward to kagent server succeeds
120+
// versions unable to obtain from the remote kagent will be reported as "unknown"
121+
defer cli.VersionCmd(cfg)
122+
123+
if err := cli.CheckServerConnection(cmd.Context(), cfg.Client()); err != nil {
124+
// silently error if kagent server is not reachable
125+
pf, _ := cli.NewPortForward(cmd.Context(), cfg)
127126
defer pf.Stop()
128127
}
129-
cli.VersionCmd(cfg)
130128
},
131129
}
132130

@@ -135,7 +133,7 @@ func main() {
135133
Short: "Open the kagent dashboard",
136134
Long: `Open the kagent dashboard`,
137135
Run: func(cmd *cobra.Command, args []string) {
138-
cli.DashboardCmd(ctx, cfg)
136+
cli.DashboardCmd(cmd.Context(), cfg)
139137
},
140138
}
141139

@@ -155,8 +153,8 @@ func main() {
155153
Short: "Get a session or list all sessions",
156154
Long: `Get a session by ID or list all sessions`,
157155
Run: func(cmd *cobra.Command, args []string) {
158-
if err := cli.CheckServerConnection(cfg.Client()); err != nil {
159-
pf, err := cli.NewPortForward(ctx, cfg)
156+
if err := cli.CheckServerConnection(cmd.Context(), cfg.Client()); err != nil {
157+
pf, err := cli.NewPortForward(cmd.Context(), cfg)
160158
if err != nil {
161159
fmt.Fprintf(os.Stderr, "Error starting port-forward: %v\n", err)
162160
return
@@ -176,8 +174,8 @@ func main() {
176174
Short: "Get an agent or list all agents",
177175
Long: `Get an agent by name or list all agents`,
178176
Run: func(cmd *cobra.Command, args []string) {
179-
if err := cli.CheckServerConnection(cfg.Client()); err != nil {
180-
pf, err := cli.NewPortForward(ctx, cfg)
177+
if err := cli.CheckServerConnection(cmd.Context(), cfg.Client()); err != nil {
178+
pf, err := cli.NewPortForward(cmd.Context(), cfg)
181179
if err != nil {
182180
return
183181
}
@@ -196,8 +194,8 @@ func main() {
196194
Short: "Get tools",
197195
Long: `List all available tools`,
198196
Run: func(cmd *cobra.Command, args []string) {
199-
if err := cli.CheckServerConnection(cfg.Client()); err != nil {
200-
pf, err := cli.NewPortForward(ctx, cfg)
197+
if err := cli.CheckServerConnection(cmd.Context(), cfg.Client()); err != nil {
198+
pf, err := cli.NewPortForward(cmd.Context(), cfg)
201199
if err != nil {
202200
fmt.Fprintf(os.Stderr, "Error starting port-forward: %v\n", err)
203201
return
@@ -313,7 +311,7 @@ Examples:
313311
Run: func(cmd *cobra.Command, args []string) {
314312
deployCfg.ProjectDir = args[0]
315313

316-
if err := cli.DeployCmd(ctx, deployCfg); err != nil {
314+
if err := cli.DeployCmd(cmd.Context(), deployCfg); err != nil {
317315
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
318316
os.Exit(1)
319317
}
@@ -343,21 +341,19 @@ Examples:
343341

344342
}
345343

346-
func runInteractive() {
344+
func runInteractive(cmd *cobra.Command, args []string) {
347345
cfg, err := config.Get()
348346
if err != nil {
349347
fmt.Fprintf(os.Stderr, "Error getting config: %v\n", err)
350348
os.Exit(1)
351349
}
352350

353351
client := cfg.Client()
354-
ctx, cancel := context.WithCancel(context.Background())
355-
defer cancel()
356352

357353
// Start port forward and ensure it is healthy.
358354
var pf *cli.PortForward
359-
if err := cli.CheckServerConnection(client); err != nil {
360-
pf, err = cli.NewPortForward(ctx, cfg)
355+
if err := cli.CheckServerConnection(cmd.Context(), client); err != nil {
356+
pf, err = cli.NewPortForward(cmd.Context(), cfg)
361357
if err != nil {
362358
fmt.Fprintf(os.Stderr, "Error starting port-forward: %v\n", err)
363359
return

go/cli/internal/cli/agent/invoke.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,9 @@ type InvokeCfg struct {
2424
}
2525

2626
func InvokeCmd(ctx context.Context, cfg *InvokeCfg) {
27-
2827
clientSet := cfg.Config.Client()
2928

30-
if err := CheckServerConnection(clientSet); err != nil {
29+
if err := CheckServerConnection(ctx, clientSet); err != nil {
3130
// If a connection does not exist, start a short-lived port-forward.
3231
pf, err := NewPortForward(ctx, cfg.Config)
3332
if err != nil {

go/cli/internal/cli/agent/utils.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ var (
1616
ErrServerConnection = fmt.Errorf("error connecting to server. Please run 'install' command first")
1717
)
1818

19-
func CheckServerConnection(client *client.ClientSet) error {
19+
func CheckServerConnection(ctx context.Context, client *client.ClientSet) error {
2020
// Only check if we have a valid client
2121
if client == nil {
2222
return ErrServerConnection
2323
}
2424

25-
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
25+
ctx, cancel := context.WithTimeout(ctx, time.Second*5)
2626
defer cancel()
2727
_, err := client.Version.GetVersion(ctx)
2828
if err != nil {
@@ -50,7 +50,7 @@ func NewPortForward(ctx context.Context, cfg *config.Config) (*PortForward, erro
5050
client := cfg.Client()
5151
var err error
5252
for i := 0; i < 10; i++ {
53-
err = CheckServerConnection(client)
53+
err = CheckServerConnection(ctx, client)
5454
if err == nil {
5555
// Connection successful, port-forward is working
5656
return &PortForward{

0 commit comments

Comments
 (0)