Skip to content
This repository has been archived by the owner on Sep 16, 2024. It is now read-only.

Commit

Permalink
feat: record environment id in service resource
Browse files Browse the repository at this point in the history
- return the related ids of a service and service resource in the graph
  apis

Signed-off-by: thxCode <[email protected]>
  • Loading branch information
thxCode authored and alexcodelf committed Aug 16, 2023
1 parent a90808d commit 00fc7f0
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 90 deletions.
29 changes: 15 additions & 14 deletions pkg/apis/environment/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@ import (
"github.com/seal-io/seal/pkg/dao/model"
"github.com/seal-io/seal/pkg/dao/model/service"
"github.com/seal-io/seal/pkg/dao/model/servicerelationship"
"github.com/seal-io/seal/pkg/dao/model/serviceresource"
"github.com/seal-io/seal/pkg/dao/types"
"github.com/seal-io/seal/pkg/dao/types/object"
optypes "github.com/seal-io/seal/pkg/operator/types"
pkgresource "github.com/seal-io/seal/pkg/serviceresources"
)

var getServiceFields = service.WithoutFields(
service.FieldUpdateTime)

func (h Handler) RouteGetGraph(req RouteGetGraphRequest) (*RouteGetGraphResponse, error) {
// Fetch service entities.
entities, err := h.modelClient.Services().Query().
Where(service.EnvironmentID(req.ID)).
Order(model.Desc(service.FieldCreateTime)).
Select(getFields...).
Select(getServiceFields...).
// Must extract dependency.
WithDependencies(func(dq *model.ServiceRelationshipQuery) {
dq.Select(servicerelationship.FieldDependencyID).
Expand All @@ -29,12 +31,7 @@ func (h Handler) RouteGetGraph(req RouteGetGraphRequest) (*RouteGetGraphResponse
}).
// Must extract resource.
WithResources(func(rq *model.ServiceResourceQuery) {
dao.ServiceResourceShapeClassQuery(rq).
Where(
serviceresource.Shape(types.ServiceResourceShapeClass),
serviceresource.Mode(types.ServiceResourceModeManaged),
).
Order(model.Desc(serviceresource.FieldCreateTime))
dao.ServiceResourceShapeClassQuery(rq)
}).
Unique(false).
All(req.Context)
Expand All @@ -53,6 +50,7 @@ func (h Handler) RouteGetGraph(req RouteGetGraphRequest) (*RouteGetGraphResponse

for i := 0; i < len(entities); i++ {
entity := entities[i]

// Append Service to vertices.
vertices = append(vertices, GraphVertex{
GraphVertexID: GraphVertexID{
Expand All @@ -65,9 +63,13 @@ func (h Handler) RouteGetGraph(req RouteGetGraphRequest) (*RouteGetGraphResponse
CreateTime: entity.CreateTime,
UpdateTime: entity.UpdateTime,
Status: entity.Status.Summary,
Extensions: map[string]any{
"projectID": entity.ProjectID,
"environmentID": entity.EnvironmentID,
},
})

// Append the edge of Service to Service.
// Append the link of related Services to edges.
for j := 0; j < len(entity.Edges.Dependencies); j++ {
edges = append(edges, GraphEdge{
Type: types.EdgeTypeDependency,
Expand All @@ -82,15 +84,14 @@ func (h Handler) RouteGetGraph(req RouteGetGraphRequest) (*RouteGetGraphResponse
})
}

// Append ServiceResource to vertices,
// and append the link of related ServiceResources to edges.
pkgresource.SetKeys(req.Context, entity.Edges.Resources, operators)
vertices, edges = pkgresource.GetVerticesAndEdges(
entity.Edges.Resources,
vertices,
edges,
)
entity.Edges.Resources, vertices, edges)

for j := 0; j < len(entity.Edges.Resources); j++ {
// Append the edge of Service to ServiceResource.
// Append the link from Service to ServiceResource into edges.
edges = append(edges, GraphEdge{
Type: types.EdgeTypeComposition,
Start: GraphVertexID{
Expand Down
1 change: 0 additions & 1 deletion pkg/apis/service/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ var (
service.FieldName,
}
getFields = service.WithoutFields(
service.FieldEnvironmentID,
service.FieldUpdateTime)
sortFields = []string{
service.FieldName,
Expand Down
30 changes: 4 additions & 26 deletions pkg/apis/service/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,31 +430,8 @@ func (h Handler) getServiceOutputs(ctx context.Context, id object.ID, onlySucces
}

func (h Handler) RouteGetGraph(req RouteGetGraphRequest) (*RouteGetGraphResponse, error) {
fields := []string{
serviceresource.FieldServiceID,
serviceresource.FieldDeployerType,
serviceresource.FieldType,
serviceresource.FieldID,
serviceresource.FieldName,
serviceresource.FieldMode,
serviceresource.FieldShape,
serviceresource.FieldClassID,
serviceresource.FieldCreateTime,
serviceresource.FieldUpdateTime,
serviceresource.FieldStatus,
}

// Fetch entities.
query := h.modelClient.ServiceResources().Query().
Select(fields...).
Order(model.Desc(serviceresource.FieldCreateTime)).
Where(
serviceresource.ServiceID(req.ID),
serviceresource.Mode(types.ServiceResourceModeManaged),
serviceresource.Shape(types.ServiceResourceShapeClass),
)

entities, err := dao.ServiceResourceShapeClassQuery(query, fields...).
entities, err := dao.ServiceResourceShapeClassQuery(h.modelClient.ServiceResources().Query()).
Where(serviceresource.ServiceID(req.ID)).
All(req.Context)
if err != nil {
return nil, err
Expand All @@ -481,7 +458,8 @@ func (h Handler) RouteGetGraph(req RouteGetGraphRequest) (*RouteGetGraphResponse
)

pkgresource.SetKeys(req.Context, entities, operators)
vertices, edges = pkgresource.GetVerticesAndEdges(entities, vertices, edges)
vertices, edges = pkgresource.GetVerticesAndEdges(
entities, vertices, edges)

return &RouteGetGraphResponse{
Vertices: vertices,
Expand Down
39 changes: 23 additions & 16 deletions pkg/dao/service_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/seal-io/seal/pkg/dao/model/connector"
"github.com/seal-io/seal/pkg/dao/model/serviceresource"
"github.com/seal-io/seal/pkg/dao/model/serviceresourcerelationship"
"github.com/seal-io/seal/pkg/dao/types"
"github.com/seal-io/seal/utils/strs"
)

Expand Down Expand Up @@ -46,9 +47,9 @@ func ServiceResourceInstancesEdgeSave(ctx context.Context, mc model.ClientSet, e
return nil
}

// ServiceResourceShapeClassQuery returns a query that selects a shape class service resource,
// components and dependencies.
func ServiceResourceShapeClassQuery(query *model.ServiceResourceQuery, fields ...string) *model.ServiceResourceQuery {
// ServiceResourceShapeClassQuery wraps the given model.ServiceResource query
// to select all shape class resources and the owned components and dependencies of them.
func ServiceResourceShapeClassQuery(query *model.ServiceResourceQuery) *model.ServiceResourceQuery {
var (
order = model.Desc(serviceresource.FieldCreateTime)
wcOpts = func(q *model.ConnectorQuery) {
Expand All @@ -62,21 +63,27 @@ func ServiceResourceShapeClassQuery(query *model.ServiceResourceQuery, fields ..
}
)

return query.Select(fields...).
WithInstances(func(srq *model.ServiceResourceQuery) {
srq.WithConnector(wcOpts).
WithComponents(func(q *model.ServiceResourceQuery) {
q.Order(order).
Select(fields...).
return query.
Where(
serviceresource.Shape(types.ServiceResourceShapeClass),
serviceresource.Mode(types.ServiceResourceModeManaged)).
Order(order).
WithInstances(func(iq *model.ServiceResourceQuery) {
iq.
Order(order).
WithConnector(wcOpts).
WithComponents(func(cq *model.ServiceResourceQuery) {
cq.
Order(order).
WithConnector(wcOpts)
})
}).WithDependencies(func(rrq *model.ServiceResourceRelationshipQuery) {
rrq.Select(
serviceresourcerelationship.FieldServiceResourceID,
serviceresourcerelationship.FieldDependencyID,
serviceresourcerelationship.FieldType,
)
})
}).
WithDependencies(func(rrq *model.ServiceResourceRelationshipQuery) {
rrq.Select(
serviceresourcerelationship.FieldServiceResourceID,
serviceresourcerelationship.FieldDependencyID,
serviceresourcerelationship.FieldType)
})
}

// ServiceResourceToMap recursive set a map of service resources indexed by its unique index.
Expand Down
3 changes: 2 additions & 1 deletion pkg/operator/k8s/operator_get_components.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ func (op Operator) GetComponents(
for i := range comps {
// Copy required field from composition resource.
comps[i].ProjectID = res.ProjectID
comps[i].CompositionID = res.ID
comps[i].EnvironmentID = res.EnvironmentID
comps[i].ServiceID = res.ServiceID
comps[i].CompositionID = res.ID
comps[i].ConnectorID = res.ConnectorID
comps[i].Mode = types.ServiceResourceModeDiscovered
comps[i].DeployerType = res.DeployerType
Expand Down
8 changes: 4 additions & 4 deletions pkg/scheduler/serviceresource/components_discover_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,19 +133,19 @@ func (in *ComponentsDiscoverTask) buildSyncTask(
return func() (berr error) {
entities, err := in.modelClient.ServiceResources().Query().
Where(
serviceresource.ModeNEQ(types.ServiceResourceModeDiscovered),
serviceresource.ConnectorID(connectorID),
serviceresource.ShapeEQ(types.ServiceResourceShapeInstance),
).
serviceresource.ModeNEQ(types.ServiceResourceModeDiscovered),
serviceresource.ShapeEQ(types.ServiceResourceShapeInstance)).
Order(model.Desc(serviceresource.FieldCreateTime)).
Offset(offset).
Limit(limit).
Unique(false).
Select(
serviceresource.FieldID,
serviceresource.FieldProjectID,
serviceresource.FieldType,
serviceresource.FieldEnvironmentID,
serviceresource.FieldServiceID,
serviceresource.FieldType,
serviceresource.FieldConnectorID,
serviceresource.FieldName,
serviceresource.FieldDeployerType).
Expand Down
32 changes: 20 additions & 12 deletions pkg/serviceresources/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package serviceresources
import (
"context"

"k8s.io/apimachinery/pkg/util/sets"

"github.com/seal-io/seal/pkg/dao/model"
"github.com/seal-io/seal/pkg/dao/types"
"github.com/seal-io/seal/pkg/dao/types/object"
Expand All @@ -11,22 +13,24 @@ import (
"github.com/seal-io/seal/utils/log"
)

// GetVerticesAndEdges returns the vertices and edges of the graph with model.ServiceResources.
// GetVerticesAndEdges constructs a graph of the given model.ServiceResource entities with DFS algorithm,
// and appends the vertices and edges to the given slices.
func GetVerticesAndEdges(
entities model.ServiceResources,
vertices []types.GraphVertex,
edges []types.GraphEdge,
) ([]types.GraphVertex, []types.GraphEdge) {
cache := make(map[object.ID]*model.ServiceResource)
var (
visited = sets.New[object.ID]()
dfs func(entity *model.ServiceResource)
)

// DFS function to get vertices and edges.
var fn func(entity *model.ServiceResource)
fn = func(entity *model.ServiceResource) {
if _, ok := cache[entity.ID]; ok {
dfs = func(entity *model.ServiceResource) {
if visited.Has(entity.ID) {
return
}

cache[entity.ID] = entity
visited.Insert(entity.ID)
kind := GetGraphVertexType(entity)

// Append ServiceResource to vertices.
Expand All @@ -40,8 +44,12 @@ func GetVerticesAndEdges(
UpdateTime: entity.UpdateTime,
Status: entity.Status.Summary,
Extensions: map[string]any{
"type": entity.Type,
"keys": entity.Keys,
"type": entity.Type,
"keys": entity.Keys,
"projectID": entity.ProjectID,
"environmentID": entity.EnvironmentID,
"serviceID": entity.ServiceID,
"connectorID": entity.ConnectorID,
},
})

Expand All @@ -59,7 +67,7 @@ func GetVerticesAndEdges(
},
})

fn(entity.Edges.Components[i])
dfs(entity.Edges.Components[i])
}

for j := 0; j < len(entity.Edges.Instances); j++ {
Expand All @@ -76,7 +84,7 @@ func GetVerticesAndEdges(
},
})

fn(entity.Edges.Instances[j])
dfs(entity.Edges.Instances[j])
}

// Hide instance resources's dependencies.
Expand All @@ -101,7 +109,7 @@ func GetVerticesAndEdges(
}

for i := 0; i < len(entities); i++ {
fn(entities[i])
dfs(entities[i])
}

return vertices, edges
Expand Down
34 changes: 18 additions & 16 deletions pkg/terraform/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,15 @@ func (p Parser) ParseState(
}

classResource := &model.ServiceResource{
ProjectID: revision.ProjectID,
ServiceID: revision.ServiceID,
ConnectorID: object.ID(connectorID),
Mode: rs.Mode,
Type: rs.Type,
Name: rs.Name,
DeployerType: revision.DeployerType,
Shape: types.ServiceResourceShapeClass,
ProjectID: revision.ProjectID,
EnvironmentID: revision.EnvironmentID,
ServiceID: revision.ServiceID,
ConnectorID: object.ID(connectorID),
Mode: rs.Mode,
Type: rs.Type,
Name: rs.Name,
DeployerType: revision.DeployerType,
Shape: types.ServiceResourceShapeClass,
}
classResource.Edges.Instances = make(model.ServiceResources, len(rs.Instances))

Expand Down Expand Up @@ -146,14 +147,15 @@ func (p Parser) ParseState(
}

instanceResource := &model.ServiceResource{
ProjectID: revision.ProjectID,
ServiceID: revision.ServiceID,
ConnectorID: object.ID(connectorID),
Mode: rs.Mode,
Type: rs.Type,
Name: instanceID,
Shape: types.ServiceResourceShapeInstance,
DeployerType: revision.DeployerType,
ProjectID: revision.ProjectID,
EnvironmentID: revision.EnvironmentID,
ServiceID: revision.ServiceID,
ConnectorID: object.ID(connectorID),
Mode: rs.Mode,
Type: rs.Type,
Name: instanceID,
Shape: types.ServiceResourceShapeInstance,
DeployerType: revision.DeployerType,
}

// Assume that the first instance's dependencies are the dependencies of the class resource.
Expand Down

0 comments on commit 00fc7f0

Please sign in to comment.