Skip to content

Commit

Permalink
fix: errors occurred while creating the built-in resource definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
aiwantaozi authored and alexcodelf committed Mar 11, 2024
1 parent fa6e56a commit 61fe2d2
Showing 1 changed file with 21 additions and 23 deletions.
44 changes: 21 additions & 23 deletions pkg/resourcedefinitions/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@ package resourcedefinitions

import (
"context"
"fmt"
"sort"
"strings"

"golang.org/x/exp/slices"

"github.com/seal-io/walrus/pkg/bus/builtin"
"github.com/seal-io/walrus/pkg/catalog"
"github.com/seal-io/walrus/pkg/dao"
"github.com/seal-io/walrus/pkg/dao/model"
"github.com/seal-io/walrus/pkg/dao/model/resourcedefinition"
"github.com/seal-io/walrus/pkg/dao/model/template"
"github.com/seal-io/walrus/pkg/dao/model/templateversion"
"github.com/seal-io/walrus/pkg/dao/types"
"github.com/seal-io/walrus/pkg/templates"
"github.com/seal-io/walrus/pkg/dao/types/object"
pkgtemplates "github.com/seal-io/walrus/pkg/templates"
"github.com/seal-io/walrus/utils/log"
)

Expand All @@ -31,7 +29,7 @@ func SyncBuiltinResourceDefinitions(ctx context.Context, m builtin.BusMessage) e
return err
}

resourceTypeToConnectorTypes := make(map[string][]string)
resourceTypeToConnectorTypes := make(map[string][]*model.Template)

for _, t := range ts {
labels := t.Labels
Expand All @@ -46,24 +44,21 @@ func SyncBuiltinResourceDefinitions(ctx context.Context, m builtin.BusMessage) e
continue
}

ct, ok := labels[types.LabelWalrusConnectorType]
_, ok = labels[types.LabelWalrusConnectorType]
if !ok {
logger.Warnf("builtin template %s missing label %s", t.Name, types.LabelWalrusConnectorType)
continue
}

resourceTypeToConnectorTypes[rt] = append(resourceTypeToConnectorTypes[rt], ct)
resourceTypeToConnectorTypes[rt] = append(resourceTypeToConnectorTypes[rt], t)
}

resourceDefinitions := make([]*model.ResourceDefinition, 0, len(resourceTypeToConnectorTypes))

for res, conns := range resourceTypeToConnectorTypes {
// Sort the connector types to ensure the order of matching rules is deterministic.
slices.Sort(conns)

for res, ts := range resourceTypeToConnectorTypes {
var definition *model.ResourceDefinition

definition, err = newResourceDefinition(ctx, mc, res, conns)
definition, err = newResourceDefinition(ctx, mc, res, ts)
if err != nil {
logger.Errorf("failed to create builtin %s resource definition: %v", res, err)
continue
Expand All @@ -90,16 +85,16 @@ func newResourceDefinition(
ctx context.Context,
mc model.ClientSet,
resourceType string,
connectorTypes []string,
templates []*model.Template,
) (*model.ResourceDefinition, error) {
logger := log.WithName("builtin").WithName("resource-definitions")

matchingRules := make([]*model.ResourceDefinitionMatchingRule, 0, len(connectorTypes))
matchingRules := make([]*model.ResourceDefinitionMatchingRule, 0, len(templates))

for _, connectorType := range connectorTypes {
ct := strings.ToLower(connectorType)
for _, t := range templates {
ct := strings.ToLower(t.Labels[types.LabelWalrusConnectorType])

m, err := newMatchingRule(ctx, mc, resourceType, ct)
m, err := newMatchingRule(ctx, mc, t.ID, ct)
if err != nil {
logger.Errorf("failed to create matching rule for builtin %s-%s: %v", ct, resourceType, err)
continue
Expand All @@ -108,6 +103,11 @@ func newResourceDefinition(
matchingRules = append(matchingRules, m)
}

// Sort the matchingRules to ensure the order is deterministic.
sort.SliceStable(matchingRules, func(i, j int) bool {
return matchingRules[i].Name < matchingRules[j].Name
})

bn := "builtin-" + resourceType
rd := &model.ResourceDefinition{
Name: bn,
Expand All @@ -119,7 +119,7 @@ func newResourceDefinition(
},
}

err := templates.SetResourceDefinitionSchemaDefault(ctx, mc, rd)
err := pkgtemplates.SetResourceDefinitionSchemaDefault(ctx, mc, rd)
if err != nil {
return nil, err
}
Expand All @@ -135,13 +135,11 @@ func newResourceDefinition(
func newMatchingRule(
ctx context.Context,
mc model.ClientSet,
resourceType string,
templateID object.ID,
connectorType string,
) (*model.ResourceDefinitionMatchingRule, error) {
name := fmt.Sprintf("%s/%s-%s", catalog.BuiltinCatalog().Name, connectorType, resourceType)

version, err := mc.TemplateVersions().Query().
Where(templateversion.Name(name)).
Where(templateversion.TemplateID(templateID)).
Order(dao.OrderSemverVersionFunc).
First(ctx)
if err != nil {
Expand Down

0 comments on commit 61fe2d2

Please sign in to comment.