3
0

Add resource skipping for store encoders

This commit is contained in:
Tomaž Jerman 2023-02-25 12:54:58 +01:00
parent f661206a79
commit 0bfc2a7f87
7 changed files with 245 additions and 2 deletions

View File

@ -13,6 +13,7 @@ import (
"github.com/cortezaproject/corteza/server/automation/types"
"github.com/cortezaproject/corteza/server/pkg/envoyx"
"github.com/cortezaproject/corteza/server/pkg/expr"
"github.com/cortezaproject/corteza/server/pkg/id"
"github.com/cortezaproject/corteza/server/store"
)
@ -119,6 +120,12 @@ func (e StoreEncoder) prepareWorkflow(ctx context.Context, p envoyx.EncodeParams
existing, hasExisting := existing[i]
// Run expressions on the nodes
err = e.runEvals(ctx, hasExisting, n)
if err != nil {
return
}
if hasExisting {
// On existing, we don't need to re-do identifiers and references; simply
// changing up the internal resource is enough.
@ -299,6 +306,12 @@ func (e StoreEncoder) prepareTrigger(ctx context.Context, p envoyx.EncodeParams,
existing, hasExisting := existing[i]
// Run expressions on the nodes
err = e.runEvals(ctx, hasExisting, n)
if err != nil {
return
}
if hasExisting {
// On existing, we don't need to re-do identifiers and references; simply
// changing up the internal resource is enough.
@ -465,3 +478,21 @@ func (e *StoreEncoder) grabStorer(p envoyx.EncodeParams) (s store.Storer, err er
return
}
func (e *StoreEncoder) runEvals(ctx context.Context, existing bool, n *envoyx.Node) (err error) {
// Skip if
if n.Config.SkipIfEval == nil {
return
}
aux, err := expr.EmptyVars().Cast(map[string]any{
"missing": !existing,
})
if err != nil {
return
}
n.Evaluated.Skip, err = n.Config.SkipIfEval.Test(ctx, aux.(*expr.Vars))
return
}

View File

@ -8,6 +8,7 @@ import (
"strconv"
"github.com/cortezaproject/corteza/server/pkg/envoyx"
"github.com/cortezaproject/corteza/server/pkg/expr"
"github.com/cortezaproject/corteza/server/pkg/id"
"github.com/cortezaproject/corteza/server/store"
@ -131,6 +132,12 @@ func (e StoreEncoder) prepare{{.expIdent}}(ctx context.Context, p envoyx.EncodeP
existing, hasExisting := existing[i]
// Run expressions on the nodes
err = e.runEvals(ctx, hasExisting, n)
if err != nil {
return
}
if hasExisting {
// On existing, we don't need to re-do identifiers and references; simply
// changing up the internal resource is enough.
@ -351,3 +358,21 @@ func (e *StoreEncoder) grabStorer(p envoyx.EncodeParams) (s store.Storer, err er
return
}
func (e *StoreEncoder) runEvals(ctx context.Context, existing bool, n *envoyx.Node) (err error) {
// Skip if
if n.Config.SkipIfEval == nil {
return
}
aux, err := expr.EmptyVars().Cast(map[string]any{
"missing": !existing,
})
if err != nil {
return
}
n.Evaluated.Skip, err = n.Config.SkipIfEval.Test(ctx, aux.(*expr.Vars))
return
}

View File

@ -13,6 +13,7 @@ import (
"github.com/cortezaproject/corteza/server/compose/types"
"github.com/cortezaproject/corteza/server/pkg/envoyx"
"github.com/cortezaproject/corteza/server/pkg/expr"
"github.com/cortezaproject/corteza/server/pkg/id"
"github.com/cortezaproject/corteza/server/store"
)
@ -134,6 +135,12 @@ func (e StoreEncoder) prepareChart(ctx context.Context, p envoyx.EncodeParams, s
existing, hasExisting := existing[i]
// Run expressions on the nodes
err = e.runEvals(ctx, hasExisting, n)
if err != nil {
return
}
if hasExisting {
// On existing, we don't need to re-do identifiers and references; simply
// changing up the internal resource is enough.
@ -314,6 +321,12 @@ func (e StoreEncoder) prepareModule(ctx context.Context, p envoyx.EncodeParams,
existing, hasExisting := existing[i]
// Run expressions on the nodes
err = e.runEvals(ctx, hasExisting, n)
if err != nil {
return
}
if hasExisting {
// On existing, we don't need to re-do identifiers and references; simply
// changing up the internal resource is enough.
@ -514,6 +527,12 @@ func (e StoreEncoder) prepareModuleField(ctx context.Context, p envoyx.EncodePar
existing, hasExisting := existing[i]
// Run expressions on the nodes
err = e.runEvals(ctx, hasExisting, n)
if err != nil {
return
}
if hasExisting {
// On existing, we don't need to re-do identifiers and references; simply
// changing up the internal resource is enough.
@ -694,6 +713,12 @@ func (e StoreEncoder) prepareNamespace(ctx context.Context, p envoyx.EncodeParam
existing, hasExisting := existing[i]
// Run expressions on the nodes
err = e.runEvals(ctx, hasExisting, n)
if err != nil {
return
}
if hasExisting {
// On existing, we don't need to re-do identifiers and references; simply
// changing up the internal resource is enough.
@ -892,6 +917,12 @@ func (e StoreEncoder) preparePage(ctx context.Context, p envoyx.EncodeParams, s
existing, hasExisting := existing[i]
// Run expressions on the nodes
err = e.runEvals(ctx, hasExisting, n)
if err != nil {
return
}
if hasExisting {
// On existing, we don't need to re-do identifiers and references; simply
// changing up the internal resource is enough.
@ -1059,3 +1090,21 @@ func (e *StoreEncoder) grabStorer(p envoyx.EncodeParams) (s store.Storer, err er
return
}
func (e *StoreEncoder) runEvals(ctx context.Context, existing bool, n *envoyx.Node) (err error) {
// Skip if
if n.Config.SkipIfEval == nil {
return
}
aux, err := expr.EmptyVars().Cast(map[string]any{
"missing": !existing,
})
if err != nil {
return
}
n.Evaluated.Skip, err = n.Config.SkipIfEval.Test(ctx, aux.(*expr.Vars))
return
}

View File

@ -4,6 +4,8 @@ import (
"context"
"fmt"
"strings"
"github.com/cortezaproject/corteza/server/pkg/expr"
)
type (
@ -89,6 +91,8 @@ type (
var (
global *service
ex = expr.NewParser()
)
const (
@ -147,6 +151,11 @@ func (svc *service) Decode(ctx context.Context, p DecodeParams) (nn NodeSet, err
func (svc *service) Bake(ctx context.Context, p EncodeParams, nodes ...*Node) (err error) {
err = svc.bakeEnvoyConfig(p.Envoy, nodes...)
if err != nil {
return
}
err = svc.bakeExpressions(nodes...)
return
}
@ -241,6 +250,21 @@ func (svc *service) bakeEnvoyConfig(dft EnvoyConfig, nodes ...*Node) (err error)
return
}
func (svc *service) bakeExpressions(nodes ...*Node) (err error) {
for _, n := range nodes {
if n.Config.SkipIf == "" {
continue
}
n.Config.SkipIfEval, err = ex.Parse(n.Config.SkipIf)
if err != nil {
return
}
}
return
}
func (svc *service) mergeEnvoyConfigs(a, b EnvoyConfig) (c EnvoyConfig) {
c = a
if c.MergeAlg == OnConflictDefault {

View File

@ -42,4 +42,25 @@ func TestBake(t *testing.T) {
req.Equal(OnConflictReplace, c.Config.MergeAlg)
req.Equal("true", c.Config.SkipIf)
})
t.Run("precompute expressions", func(t *testing.T) {
a := &Node{
Config: EnvoyConfig{
MergeAlg: OnConflictDefault,
SkipIf: "",
},
}
b := &Node{
Config: EnvoyConfig{
MergeAlg: OnConflictReplace,
SkipIf: "a == b",
},
}
err := (&service{}).Bake(ctx, EncodeParams{}, a, b)
req.NoError(err)
req.Nil(a.Config.SkipIfEval)
req.NotNil(b.Config.SkipIfEval)
})
}

View File

@ -3,6 +3,7 @@ package envoyx
import (
"strconv"
"github.com/cortezaproject/corteza/server/pkg/expr"
"github.com/spf13/cast"
)
@ -20,10 +21,17 @@ type (
// Placeholders are resources which were added to help resolve missing deps
Placeholder bool
Config EnvoyConfig
Evaluated Evaluated
}
Evaluated struct {
Skip bool
}
EnvoyConfig struct {
MergeAlg mergeAlg
SkipIf string
MergeAlg mergeAlg
SkipIf string
SkipIfEval expr.Evaluable
}
NodeSet []*Node

View File

@ -12,6 +12,7 @@ import (
"strconv"
"github.com/cortezaproject/corteza/server/pkg/envoyx"
"github.com/cortezaproject/corteza/server/pkg/expr"
"github.com/cortezaproject/corteza/server/pkg/id"
"github.com/cortezaproject/corteza/server/store"
"github.com/cortezaproject/corteza/server/system/types"
@ -167,6 +168,12 @@ func (e StoreEncoder) prepareApplication(ctx context.Context, p envoyx.EncodePar
existing, hasExisting := existing[i]
// Run expressions on the nodes
err = e.runEvals(ctx, hasExisting, n)
if err != nil {
return
}
if hasExisting {
// On existing, we don't need to re-do identifiers and references; simply
// changing up the internal resource is enough.
@ -346,6 +353,12 @@ func (e StoreEncoder) prepareApigwRoute(ctx context.Context, p envoyx.EncodePara
existing, hasExisting := existing[i]
// Run expressions on the nodes
err = e.runEvals(ctx, hasExisting, n)
if err != nil {
return
}
if hasExisting {
// On existing, we don't need to re-do identifiers and references; simply
// changing up the internal resource is enough.
@ -525,6 +538,12 @@ func (e StoreEncoder) prepareApigwFilter(ctx context.Context, p envoyx.EncodePar
existing, hasExisting := existing[i]
// Run expressions on the nodes
err = e.runEvals(ctx, hasExisting, n)
if err != nil {
return
}
if hasExisting {
// On existing, we don't need to re-do identifiers and references; simply
// changing up the internal resource is enough.
@ -704,6 +723,12 @@ func (e StoreEncoder) prepareAuthClient(ctx context.Context, p envoyx.EncodePara
existing, hasExisting := existing[i]
// Run expressions on the nodes
err = e.runEvals(ctx, hasExisting, n)
if err != nil {
return
}
if hasExisting {
// On existing, we don't need to re-do identifiers and references; simply
// changing up the internal resource is enough.
@ -884,6 +909,12 @@ func (e StoreEncoder) prepareQueue(ctx context.Context, p envoyx.EncodeParams, s
existing, hasExisting := existing[i]
// Run expressions on the nodes
err = e.runEvals(ctx, hasExisting, n)
if err != nil {
return
}
if hasExisting {
// On existing, we don't need to re-do identifiers and references; simply
// changing up the internal resource is enough.
@ -1064,6 +1095,12 @@ func (e StoreEncoder) prepareReport(ctx context.Context, p envoyx.EncodeParams,
existing, hasExisting := existing[i]
// Run expressions on the nodes
err = e.runEvals(ctx, hasExisting, n)
if err != nil {
return
}
if hasExisting {
// On existing, we don't need to re-do identifiers and references; simply
// changing up the internal resource is enough.
@ -1244,6 +1281,12 @@ func (e StoreEncoder) prepareRole(ctx context.Context, p envoyx.EncodeParams, s
existing, hasExisting := existing[i]
// Run expressions on the nodes
err = e.runEvals(ctx, hasExisting, n)
if err != nil {
return
}
if hasExisting {
// On existing, we don't need to re-do identifiers and references; simply
// changing up the internal resource is enough.
@ -1424,6 +1467,12 @@ func (e StoreEncoder) prepareTemplate(ctx context.Context, p envoyx.EncodeParams
existing, hasExisting := existing[i]
// Run expressions on the nodes
err = e.runEvals(ctx, hasExisting, n)
if err != nil {
return
}
if hasExisting {
// On existing, we don't need to re-do identifiers and references; simply
// changing up the internal resource is enough.
@ -1604,6 +1653,12 @@ func (e StoreEncoder) prepareUser(ctx context.Context, p envoyx.EncodeParams, s
existing, hasExisting := existing[i]
// Run expressions on the nodes
err = e.runEvals(ctx, hasExisting, n)
if err != nil {
return
}
if hasExisting {
// On existing, we don't need to re-do identifiers and references; simply
// changing up the internal resource is enough.
@ -1784,6 +1839,12 @@ func (e StoreEncoder) prepareDalConnection(ctx context.Context, p envoyx.EncodeP
existing, hasExisting := existing[i]
// Run expressions on the nodes
err = e.runEvals(ctx, hasExisting, n)
if err != nil {
return
}
if hasExisting {
// On existing, we don't need to re-do identifiers and references; simply
// changing up the internal resource is enough.
@ -1964,6 +2025,12 @@ func (e StoreEncoder) prepareDalSensitivityLevel(ctx context.Context, p envoyx.E
existing, hasExisting := existing[i]
// Run expressions on the nodes
err = e.runEvals(ctx, hasExisting, n)
if err != nil {
return
}
if hasExisting {
// On existing, we don't need to re-do identifiers and references; simply
// changing up the internal resource is enough.
@ -2131,3 +2198,21 @@ func (e *StoreEncoder) grabStorer(p envoyx.EncodeParams) (s store.Storer, err er
return
}
func (e *StoreEncoder) runEvals(ctx context.Context, existing bool, n *envoyx.Node) (err error) {
// Skip if
if n.Config.SkipIfEval == nil {
return
}
aux, err := expr.EmptyVars().Cast(map[string]any{
"missing": !existing,
})
if err != nil {
return
}
n.Evaluated.Skip, err = n.Config.SkipIfEval.Test(ctx, aux.(*expr.Vars))
return
}