Skip to content

Commit

Permalink
call notify func
Browse files Browse the repository at this point in the history
  • Loading branch information
mrymam committed Jul 27, 2023
1 parent a590d5b commit 5affce1
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 13 deletions.
4 changes: 2 additions & 2 deletions pkg/app/piped/driftdetector/cloudrun/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type secretDecrypter interface {
}

type reporter interface {
ReportApplicationSyncState(ctx context.Context, appID string, state model.ApplicationSyncState) error
ReportApplicationSyncState(ctx context.Context, app model.Application, state model.ApplicationSyncState) error
}

type Detector interface {
Expand Down Expand Up @@ -219,7 +219,7 @@ func (d *detector) checkApplication(ctx context.Context, app *model.Application,

state := makeSyncState(result, headCommit.Hash)

return d.reporter.ReportApplicationSyncState(ctx, app.Id, state)
return d.reporter.ReportApplicationSyncState(ctx, *app, state)
}

func (d *detector) loadHeadServiceManifest(app *model.Application, repo git.Repo, headCommit git.Commit) (provider.ServiceManifest, error) {
Expand Down
33 changes: 26 additions & 7 deletions pkg/app/piped/driftdetector/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,32 +190,51 @@ func (d *detector) Run(ctx context.Context) error {
return nil
}

func (d *detector) ReportApplicationSyncState(ctx context.Context, appID string, state model.ApplicationSyncState) error {
func (d *detector) ReportApplicationSyncState(ctx context.Context, app model.Application, state model.ApplicationSyncState) error {
d.mu.RLock()
curState, ok := d.syncStates[appID]
curState, ok := d.syncStates[app.Id]
d.mu.RUnlock()

if ok && !curState.HasChanged(state) {
return nil
}

_, err := d.apiClient.ReportApplicationSyncState(ctx, &pipedservice.ReportApplicationSyncStateRequest{
ApplicationId: appID,
ApplicationId: app.Id,
State: &state,
})
if err != nil {
d.logger.Error("failed to report application sync state",
zap.String("application-id", appID),
zap.String("application-id", app.Id),
zap.Any("state", state),
zap.Error(err),
)
return err
}
// TODO notify a event of NotificationEventType_EVENT_APPLICATION_OUT_OF_SYNC
// TODO notify a event of NotificationEventType_EVENT_APPLICATION_SYNCED

defer func() {
switch state.Status {
case model.ApplicationSyncStatus_SYNCED:
d.notifier.Notify(model.NotificationEvent{
Type: model.NotificationEventType_EVENT_APPLICATION_SYNCED,
Metadata: &model.NotificationEventApplicationSynced{
Application: &app,
State: &state,
},
})
case model.ApplicationSyncStatus_OUT_OF_SYNC:
d.notifier.Notify(model.NotificationEvent{
Type: model.NotificationEventType_EVENT_APPLICATION_OUT_OF_SYNC,
Metadata: &model.NotificationEventApplicationOutOfSync{
Application: &app,
State: &state,
},
})
}
}()

d.mu.Lock()
d.syncStates[appID] = state
d.syncStates[app.Id] = state
d.mu.Unlock()

return nil
Expand Down
4 changes: 2 additions & 2 deletions pkg/app/piped/driftdetector/kubernetes/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type secretDecrypter interface {
}

type reporter interface {
ReportApplicationSyncState(ctx context.Context, appID string, state model.ApplicationSyncState) error
ReportApplicationSyncState(ctx context.Context, app model.Application, state model.ApplicationSyncState) error
}

type Detector interface {
Expand Down Expand Up @@ -218,7 +218,7 @@ func (d *detector) checkApplication(ctx context.Context, app *model.Application,

state := makeSyncState(result, headCommit.Hash)

return d.reporter.ReportApplicationSyncState(ctx, app.Id, state)
return d.reporter.ReportApplicationSyncState(ctx, *app, state)
}

func (d *detector) loadHeadManifests(ctx context.Context, app *model.Application, repo git.Repo, headCommit git.Commit, watchingResourceKinds []provider.APIVersionKind) ([]provider.Manifest, error) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/app/piped/driftdetector/terraform/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type secretDecrypter interface {
}

type reporter interface {
ReportApplicationSyncState(ctx context.Context, appID string, state model.ApplicationSyncState) error
ReportApplicationSyncState(ctx context.Context, app model.Application, state model.ApplicationSyncState) error
}

type Detector interface {
Expand Down Expand Up @@ -278,7 +278,7 @@ func (d *detector) checkApplication(ctx context.Context, app *model.Application,
return err
}

return d.reporter.ReportApplicationSyncState(ctx, app.Id, *state)
return d.reporter.ReportApplicationSyncState(ctx, *app, *state)
}

func makeSyncState(r provider.PlanResult, commit string) (*model.ApplicationSyncState, error) {
Expand Down

0 comments on commit 5affce1

Please sign in to comment.