Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use rule instance table in executor #3899

Merged
merged 8 commits into from
Jul 19, 2024
Merged
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
15 changes: 13 additions & 2 deletions cmd/dev/app/rule_type/rttst.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"github.com/stacklok/minder/internal/engine/rtengine"
"github.com/stacklok/minder/internal/logger"
"github.com/stacklok/minder/internal/profiles"
"github.com/stacklok/minder/internal/profiles/models"
"github.com/stacklok/minder/internal/providers/credentials"
"github.com/stacklok/minder/internal/providers/dockerhub"
"github.com/stacklok/minder/internal/providers/github/clients"
Expand Down Expand Up @@ -170,13 +171,18 @@ func testCmdRun(cmd *cobra.Command, _ []string) error {
return err
}

actionConfig := models.ActionConfiguration{
Remediate: models.ActionOptFromString(profile.Remediate, models.ActionOptOff),
Alert: models.ActionOptFromString(profile.Alert, models.ActionOptOff),
}

// TODO: use cobra context here
ctx := context.Background()
eng, err := rtengine.NewRuleTypeEngine(ctx, ruletype, prov)
if err != nil {
return fmt.Errorf("cannot create rule type engine: %w", err)
}
actionEngine, err := actions.NewRuleActions(ctx, profile, ruletype, prov)
actionEngine, err := actions.NewRuleActions(ctx, ruletype, prov, &actionConfig)
if err != nil {
return fmt.Errorf("cannot create rule actions engine: %w", err)
}
Expand Down Expand Up @@ -217,9 +223,14 @@ func runEvaluationForRules(
return fmt.Errorf("error validating params against schema: %w", err)
}

rule := models.RuleFromPB(
uuid.New(), // Actual rule type ID does not matter here
frag,
)

// Create the eval status params
evalStatus := &engif.EvalStatusParams{
Rule: frag,
Rule: &rule,
EvalStatusFromDb: &db.ListRuleEvaluationsByProfileIdRow{
RemStatus: remediateStatus,
RemMetadata: remMetadata,
Expand Down
69 changes: 42 additions & 27 deletions database/mock/store.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions database/query/profiles.sql
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,8 @@ GROUP BY r.entity_type;

-- name: CountProfilesByName :one
SELECT COUNT(*) AS num_named_profiles FROM profiles WHERE lower(name) = lower(sqlc.arg(name));

-- name: BulkGetProfilesByID :many
SELECT *
FROM profiles
WHERE id = ANY(sqlc.arg(profile_ids)::UUID[]);
12 changes: 4 additions & 8 deletions database/query/rule_instances.sql
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,17 @@ RETURNING id;
-- name: GetRuleInstancesForProfile :many
SELECT * FROM rule_instances WHERE profile_id = $1;

-- name: GetRuleInstancesForProfileEntity :many
SELECT * FROM rule_instances WHERE profile_id = $1 AND entity_type = $2;
-- name: GetRuleInstancesEntityInProjects :many
SELECT * FROM rule_instances
WHERE entity_type = $1
AND project_id = ANY(sqlc.arg(project_ids)::UUID[]);

-- name: DeleteNonUpdatedRules :exec
DELETE FROM rule_instances
WHERE profile_id = $1
AND entity_type = $2
AND NOT id = ANY(sqlc.arg(updated_ids)::UUID[]);

-- name: GetIDByProfileEntityName :one
SELECT id FROM rule_instances
WHERE profile_id = $1
AND entity_type = $2
AND name = $3;

-- intended as a temporary transition query
-- this will be removed once rule_instances is used consistently in the engine
-- name: GetRuleTypeIDByRuleNameEntityProfile :one
Expand Down
6 changes: 6 additions & 0 deletions database/query/rule_types.sql
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,9 @@ SELECT rt.* FROM rule_type AS rt
JOIN rule_instances AS ri ON ri.rule_type_id = rt.id
WHERE ri.entity_type = $1
AND ri.project_id = ANY(sqlc.arg(projects)::uuid[]);

-- intended as a temporary transition query
-- this will be removed once the evaluation history tables replace the old state tables
-- name: GetRuleTypeNameByID :one
SELECT name FROM rule_type
WHERE id = $1;
42 changes: 42 additions & 0 deletions internal/db/profiles.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions internal/db/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 13 additions & 31 deletions internal/db/rule_instances.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions internal/db/rule_types.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions internal/engine/actions/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ type RuleActionsEngine struct {
// NewRuleActions creates a new rule actions engine
func NewRuleActions(
ctx context.Context,
profile *minderv1.Profile,
ruletype *minderv1.RuleType,
provider provinfv1.Provider,
actionConfig *models.ActionConfiguration,
) (*RuleActionsEngine, error) {
// Create the remediation engine
remEngine, err := remediate.NewRuleRemediator(ruletype, provider)
Expand All @@ -72,8 +72,8 @@ func NewRuleActions(
// The on/off state of the actions is an integral part of the action engine
// and should be set upon creation.
actionsOnOff: map[engif.ActionType]models.ActionOpt{
remEngine.Class(): remEngine.GetOnOffState(profile),
alertEngine.Class(): alertEngine.GetOnOffState(profile),
remEngine.Class(): actionConfig.Remediate,
alertEngine.Class(): actionConfig.Alert,
},
}, nil
}
Expand Down
Loading