Skip to content
Open
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
10 changes: 0 additions & 10 deletions api/context.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package api

import (
"context"
"fmt"
"sync"
"time"

"github.com/flanksource/commons/har"
"github.com/flanksource/commons/logger"
Expand Down Expand Up @@ -155,14 +153,6 @@ func (ctx ScrapeContext) WithScrapeConfig(scraper *v1.ScrapeConfig, plugins ...v
return ctx
}

func (ctx ScrapeContext) WithTimeout(timeout time.Duration) (c ScrapeContext, cancel context.CancelFunc, cancelTimeout context.CancelFunc) {

ctx.Context, cancelTimeout = ctx.Context.WithTimeout(ctx.Properties().Duration("scraper.timeout", 4*time.Hour))
c2, cancel := context.WithCancel(ctx.Context)
ctx.Context = ctx.Context.Wrap(c2)
return ctx, cancel, cancelTimeout
}

func (ctx ScrapeContext) WithJobHistory(jobHistory *models.JobHistory) ScrapeContext {
ctx.jobHistory = jobHistory
return ctx
Expand Down
47 changes: 47 additions & 0 deletions api/v1/timeout_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package v1

import (
"testing"
"time"
)

func TestScraperSpecTimeout(t *testing.T) {
defaultTimeout := 4 * time.Hour

t.Run("default", func(t *testing.T) {
got, err := (ScraperSpec{}).TimeoutDuration(defaultTimeout)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got != defaultTimeout {
t.Fatalf("expected %s, got %s", defaultTimeout, got)
}
})

t.Run("explicit timeout shorter than default", func(t *testing.T) {
got, err := (ScraperSpec{Timeout: "30m"}).TimeoutDuration(defaultTimeout)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got != 30*time.Minute {
t.Fatalf("expected 30m, got %s", got)
}
})

t.Run("extended duration units", func(t *testing.T) {
got, err := (ScraperSpec{Timeout: "1d"}).TimeoutDuration(defaultTimeout)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got != 24*time.Hour {
t.Fatalf("expected 24h, got %s", got)
}
})

t.Run("invalid timeout", func(t *testing.T) {
_, err := (ScraperSpec{Timeout: "soon"}).TimeoutDuration(defaultTimeout)
if err == nil {
t.Fatal("expected error")
}
})
}
21 changes: 20 additions & 1 deletion api/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"fmt"
"slices"
"strings"
"time"

"github.com/flanksource/clicky"
"github.com/flanksource/clicky/api"
"github.com/flanksource/commons/duration"
"github.com/flanksource/config-db/utils"

"github.com/google/uuid"
Expand Down Expand Up @@ -75,7 +77,11 @@ type ScraperSpec struct {
LogLevel string `json:"logLevel,omitempty" yaml:"logLevel,omitempty"`

// Schedule is a cron expression for when to run the scraper. Example: `@every 1m`, `0 */6 * * *` (every 6 hours)
Schedule string `json:"schedule,omitempty" yaml:"schedule,omitempty"`
Schedule string `json:"schedule,omitempty" yaml:"schedule,omitempty"`

// Timeout is the maximum duration for a scrape run. Uses duration strings, e.g. "30m", "1h", "1d".
Timeout string `json:"timeout,omitempty" yaml:"timeout,omitempty"`

GCP []GCP `json:"gcp,omitempty" yaml:"gcp,omitempty"`
AWS []AWS `json:"aws,omitempty" yaml:"aws,omitempty"`
File []File `json:"file,omitempty" yaml:"file,omitempty"`
Expand Down Expand Up @@ -109,6 +115,19 @@ type ScraperSpec struct {
Full bool `json:"full,omitempty"`
}

// TimeoutDuration returns the configured scrape timeout, falling back to defaultTimeout.
func (c ScraperSpec) TimeoutDuration(defaultTimeout time.Duration) (time.Duration, error) {
if c.Timeout == "" {
return defaultTimeout, nil
}

timeout, err := duration.ParseDuration(c.Timeout)
if err != nil {
return 0, fmt.Errorf("invalid scraper timeout %q: %w", c.Timeout, err)
}
return time.Duration(timeout), nil
}
Comment thread
adityathebe marked this conversation as resolved.

func (c ScraperSpec) ApplyPlugin(plugins []ScrapePluginSpec) ScraperSpec {
spec := c.DeepCopy()

Expand Down
223 changes: 219 additions & 4 deletions chart/crds/configs.flanksource.com_scrapeconfigs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3895,6 +3895,112 @@ spec:
type: object
type: object
type: object
opensearch:
properties:
digest:
type: boolean
index:
type: string
insecureSkipVerify:
type: boolean
ntlm:
type: boolean
ntlmv2:
type: boolean
password:
properties:
name:
type: string
value:
type: string
valueFrom:
properties:
configMapKeyRef:
properties:
key:
type: string
name:
type: string
required:
- key
type: object
helmRef:
properties:
key:
description: Key is a JSONPath expression
used to fetch the key from the merged
JSON.
type: string
name:
type: string
required:
- key
type: object
secretKeyRef:
properties:
key:
type: string
name:
type: string
required:
- key
type: object
serviceAccount:
description: ServiceAccount specifies the service
account whose token should be fetched
type: string
type: object
type: object
urls:
items:
type: string
minItems: 1
type: array
username:
properties:
name:
type: string
value:
type: string
valueFrom:
properties:
configMapKeyRef:
properties:
key:
type: string
name:
type: string
required:
- key
type: object
helmRef:
properties:
key:
description: Key is a JSONPath expression
used to fetch the key from the merged
JSON.
type: string
name:
type: string
required:
- key
type: object
secretKeyRef:
properties:
key:
type: string
name:
type: string
required:
- key
type: object
serviceAccount:
description: ServiceAccount specifies the service
account whose token should be fetched
type: string
type: object
type: object
type: object
serviceAccount:
description: ServiceAccount when enabled will allow access
to KUBERNETES env vars
Expand Down Expand Up @@ -5930,10 +6036,9 @@ spec:
owner:
type: string
repo:
description: Exact repository name or comma-separated
collections.MatchItems patterns (`*`, `prefix*`, `*suffix`,
`!name`). Pattern selectors discover matching non-archived
repositories for owner.
description: |-
Repo can be an exact repository name or comma-separated collections.MatchItems patterns.
Pattern selectors discover matching non-archived repositories for Owner.
type: string
required:
- owner
Expand Down Expand Up @@ -11933,6 +12038,112 @@ spec:
type: object
type: object
type: object
opensearch:
properties:
digest:
type: boolean
index:
type: string
insecureSkipVerify:
type: boolean
ntlm:
type: boolean
ntlmv2:
type: boolean
password:
properties:
name:
type: string
value:
type: string
valueFrom:
properties:
configMapKeyRef:
properties:
key:
type: string
name:
type: string
required:
- key
type: object
helmRef:
properties:
key:
description: Key is a JSONPath expression
used to fetch the key from the merged
JSON.
type: string
name:
type: string
required:
- key
type: object
secretKeyRef:
properties:
key:
type: string
name:
type: string
required:
- key
type: object
serviceAccount:
description: ServiceAccount specifies the service
account whose token should be fetched
type: string
type: object
type: object
urls:
items:
type: string
minItems: 1
type: array
username:
properties:
name:
type: string
value:
type: string
valueFrom:
properties:
configMapKeyRef:
properties:
key:
type: string
name:
type: string
required:
- key
type: object
helmRef:
properties:
key:
description: Key is a JSONPath expression
used to fetch the key from the merged
JSON.
type: string
name:
type: string
required:
- key
type: object
secretKeyRef:
properties:
key:
type: string
name:
type: string
required:
- key
type: object
serviceAccount:
description: ServiceAccount specifies the service
account whose token should be fetched
type: string
type: object
type: object
type: object
serviceAccount:
description: ServiceAccount when enabled will allow access
to KUBERNETES env vars
Expand Down Expand Up @@ -16353,6 +16564,10 @@ spec:
- state
type: object
type: array
timeout:
description: Timeout is the maximum duration for a scrape run. Uses
duration strings, e.g. "30m", "1h", "1d".
type: string
trivy:
items:
properties:
Expand Down
5 changes: 1 addition & 4 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,7 @@ var Run = &cobra.Command{
uiServer.UpdateScraper(scraperConfigs[i].Name, scrapeui.ScraperRunning, nil, nil, nil)
}

scrapeCtx, cancel, cancelTimeout := api.NewScrapeContext(dutyCtx).WithScrapeConfig(&scraperConfigs[i]).
WithTimeout(dutyCtx.Properties().Duration("scraper.timeout", 4*time.Hour))
defer cancelTimeout()
shutdown.AddHook(func() { defer cancel() })
scrapeCtx := api.NewScrapeContext(dutyCtx).WithScrapeConfig(&scraperConfigs[i])

if save && dutyapi.DefaultConfig.ConnectionString != "" {
prev := scrapers.GetLastScrapeSummary(dutyCtx, string(scraperConfigs[i].GetUID()))
Expand Down
Loading
Loading