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
1 change: 0 additions & 1 deletion pkg/app/piped/cmd/piped/piped.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,6 @@ func (p *piped) run(ctx context.Context, t cli.Telemetry) (runErr error) {
{
d := driftdetector.NewDetector(
applicationLister,
deploymentLister,
gitClient,
liveStateGetter,
apiClient,
Expand Down
2 changes: 0 additions & 2 deletions pkg/app/piped/driftdetector/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ type providerDetector interface {

func NewDetector(
appLister applicationLister,
deploymentLister deploymentLister,
gitClient gitClient,
stateGetter livestatestore.Getter,
apiClient apiClient,
Expand Down Expand Up @@ -103,7 +102,6 @@ func NewDetector(
d.detectors = append(d.detectors, kubernetes.NewDetector(
cp,
appLister,
deploymentLister,
gitClient,
sg,
d,
Expand Down
66 changes: 9 additions & 57 deletions pkg/app/piped/driftdetector/kubernetes/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ type applicationLister interface {
ListByCloudProvider(name string) []*model.Application
}

type deploymentLister interface {
ListAppHeadDeployments() map[string]*model.Deployment
}

type gitClient interface {
Clone(ctx context.Context, repoID, remote, branch, destination string) (git.Repo, error)
}
Expand All @@ -58,7 +54,6 @@ type reporter interface {
type detector struct {
provider config.PipedCloudProvider
appLister applicationLister
deploymentLister deploymentLister
gitClient gitClient
stateGetter kubernetes.Getter
reporter reporter
Expand All @@ -75,7 +70,6 @@ type detector struct {
func NewDetector(
cp config.PipedCloudProvider,
appLister applicationLister,
deploymentLister deploymentLister,
gitClient gitClient,
stateGetter kubernetes.Getter,
reporter reporter,
Expand All @@ -91,7 +85,6 @@ func NewDetector(
return &detector{
provider: cp,
appLister: appLister,
deploymentLister: deploymentLister,
gitClient: gitClient,
stateGetter: stateGetter,
reporter: reporter,
Expand Down Expand Up @@ -127,35 +120,9 @@ L:
}

func (d *detector) check(ctx context.Context) error {
var (
err error
applications = d.listApplications()
headDeployments = d.deploymentLister.ListAppHeadDeployments()
)

for repoID, apps := range applications {
var notDeployingApps []*model.Application

// Firstly, handle all deploying applications
// and remove them from the list.
for _, app := range apps {
headDeployment, ok := headDeployments[app.Id]
if !ok {
notDeployingApps = append(notDeployingApps, app)
continue
}
state := makeDeployingState(headDeployment)
if err := d.reporter.ReportApplicationSyncState(ctx, app.Id, state); err != nil {
d.logger.Error("failed to report application sync state", zap.Error(err))
}
}
appsByRepo := d.listGroupedApplication()

if len(notDeployingApps) == 0 {
continue
}

// Next, we have to clone the lastest commit of repository
// to compare the states.
for repoID, apps := range appsByRepo {
gitRepo, ok := d.gitRepos[repoID]
if !ok {
// Clone repository for the first time.
Expand All @@ -164,18 +131,19 @@ func (d *detector) check(ctx context.Context) error {
d.logger.Error(fmt.Sprintf("repository %s was not found in piped configuration", repoID))
continue
}
gitRepo, err = d.gitClient.Clone(ctx, repoID, repoCfg.Remote, repoCfg.Branch, "")
gr, err := d.gitClient.Clone(ctx, repoID, repoCfg.Remote, repoCfg.Branch, "")
if err != nil {
d.logger.Error("failed to clone repository",
zap.String("repo-id", repoID),
zap.Error(err),
)
continue
}
gitRepo = gr
d.gitRepos[repoID] = gitRepo
}

// Fetch to update the repository.
// Fetch the latest commit to compare the states.
branch := gitRepo.GetClonedBranch()
if err := gitRepo.Pull(ctx, branch); err != nil {
d.logger.Error("failed to update repository branch",
Expand All @@ -195,7 +163,8 @@ func (d *detector) check(ctx context.Context) error {
continue
}

for _, app := range notDeployingApps {
// Start checking all applications in this repository.
for _, app := range apps {
if err := d.checkApplication(ctx, app, gitRepo, headCommit); err != nil {
d.logger.Error(fmt.Sprintf("failed to check application: %s", app.Id), zap.Error(err))
}
Expand Down Expand Up @@ -356,9 +325,9 @@ func decryptSealedSecrets(appDir string, secrets []config.SealedSecretMapping, d
return nil
}

// listApplications retrieves all applications those should be handled by this director
// listGroupedApplication retrieves all applications those should be handled by this director
// and then groups them by repoID.
func (d *detector) listApplications() map[string][]*model.Application {
func (d *detector) listGroupedApplication() map[string][]*model.Application {
var (
apps = d.appLister.ListByCloudProvider(d.provider.Name)
m = make(map[string][]*model.Application)
Expand Down Expand Up @@ -435,23 +404,6 @@ func groupManifests(heads, lives []provider.Manifest) (adds, deletes, headInters
return
}

func makeDeployingState(deployment *model.Deployment) model.ApplicationSyncState {
var (
shortReason = "A deployment of this application is running"
reason = deployment.Summary
)
if reason == "" {
reason = shortReason
}
return model.ApplicationSyncState{
Status: model.ApplicationSyncStatus_DEPLOYING,
ShortReason: shortReason,
Reason: reason,
HeadDeploymentId: deployment.Id,
Timestamp: time.Now().Unix(),
}
}

func makeSyncedState() model.ApplicationSyncState {
return model.ApplicationSyncState{
Status: model.ApplicationSyncStatus_SYNCED,
Expand Down