3
0

Tweak envoy config structs

Have each node define it's own configs where the decoder may
define the wanted values and the global config populates missing
values in the Bake step.
This commit is contained in:
Tomaž Jerman 2023-02-25 12:06:50 +01:00
parent 2e9ba97dda
commit f661206a79
7 changed files with 121 additions and 46 deletions

View File

@ -124,7 +124,7 @@ func (e StoreEncoder) prepareWorkflow(ctx context.Context, p envoyx.EncodeParams
// changing up the internal resource is enough.
//
// In the future, we can pass down the tree and re-do the deps like that
switch p.Config.OnExisting {
switch n.Config.MergeAlg {
case envoyx.OnConflictPanic:
err = fmt.Errorf("resource already exists")
return
@ -149,7 +149,7 @@ func (e StoreEncoder) prepareWorkflow(ctx context.Context, p envoyx.EncodeParams
// We can skip validation/defaults when the resource is overwritten by
// the one already stored (the panic one errors out anyway) since it
// should already be ok.
if !hasExisting || p.Config.OnExisting != envoyx.OnConflictSkip {
if !hasExisting || n.Config.MergeAlg != envoyx.OnConflictSkip {
err = e.setWorkflowDefaults(res)
if err != nil {
return err
@ -304,7 +304,7 @@ func (e StoreEncoder) prepareTrigger(ctx context.Context, p envoyx.EncodeParams,
// changing up the internal resource is enough.
//
// In the future, we can pass down the tree and re-do the deps like that
switch p.Config.OnExisting {
switch n.Config.MergeAlg {
case envoyx.OnConflictPanic:
err = fmt.Errorf("resource already exists")
return
@ -329,7 +329,7 @@ func (e StoreEncoder) prepareTrigger(ctx context.Context, p envoyx.EncodeParams,
// We can skip validation/defaults when the resource is overwritten by
// the one already stored (the panic one errors out anyway) since it
// should already be ok.
if !hasExisting || p.Config.OnExisting != envoyx.OnConflictSkip {
if !hasExisting || n.Config.MergeAlg != envoyx.OnConflictSkip {
err = e.setTriggerDefaults(res)
if err != nil {
return err

View File

@ -136,7 +136,7 @@ func (e StoreEncoder) prepare{{.expIdent}}(ctx context.Context, p envoyx.EncodeP
// changing up the internal resource is enough.
//
// In the future, we can pass down the tree and re-do the deps like that
switch p.Config.OnExisting {
switch n.Config.MergeAlg {
case envoyx.OnConflictPanic:
err = fmt.Errorf("resource already exists")
return
@ -161,7 +161,7 @@ func (e StoreEncoder) prepare{{.expIdent}}(ctx context.Context, p envoyx.EncodeP
// We can skip validation/defaults when the resource is overwritten by
// the one already stored (the panic one errors out anyway) since it
// should already be ok.
if !hasExisting || p.Config.OnExisting != envoyx.OnConflictSkip {
if !hasExisting || n.Config.MergeAlg != envoyx.OnConflictSkip {
err = e.set{{.expIdent}}Defaults(res)
if err != nil {
return err

View File

@ -139,7 +139,7 @@ func (e StoreEncoder) prepareChart(ctx context.Context, p envoyx.EncodeParams, s
// changing up the internal resource is enough.
//
// In the future, we can pass down the tree and re-do the deps like that
switch p.Config.OnExisting {
switch n.Config.MergeAlg {
case envoyx.OnConflictPanic:
err = fmt.Errorf("resource already exists")
return
@ -164,7 +164,7 @@ func (e StoreEncoder) prepareChart(ctx context.Context, p envoyx.EncodeParams, s
// We can skip validation/defaults when the resource is overwritten by
// the one already stored (the panic one errors out anyway) since it
// should already be ok.
if !hasExisting || p.Config.OnExisting != envoyx.OnConflictSkip {
if !hasExisting || n.Config.MergeAlg != envoyx.OnConflictSkip {
err = e.setChartDefaults(res)
if err != nil {
return err
@ -319,7 +319,7 @@ func (e StoreEncoder) prepareModule(ctx context.Context, p envoyx.EncodeParams,
// changing up the internal resource is enough.
//
// In the future, we can pass down the tree and re-do the deps like that
switch p.Config.OnExisting {
switch n.Config.MergeAlg {
case envoyx.OnConflictPanic:
err = fmt.Errorf("resource already exists")
return
@ -344,7 +344,7 @@ func (e StoreEncoder) prepareModule(ctx context.Context, p envoyx.EncodeParams,
// We can skip validation/defaults when the resource is overwritten by
// the one already stored (the panic one errors out anyway) since it
// should already be ok.
if !hasExisting || p.Config.OnExisting != envoyx.OnConflictSkip {
if !hasExisting || n.Config.MergeAlg != envoyx.OnConflictSkip {
err = e.setModuleDefaults(res)
if err != nil {
return err
@ -519,7 +519,7 @@ func (e StoreEncoder) prepareModuleField(ctx context.Context, p envoyx.EncodePar
// changing up the internal resource is enough.
//
// In the future, we can pass down the tree and re-do the deps like that
switch p.Config.OnExisting {
switch n.Config.MergeAlg {
case envoyx.OnConflictPanic:
err = fmt.Errorf("resource already exists")
return
@ -544,7 +544,7 @@ func (e StoreEncoder) prepareModuleField(ctx context.Context, p envoyx.EncodePar
// We can skip validation/defaults when the resource is overwritten by
// the one already stored (the panic one errors out anyway) since it
// should already be ok.
if !hasExisting || p.Config.OnExisting != envoyx.OnConflictSkip {
if !hasExisting || n.Config.MergeAlg != envoyx.OnConflictSkip {
err = e.setModuleFieldDefaults(res)
if err != nil {
return err
@ -699,7 +699,7 @@ func (e StoreEncoder) prepareNamespace(ctx context.Context, p envoyx.EncodeParam
// changing up the internal resource is enough.
//
// In the future, we can pass down the tree and re-do the deps like that
switch p.Config.OnExisting {
switch n.Config.MergeAlg {
case envoyx.OnConflictPanic:
err = fmt.Errorf("resource already exists")
return
@ -724,7 +724,7 @@ func (e StoreEncoder) prepareNamespace(ctx context.Context, p envoyx.EncodeParam
// We can skip validation/defaults when the resource is overwritten by
// the one already stored (the panic one errors out anyway) since it
// should already be ok.
if !hasExisting || p.Config.OnExisting != envoyx.OnConflictSkip {
if !hasExisting || n.Config.MergeAlg != envoyx.OnConflictSkip {
err = e.setNamespaceDefaults(res)
if err != nil {
return err
@ -897,7 +897,7 @@ func (e StoreEncoder) preparePage(ctx context.Context, p envoyx.EncodeParams, s
// changing up the internal resource is enough.
//
// In the future, we can pass down the tree and re-do the deps like that
switch p.Config.OnExisting {
switch n.Config.MergeAlg {
case envoyx.OnConflictPanic:
err = fmt.Errorf("resource already exists")
return
@ -922,7 +922,7 @@ func (e StoreEncoder) preparePage(ctx context.Context, p envoyx.EncodeParams, s
// We can skip validation/defaults when the resource is overwritten by
// the one already stored (the panic one errors out anyway) since it
// should already be ok.
if !hasExisting || p.Config.OnExisting != envoyx.OnConflictSkip {
if !hasExisting || n.Config.MergeAlg != envoyx.OnConflictSkip {
err = e.setPageDefaults(res)
if err != nil {
return err

View File

@ -64,13 +64,12 @@ type (
DecoderConfig struct{}
EncodeParams struct {
Type encodeType
Params map[string]any
Config EncoderConfig
Type encodeType
Params map[string]any
Envoy EnvoyConfig
Encoder EncoderConfig
}
EncoderConfig struct {
OnExisting mergeAlg
PreferredTimeLayout string
PreferredTimezone string
}
@ -93,7 +92,8 @@ var (
)
const (
OnConflictReplace mergeAlg = iota
OnConflictDefault mergeAlg = iota
OnConflictReplace
OnConflictSkip
OnConflictPanic
// OnConflictMergeLeft mergeAlg = "mergeLeft"
@ -145,6 +145,11 @@ func (svc *service) Decode(ctx context.Context, p DecodeParams) (nn NodeSet, err
return
}
func (svc *service) Bake(ctx context.Context, p EncodeParams, nodes ...*Node) (err error) {
err = svc.bakeEnvoyConfig(p.Envoy, nodes...)
return
}
// Encode encodes Corteza resources bases on the provided encode params
//
// use the BuildDepGraph function to build the default dependency graph.
@ -227,3 +232,28 @@ func CastMergeAlg(v string) (mergeAlg mergeAlg) {
return
}
func (svc *service) bakeEnvoyConfig(dft EnvoyConfig, nodes ...*Node) (err error) {
for _, n := range nodes {
n.Config = svc.mergeEnvoyConfigs(n.Config, dft)
}
return
}
func (svc *service) mergeEnvoyConfigs(a, b EnvoyConfig) (c EnvoyConfig) {
c = a
if c.MergeAlg == OnConflictDefault {
c.MergeAlg = b.MergeAlg
}
if c.MergeAlg == OnConflictDefault {
c.MergeAlg = OnConflictReplace
}
// @todo pre eval this?
if c.SkipIf == "" {
c.SkipIf = b.SkipIf
}
return
}

View File

@ -0,0 +1,45 @@
package envoyx
import (
"context"
"testing"
"github.com/stretchr/testify/require"
)
func TestBake(t *testing.T) {
ctx := context.Background()
req := require.New(t)
t.Run("set default envoy config", func(t *testing.T) {
a := &Node{
Config: EnvoyConfig{
MergeAlg: OnConflictDefault,
SkipIf: "",
},
}
b := &Node{
Config: EnvoyConfig{
MergeAlg: OnConflictReplace,
SkipIf: "",
},
}
c := &Node{
Config: EnvoyConfig{
MergeAlg: OnConflictReplace,
SkipIf: "true",
},
}
err := (&service{}).Bake(ctx, EncodeParams{Envoy: EnvoyConfig{MergeAlg: OnConflictPanic, SkipIf: "false"}}, a, b, c)
req.NoError(err)
req.Equal(OnConflictPanic, a.Config.MergeAlg)
req.Equal("false", a.Config.SkipIf)
req.Equal(OnConflictReplace, b.Config.MergeAlg)
req.Equal("false", b.Config.SkipIf)
req.Equal(OnConflictReplace, c.Config.MergeAlg)
req.Equal("true", c.Config.SkipIf)
})
}

View File

@ -19,9 +19,9 @@ type (
// Placeholders are resources which were added to help resolve missing deps
Placeholder bool
Config NodeConfig
Config EnvoyConfig
}
NodeConfig struct {
EnvoyConfig struct {
MergeAlg mergeAlg
SkipIf string
}

View File

@ -172,7 +172,7 @@ func (e StoreEncoder) prepareApplication(ctx context.Context, p envoyx.EncodePar
// changing up the internal resource is enough.
//
// In the future, we can pass down the tree and re-do the deps like that
switch p.Config.OnExisting {
switch n.Config.MergeAlg {
case envoyx.OnConflictPanic:
err = fmt.Errorf("resource already exists")
return
@ -197,7 +197,7 @@ func (e StoreEncoder) prepareApplication(ctx context.Context, p envoyx.EncodePar
// We can skip validation/defaults when the resource is overwritten by
// the one already stored (the panic one errors out anyway) since it
// should already be ok.
if !hasExisting || p.Config.OnExisting != envoyx.OnConflictSkip {
if !hasExisting || n.Config.MergeAlg != envoyx.OnConflictSkip {
err = e.setApplicationDefaults(res)
if err != nil {
return err
@ -351,7 +351,7 @@ func (e StoreEncoder) prepareApigwRoute(ctx context.Context, p envoyx.EncodePara
// changing up the internal resource is enough.
//
// In the future, we can pass down the tree and re-do the deps like that
switch p.Config.OnExisting {
switch n.Config.MergeAlg {
case envoyx.OnConflictPanic:
err = fmt.Errorf("resource already exists")
return
@ -376,7 +376,7 @@ func (e StoreEncoder) prepareApigwRoute(ctx context.Context, p envoyx.EncodePara
// We can skip validation/defaults when the resource is overwritten by
// the one already stored (the panic one errors out anyway) since it
// should already be ok.
if !hasExisting || p.Config.OnExisting != envoyx.OnConflictSkip {
if !hasExisting || n.Config.MergeAlg != envoyx.OnConflictSkip {
err = e.setApigwRouteDefaults(res)
if err != nil {
return err
@ -530,7 +530,7 @@ func (e StoreEncoder) prepareApigwFilter(ctx context.Context, p envoyx.EncodePar
// changing up the internal resource is enough.
//
// In the future, we can pass down the tree and re-do the deps like that
switch p.Config.OnExisting {
switch n.Config.MergeAlg {
case envoyx.OnConflictPanic:
err = fmt.Errorf("resource already exists")
return
@ -555,7 +555,7 @@ func (e StoreEncoder) prepareApigwFilter(ctx context.Context, p envoyx.EncodePar
// We can skip validation/defaults when the resource is overwritten by
// the one already stored (the panic one errors out anyway) since it
// should already be ok.
if !hasExisting || p.Config.OnExisting != envoyx.OnConflictSkip {
if !hasExisting || n.Config.MergeAlg != envoyx.OnConflictSkip {
err = e.setApigwFilterDefaults(res)
if err != nil {
return err
@ -709,7 +709,7 @@ func (e StoreEncoder) prepareAuthClient(ctx context.Context, p envoyx.EncodePara
// changing up the internal resource is enough.
//
// In the future, we can pass down the tree and re-do the deps like that
switch p.Config.OnExisting {
switch n.Config.MergeAlg {
case envoyx.OnConflictPanic:
err = fmt.Errorf("resource already exists")
return
@ -734,7 +734,7 @@ func (e StoreEncoder) prepareAuthClient(ctx context.Context, p envoyx.EncodePara
// We can skip validation/defaults when the resource is overwritten by
// the one already stored (the panic one errors out anyway) since it
// should already be ok.
if !hasExisting || p.Config.OnExisting != envoyx.OnConflictSkip {
if !hasExisting || n.Config.MergeAlg != envoyx.OnConflictSkip {
err = e.setAuthClientDefaults(res)
if err != nil {
return err
@ -889,7 +889,7 @@ func (e StoreEncoder) prepareQueue(ctx context.Context, p envoyx.EncodeParams, s
// changing up the internal resource is enough.
//
// In the future, we can pass down the tree and re-do the deps like that
switch p.Config.OnExisting {
switch n.Config.MergeAlg {
case envoyx.OnConflictPanic:
err = fmt.Errorf("resource already exists")
return
@ -914,7 +914,7 @@ func (e StoreEncoder) prepareQueue(ctx context.Context, p envoyx.EncodeParams, s
// We can skip validation/defaults when the resource is overwritten by
// the one already stored (the panic one errors out anyway) since it
// should already be ok.
if !hasExisting || p.Config.OnExisting != envoyx.OnConflictSkip {
if !hasExisting || n.Config.MergeAlg != envoyx.OnConflictSkip {
err = e.setQueueDefaults(res)
if err != nil {
return err
@ -1069,7 +1069,7 @@ func (e StoreEncoder) prepareReport(ctx context.Context, p envoyx.EncodeParams,
// changing up the internal resource is enough.
//
// In the future, we can pass down the tree and re-do the deps like that
switch p.Config.OnExisting {
switch n.Config.MergeAlg {
case envoyx.OnConflictPanic:
err = fmt.Errorf("resource already exists")
return
@ -1094,7 +1094,7 @@ func (e StoreEncoder) prepareReport(ctx context.Context, p envoyx.EncodeParams,
// We can skip validation/defaults when the resource is overwritten by
// the one already stored (the panic one errors out anyway) since it
// should already be ok.
if !hasExisting || p.Config.OnExisting != envoyx.OnConflictSkip {
if !hasExisting || n.Config.MergeAlg != envoyx.OnConflictSkip {
err = e.setReportDefaults(res)
if err != nil {
return err
@ -1249,7 +1249,7 @@ func (e StoreEncoder) prepareRole(ctx context.Context, p envoyx.EncodeParams, s
// changing up the internal resource is enough.
//
// In the future, we can pass down the tree and re-do the deps like that
switch p.Config.OnExisting {
switch n.Config.MergeAlg {
case envoyx.OnConflictPanic:
err = fmt.Errorf("resource already exists")
return
@ -1274,7 +1274,7 @@ func (e StoreEncoder) prepareRole(ctx context.Context, p envoyx.EncodeParams, s
// We can skip validation/defaults when the resource is overwritten by
// the one already stored (the panic one errors out anyway) since it
// should already be ok.
if !hasExisting || p.Config.OnExisting != envoyx.OnConflictSkip {
if !hasExisting || n.Config.MergeAlg != envoyx.OnConflictSkip {
err = e.setRoleDefaults(res)
if err != nil {
return err
@ -1429,7 +1429,7 @@ func (e StoreEncoder) prepareTemplate(ctx context.Context, p envoyx.EncodeParams
// changing up the internal resource is enough.
//
// In the future, we can pass down the tree and re-do the deps like that
switch p.Config.OnExisting {
switch n.Config.MergeAlg {
case envoyx.OnConflictPanic:
err = fmt.Errorf("resource already exists")
return
@ -1454,7 +1454,7 @@ func (e StoreEncoder) prepareTemplate(ctx context.Context, p envoyx.EncodeParams
// We can skip validation/defaults when the resource is overwritten by
// the one already stored (the panic one errors out anyway) since it
// should already be ok.
if !hasExisting || p.Config.OnExisting != envoyx.OnConflictSkip {
if !hasExisting || n.Config.MergeAlg != envoyx.OnConflictSkip {
err = e.setTemplateDefaults(res)
if err != nil {
return err
@ -1609,7 +1609,7 @@ func (e StoreEncoder) prepareUser(ctx context.Context, p envoyx.EncodeParams, s
// changing up the internal resource is enough.
//
// In the future, we can pass down the tree and re-do the deps like that
switch p.Config.OnExisting {
switch n.Config.MergeAlg {
case envoyx.OnConflictPanic:
err = fmt.Errorf("resource already exists")
return
@ -1634,7 +1634,7 @@ func (e StoreEncoder) prepareUser(ctx context.Context, p envoyx.EncodeParams, s
// We can skip validation/defaults when the resource is overwritten by
// the one already stored (the panic one errors out anyway) since it
// should already be ok.
if !hasExisting || p.Config.OnExisting != envoyx.OnConflictSkip {
if !hasExisting || n.Config.MergeAlg != envoyx.OnConflictSkip {
err = e.setUserDefaults(res)
if err != nil {
return err
@ -1789,7 +1789,7 @@ func (e StoreEncoder) prepareDalConnection(ctx context.Context, p envoyx.EncodeP
// changing up the internal resource is enough.
//
// In the future, we can pass down the tree and re-do the deps like that
switch p.Config.OnExisting {
switch n.Config.MergeAlg {
case envoyx.OnConflictPanic:
err = fmt.Errorf("resource already exists")
return
@ -1814,7 +1814,7 @@ func (e StoreEncoder) prepareDalConnection(ctx context.Context, p envoyx.EncodeP
// We can skip validation/defaults when the resource is overwritten by
// the one already stored (the panic one errors out anyway) since it
// should already be ok.
if !hasExisting || p.Config.OnExisting != envoyx.OnConflictSkip {
if !hasExisting || n.Config.MergeAlg != envoyx.OnConflictSkip {
err = e.setDalConnectionDefaults(res)
if err != nil {
return err
@ -1969,7 +1969,7 @@ func (e StoreEncoder) prepareDalSensitivityLevel(ctx context.Context, p envoyx.E
// changing up the internal resource is enough.
//
// In the future, we can pass down the tree and re-do the deps like that
switch p.Config.OnExisting {
switch n.Config.MergeAlg {
case envoyx.OnConflictPanic:
err = fmt.Errorf("resource already exists")
return
@ -1994,7 +1994,7 @@ func (e StoreEncoder) prepareDalSensitivityLevel(ctx context.Context, p envoyx.E
// We can skip validation/defaults when the resource is overwritten by
// the one already stored (the panic one errors out anyway) since it
// should already be ok.
if !hasExisting || p.Config.OnExisting != envoyx.OnConflictSkip {
if !hasExisting || n.Config.MergeAlg != envoyx.OnConflictSkip {
err = e.setDalSensitivityLevelDefaults(res)
if err != nil {
return err