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
55 changes: 43 additions & 12 deletions pkg/app/piped/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,17 @@ func (c *controller) syncPlanners(ctx context.Context) error {
)
c.donePlanners[p.ID()] = p.DoneTimestamp()
delete(c.planners, id)

// Application will be marked as NOT deploying when planner's deployment was completed.
if model.IsCompletedDeployment(p.DoneDeploymentStatus()) {
if err := reportApplicationDeployingStatus(ctx, c.apiClient, id, false); err != nil {
c.logger.Error("failed to mark application as NOT deploying",
zap.String("deployment-id", p.ID()),
zap.String("app-id", id),
zap.Error(err),
)
}
}
}

// Add missing planners.
Expand Down Expand Up @@ -347,6 +358,15 @@ func (c *controller) syncPlanners(ctx context.Context) error {
continue
}
c.planners[appID] = planner

// Application will be marked as DEPLOYING after its planner was successfully created.
if err := reportApplicationDeployingStatus(ctx, c.apiClient, d.ApplicationId, true); err != nil {
c.logger.Error("failed to mark application as deploying",
zap.String("deployment-id", d.Id),
zap.String("app-id", d.ApplicationId),
zap.Error(err),
)
}
}

return nil
Expand Down Expand Up @@ -456,6 +476,17 @@ func (c *controller) syncSchedulers(ctx context.Context) error {
)
c.doneSchedulers[s.ID()] = s.DoneTimestamp()
delete(c.schedulers, id)

// Application will be marked as NOT deploying when scheduler's deployment was completed.
if model.IsCompletedDeployment(s.DoneDeploymentStatus()) {
if err := reportApplicationDeployingStatus(ctx, c.apiClient, id, false); err != nil {
c.logger.Error("failed to mark application as NOT deploying",
zap.String("deployment-id", s.ID()),
zap.String("app-id", id),
zap.Error(err),
)
}
}
}

// Add missing schedulers.
Expand Down Expand Up @@ -590,7 +621,17 @@ func (c *controller) getMostRecentlySuccessfulDeployment(ctx context.Context, ap
return nil, err
}

func (c *controller) reportApplicationDeployingStatus(ctx context.Context, appID string, deploying bool) error {
type appLiveResourceLister struct {
lister liveResourceLister
cloudProvider string
appID string
}

func (l appLiveResourceLister) ListKubernetesResources() ([]provider.Manifest, bool) {
return l.lister.ListKubernetesAppLiveResources(l.cloudProvider, l.appID)
}

func reportApplicationDeployingStatus(ctx context.Context, c apiClient, appID string, deploying bool) error {
var (
err error
retry = pipedservice.NewRetry(10)
Expand All @@ -601,20 +642,10 @@ func (c *controller) reportApplicationDeployingStatus(ctx context.Context, appID
)

for retry.WaitNext(ctx) {
if _, err = c.apiClient.ReportApplicationDeployingStatus(ctx, req); err == nil {
if _, err = c.ReportApplicationDeployingStatus(ctx, req); err == nil {
return nil
}
err = fmt.Errorf("failed to report application deploying status to control-plane: %w", err)
}
return err
}

type appLiveResourceLister struct {
lister liveResourceLister
cloudProvider string
appID string
}

func (l appLiveResourceLister) ListKubernetesResources() ([]provider.Manifest, bool) {
return l.lister.ListKubernetesAppLiveResources(l.cloudProvider, l.appID)
}
21 changes: 17 additions & 4 deletions pkg/app/piped/controller/planner.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ type planner struct {
appManifestsCache cache.Cache
logger *zap.Logger

done atomic.Bool
doneTimestamp time.Time
cancelled bool
cancelledCh chan *model.ReportableCommand
done atomic.Bool
doneTimestamp time.Time
doneDeploymentStatus model.DeploymentStatus
cancelled bool
cancelledCh chan *model.ReportableCommand

nowFunc func() time.Time
}
Expand Down Expand Up @@ -97,6 +98,7 @@ func newPlanner(
pipedConfig: pipedConfig,
plannerRegistry: registry.DefaultRegistry(),
appManifestsCache: appManifestsCache,
doneDeploymentStatus: d.Status,
cancelledCh: make(chan *model.ReportableCommand, 1),
nowFunc: time.Now,
logger: logger,
Expand All @@ -121,6 +123,12 @@ func (p *planner) DoneTimestamp() time.Time {
return p.doneTimestamp
}

// DoneDeploymentStatus returns the deployment status when planner has done.
// This can be used only after IsDone() returns true.
func (p *planner) DoneDeploymentStatus() model.DeploymentStatus {
return p.doneDeploymentStatus
}

func (p *planner) Cancel(cmd model.ReportableCommand) {
if p.cancelled {
return
Expand All @@ -140,13 +148,15 @@ func (p *planner) Run(ctx context.Context) error {

planner, ok := p.plannerRegistry.Planner(p.deployment.Kind)
if !ok {
p.doneDeploymentStatus = model.DeploymentStatus_DEPLOYMENT_FAILURE
p.reportDeploymentFailed(ctx, "Unable to find the planner for this application kind")
return fmt.Errorf("unable to find the planner for application %v", p.deployment.Kind)
}

repoID := p.deployment.GitPath.Repo.Id
repoCfg, ok := p.pipedConfig.GetRepository(repoID)
if !ok {
p.doneDeploymentStatus = model.DeploymentStatus_DEPLOYMENT_FAILURE
p.reportDeploymentFailed(ctx, fmt.Sprintf("Unable to find %q from the repository list in piped config", repoID))
return fmt.Errorf("unable to find %q from the repository list in piped config", repoID)
}
Expand Down Expand Up @@ -187,6 +197,7 @@ func (p *planner) Run(ctx context.Context) error {
select {
case cmd := <-p.cancelledCh:
if cmd != nil {
p.doneDeploymentStatus = model.DeploymentStatus_DEPLOYMENT_CANCELLED
desc := fmt.Sprintf("Deployment was cancelled by %s while planning", cmd.Commander)
p.reportDeploymentCancelled(ctx, cmd.Commander, desc)
return cmd.Report(ctx, model.CommandStatus_COMMAND_SUCCEEDED, nil)
Expand All @@ -195,9 +206,11 @@ func (p *planner) Run(ctx context.Context) error {
}

if err != nil {
p.doneDeploymentStatus = model.DeploymentStatus_DEPLOYMENT_FAILURE
return p.reportDeploymentFailed(ctx, fmt.Sprintf("Unable to plan the deployment (%v)", err))
}

p.doneDeploymentStatus = model.DeploymentStatus_DEPLOYMENT_PLANNED
return p.reportDeploymentPlanned(ctx, p.lastSuccessfulCommitHash, out)
}

Expand Down