diff --git a/api/v1/common.go b/api/v1/common.go index cb1411de4..6c1edb258 100644 --- a/api/v1/common.go +++ b/api/v1/common.go @@ -10,6 +10,7 @@ import ( "github.com/c2h5oh/datasize" "github.com/flanksource/commons/duration" + "github.com/flanksource/duty/types" "github.com/flanksource/kommons" ) @@ -228,7 +229,7 @@ type DisplayTemplate interface { // +k8s:deepcopy-gen=false type TestFunction interface { GetTestFunction() Template - GetTestThreshold() *TestThreshold + GetTestThreshold() *types.TestThreshold } // +k8s:deepcopy-gen=false @@ -236,15 +237,9 @@ type Transformer interface { GetTransformer() Template } -type TestThreshold struct { - High string `yaml:"high,omitempty" json:"high,omitempty"` - Low string `yaml:"low,omitempty" json:"low,omitempty"` - Critical string `yaml:"critical,omitempty" json:"critical,omitempty"` -} - type TestTemplate struct { - Template `yaml:",inline" json:",inline"` - *TestThreshold `yaml:",inline" json:",inline"` + Template `yaml:",inline" json:",inline"` + *types.TestThreshold `yaml:",inline" json:",inline"` } type Templatable struct { @@ -257,7 +252,7 @@ func (t Templatable) GetTestFunction() Template { return t.Test.Template } -func (t Templatable) GetTestThreshold() *TestThreshold { +func (t Templatable) GetTestThreshold() *types.TestThreshold { return t.Test.TestThreshold } diff --git a/checks/runchecks.go b/checks/runchecks.go index f5599fc82..849466d35 100644 --- a/checks/runchecks.go +++ b/checks/runchecks.go @@ -7,6 +7,7 @@ import ( v1 "github.com/flanksource/canary-checker/api/v1" "github.com/flanksource/canary-checker/pkg" "github.com/flanksource/commons/logger" + "github.com/flanksource/duty/types" ) func RunChecks(ctx *context.Context) []*pkg.CheckResult { @@ -115,7 +116,7 @@ func processTemplates(ctx *context.Context, r *pkg.CheckResult) *pkg.CheckResult return r } -func measureTestSeverity(ctx *context.Context, threshold *v1.TestThreshold) pkg.TestSeverity { +func measureTestSeverity(ctx *context.Context, threshold *types.TestThreshold) pkg.TestSeverity { if threshold == nil { return pkg.TestSeverityUnknown } diff --git a/checks/runchecks_test.go b/checks/runchecks_test.go index 63ac6e4b3..2bda274d8 100644 --- a/checks/runchecks_test.go +++ b/checks/runchecks_test.go @@ -5,14 +5,14 @@ import ( "testing" "github.com/flanksource/canary-checker/api/context" - v1 "github.com/flanksource/canary-checker/api/v1" "github.com/flanksource/canary-checker/pkg" + "github.com/flanksource/duty/types" ) func Test_measureTestSeverity(t *testing.T) { type args struct { duration int - threshold *v1.TestThreshold + threshold *types.TestThreshold } tests := []struct { name string @@ -23,7 +23,7 @@ func Test_measureTestSeverity(t *testing.T) { name: "simple - critical", args: args{ duration: 2000, - threshold: &v1.TestThreshold{ + threshold: &types.TestThreshold{ Critical: "duration > 1500", }, }, @@ -33,7 +33,7 @@ func Test_measureTestSeverity(t *testing.T) { name: "simple - high", args: args{ duration: 1200, - threshold: &v1.TestThreshold{ + threshold: &types.TestThreshold{ Critical: "duration > 1500", High: "duration > 1000", }, @@ -44,7 +44,7 @@ func Test_measureTestSeverity(t *testing.T) { name: "simple - low", args: args{ duration: 600, - threshold: &v1.TestThreshold{ + threshold: &types.TestThreshold{ Critical: "duration > 1500", High: "duration > 1000", Low: "duration > 500", @@ -56,7 +56,7 @@ func Test_measureTestSeverity(t *testing.T) { name: "complex expression", args: args{ duration: 2100, - threshold: &v1.TestThreshold{ + threshold: &types.TestThreshold{ Critical: "duration > 1500 && duration < 2000", High: "duration > 1000 && duration < 1500", Low: "duration > 500 && duration < 1000", @@ -75,7 +75,7 @@ func Test_measureTestSeverity(t *testing.T) { name: "no severity match", args: args{ duration: 400, - threshold: &v1.TestThreshold{ + threshold: &types.TestThreshold{ Critical: "duration > 1500", High: "duration > 1000", Low: "duration > 500", @@ -87,7 +87,7 @@ func Test_measureTestSeverity(t *testing.T) { name: "invalid expression", args: args{ duration: 400, - threshold: &v1.TestThreshold{ + threshold: &types.TestThreshold{ Critical: "duration >>> 1500", High: "duration > 1000", Low: "duration > 500", @@ -99,7 +99,7 @@ func Test_measureTestSeverity(t *testing.T) { name: "use of undefined var", args: args{ duration: 400, - threshold: &v1.TestThreshold{ + threshold: &types.TestThreshold{ Critical: "Duration > 1500", High: "Duration > 1000", Low: "Duration > 500", diff --git a/go.mod b/go.mod index 9a3b9bf10..c81243b2c 100644 --- a/go.mod +++ b/go.mod @@ -290,3 +290,5 @@ require ( sigs.k8s.io/kustomize/kyaml v0.13.9 // indirect sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect ) + +replace github.com/flanksource/duty => ../duty diff --git a/go.sum b/go.sum index deecce3bf..bb0fca932 100644 --- a/go.sum +++ b/go.sum @@ -1086,8 +1086,6 @@ github.com/fergusstrange/embedded-postgres v1.21.0 h1:Sups0nR31+OB4iOZ0ZU4IwUDsB github.com/fergusstrange/embedded-postgres v1.21.0/go.mod h1:wL562t1V+iuFwq0UcgMi2e9rp8CROY9wxWZEfP8Y874= github.com/flanksource/commons v1.10.0 h1:Mc+fzxq1rOJ08lapF0cc0bo9ZKNDxA7/81q2D/5jt0Q= github.com/flanksource/commons v1.10.0/go.mod h1:HpVjPtNe7v0UPk97kO/uUhOrYQ8yFD/mGglrTCkc9Ag= -github.com/flanksource/duty v1.0.84 h1:chDSTw9n1YLFhQSbTtDWoDjzKkT9t3oZWkFa+5YV9Uc= -github.com/flanksource/duty v1.0.84/go.mod h1:RJ/kcZ7dbL8/52tem757szVIA3IomS8bOAZIK0xb4rk= github.com/flanksource/gomplate/v3 v3.20.1/go.mod h1:LPpzujBIg9HBXRUngDKK/zNmEjHpEUieKa/2oRjkCzI= github.com/flanksource/gomplate/v3 v3.20.3 h1:mnNaO37uwvv8Kvi4Xswj1tHKP5UMre3FLNaKvAwqhSw= github.com/flanksource/gomplate/v3 v3.20.3/go.mod h1:l4iCvp30TdhZk89eRGuNkPwlRjJLXdUSblr+VyuZfAk= diff --git a/pkg/api.go b/pkg/api.go index 7127b3926..43ad8bcaa 100644 --- a/pkg/api.go +++ b/pkg/api.go @@ -12,6 +12,7 @@ import ( "github.com/flanksource/canary-checker/pkg/utils" "github.com/flanksource/commons/console" "github.com/flanksource/commons/logger" + dutyTypes "github.com/flanksource/duty/types" "github.com/google/uuid" "github.com/lib/pq" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -175,36 +176,37 @@ func CanaryFromV1(canary v1.Canary) (Canary, error) { } type Check struct { - ID uuid.UUID `json:"id" gorm:"default:generate_ulid()"` - CanaryID uuid.UUID `json:"canary_id"` - Spec types.JSON `json:"-"` - Type string `json:"type"` - Name string `json:"name"` - CanaryName string `json:"canary_name" gorm:"-"` - Namespace string `json:"namespace" gorm:"-"` - Labels types.JSONStringMap `json:"labels" gorm:"type:jsonstringmap"` - Description string `json:"description,omitempty"` - Status string `json:"status,omitempty"` - Uptime Uptime `json:"uptime" gorm:"-"` - Latency Latency `json:"latency" gorm:"-"` - Statuses []CheckStatus `json:"checkStatuses" gorm:"-"` - Owner string `json:"owner,omitempty"` - Severity string `json:"severity,omitempty"` - Icon string `json:"icon,omitempty"` - DisplayType string `json:"displayType,omitempty" gorm:"-"` - Transformed bool `json:"transformed,omitempty"` - LastRuntime *time.Time `json:"lastRuntime,omitempty"` - LastTransitionTime *time.Time `json:"lastTransitionTime,omitempty"` - NextRuntime *time.Time `json:"nextRuntime,omitempty"` - UpdatedAt *time.Time `json:"updatedAt,omitempty"` - CreatedAt *time.Time `json:"createdAt,omitempty"` - DeletedAt *time.Time `json:"deletedAt,omitempty"` - SilencedAt *time.Time `json:"silencedAt,omitempty"` - Canary *v1.Canary `json:"-" gorm:"-"` + ID uuid.UUID `json:"id" gorm:"default:generate_ulid()"` + CanaryID uuid.UUID `json:"canary_id"` + Spec types.JSON `json:"-"` + Type string `json:"type"` + Name string `json:"name"` + CanaryName string `json:"canary_name" gorm:"-"` + Namespace string `json:"namespace" gorm:"-"` + Labels types.JSONStringMap `json:"labels" gorm:"type:jsonstringmap"` + TestThreshold *dutyTypes.TestThreshold `json:"testThreshold,omitempty" gorm:"column:test_threshold"` + Description string `json:"description,omitempty"` + Status string `json:"status,omitempty"` + Uptime Uptime `json:"uptime" gorm:"-"` + Latency Latency `json:"latency" gorm:"-"` + Statuses []CheckStatus `json:"checkStatuses" gorm:"-"` + Owner string `json:"owner,omitempty"` + Severity string `json:"severity,omitempty"` + Icon string `json:"icon,omitempty"` + DisplayType string `json:"displayType,omitempty" gorm:"-"` + Transformed bool `json:"transformed,omitempty"` + LastRuntime *time.Time `json:"lastRuntime,omitempty"` + LastTransitionTime *time.Time `json:"lastTransitionTime,omitempty"` + NextRuntime *time.Time `json:"nextRuntime,omitempty"` + UpdatedAt *time.Time `json:"updatedAt,omitempty"` + CreatedAt *time.Time `json:"createdAt,omitempty"` + DeletedAt *time.Time `json:"deletedAt,omitempty"` + SilencedAt *time.Time `json:"silencedAt,omitempty"` + Canary *v1.Canary `json:"-" gorm:"-"` } func FromExternalCheck(canary Canary, check external.Check) Check { - return Check{ + c := Check{ CanaryID: canary.ID, Type: check.GetType(), Icon: check.GetIcon(), @@ -214,6 +216,12 @@ func FromExternalCheck(canary Canary, check external.Check) Check { CanaryName: canary.Name, Labels: labels.FilterLabels(canary.Labels), } + + if testable, ok := check.(v1.TestFunction); ok { + c.TestThreshold = testable.GetTestThreshold() + } + + return c } func FromResult(result CheckResult) CheckStatus {