Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions modules/gitops/gitops.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func ModuleInit(reg registry.ModuleRegistrar) {
reg.RegisterBodyFn(updateAppProjectMappingBodyFnID, updateAppProjectMappingBody)
reg.RegisterBodyFn(importAgentBodyFnID, gitopsAgentImportBody)
reg.RegisterTextFormatter(importAgentSummaryFormatterID, formatGitopsAgentImportSummary)
reg.RegisterQueryParamsFn(metricsRangeFnID, metricsRangeParams)
}

func executeGitopsAgentImport(ctx *cmdctx.Ctx) error {
Expand Down
47 changes: 47 additions & 0 deletions modules/gitops/metrics_range.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright © 2026 Harness Inc.
// SPDX-License-Identifier: Apache-2.0

package gitops

import (
"fmt"
"time"

"github.com/harness/cli/v3/pkg/cmdctx"
)

const (
metricsRangeFnID = "gitops_agent_metrics_range"
metricsMaxRange = 14 * 24 * time.Hour
)

// The API accepts an inverted or over-long range and answers with an empty result, so reject it here.
func metricsRangeParams(ctx *cmdctx.Ctx) (map[string]string, error) {
start, err := parseMetricsTime(ctx, "start-time")
if err != nil {
return nil, err
}
end, err := parseMetricsTime(ctx, "end-time")
if err != nil {
return nil, err
}
if start.After(end) {
return nil, fmt.Errorf("--start-time is after --end-time")
}
if end.Sub(start) > metricsMaxRange {
return nil, fmt.Errorf("range is over 14 days (the metrics retention window); use a shorter range")
}
return map[string]string{
"startTime": start.UTC().Format(time.RFC3339),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deep Code Review Agent🐛

metricsRangeParams validates timestamps with fractional seconds but drops the fraction when building startTime and endTime. A valid range entirely within one second is sent as two identical timestamps, so the API does not receive the window the CLI validated.

Severity: medium


🤖 Was this useful? React with 👍 or 👎

"endTime": end.UTC().Format(time.RFC3339),
}, nil
}

func parseMetricsTime(ctx *cmdctx.Ctx, flag string) (time.Time, error) {
raw := cmdctx.GetString(ctx.FlagValues, flag)
t, err := time.Parse(time.RFC3339, raw)
if err != nil {
return time.Time{}, fmt.Errorf("--%s %q is not an RFC3339 time, e.g. 2026-11-18T00:00:00Z", flag, raw)
}
return t, nil
}
41 changes: 41 additions & 0 deletions modules/gitops/metrics_range_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright © 2026 Harness Inc.
// SPDX-License-Identifier: Apache-2.0

package gitops

import (
"strings"
"testing"

"github.com/harness/cli/v3/pkg/cmdctx"
)

func TestMetricsRangeParams(t *testing.T) {
cases := []struct {
name, start, end, wantErr string
}{
{"valid", "2026-11-18T00:00:00Z", "2026-11-18T06:00:00Z", ""},
{"exactly 14 days", "2026-11-01T00:00:00Z", "2026-11-15T00:00:00Z", ""},
{"start after end", "2026-11-18T06:00:00Z", "2026-11-18T00:00:00Z", "after"},
{"over 14 days", "2026-11-01T00:00:00Z", "2026-11-15T00:00:01Z", "14 days"},
{"bad format", "yesterday", "2026-11-18T00:00:00Z", "RFC3339"},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
ctx := &cmdctx.Ctx{FlagValues: map[string]any{"start-time": c.start, "end-time": c.end}}
got, err := metricsRangeParams(ctx)
if c.wantErr != "" {
if err == nil || !strings.Contains(err.Error(), c.wantErr) {
t.Fatalf("err = %v, want containing %q", err, c.wantErr)
}
return
}
if err != nil {
t.Fatal(err)
}
if got["startTime"] != c.start || got["endTime"] != c.end {
t.Fatalf("got %v", got)
}
})
}
}
246 changes: 246 additions & 0 deletions pkg/spec/gitops.spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,95 @@ nouns:
- id: created
expr: it.createdAt

- noun: gitops_tree
noun_aliases: [gitops_trees]
short_desc: "Kubernetes resource tree of a GitOps application."
fields:
- id: response
expr: it

- noun: gitops_event
noun_aliases: [gitops_events]
short_desc: "Events of a GitOps application."
fields:
- id: response
expr: it
- id: type
expr: it.type
- id: reason
expr: it.reason
- id: object
expr: (it.involvedObject.kind ?? "") + "/" + (it.involvedObject.name ?? "")
- id: message
expr: it.message
width_max: 60
- id: count
expr: it.count
- id: last_seen
expr: it.lastTimestamp

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deep Code Review Agent🐛

gitops_event.last_seen reads only the legacy lastTimestamp field, so events that report their observation time through series.lastObservedTime or eventTime can show a blank last_seen even though the API response includes the time.

Severity: low


🤖 Was this useful? React with 👍 or 👎


- noun: gitops_log
noun_aliases: [gitops_logs]
short_desc: "Pod logs of a GitOps application."
fields:
- id: response
expr: it

- noun: gitops_resource
noun_aliases: [gitops_resources]
short_desc: "Managed resources of a GitOps application."
fields:
- id: response
expr: it
- id: kind
expr: it.kind
- id: namespace
expr: it.namespace
- id: name
expr: it.name
- id: group
expr: it.group
- id: hook
expr: it.hook

- noun: gitops_action
noun_aliases: [gitops_actions]
short_desc: "Actions available on a resource of a GitOps application."
fields:
- id: response
expr: it
- id: name
expr: it.name
- id: disabled
expr: it.disabled ?? false
- id: params
expr: join(map(it.params ?? [], {#.name}), ",")

- noun: gitops_metrics
noun_aliases: [gitops_metric]
short_desc: "Metric samples of a GitOps agent."
fields:
- id: response
expr: it

- noun: gitops_dashboard
noun_aliases: [gitops_dashboards]
short_desc: "GitOps dashboard overview: counts of applications, clusters and repositories."
fields:
- id: applications
expr: it.applicationCount
- id: healthy
expr: it.applicationStatusCounts.healthStatusCounts.healthy ?? 0
- id: synced
expr: it.applicationStatusCounts.syncStatusCounts.synced ?? 0
- id: out_of_sync
label: Out of Sync
expr: it.applicationStatusCounts.syncStatusCounts.outOfSync ?? 0
- id: clusters
expr: it.clusterCount
- id: repositories
expr: it.repositoryCount

commands:
- command: list gitops_agent
verb: list
Expand Down Expand Up @@ -1233,3 +1322,160 @@ commands:
projectIdentifier: 'ctx.level == "project" ? (auth.project != "" ? auth.project : nil) : nil'
no_fields: true
text_header: "\nUpdated mapping {{ctx.idParts[1]}} on agent {{ctx.idParts[0]}}\n"

- command: get gitops_tree
verb: get
noun: gitops_tree
short: "Get the Kubernetes resource tree of an application: harness get gitops_tree <agent/app> [--app-namespace <ns>]"
handler_type: endpoint
id_parts: 2

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deep Code Review Agent🐛

get gitops_app_resource_tree cannot target applications stored as namespace/app: id_parts: 2 rejects agent/namespace/app, and the command does not expose the API’s separate application-namespace parameter. Other locations where this applies: pkg/spec/gitops.spec.yaml:1307, :1319, :1345, :1357.

Severity: medium


🤖 Was this useful? React with 👍 or 👎

id_label: "<agent/app>"
flags:
- name: app-namespace
description: "Namespace of the application, for applications stored as namespace/app"
endpoint:
path: /gitops/api/v1/agents/{{ctx.idParts[0]}}/applications/{{ctx.idParts[1]}}/resource-tree
item_expr: it
yaml_pick_expr: it
query_params:
query.appNamespace: 'flags["app-namespace"] != "" ? flags["app-namespace"] : nil'

- command: list gitops_event
verb: list
noun: gitops_event
short: "List events of an application: harness list gitops_event <agent/app> [--app-namespace <ns>]"
handler_type: endpoint
requires_parentid: true
parentid_label: "<agent/app>"
id_parts: 2
flags:
- name: app-namespace
description: "Namespace of the application, for applications stored as namespace/app"
endpoint:
path: /gitops/api/v1/agents/{{ctx.parentIdParts[0]}}/applications/{{ctx.parentIdParts[1]}}/events
items_expr: it.items ?? []
get_id_expr: "-"
paging:
paging_strategy: flat_list
columns: [type, reason, object, message, count, last_seen]
query_params:
query.appNamespace: 'flags["app-namespace"] != "" ? flags["app-namespace"] : nil'

- command: get gitops_log
verb: get
noun: gitops_log
short: "Get pod logs of an application: harness get gitops_log <agent/app> [--pod-name ...] [--app-namespace <ns>]"
handler_type: endpoint

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deep Code Review Agent🐛

get gitops_pod_log uses the generic endpoint handler for the streaming PodLogs API. That handler reads the whole body and unmarshals it as one JSON value (pkg/client/client.go:268–291), so a successful response with multiple log entries fails as malformed API response before any output can be rendered.

Severity: high


🤖 Was this useful? React with 👍 or 👎

id_parts: 2
id_label: "<agent/app>"
flags:
- name: pod-name
description: "Pod name"
- name: namespace
description: "Kubernetes namespace"
- name: container
description: "Container name"
- name: tail-lines
description: "Number of log lines to return"
- name: app-namespace
description: "Namespace of the application, for applications stored as namespace/app"
endpoint:
path: /gitops/api/v1/agents/{{ctx.idParts[0]}}/applications/{{ctx.idParts[1]}}/logs
item_expr: it
yaml_pick_expr: it
query_params:
query.podName: 'flags["pod-name"] != "" ? flags["pod-name"] : nil'
query.namespace: 'flags.namespace != "" ? flags.namespace : nil'
query.container: 'flags.container != "" ? flags.container : nil'
query.tailLines: 'flags["tail-lines"] != "" ? flags["tail-lines"] : nil'
query.appNamespace: 'flags["app-namespace"] != "" ? flags["app-namespace"] : nil'

- command: list gitops_resource
verb: list
noun: gitops_resource
short: "List managed resources of an application: harness list gitops_resource <agent/app> [--app-namespace <ns>]"
handler_type: endpoint
requires_parentid: true
parentid_label: "<agent/app>"
id_parts: 2
flags:
- name: app-namespace
description: "Namespace of the application, for applications stored as namespace/app"
endpoint:
path: /gitops/api/v1/agents/{{ctx.parentIdParts[0]}}/applications/{{ctx.parentIdParts[1]}}/managed-resources
items_expr: it.items ?? []
get_id_expr: "-"
paging:
paging_strategy: flat_list
columns: [kind, namespace, name, group, hook]
query_params:
query.appNamespace: 'flags["app-namespace"] != "" ? flags["app-namespace"] : nil'

- command: list gitops_action
verb: list
noun: gitops_action
short: "List actions available on a resource: harness list gitops_action <agent/app> --namespace <ns> --resource-name <name> --kind <kind> [--app-namespace <ns>]"
handler_type: endpoint
requires_parentid: true
parentid_label: "<agent/app>"
id_parts: 2
flags:
- name: namespace
description: "Kubernetes namespace of the resource"
- name: resource-name
description: "Resource name"
- name: kind
description: "Resource kind, e.g. Deployment"
- name: group
description: "API group, e.g. apps"
- name: version
description: "API version, e.g. v1"
- name: app-namespace
description: "Namespace of the application, for applications stored as namespace/app"
endpoint:
path: /gitops/api/v1/agents/{{ctx.parentIdParts[0]}}/applications/{{ctx.parentIdParts[1]}}/resource/actions
items_expr: it.actions ?? []
get_id_expr: "-"
paging:
paging_strategy: flat_list
columns: [name, disabled, params]
query_params:
request.namespace: 'flags.namespace != "" ? flags.namespace : nil'
request.resourceName: 'flags["resource-name"] != "" ? flags["resource-name"] : nil'
request.kind: 'flags.kind != "" ? flags.kind : nil'
request.group: 'flags.group != "" ? flags.group : nil'
request.version: 'flags.version != "" ? flags.version : nil'
request.appNamespace: 'flags["app-namespace"] != "" ? flags["app-namespace"] : nil'

- command: get gitops_metrics
verb: get
noun: gitops_metrics
short: "Get metric samples of an agent: harness get gitops_metrics <agent> --start-time <RFC3339> --end-time <RFC3339>"
handler_type: endpoint
id_label: "<agent>"
flags:
- name: start-time
description: "Range start, RFC3339 UTC"
required: true
- name: end-time
description: "Range end, RFC3339 UTC (at most 14 days after start)"
required: true
- name: source
description: "Restrict to one metrics source"
endpoint:
path: /gitops/api/v1/agents/{{ctx.id}}/metrics
item_expr: it
yaml_pick_expr: it
query_params_fn: gitops_agent_metrics_range
query_params:
source: 'flags.source != "" ? flags.source : nil'

- command: get gitops_dashboard
verb: get
noun: gitops_dashboard
short: "Get the GitOps dashboard overview: harness get gitops_dashboard"
handler_type: endpoint
no_id: true
endpoint:
path: /gitops/api/v1/dashboard/overview
item_expr: it
Loading