3
0

Fix invalid matchup when importing resources

Matchups were not scoped which caused them to get matched
to any matched ident, regardles of scope.

This caused NS duplication to "move" original resources to the
cloned one instead of copying them.
This commit is contained in:
Tomaž Jerman
2023-03-20 16:03:52 +01:00
parent ac290e3245
commit 22a70f61a8
6 changed files with 473 additions and 44 deletions
+38 -4
View File
@@ -102,9 +102,16 @@ func (e StoreEncoder) prepareWorkflow(ctx context.Context, p envoyx.EncodeParams
// @todo do some benchmarks and potentially implement some smarter check such as
// a bloom filter or something similar.
// Get node scopes
scopedNodes, err := e.getScopeNodes(ctx, s, nn)
if err != nil {
err = errors.Wrap(err, "failed to get scope nodes")
return
}
// Initializing the index here (and using a hashmap) so it's not escaped to the heap
existing := make(map[int]types.Workflow, len(nn))
err = e.matchupWorkflows(ctx, s, existing, nn)
err = e.matchupWorkflows(ctx, s, existing, scopedNodes, nn)
if err != nil {
err = errors.Wrap(err, "failed to matchup existing Workflows")
return
@@ -250,7 +257,7 @@ func (e StoreEncoder) encodeWorkflow(ctx context.Context, p envoyx.EncodeParams,
}
// matchupWorkflows returns an index with indicates what resources already exist
func (e StoreEncoder) matchupWorkflows(ctx context.Context, s store.Storer, uu map[int]types.Workflow, nn envoyx.NodeSet) (err error) {
func (e StoreEncoder) matchupWorkflows(ctx context.Context, s store.Storer, uu map[int]types.Workflow, scopes envoyx.NodeSet, nn envoyx.NodeSet) (err error) {
// @todo might need to do it smarter then this.
// Most resources won't really be that vast so this should be acceptable for now.
aa, _, err := store.SearchAutomationWorkflows(ctx, s, types.WorkflowFilter{})
@@ -270,6 +277,11 @@ func (e StoreEncoder) matchupWorkflows(ctx context.Context, s store.Storer, uu m
var aux *types.Workflow
var ok bool
for i, n := range nn {
scope := scopes[i]
if scope == nil {
continue
}
for _, idf := range n.Identifiers.Slice {
if id, err := strconv.ParseUint(idf, 10, 64); err == nil {
aux, ok = idMap[id]
@@ -305,9 +317,16 @@ func (e StoreEncoder) prepareTrigger(ctx context.Context, p envoyx.EncodeParams,
// @todo do some benchmarks and potentially implement some smarter check such as
// a bloom filter or something similar.
// Get node scopes
scopedNodes, err := e.getScopeNodes(ctx, s, nn)
if err != nil {
err = errors.Wrap(err, "failed to get scope nodes")
return
}
// Initializing the index here (and using a hashmap) so it's not escaped to the heap
existing := make(map[int]types.Trigger, len(nn))
err = e.matchupTriggers(ctx, s, existing, nn)
err = e.matchupTriggers(ctx, s, existing, scopedNodes, nn)
if err != nil {
err = errors.Wrap(err, "failed to matchup existing Triggers")
return
@@ -453,7 +472,7 @@ func (e StoreEncoder) encodeTrigger(ctx context.Context, p envoyx.EncodeParams,
}
// matchupTriggers returns an index with indicates what resources already exist
func (e StoreEncoder) matchupTriggers(ctx context.Context, s store.Storer, uu map[int]types.Trigger, nn envoyx.NodeSet) (err error) {
func (e StoreEncoder) matchupTriggers(ctx context.Context, s store.Storer, uu map[int]types.Trigger, scopes envoyx.NodeSet, nn envoyx.NodeSet) (err error) {
// @todo might need to do it smarter then this.
// Most resources won't really be that vast so this should be acceptable for now.
aa, _, err := store.SearchAutomationTriggers(ctx, s, types.TriggerFilter{})
@@ -472,6 +491,11 @@ func (e StoreEncoder) matchupTriggers(ctx context.Context, s store.Storer, uu ma
var aux *types.Trigger
var ok bool
for i, n := range nn {
scope := scopes[i]
if scope == nil {
continue
}
for _, idf := range n.Identifiers.Slice {
if id, err := strconv.ParseUint(idf, 10, 64); err == nil {
aux, ok = idMap[id]
@@ -530,3 +554,13 @@ func (e *StoreEncoder) runEvals(ctx context.Context, existing bool, n *envoyx.No
n.Evaluated.Skip, err = n.Config.SkipIfEval.Test(ctx, aux.(*expr.Vars))
return
}
func (e StoreEncoder) getScopeNodes(ctx context.Context, s store.Storer, nn envoyx.NodeSet) (scopes envoyx.NodeSet, err error) {
// Get all requested scopes
scopes = make(envoyx.NodeSet, len(nn))
// @note skipping scope logic since it's currently only supported within
// Compose resources.
return
}
@@ -111,10 +111,17 @@ func (e StoreEncoder) prepare{{.expIdent}}(ctx context.Context, p envoyx.EncodeP
// into memory shouldn't hurt too much.
// @todo do some benchmarks and potentially implement some smarter check such as
// a bloom filter or something similar.
// Get node scopes
scopedNodes, err := e.getScopeNodes(ctx, s, nn)
if err != nil {
err = errors.Wrap(err, "failed to get scope nodes")
return
}
// Initializing the index here (and using a hashmap) so it's not escaped to the heap
existing := make(map[int]types.{{.expIdent}}, len(nn))
err = e.matchup{{.expIdent}}s(ctx, s, existing, nn)
err = e.matchup{{.expIdent}}s(ctx, s, existing, scopedNodes, nn)
if err != nil {
err = errors.Wrap(err, "failed to matchup existing {{.expIdent}}s")
return
@@ -312,7 +319,7 @@ func (e StoreEncoder) encode{{.expIdent}}(ctx context.Context, p envoyx.EncodePa
}
// matchup{{.expIdent}}s returns an index with indicates what resources already exist
func (e StoreEncoder) matchup{{.expIdent}}s(ctx context.Context, s store.Storer, uu map[int]types.{{.expIdent}}, nn envoyx.NodeSet) (err error) {
func (e StoreEncoder) matchup{{.expIdent}}s(ctx context.Context, s store.Storer, uu map[int]types.{{.expIdent}}, scopes envoyx.NodeSet, nn envoyx.NodeSet) (err error) {
// @todo might need to do it smarter then this.
// Most resources won't really be that vast so this should be acceptable for now.
aa, _, err := store.Search{{.store.expIdentPlural}}(ctx, s, types.{{.expIdent}}Filter{})
@@ -339,6 +346,11 @@ func (e StoreEncoder) matchup{{.expIdent}}s(ctx context.Context, s store.Storer,
var aux *types.{{.expIdent}}
var ok bool
for i, n := range nn {
scope := scopes[i]
if scope == nil {
continue
}
for _, idf := range n.Identifiers.Slice {
if id, err := strconv.ParseUint(idf, 10, 64); err == nil {
aux, ok = idMap[id]
@@ -399,3 +411,93 @@ func (e *StoreEncoder) runEvals(ctx context.Context, existing bool, n *envoyx.No
n.Evaluated.Skip, err = n.Config.SkipIfEval.Test(ctx, aux.(*expr.Vars))
return
}
func (e StoreEncoder) getScopeNodes(ctx context.Context, s store.Storer, nn envoyx.NodeSet) (scopes envoyx.NodeSet, err error) {
// Get all requested scopes
scopes = make(envoyx.NodeSet, len(nn))
{{ if eq .componentIdent "compose" }}
err = func() (err error) {
for i, n := range nn {
if n.Scope.ResourceType == "" {
continue
}
// For now the scope can only point to namespace so this will do
var nn envoyx.NodeSet
nn, err = e.decodeNamespace(ctx, s, e.makeNamespaceFilter(nil, nil, envoyx.ResourceFilter{Identifiers: n.Scope.Identifiers}))
if err != nil {
return
}
if len(nn) > 1 {
err = fmt.Errorf("ambiguous scope %v: matches multiple resources", n.Scope)
return
}
// when encoding, it could be missing
if len(nn) == 0 {
return
}
scopes[i] = nn[0]
}
return
}()
if err != nil {
err = errors.Wrap(err, "failed to decode node scopes")
return
}
{{ else }}
// @note skipping scope logic since it's currently only supported within
// Compose resources.
{{ end }}
return
}
{{ if eq .componentIdent "compose" }}
func (e StoreEncoder) decodeNamespace(ctx context.Context, s store.Storer, f types.NamespaceFilter) (out envoyx.NodeSet, err error) {
// @todo this might need to be improved.
// Currently, no resource is vast enough to pose a problem.
rr, _, err := store.SearchComposeNamespaces(ctx, s, f)
if err != nil {
return
}
for _, r := range rr {
var n *envoyx.Node
n, err = NamespaceToEnvoyNode(r)
if err != nil {
return
}
out = append(out, n)
}
return
}
func (e StoreEncoder) makeNamespaceFilter(scope *envoyx.Node, refs map[string]*envoyx.Node, auxf envoyx.ResourceFilter) (out types.NamespaceFilter) {
out.Limit = auxf.Limit
ids, hh := auxf.Identifiers.Idents()
_ = ids
_ = hh
out.NamespaceID = ids
if len(hh) > 0 {
out.Slug = hh[0]
}
if scope == nil {
return
}
if scope.ResourceType == "" {
return
}
// Overwrite it
out.NamespaceID = []uint64{scope.Resource.GetID()}
return
}
{{ end }}
+155 -10
View File
@@ -117,9 +117,16 @@ func (e StoreEncoder) prepareChart(ctx context.Context, p envoyx.EncodeParams, s
// @todo do some benchmarks and potentially implement some smarter check such as
// a bloom filter or something similar.
// Get node scopes
scopedNodes, err := e.getScopeNodes(ctx, s, nn)
if err != nil {
err = errors.Wrap(err, "failed to get scope nodes")
return
}
// Initializing the index here (and using a hashmap) so it's not escaped to the heap
existing := make(map[int]types.Chart, len(nn))
err = e.matchupCharts(ctx, s, existing, nn)
err = e.matchupCharts(ctx, s, existing, scopedNodes, nn)
if err != nil {
err = errors.Wrap(err, "failed to matchup existing Charts")
return
@@ -265,7 +272,7 @@ func (e StoreEncoder) encodeChart(ctx context.Context, p envoyx.EncodeParams, s
}
// matchupCharts returns an index with indicates what resources already exist
func (e StoreEncoder) matchupCharts(ctx context.Context, s store.Storer, uu map[int]types.Chart, nn envoyx.NodeSet) (err error) {
func (e StoreEncoder) matchupCharts(ctx context.Context, s store.Storer, uu map[int]types.Chart, scopes envoyx.NodeSet, nn envoyx.NodeSet) (err error) {
// @todo might need to do it smarter then this.
// Most resources won't really be that vast so this should be acceptable for now.
aa, _, err := store.SearchComposeCharts(ctx, s, types.ChartFilter{})
@@ -285,6 +292,11 @@ func (e StoreEncoder) matchupCharts(ctx context.Context, s store.Storer, uu map[
var aux *types.Chart
var ok bool
for i, n := range nn {
scope := scopes[i]
if scope == nil {
continue
}
for _, idf := range n.Identifiers.Slice {
if id, err := strconv.ParseUint(idf, 10, 64); err == nil {
aux, ok = idMap[id]
@@ -320,9 +332,16 @@ func (e StoreEncoder) prepareModule(ctx context.Context, p envoyx.EncodeParams,
// @todo do some benchmarks and potentially implement some smarter check such as
// a bloom filter or something similar.
// Get node scopes
scopedNodes, err := e.getScopeNodes(ctx, s, nn)
if err != nil {
err = errors.Wrap(err, "failed to get scope nodes")
return
}
// Initializing the index here (and using a hashmap) so it's not escaped to the heap
existing := make(map[int]types.Module, len(nn))
err = e.matchupModules(ctx, s, existing, nn)
err = e.matchupModules(ctx, s, existing, scopedNodes, nn)
if err != nil {
err = errors.Wrap(err, "failed to matchup existing Modules")
return
@@ -490,7 +509,7 @@ func (e StoreEncoder) encodeModule(ctx context.Context, p envoyx.EncodeParams, s
}
// matchupModules returns an index with indicates what resources already exist
func (e StoreEncoder) matchupModules(ctx context.Context, s store.Storer, uu map[int]types.Module, nn envoyx.NodeSet) (err error) {
func (e StoreEncoder) matchupModules(ctx context.Context, s store.Storer, uu map[int]types.Module, scopes envoyx.NodeSet, nn envoyx.NodeSet) (err error) {
// @todo might need to do it smarter then this.
// Most resources won't really be that vast so this should be acceptable for now.
aa, _, err := store.SearchComposeModules(ctx, s, types.ModuleFilter{})
@@ -510,6 +529,11 @@ func (e StoreEncoder) matchupModules(ctx context.Context, s store.Storer, uu map
var aux *types.Module
var ok bool
for i, n := range nn {
scope := scopes[i]
if scope == nil {
continue
}
for _, idf := range n.Identifiers.Slice {
if id, err := strconv.ParseUint(idf, 10, 64); err == nil {
aux, ok = idMap[id]
@@ -545,9 +569,16 @@ func (e StoreEncoder) prepareModuleField(ctx context.Context, p envoyx.EncodePar
// @todo do some benchmarks and potentially implement some smarter check such as
// a bloom filter or something similar.
// Get node scopes
scopedNodes, err := e.getScopeNodes(ctx, s, nn)
if err != nil {
err = errors.Wrap(err, "failed to get scope nodes")
return
}
// Initializing the index here (and using a hashmap) so it's not escaped to the heap
existing := make(map[int]types.ModuleField, len(nn))
err = e.matchupModuleFields(ctx, s, existing, nn)
err = e.matchupModuleFields(ctx, s, existing, scopedNodes, nn)
if err != nil {
err = errors.Wrap(err, "failed to matchup existing ModuleFields")
return
@@ -697,7 +728,7 @@ func (e StoreEncoder) encodeModuleField(ctx context.Context, p envoyx.EncodePara
}
// matchupModuleFields returns an index with indicates what resources already exist
func (e StoreEncoder) matchupModuleFields(ctx context.Context, s store.Storer, uu map[int]types.ModuleField, nn envoyx.NodeSet) (err error) {
func (e StoreEncoder) matchupModuleFields(ctx context.Context, s store.Storer, uu map[int]types.ModuleField, scopes envoyx.NodeSet, nn envoyx.NodeSet) (err error) {
// @todo might need to do it smarter then this.
// Most resources won't really be that vast so this should be acceptable for now.
aa, _, err := store.SearchComposeModuleFields(ctx, s, types.ModuleFieldFilter{})
@@ -717,6 +748,11 @@ func (e StoreEncoder) matchupModuleFields(ctx context.Context, s store.Storer, u
var aux *types.ModuleField
var ok bool
for i, n := range nn {
scope := scopes[i]
if scope == nil {
continue
}
for _, idf := range n.Identifiers.Slice {
if id, err := strconv.ParseUint(idf, 10, 64); err == nil {
aux, ok = idMap[id]
@@ -752,9 +788,16 @@ func (e StoreEncoder) prepareNamespace(ctx context.Context, p envoyx.EncodeParam
// @todo do some benchmarks and potentially implement some smarter check such as
// a bloom filter or something similar.
// Get node scopes
scopedNodes, err := e.getScopeNodes(ctx, s, nn)
if err != nil {
err = errors.Wrap(err, "failed to get scope nodes")
return
}
// Initializing the index here (and using a hashmap) so it's not escaped to the heap
existing := make(map[int]types.Namespace, len(nn))
err = e.matchupNamespaces(ctx, s, existing, nn)
err = e.matchupNamespaces(ctx, s, existing, scopedNodes, nn)
if err != nil {
err = errors.Wrap(err, "failed to matchup existing Namespaces")
return
@@ -918,7 +961,7 @@ func (e StoreEncoder) encodeNamespace(ctx context.Context, p envoyx.EncodeParams
}
// matchupNamespaces returns an index with indicates what resources already exist
func (e StoreEncoder) matchupNamespaces(ctx context.Context, s store.Storer, uu map[int]types.Namespace, nn envoyx.NodeSet) (err error) {
func (e StoreEncoder) matchupNamespaces(ctx context.Context, s store.Storer, uu map[int]types.Namespace, scopes envoyx.NodeSet, nn envoyx.NodeSet) (err error) {
// @todo might need to do it smarter then this.
// Most resources won't really be that vast so this should be acceptable for now.
aa, _, err := store.SearchComposeNamespaces(ctx, s, types.NamespaceFilter{})
@@ -938,6 +981,11 @@ func (e StoreEncoder) matchupNamespaces(ctx context.Context, s store.Storer, uu
var aux *types.Namespace
var ok bool
for i, n := range nn {
scope := scopes[i]
if scope == nil {
continue
}
for _, idf := range n.Identifiers.Slice {
if id, err := strconv.ParseUint(idf, 10, 64); err == nil {
aux, ok = idMap[id]
@@ -973,9 +1021,16 @@ func (e StoreEncoder) preparePage(ctx context.Context, p envoyx.EncodeParams, s
// @todo do some benchmarks and potentially implement some smarter check such as
// a bloom filter or something similar.
// Get node scopes
scopedNodes, err := e.getScopeNodes(ctx, s, nn)
if err != nil {
err = errors.Wrap(err, "failed to get scope nodes")
return
}
// Initializing the index here (and using a hashmap) so it's not escaped to the heap
existing := make(map[int]types.Page, len(nn))
err = e.matchupPages(ctx, s, existing, nn)
err = e.matchupPages(ctx, s, existing, scopedNodes, nn)
if err != nil {
err = errors.Wrap(err, "failed to matchup existing Pages")
return
@@ -1121,7 +1176,7 @@ func (e StoreEncoder) encodePage(ctx context.Context, p envoyx.EncodeParams, s s
}
// matchupPages returns an index with indicates what resources already exist
func (e StoreEncoder) matchupPages(ctx context.Context, s store.Storer, uu map[int]types.Page, nn envoyx.NodeSet) (err error) {
func (e StoreEncoder) matchupPages(ctx context.Context, s store.Storer, uu map[int]types.Page, scopes envoyx.NodeSet, nn envoyx.NodeSet) (err error) {
// @todo might need to do it smarter then this.
// Most resources won't really be that vast so this should be acceptable for now.
aa, _, err := store.SearchComposePages(ctx, s, types.PageFilter{})
@@ -1141,6 +1196,11 @@ func (e StoreEncoder) matchupPages(ctx context.Context, s store.Storer, uu map[i
var aux *types.Page
var ok bool
for i, n := range nn {
scope := scopes[i]
if scope == nil {
continue
}
for _, idf := range n.Identifiers.Slice {
if id, err := strconv.ParseUint(idf, 10, 64); err == nil {
aux, ok = idMap[id]
@@ -1199,3 +1259,88 @@ func (e *StoreEncoder) runEvals(ctx context.Context, existing bool, n *envoyx.No
n.Evaluated.Skip, err = n.Config.SkipIfEval.Test(ctx, aux.(*expr.Vars))
return
}
func (e StoreEncoder) getScopeNodes(ctx context.Context, s store.Storer, nn envoyx.NodeSet) (scopes envoyx.NodeSet, err error) {
// Get all requested scopes
scopes = make(envoyx.NodeSet, len(nn))
err = func() (err error) {
for i, n := range nn {
if n.Scope.ResourceType == "" {
continue
}
// For now the scope can only point to namespace so this will do
var nn envoyx.NodeSet
nn, err = e.decodeNamespace(ctx, s, e.makeNamespaceFilter(nil, nil, envoyx.ResourceFilter{Identifiers: n.Scope.Identifiers}))
if err != nil {
return
}
if len(nn) > 1 {
err = fmt.Errorf("ambiguous scope %v: matches multiple resources", n.Scope)
return
}
// when encoding, it could be missing
if len(nn) == 0 {
return
}
scopes[i] = nn[0]
}
return
}()
if err != nil {
err = errors.Wrap(err, "failed to decode node scopes")
return
}
return
}
func (e StoreEncoder) decodeNamespace(ctx context.Context, s store.Storer, f types.NamespaceFilter) (out envoyx.NodeSet, err error) {
// @todo this might need to be improved.
// Currently, no resource is vast enough to pose a problem.
rr, _, err := store.SearchComposeNamespaces(ctx, s, f)
if err != nil {
return
}
for _, r := range rr {
var n *envoyx.Node
n, err = NamespaceToEnvoyNode(r)
if err != nil {
return
}
out = append(out, n)
}
return
}
func (e StoreEncoder) makeNamespaceFilter(scope *envoyx.Node, refs map[string]*envoyx.Node, auxf envoyx.ResourceFilter) (out types.NamespaceFilter) {
out.Limit = auxf.Limit
ids, hh := auxf.Identifiers.Idents()
_ = ids
_ = hh
out.NamespaceID = ids
if len(hh) > 0 {
out.Slug = hh[0]
}
if scope == nil {
return
}
if scope.ResourceType == "" {
return
}
// Overwrite it
out.NamespaceID = []uint64{scope.Resource.GetID()}
return
}
+6
View File
@@ -45,6 +45,12 @@ func RBACRulesForNodes(rr rbac.RuleSet, nn ...*Node) (rules NodeSet, err error)
// @todo move over to that generated function
rulePath := splitResourcePath(r.Resource)
// Don't handle rules that are not a subset of the resource
// @todo this should be handled by the parent probably
if len(rulePath) > 0 && rulePath[0] == "*" || rulePath[0] == "" {
continue
}
if !isPathSubset(rulePath, resPath, true) {
// Mismatch; skip
continue
+164 -22
View File
@@ -150,9 +150,16 @@ func (e StoreEncoder) prepareApplication(ctx context.Context, p envoyx.EncodePar
// @todo do some benchmarks and potentially implement some smarter check such as
// a bloom filter or something similar.
// Get node scopes
scopedNodes, err := e.getScopeNodes(ctx, s, nn)
if err != nil {
err = errors.Wrap(err, "failed to get scope nodes")
return
}
// Initializing the index here (and using a hashmap) so it's not escaped to the heap
existing := make(map[int]types.Application, len(nn))
err = e.matchupApplications(ctx, s, existing, nn)
err = e.matchupApplications(ctx, s, existing, scopedNodes, nn)
if err != nil {
err = errors.Wrap(err, "failed to matchup existing Applications")
return
@@ -298,7 +305,7 @@ func (e StoreEncoder) encodeApplication(ctx context.Context, p envoyx.EncodePara
}
// matchupApplications returns an index with indicates what resources already exist
func (e StoreEncoder) matchupApplications(ctx context.Context, s store.Storer, uu map[int]types.Application, nn envoyx.NodeSet) (err error) {
func (e StoreEncoder) matchupApplications(ctx context.Context, s store.Storer, uu map[int]types.Application, scopes envoyx.NodeSet, nn envoyx.NodeSet) (err error) {
// @todo might need to do it smarter then this.
// Most resources won't really be that vast so this should be acceptable for now.
aa, _, err := store.SearchApplications(ctx, s, types.ApplicationFilter{})
@@ -317,6 +324,11 @@ func (e StoreEncoder) matchupApplications(ctx context.Context, s store.Storer, u
var aux *types.Application
var ok bool
for i, n := range nn {
scope := scopes[i]
if scope == nil {
continue
}
for _, idf := range n.Identifiers.Slice {
if id, err := strconv.ParseUint(idf, 10, 64); err == nil {
aux, ok = idMap[id]
@@ -352,9 +364,16 @@ func (e StoreEncoder) prepareApigwRoute(ctx context.Context, p envoyx.EncodePara
// @todo do some benchmarks and potentially implement some smarter check such as
// a bloom filter or something similar.
// Get node scopes
scopedNodes, err := e.getScopeNodes(ctx, s, nn)
if err != nil {
err = errors.Wrap(err, "failed to get scope nodes")
return
}
// Initializing the index here (and using a hashmap) so it's not escaped to the heap
existing := make(map[int]types.ApigwRoute, len(nn))
err = e.matchupApigwRoutes(ctx, s, existing, nn)
err = e.matchupApigwRoutes(ctx, s, existing, scopedNodes, nn)
if err != nil {
err = errors.Wrap(err, "failed to matchup existing ApigwRoutes")
return
@@ -500,7 +519,7 @@ func (e StoreEncoder) encodeApigwRoute(ctx context.Context, p envoyx.EncodeParam
}
// matchupApigwRoutes returns an index with indicates what resources already exist
func (e StoreEncoder) matchupApigwRoutes(ctx context.Context, s store.Storer, uu map[int]types.ApigwRoute, nn envoyx.NodeSet) (err error) {
func (e StoreEncoder) matchupApigwRoutes(ctx context.Context, s store.Storer, uu map[int]types.ApigwRoute, scopes envoyx.NodeSet, nn envoyx.NodeSet) (err error) {
// @todo might need to do it smarter then this.
// Most resources won't really be that vast so this should be acceptable for now.
aa, _, err := store.SearchApigwRoutes(ctx, s, types.ApigwRouteFilter{})
@@ -519,6 +538,11 @@ func (e StoreEncoder) matchupApigwRoutes(ctx context.Context, s store.Storer, uu
var aux *types.ApigwRoute
var ok bool
for i, n := range nn {
scope := scopes[i]
if scope == nil {
continue
}
for _, idf := range n.Identifiers.Slice {
if id, err := strconv.ParseUint(idf, 10, 64); err == nil {
aux, ok = idMap[id]
@@ -554,9 +578,16 @@ func (e StoreEncoder) prepareApigwFilter(ctx context.Context, p envoyx.EncodePar
// @todo do some benchmarks and potentially implement some smarter check such as
// a bloom filter or something similar.
// Get node scopes
scopedNodes, err := e.getScopeNodes(ctx, s, nn)
if err != nil {
err = errors.Wrap(err, "failed to get scope nodes")
return
}
// Initializing the index here (and using a hashmap) so it's not escaped to the heap
existing := make(map[int]types.ApigwFilter, len(nn))
err = e.matchupApigwFilters(ctx, s, existing, nn)
err = e.matchupApigwFilters(ctx, s, existing, scopedNodes, nn)
if err != nil {
err = errors.Wrap(err, "failed to matchup existing ApigwFilters")
return
@@ -702,7 +733,7 @@ func (e StoreEncoder) encodeApigwFilter(ctx context.Context, p envoyx.EncodePara
}
// matchupApigwFilters returns an index with indicates what resources already exist
func (e StoreEncoder) matchupApigwFilters(ctx context.Context, s store.Storer, uu map[int]types.ApigwFilter, nn envoyx.NodeSet) (err error) {
func (e StoreEncoder) matchupApigwFilters(ctx context.Context, s store.Storer, uu map[int]types.ApigwFilter, scopes envoyx.NodeSet, nn envoyx.NodeSet) (err error) {
// @todo might need to do it smarter then this.
// Most resources won't really be that vast so this should be acceptable for now.
aa, _, err := store.SearchApigwFilters(ctx, s, types.ApigwFilterFilter{})
@@ -721,6 +752,11 @@ func (e StoreEncoder) matchupApigwFilters(ctx context.Context, s store.Storer, u
var aux *types.ApigwFilter
var ok bool
for i, n := range nn {
scope := scopes[i]
if scope == nil {
continue
}
for _, idf := range n.Identifiers.Slice {
if id, err := strconv.ParseUint(idf, 10, 64); err == nil {
aux, ok = idMap[id]
@@ -756,9 +792,16 @@ func (e StoreEncoder) prepareAuthClient(ctx context.Context, p envoyx.EncodePara
// @todo do some benchmarks and potentially implement some smarter check such as
// a bloom filter or something similar.
// Get node scopes
scopedNodes, err := e.getScopeNodes(ctx, s, nn)
if err != nil {
err = errors.Wrap(err, "failed to get scope nodes")
return
}
// Initializing the index here (and using a hashmap) so it's not escaped to the heap
existing := make(map[int]types.AuthClient, len(nn))
err = e.matchupAuthClients(ctx, s, existing, nn)
err = e.matchupAuthClients(ctx, s, existing, scopedNodes, nn)
if err != nil {
err = errors.Wrap(err, "failed to matchup existing AuthClients")
return
@@ -904,7 +947,7 @@ func (e StoreEncoder) encodeAuthClient(ctx context.Context, p envoyx.EncodeParam
}
// matchupAuthClients returns an index with indicates what resources already exist
func (e StoreEncoder) matchupAuthClients(ctx context.Context, s store.Storer, uu map[int]types.AuthClient, nn envoyx.NodeSet) (err error) {
func (e StoreEncoder) matchupAuthClients(ctx context.Context, s store.Storer, uu map[int]types.AuthClient, scopes envoyx.NodeSet, nn envoyx.NodeSet) (err error) {
// @todo might need to do it smarter then this.
// Most resources won't really be that vast so this should be acceptable for now.
aa, _, err := store.SearchAuthClients(ctx, s, types.AuthClientFilter{})
@@ -924,6 +967,11 @@ func (e StoreEncoder) matchupAuthClients(ctx context.Context, s store.Storer, uu
var aux *types.AuthClient
var ok bool
for i, n := range nn {
scope := scopes[i]
if scope == nil {
continue
}
for _, idf := range n.Identifiers.Slice {
if id, err := strconv.ParseUint(idf, 10, 64); err == nil {
aux, ok = idMap[id]
@@ -959,9 +1007,16 @@ func (e StoreEncoder) prepareQueue(ctx context.Context, p envoyx.EncodeParams, s
// @todo do some benchmarks and potentially implement some smarter check such as
// a bloom filter or something similar.
// Get node scopes
scopedNodes, err := e.getScopeNodes(ctx, s, nn)
if err != nil {
err = errors.Wrap(err, "failed to get scope nodes")
return
}
// Initializing the index here (and using a hashmap) so it's not escaped to the heap
existing := make(map[int]types.Queue, len(nn))
err = e.matchupQueues(ctx, s, existing, nn)
err = e.matchupQueues(ctx, s, existing, scopedNodes, nn)
if err != nil {
err = errors.Wrap(err, "failed to matchup existing Queues")
return
@@ -1107,7 +1162,7 @@ func (e StoreEncoder) encodeQueue(ctx context.Context, p envoyx.EncodeParams, s
}
// matchupQueues returns an index with indicates what resources already exist
func (e StoreEncoder) matchupQueues(ctx context.Context, s store.Storer, uu map[int]types.Queue, nn envoyx.NodeSet) (err error) {
func (e StoreEncoder) matchupQueues(ctx context.Context, s store.Storer, uu map[int]types.Queue, scopes envoyx.NodeSet, nn envoyx.NodeSet) (err error) {
// @todo might need to do it smarter then this.
// Most resources won't really be that vast so this should be acceptable for now.
aa, _, err := store.SearchQueues(ctx, s, types.QueueFilter{})
@@ -1127,6 +1182,11 @@ func (e StoreEncoder) matchupQueues(ctx context.Context, s store.Storer, uu map[
var aux *types.Queue
var ok bool
for i, n := range nn {
scope := scopes[i]
if scope == nil {
continue
}
for _, idf := range n.Identifiers.Slice {
if id, err := strconv.ParseUint(idf, 10, 64); err == nil {
aux, ok = idMap[id]
@@ -1162,9 +1222,16 @@ func (e StoreEncoder) prepareReport(ctx context.Context, p envoyx.EncodeParams,
// @todo do some benchmarks and potentially implement some smarter check such as
// a bloom filter or something similar.
// Get node scopes
scopedNodes, err := e.getScopeNodes(ctx, s, nn)
if err != nil {
err = errors.Wrap(err, "failed to get scope nodes")
return
}
// Initializing the index here (and using a hashmap) so it's not escaped to the heap
existing := make(map[int]types.Report, len(nn))
err = e.matchupReports(ctx, s, existing, nn)
err = e.matchupReports(ctx, s, existing, scopedNodes, nn)
if err != nil {
err = errors.Wrap(err, "failed to matchup existing Reports")
return
@@ -1310,7 +1377,7 @@ func (e StoreEncoder) encodeReport(ctx context.Context, p envoyx.EncodeParams, s
}
// matchupReports returns an index with indicates what resources already exist
func (e StoreEncoder) matchupReports(ctx context.Context, s store.Storer, uu map[int]types.Report, nn envoyx.NodeSet) (err error) {
func (e StoreEncoder) matchupReports(ctx context.Context, s store.Storer, uu map[int]types.Report, scopes envoyx.NodeSet, nn envoyx.NodeSet) (err error) {
// @todo might need to do it smarter then this.
// Most resources won't really be that vast so this should be acceptable for now.
aa, _, err := store.SearchReports(ctx, s, types.ReportFilter{})
@@ -1330,6 +1397,11 @@ func (e StoreEncoder) matchupReports(ctx context.Context, s store.Storer, uu map
var aux *types.Report
var ok bool
for i, n := range nn {
scope := scopes[i]
if scope == nil {
continue
}
for _, idf := range n.Identifiers.Slice {
if id, err := strconv.ParseUint(idf, 10, 64); err == nil {
aux, ok = idMap[id]
@@ -1365,9 +1437,16 @@ func (e StoreEncoder) prepareRole(ctx context.Context, p envoyx.EncodeParams, s
// @todo do some benchmarks and potentially implement some smarter check such as
// a bloom filter or something similar.
// Get node scopes
scopedNodes, err := e.getScopeNodes(ctx, s, nn)
if err != nil {
err = errors.Wrap(err, "failed to get scope nodes")
return
}
// Initializing the index here (and using a hashmap) so it's not escaped to the heap
existing := make(map[int]types.Role, len(nn))
err = e.matchupRoles(ctx, s, existing, nn)
err = e.matchupRoles(ctx, s, existing, scopedNodes, nn)
if err != nil {
err = errors.Wrap(err, "failed to matchup existing Roles")
return
@@ -1513,7 +1592,7 @@ func (e StoreEncoder) encodeRole(ctx context.Context, p envoyx.EncodeParams, s s
}
// matchupRoles returns an index with indicates what resources already exist
func (e StoreEncoder) matchupRoles(ctx context.Context, s store.Storer, uu map[int]types.Role, nn envoyx.NodeSet) (err error) {
func (e StoreEncoder) matchupRoles(ctx context.Context, s store.Storer, uu map[int]types.Role, scopes envoyx.NodeSet, nn envoyx.NodeSet) (err error) {
// @todo might need to do it smarter then this.
// Most resources won't really be that vast so this should be acceptable for now.
aa, _, err := store.SearchRoles(ctx, s, types.RoleFilter{})
@@ -1533,6 +1612,11 @@ func (e StoreEncoder) matchupRoles(ctx context.Context, s store.Storer, uu map[i
var aux *types.Role
var ok bool
for i, n := range nn {
scope := scopes[i]
if scope == nil {
continue
}
for _, idf := range n.Identifiers.Slice {
if id, err := strconv.ParseUint(idf, 10, 64); err == nil {
aux, ok = idMap[id]
@@ -1568,9 +1652,16 @@ func (e StoreEncoder) prepareTemplate(ctx context.Context, p envoyx.EncodeParams
// @todo do some benchmarks and potentially implement some smarter check such as
// a bloom filter or something similar.
// Get node scopes
scopedNodes, err := e.getScopeNodes(ctx, s, nn)
if err != nil {
err = errors.Wrap(err, "failed to get scope nodes")
return
}
// Initializing the index here (and using a hashmap) so it's not escaped to the heap
existing := make(map[int]types.Template, len(nn))
err = e.matchupTemplates(ctx, s, existing, nn)
err = e.matchupTemplates(ctx, s, existing, scopedNodes, nn)
if err != nil {
err = errors.Wrap(err, "failed to matchup existing Templates")
return
@@ -1716,7 +1807,7 @@ func (e StoreEncoder) encodeTemplate(ctx context.Context, p envoyx.EncodeParams,
}
// matchupTemplates returns an index with indicates what resources already exist
func (e StoreEncoder) matchupTemplates(ctx context.Context, s store.Storer, uu map[int]types.Template, nn envoyx.NodeSet) (err error) {
func (e StoreEncoder) matchupTemplates(ctx context.Context, s store.Storer, uu map[int]types.Template, scopes envoyx.NodeSet, nn envoyx.NodeSet) (err error) {
// @todo might need to do it smarter then this.
// Most resources won't really be that vast so this should be acceptable for now.
aa, _, err := store.SearchTemplates(ctx, s, types.TemplateFilter{})
@@ -1736,6 +1827,11 @@ func (e StoreEncoder) matchupTemplates(ctx context.Context, s store.Storer, uu m
var aux *types.Template
var ok bool
for i, n := range nn {
scope := scopes[i]
if scope == nil {
continue
}
for _, idf := range n.Identifiers.Slice {
if id, err := strconv.ParseUint(idf, 10, 64); err == nil {
aux, ok = idMap[id]
@@ -1771,9 +1867,16 @@ func (e StoreEncoder) prepareUser(ctx context.Context, p envoyx.EncodeParams, s
// @todo do some benchmarks and potentially implement some smarter check such as
// a bloom filter or something similar.
// Get node scopes
scopedNodes, err := e.getScopeNodes(ctx, s, nn)
if err != nil {
err = errors.Wrap(err, "failed to get scope nodes")
return
}
// Initializing the index here (and using a hashmap) so it's not escaped to the heap
existing := make(map[int]types.User, len(nn))
err = e.matchupUsers(ctx, s, existing, nn)
err = e.matchupUsers(ctx, s, existing, scopedNodes, nn)
if err != nil {
err = errors.Wrap(err, "failed to matchup existing Users")
return
@@ -1919,7 +2022,7 @@ func (e StoreEncoder) encodeUser(ctx context.Context, p envoyx.EncodeParams, s s
}
// matchupUsers returns an index with indicates what resources already exist
func (e StoreEncoder) matchupUsers(ctx context.Context, s store.Storer, uu map[int]types.User, nn envoyx.NodeSet) (err error) {
func (e StoreEncoder) matchupUsers(ctx context.Context, s store.Storer, uu map[int]types.User, scopes envoyx.NodeSet, nn envoyx.NodeSet) (err error) {
// @todo might need to do it smarter then this.
// Most resources won't really be that vast so this should be acceptable for now.
aa, _, err := store.SearchUsers(ctx, s, types.UserFilter{})
@@ -1939,6 +2042,11 @@ func (e StoreEncoder) matchupUsers(ctx context.Context, s store.Storer, uu map[i
var aux *types.User
var ok bool
for i, n := range nn {
scope := scopes[i]
if scope == nil {
continue
}
for _, idf := range n.Identifiers.Slice {
if id, err := strconv.ParseUint(idf, 10, 64); err == nil {
aux, ok = idMap[id]
@@ -1974,9 +2082,16 @@ func (e StoreEncoder) prepareDalConnection(ctx context.Context, p envoyx.EncodeP
// @todo do some benchmarks and potentially implement some smarter check such as
// a bloom filter or something similar.
// Get node scopes
scopedNodes, err := e.getScopeNodes(ctx, s, nn)
if err != nil {
err = errors.Wrap(err, "failed to get scope nodes")
return
}
// Initializing the index here (and using a hashmap) so it's not escaped to the heap
existing := make(map[int]types.DalConnection, len(nn))
err = e.matchupDalConnections(ctx, s, existing, nn)
err = e.matchupDalConnections(ctx, s, existing, scopedNodes, nn)
if err != nil {
err = errors.Wrap(err, "failed to matchup existing DalConnections")
return
@@ -2122,7 +2237,7 @@ func (e StoreEncoder) encodeDalConnection(ctx context.Context, p envoyx.EncodePa
}
// matchupDalConnections returns an index with indicates what resources already exist
func (e StoreEncoder) matchupDalConnections(ctx context.Context, s store.Storer, uu map[int]types.DalConnection, nn envoyx.NodeSet) (err error) {
func (e StoreEncoder) matchupDalConnections(ctx context.Context, s store.Storer, uu map[int]types.DalConnection, scopes envoyx.NodeSet, nn envoyx.NodeSet) (err error) {
// @todo might need to do it smarter then this.
// Most resources won't really be that vast so this should be acceptable for now.
aa, _, err := store.SearchDalConnections(ctx, s, types.DalConnectionFilter{})
@@ -2142,6 +2257,11 @@ func (e StoreEncoder) matchupDalConnections(ctx context.Context, s store.Storer,
var aux *types.DalConnection
var ok bool
for i, n := range nn {
scope := scopes[i]
if scope == nil {
continue
}
for _, idf := range n.Identifiers.Slice {
if id, err := strconv.ParseUint(idf, 10, 64); err == nil {
aux, ok = idMap[id]
@@ -2177,9 +2297,16 @@ func (e StoreEncoder) prepareDalSensitivityLevel(ctx context.Context, p envoyx.E
// @todo do some benchmarks and potentially implement some smarter check such as
// a bloom filter or something similar.
// Get node scopes
scopedNodes, err := e.getScopeNodes(ctx, s, nn)
if err != nil {
err = errors.Wrap(err, "failed to get scope nodes")
return
}
// Initializing the index here (and using a hashmap) so it's not escaped to the heap
existing := make(map[int]types.DalSensitivityLevel, len(nn))
err = e.matchupDalSensitivityLevels(ctx, s, existing, nn)
err = e.matchupDalSensitivityLevels(ctx, s, existing, scopedNodes, nn)
if err != nil {
err = errors.Wrap(err, "failed to matchup existing DalSensitivityLevels")
return
@@ -2325,7 +2452,7 @@ func (e StoreEncoder) encodeDalSensitivityLevel(ctx context.Context, p envoyx.En
}
// matchupDalSensitivityLevels returns an index with indicates what resources already exist
func (e StoreEncoder) matchupDalSensitivityLevels(ctx context.Context, s store.Storer, uu map[int]types.DalSensitivityLevel, nn envoyx.NodeSet) (err error) {
func (e StoreEncoder) matchupDalSensitivityLevels(ctx context.Context, s store.Storer, uu map[int]types.DalSensitivityLevel, scopes envoyx.NodeSet, nn envoyx.NodeSet) (err error) {
// @todo might need to do it smarter then this.
// Most resources won't really be that vast so this should be acceptable for now.
aa, _, err := store.SearchDalSensitivityLevels(ctx, s, types.DalSensitivityLevelFilter{})
@@ -2345,6 +2472,11 @@ func (e StoreEncoder) matchupDalSensitivityLevels(ctx context.Context, s store.S
var aux *types.DalSensitivityLevel
var ok bool
for i, n := range nn {
scope := scopes[i]
if scope == nil {
continue
}
for _, idf := range n.Identifiers.Slice {
if id, err := strconv.ParseUint(idf, 10, 64); err == nil {
aux, ok = idMap[id]
@@ -2403,3 +2535,13 @@ func (e *StoreEncoder) runEvals(ctx context.Context, existing bool, n *envoyx.No
n.Evaluated.Skip, err = n.Config.SkipIfEval.Test(ctx, aux.(*expr.Vars))
return
}
func (e StoreEncoder) getScopeNodes(ctx context.Context, s store.Storer, nn envoyx.NodeSet) (scopes envoyx.NodeSet, err error) {
// Get all requested scopes
scopes = make(envoyx.NodeSet, len(nn))
// @note skipping scope logic since it's currently only supported within
// Compose resources.
return
}
@@ -127,14 +127,14 @@ func Test_namespace_duplicate(t *testing.T) {
checkID(h, mod3.ID)
h.a.Len(mod1.Fields, 4)
h.a.Equal("Record", mod1.Fields[3].Kind)
h.a.Equal(mod2.ID, mod1.Fields[3].Options.UInt64("moduleID"))
h.a.Equal("Record", mod1.Fields.FindByName("f4").Kind)
h.a.Equal(mod2.ID, mod1.Fields.FindByName("f4").Options.UInt64("moduleID"))
h.a.Len(mod2.Fields, 4)
h.a.Equal("Record", mod2.Fields[1].Kind)
h.a.Equal(mod2.ID, mod2.Fields[1].Options.UInt64("moduleID"))
h.a.Equal("Record", mod2.Fields[3].Kind)
h.a.Equal(mod3.ID, mod2.Fields[3].Options.UInt64("moduleID"))
h.a.Equal("Record", mod2.Fields.FindByName("f_ref_self").Kind)
h.a.Equal(mod2.ID, mod2.Fields.FindByName("f_ref_self").Options.UInt64("moduleID"))
h.a.Equal("Record", mod2.Fields.FindByName("f2").Kind)
h.a.Equal(mod3.ID, mod2.Fields.FindByName("f2").Options.UInt64("moduleID"))
h.a.Len(mod3.Fields, 1)