Skip to content

Commit

Permalink
chore: revert unnecessary changes
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe committed Nov 27, 2024
1 parent ddfd86a commit 865c69e
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 45 deletions.
19 changes: 3 additions & 16 deletions job/catalog.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,17 @@
package job

import (
"fmt"

"github.com/flanksource/duty/context"
"gorm.io/gorm"
)

func RefreshConfigItemSummary3d(ctx context.Context) error {
return refreshMatView(ctx, "config_item_summary_3d")
return ctx.DB().Exec("REFRESH MATERIALIZED VIEW config_item_summary_3d").Error
}

func RefreshConfigItemSummary7d(ctx context.Context) error {
return refreshMatView(ctx, "config_item_summary_7d")
return ctx.DB().Exec("REFRESH MATERIALIZED VIEW config_item_summary_7d").Error
}

func RefreshConfigItemSummary30d(ctx context.Context) error {
return refreshMatView(ctx, "config_item_summary_30d")
}

func refreshMatView(ctx context.Context, view string) error {
return ctx.DB().Transaction(func(tx *gorm.DB) error {
if err := tx.Exec("SET ROLE 'postgres'").Error; err != nil {
return err
}

return tx.Exec(fmt.Sprintf("REFRESH MATERIALIZED VIEW %s", view)).Error
})
return ctx.DB().Exec("REFRESH MATERIALIZED VIEW config_item_summary_30d").Error
}
18 changes: 1 addition & 17 deletions tests/config_gitops_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package tests

import (
"github.com/flanksource/duty/context"
"github.com/flanksource/duty/job"
"github.com/flanksource/duty/query"
"github.com/flanksource/duty/tests/fixtures/dummy"
Expand All @@ -21,29 +20,14 @@ var gitopsFixtures = []struct {
{dummy.Namespace.ID.String(), gitopsPath},
{dummy.Namespace.AsMap(), gitopsPath},
}

func RefreshConfigItemSummary7d(ctx context.Context) error {
// Not sure why but despite refreshing this view, the `configs` view
// doesn't produce accurate result. It returns fewer configs.
// Running this a couple of times to ensure it's refreshed.
for range 3 {
err := job.RefreshConfigItemSummary7d(ctx)
if err != nil {
return err
}
}

return nil
}

var _ = ginkgo.Describe("Config Gitops Source", ginkgo.Ordered, func() {
ginkgo.It("should resolve kustomize references", func() {
Expect(dummy.Kustomization.ID.String()).NotTo(BeEmpty())
Expect(dummy.GitRepository.ID.String()).NotTo(BeEmpty())
Expect(dummy.Namespace.ID.String()).NotTo(BeEmpty())

// Config Traverse uses the 7d summary view internally
err := RefreshConfigItemSummary7d(DefaultContext)
err := job.RefreshConfigItemSummary7d(DefaultContext)
Expect(err).To(BeNil())

source, err := query.GetGitOpsSource(DefaultContext, dummy.Namespace.ID)
Expand Down
7 changes: 4 additions & 3 deletions tests/config_relationship_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tests
import (
"fmt"

"github.com/flanksource/duty/job"
"github.com/flanksource/duty/models"
"github.com/flanksource/duty/query"
"github.com/flanksource/duty/tests/fixtures/dummy"
Expand Down Expand Up @@ -134,7 +135,7 @@ var _ = ginkgo.Describe("Config relationship recursive", ginkgo.Ordered, func()

ginkgo.Context("Multiple parent graph", func() {
ginkgo.It("should not return duplicate parents", func() {
err := RefreshConfigItemSummary7d(DefaultContext)
err := job.RefreshConfigItemSummary7d(DefaultContext)
Expect(err).To(BeNil())

var relatedConfigs []query.RelatedConfig
Expand Down Expand Up @@ -344,7 +345,7 @@ var _ = ginkgo.Describe("Config relationship Kubernetes", ginkgo.Ordered, func()
})

ginkgo.It("should return deployment outgoing", func() {
err := RefreshConfigItemSummary7d(DefaultContext)
err := job.RefreshConfigItemSummary7d(DefaultContext)
Expect(err).To(BeNil())

relatedConfigs, err := query.GetRelatedConfigs(DefaultContext, query.RelationQuery{ID: deployment.ID, Relation: query.Outgoing})
Expand Down Expand Up @@ -488,7 +489,7 @@ var _ = ginkgo.Describe("config relationship depth", ginkgo.Ordered, func() {

ginkgo.Context("cluster relationship", func() {
ginkgo.It("should fetch level 1", func() {
err := RefreshConfigItemSummary7d(DefaultContext)
err := job.RefreshConfigItemSummary7d(DefaultContext)
Expect(err).To(BeNil())

cluster := generator.Generated.ConfigByTypes("Kubernetes::Cluster")[0]
Expand Down
3 changes: 2 additions & 1 deletion tests/config_summary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tests
import (
"encoding/json"

"github.com/flanksource/duty/job"
"github.com/flanksource/duty/models"
"github.com/flanksource/duty/query"
"github.com/flanksource/duty/tests/fixtures/dummy"
Expand Down Expand Up @@ -144,7 +145,7 @@ var _ = ginkgo.Describe("Config Summary Search", ginkgo.Ordered, func() {

ginkgo.Context("should query changes by range", func() {
ginkgo.It("small range", func() {
err := RefreshConfigItemSummary7d(DefaultContext)
err := job.RefreshConfigItemSummary7d(DefaultContext)
Expect(err).To(BeNil())

request := query.ConfigSummaryRequest{
Expand Down
3 changes: 2 additions & 1 deletion tests/config_traversal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/flanksource/commons/utils"
"github.com/flanksource/duty/job"
"github.com/flanksource/duty/models"
"github.com/flanksource/duty/query"
"github.com/flanksource/gomplate/v3"
Expand Down Expand Up @@ -51,7 +52,7 @@ var _ = ginkgo.Describe("Config traversal", ginkgo.Ordered, func() {
err = ctx.DB().Clauses(clause.OnConflict{DoNothing: true}).Save(configRelations).Error
Expect(err).ToNot(HaveOccurred())

err = RefreshConfigItemSummary7d(DefaultContext)
err = job.RefreshConfigItemSummary7d(DefaultContext)
Expect(err).To(BeNil())

err = query.SyncConfigCache(DefaultContext)
Expand Down
2 changes: 1 addition & 1 deletion tests/config_type_summary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ var _ = ginkgo.Describe("Check config_class_summary view", ginkgo.Ordered, func(
analysis0 := gen.Generated.Analysis[0]
DefaultContext.DB().Model(&models.ConfigAnalysis{}).Where("id = ?", analysis0.ID).UpdateColumn("status", "closed")

err = RefreshConfigItemSummary7d(DefaultContext)
err = job.RefreshConfigItemSummary7d(DefaultContext)
Expect(err).To(BeNil())
summary7D, err := query.ConfigSummary(DefaultContext, query.ConfigSummaryRequest{
GroupBy: []string{"type"},
Expand Down
12 changes: 6 additions & 6 deletions views/034_rls_enable.sql
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ CREATE POLICY config_items_auth ON config_items
USING (
CASE WHEN (
current_setting('request.jwt.claims', TRUE) IS NULL
OR current_setting('request.jwt.claims', TRUE) = '' -- when the parameter is set it cannot be deleted. it's value is set to empty string.
OR current_setting('request.jwt.claims', TRUE) = '' -- when the parameter is set, it cannot be deleted. It's value is set to empty string.
OR current_setting('request.jwt.claims', TRUE)::jsonb ->> 'disable_rls' IS NOT NULL
)
THEN TRUE
Expand All @@ -52,7 +52,7 @@ CREATE POLICY config_changes_auth ON config_changes
USING (
CASE WHEN (
current_setting('request.jwt.claims', TRUE) IS NULL
OR current_setting('request.jwt.claims', TRUE) = '' -- when the parameter is set it cannot be deleted. it's value is set to empty string.
OR current_setting('request.jwt.claims', TRUE) = '' -- when the parameter is set, it cannot be deleted. It's value is set to empty string.
OR current_setting('request.jwt.claims', TRUE)::jsonb ->> 'disable_rls' IS NOT NULL
)
THEN TRUE
Expand All @@ -75,7 +75,7 @@ CREATE POLICY config_analysis_auth ON config_analysis
USING (
CASE WHEN (
current_setting('request.jwt.claims', TRUE) IS NULL
OR current_setting('request.jwt.claims', TRUE) = '' -- when the parameter is set it cannot be deleted. it's value is set to empty string.
OR current_setting('request.jwt.claims', TRUE) = '' -- when the parameter is set, it cannot be deleted. It's value is set to empty string.
OR current_setting('request.jwt.claims', TRUE)::jsonb ->> 'disable_rls' IS NOT NULL
)
THEN TRUE
Expand All @@ -98,7 +98,7 @@ CREATE POLICY config_relationships_auth ON config_relationships
USING (
CASE WHEN (
current_setting('request.jwt.claims', TRUE) IS NULL
OR current_setting('request.jwt.claims', TRUE) = '' -- when the parameter is set it cannot be deleted. it's value is set to empty string.
OR current_setting('request.jwt.claims', TRUE) = '' -- when the parameter is set, it cannot be deleted. It's value is set to empty string.
OR current_setting('request.jwt.claims', TRUE)::jsonb ->> 'disable_rls' IS NOT NULL
)
THEN TRUE
Expand All @@ -121,7 +121,7 @@ CREATE POLICY config_component_relationships_auth ON config_component_relationsh
USING (
CASE WHEN (
current_setting('request.jwt.claims', TRUE) IS NULL
OR current_setting('request.jwt.claims', TRUE) = '' -- when the parameter is set it cannot be deleted. it's value is set to empty string.
OR current_setting('request.jwt.claims', TRUE) = '' -- when the parameter is set, it cannot be deleted. It's value is set to empty string.
OR current_setting('request.jwt.claims', TRUE)::jsonb ->> 'disable_rls' IS NOT NULL
)
THEN TRUE
Expand All @@ -144,7 +144,7 @@ CREATE POLICY components_auth ON components
USING (
CASE WHEN (
current_setting('request.jwt.claims', TRUE) IS NULL
OR current_setting('request.jwt.claims', TRUE) = '' -- when the parameter is set it cannot be deleted. it's value is set to empty string.
OR current_setting('request.jwt.claims', TRUE) = '' -- when the parameter is set, it cannot be deleted. It's value is set to empty string.
OR current_setting('request.jwt.claims', TRUE)::jsonb ->> 'disable_rls' IS NOT NULL
)
THEN TRUE
Expand Down

0 comments on commit 865c69e

Please sign in to comment.