Documentation
¶
Index ¶
- Variables
- func Run(ctx dutyCtx.Context, topology pkg.Topology) (pkg.Components, *models.JobHistory, error)
- type ComponentContext
- func (c *ComponentContext) Clone() *ComponentContext
- func (c *ComponentContext) GetTemplater() gomplate.StructTemplater
- func (c *ComponentContext) SetCurrentComponent(component *pkg.Component)
- func (c *ComponentContext) String() string
- func (c *ComponentContext) TemplateComponent(component *v1.ComponentSpec) error
- func (c *ComponentContext) TemplateConfig(config *types.ConfigQuery) error
- func (c *ComponentContext) TemplateProperty(property *v1.Property) error
- func (c *ComponentContext) TemplateStruct(data interface{}) error
- func (c *ComponentContext) WithComponents(components *pkg.Components, current *pkg.Component) *ComponentContext
- type TopologyJob
- type TopologyRunOptions
Constants ¶
This section is empty.
Variables ¶
View Source
var CleanupCanaries = &job.Job{ Name: "CleanupCanaries", Schedule: "@every 12h", Singleton: true, JobHistory: true, Retention: job.RetentionBalanced, RunNow: true, Fn: func(ctx job.JobRuntime) error { retention := ctx.Properties().Duration("canary.retention.age", DefaultRetention) tx := ctx.DB().Exec(` DELETE FROM canaries WHERE id NOT IN (SELECT canary_id FROM checks) AND (NOW() - deleted_at) > INTERVAL '1 second' * ? `, int64(retention.Seconds())) ctx.History.SuccessCount = int(tx.RowsAffected) return tx.Error }, }
View Source
var CleanupChecks = &job.Job{ Name: "CleanupChecks", Schedule: "@every 12h", Singleton: true, JobHistory: true, Retention: job.RetentionBalanced, Fn: func(ctx job.JobRuntime) error { retention := ctx.Properties().Duration("check.retention.age", DefaultRetention) tx := ctx.DB().Exec(` DELETE FROM checks WHERE id NOT IN (SELECT check_id FROM evidences WHERE check_id IS NOT NULL) AND (NOW() - deleted_at) > INTERVAL '1 second' * ? `, int64(retention.Seconds()), ) ctx.History.SuccessCount = int(tx.RowsAffected) return tx.Error }, }
View Source
var CleanupJobs = []*job.Job{ CleanupSoftDeletedComponents, CleanupCanaries, CleanupChecks, CleanupMetricsGauges, }
View Source
var CleanupMetricsGauges = &job.Job{ Name: "CleanupMetricsGauges", Schedule: "@every 1h", Singleton: true, JobHistory: true, Retention: job.RetentionBalanced, RunNow: true, Fn: func(ctx job.JobRuntime) error { sevenDaysAgo := time.Now().Add(-time.Hour * 24 * 7) var deletedCheckIDs []string if err := ctx.DB().Model(&models.Check{}).Where("deleted_at > ?", sevenDaysAgo).Pluck("id", &deletedCheckIDs).Error; err != nil { return fmt.Errorf("error finding deleted checks: %v", err) } if ctx.IsDebug() { ctx.Debugf("Found %d deleted checks since %s", len(deletedCheckIDs), sevenDaysAgo.Format("2006-01-02 15:04:05")) } for _, id := range deletedCheckIDs { if metrics.Gauge.DeletePartialMatch(prometheus.Labels{"key": id}) > 0 { logger.Debugf("Deleted gauge for check: %s", id) ctx.History.IncrSuccess() } } return nil }, }
CleanupMetricsGauges removes gauges for checks that no longer exist.
View Source
var CleanupSoftDeletedComponents = &job.Job{ Name: "CleanupSoftDeletedComponents", Schedule: "@every 24h", Singleton: true, JobHistory: true, Retention: job.RetentionBalanced, Fn: func(ctx job.JobRuntime) error { ctx.History.ResourceType = job.ResourceTypeComponent retention := ctx.Properties().Duration("component.retention.period", DefaultRetention) seconds := int64(retention.Seconds()) linkedComponents := ` SELECT component_id FROM evidences WHERE component_id IS NOT NULL UNION SELECT component_id FROM playbook_runs WHERE component_id IS NOT NULL ` tx := ctx.Context.DB().Exec( fmt.Sprintf(` DELETE FROM component_relationships WHERE deleted_at < NOW() - interval '1 SECONDS' * ? OR component_id in (SELECT id FROM components WHERE id NOT IN (%s) AND deleted_at < NOW() - interval '1 SECONDS' * ?) OR relationship_id in (SELECT id FROM components WHERE id NOT IN (%s) AND deleted_at < NOW() - interval '1 SECONDS' * ?) `, linkedComponents, linkedComponents), seconds, seconds, seconds) if tx.Error != nil { return tx.Error } if tx := ctx.Context.DB().Exec("UPDATE components SET parent_id = null WHERE id IN (SELECT component_id FROM evidences WHERE component_id IS NOT NULL) AND parent_id is not null AND deleted_at < NOW() - interval '7 days'"); tx.Error != nil { return tx.Error } for { tx = ctx.Context.DB().Exec(fmt.Sprintf(` DELETE FROM components WHERE id in ( SELECT id FROM components WHERE id NOT IN (%s) AND deleted_at < NOW() - interval '1 SECONDS' * ? ORDER BY length(path) DESC LIMIT 1000 )`, linkedComponents), seconds) if tx.Error != nil { return tx.Error } ctx.History.SuccessCount += int(tx.RowsAffected) if tx.RowsAffected == 0 { break } } return nil }, }
View Source
var DefaultRetention = time.Hour * 24 * 7
Functions ¶
Types ¶
type ComponentContext ¶ added in v0.38.135
type ComponentContext struct {
Topology v1.Topology
ComponentAPI v1.Component
// Components keep track of the components that properties can apply to,
// properties can return a map of component names to properties to facilitate
// queries that are more efficient to perform for all components rather than a component at a time
Components *pkg.Components
// Properties can either be looked up on an individual component, or act as a summary across all components
CurrentComponent *pkg.Component
JobHistory *models.JobHistory
dutyContext.Context
// contains filtered or unexported fields
}
func NewComponentContext ¶ added in v0.38.135
func NewComponentContext(ctx dutyContext.Context, system v1.Topology) *ComponentContext
func (*ComponentContext) Clone ¶ added in v0.38.135
func (c *ComponentContext) Clone() *ComponentContext
func (*ComponentContext) GetTemplater ¶ added in v0.38.193
func (c *ComponentContext) GetTemplater() gomplate.StructTemplater
func (*ComponentContext) SetCurrentComponent ¶ added in v0.38.193
func (c *ComponentContext) SetCurrentComponent(component *pkg.Component)
func (*ComponentContext) String ¶ added in v1.0.152
func (c *ComponentContext) String() string
func (*ComponentContext) TemplateComponent ¶ added in v0.38.193
func (c *ComponentContext) TemplateComponent(component *v1.ComponentSpec) error
func (*ComponentContext) TemplateConfig ¶ added in v0.38.193
func (c *ComponentContext) TemplateConfig(config *types.ConfigQuery) error
func (*ComponentContext) TemplateProperty ¶ added in v0.38.193
func (c *ComponentContext) TemplateProperty(property *v1.Property) error
func (*ComponentContext) TemplateStruct ¶ added in v0.38.193
func (c *ComponentContext) TemplateStruct(data interface{}) error
func (*ComponentContext) WithComponents ¶ added in v0.38.135
func (c *ComponentContext) WithComponents(components *pkg.Components, current *pkg.Component) *ComponentContext
type TopologyJob ¶ added in v1.0.197
type TopologyJob struct {
Topology v1.Topology
Namespace string
Output pkg.Components
}
func (*TopologyJob) Run ¶ added in v1.0.197
func (tj *TopologyJob) Run(job job.JobRuntime) error
type TopologyRunOptions ¶
type TopologyRunOptions struct {
job.JobRuntime
Depth int
Namespace string
}
Click to show internal directories.
Click to hide internal directories.