Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions pkg/app/api/grpcapi/piped_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,16 @@ func (a *PipedAPI) CreateDeployment(ctx context.Context, req *pipedservice.Creat
a.logger.Error("failed to create deployment", zap.Error(err))
return nil, status.Error(codes.Internal, "failed to create deployment")
}

// If the deployment doesn't belong to another chain, return immediately.
if req.Deployment.DeploymentChainId == "" {
return &pipedservice.CreateDeploymentResponse{}, nil
}

// Otherwise, add the created deployment ref to its chain block model.
if err = a.deploymentChainStore.UpdateDeploymentChain(ctx, req.Deployment.DeploymentChainId, datastore.DeploymentChainAddDeploymentToBlock(req.Deployment)); err != nil {
return nil, status.Error(codes.Internal, "failed to add deployment ref to its chain block")
}
return &pipedservice.CreateDeploymentResponse{}, nil
}

Expand Down
20 changes: 11 additions & 9 deletions pkg/app/piped/trigger/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func buildDeployment(
now time.Time,
noti *config.DeploymentNotification,
deploymentChainID string,
deploymentChainBlockIndex int32,
) (*model.Deployment, error) {

var commitURL string
Expand Down Expand Up @@ -92,15 +93,16 @@ func buildDeployment(
SyncStrategy: syncStrategy,
StrategySummary: strategySummary,
},
GitPath: app.GitPath,
CloudProvider: app.CloudProvider,
Labels: app.Labels,
Status: model.DeploymentStatus_DEPLOYMENT_PENDING,
StatusReason: "The deployment is waiting to be planned",
Metadata: metadata,
CreatedAt: now.Unix(),
UpdatedAt: now.Unix(),
DeploymentChainId: deploymentChainID,
GitPath: app.GitPath,
CloudProvider: app.CloudProvider,
Labels: app.Labels,
Status: model.DeploymentStatus_DEPLOYMENT_PENDING,
StatusReason: "The deployment is waiting to be planned",
Metadata: metadata,
CreatedAt: now.Unix(),
UpdatedAt: now.Unix(),
DeploymentChainId: deploymentChainID,
DeploymentChainBlockIndex: deploymentChainBlockIndex,
}

return deployment, nil
Expand Down
11 changes: 7 additions & 4 deletions pkg/app/piped/trigger/trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,11 @@ func (t *Trigger) checkRepoCandidates(ctx context.Context, repoID string, cs []c
}

var (
commander string
strategy model.SyncStrategy
strategySummary string
deploymentChainID string
commander string
strategy model.SyncStrategy
strategySummary string
deploymentChainID string
deploymentChainBlockIndex int32
)

switch c.kind {
Expand All @@ -269,6 +270,7 @@ func (t *Trigger) checkRepoCandidates(ctx context.Context, repoID string, cs []c
commander = c.command.Commander
strategySummary = "Sync application in chain"
deploymentChainID = c.command.GetChainSyncApplication().DeploymentChainId
deploymentChainBlockIndex = c.command.GetChainSyncApplication().BlockIndex

case model.TriggerKind_ON_OUT_OF_SYNC:
strategy = model.SyncStrategy_QUICK_SYNC
Expand All @@ -289,6 +291,7 @@ func (t *Trigger) checkRepoCandidates(ctx context.Context, repoID string, cs []c
time.Now(),
appCfg.DeploymentNotification,
deploymentChainID,
deploymentChainBlockIndex,
)
if err != nil {
msg := fmt.Sprintf("failed to build deployment for application %s: %v", app.Id, err)
Expand Down
1 change: 1 addition & 0 deletions pkg/app/web/src/__fixtures__/dummy-deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export const dummyDeployment: Deployment.AsObject = {
kind: ApplicationKind.KUBERNETES,
metadataMap: [],
deploymentChainId: "",
deploymentChainBlockIndex: 0,
};

export function createDeploymentFromObject(o: Deployment.AsObject): Deployment {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ const fakeDeployment: Deployment.AsObject = {
createdAt: 1592203166,
updatedAt: 1592203166,
deploymentChainId: "",
deploymentChainBlockIndex: 0,
};

export default {
Expand Down
43 changes: 43 additions & 0 deletions pkg/datastore/deploymentchainstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,46 @@ package datastore

import (
"context"
"fmt"
"time"

"github.com/pipe-cd/pipe/pkg/model"
)

const DeploymentChainModelKind = "DeploymentChain"

var deploymentChainFactory = func() interface{} {
return &model.DeploymentChain{}
}

var (
DeploymentChainAddDeploymentToBlock = func(deployment *model.Deployment) func(*model.DeploymentChain) error {
return func(dc *model.DeploymentChain) error {
if deployment.DeploymentChainBlockIndex == 0 || deployment.DeploymentChainBlockIndex >= int32(len(dc.Blocks)) {
return fmt.Errorf("invalid block index provided")
}
block := dc.Blocks[deployment.DeploymentChainBlockIndex]
var updated bool
for _, node := range block.Nodes {
if node.ApplicationRef.ApplicationId != deployment.ApplicationId {
continue
}
node.DeploymentRef = &model.ChainDeploymentRef{
DeploymentId: deployment.Id,
}
updated = true
}
if !updated {
return fmt.Errorf("unable to find the right node in chain to assign deployment to")
}
return nil
}
}
)

type DeploymentChainStore interface {
AddDeploymentChain(ctx context.Context, d *model.DeploymentChain) error
UpdateDeploymentChain(ctx context.Context, id string, updater func(*model.DeploymentChain) error) error
}

type deploymentChainStore struct {
Expand Down Expand Up @@ -54,3 +85,15 @@ func (s *deploymentChainStore) AddDeploymentChain(ctx context.Context, dc *model
}
return s.ds.Create(ctx, DeploymentChainModelKind, dc.Id, dc)
}

func (s *deploymentChainStore) UpdateDeploymentChain(ctx context.Context, id string, updater func(*model.DeploymentChain) error) error {
now := s.nowFunc().Unix()
return s.ds.Update(ctx, DeploymentChainModelKind, id, deploymentChainFactory, func(e interface{}) error {
dc := e.(*model.DeploymentChain)
if err := updater(dc); err != nil {
return err
}
dc.UpdatedAt = now
return dc.Validate()
})
}
3 changes: 3 additions & 0 deletions pkg/model/deployment.proto
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ message Deployment {
// Reference to the chain which the deployment belongs to.
// Empty means the deployment is a standalone deployment.
string deployment_chain_id = 40;
// Index represents the offset of the node which this deployment
// belongs to.
int32 deployment_chain_block_index = 41;

int64 completed_at = 100 [(validate.rules).int64.gte = 0];
int64 created_at = 101 [(validate.rules).int64.gte = 0];
Expand Down