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
8 changes: 4 additions & 4 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, appID string, state *model.ApplicationSyncState) error
}

type Detector interface {
Expand Down Expand Up @@ -312,9 +312,9 @@ func (d *detector) loadApplicationConfiguration(repoPath string, app *model.Appl
return cfg, nil
}

func makeSyncState(r *provider.DiffResult, commit string) model.ApplicationSyncState {
func makeSyncState(r *provider.DiffResult, commit string) *model.ApplicationSyncState {
if r.NoChange() {
return model.ApplicationSyncState{
return &model.ApplicationSyncState{
Status: model.ApplicationSyncStatus_SYNCED,
Timestamp: time.Now().Unix(),
}
Expand All @@ -337,7 +337,7 @@ func makeSyncState(r *provider.DiffResult, commit string) model.ApplicationSyncS
})
b.WriteString(details)

return model.ApplicationSyncState{
return &model.ApplicationSyncState{
Status: model.ApplicationSyncStatus_OUT_OF_SYNC,
ShortReason: shortReason,
Reason: b.String(),
Expand Down
8 changes: 4 additions & 4 deletions pkg/app/piped/driftdetector/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ type Detector interface {
type detector struct {
apiClient apiClient
detectors []providerDetector
syncStates map[string]model.ApplicationSyncState
syncStates map[string]*model.ApplicationSyncState
mu sync.RWMutex
logger *zap.Logger
}
Expand All @@ -91,7 +91,7 @@ func NewDetector(
d := &detector{
apiClient: apiClient,
detectors: make([]providerDetector, 0, len(cfg.PlatformProviders)),
syncStates: make(map[string]model.ApplicationSyncState),
syncStates: make(map[string]*model.ApplicationSyncState),
logger: logger.Named("drift-detector"),
}

Expand Down Expand Up @@ -219,7 +219,7 @@ 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, appID string, state *model.ApplicationSyncState) error {
d.mu.RLock()
curState, ok := d.syncStates[appID]
d.mu.RUnlock()
Expand All @@ -230,7 +230,7 @@ func (d *detector) ReportApplicationSyncState(ctx context.Context, appID string,

_, err := d.apiClient.ReportApplicationSyncState(ctx, &pipedservice.ReportApplicationSyncStateRequest{
ApplicationId: appID,
State: &state,
State: state,
})
if err != nil {
d.logger.Error("failed to report application sync state",
Expand Down
10 changes: 5 additions & 5 deletions pkg/app/piped/driftdetector/ecs/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type secretDecrypter interface {
}

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

type Detector interface {
Expand Down Expand Up @@ -445,16 +445,16 @@ func (d *detector) loadApplicationConfiguration(repoPath string, app *model.Appl
return cfg, nil
}

func makeSyncState(r *provider.DiffResult, commit string) model.ApplicationSyncState {
func makeSyncState(r *provider.DiffResult, commit string) *model.ApplicationSyncState {
if r.NoChange() {
return model.ApplicationSyncState{
return &model.ApplicationSyncState{
Status: model.ApplicationSyncStatus_SYNCED,
Timestamp: time.Now().Unix(),
}
}

if ignoreAutoScalingDiff(r) {
return model.ApplicationSyncState{
return &model.ApplicationSyncState{
Status: model.ApplicationSyncStatus_SYNCED,
ShortReason: "Ignore diff of `desiredCount`.",
Reason: "`desiredCount` is 0 or not defined in your config (which means ignoring updating desiredCount) and only `desiredCount` is changed.",
Expand All @@ -479,7 +479,7 @@ func makeSyncState(r *provider.DiffResult, commit string) model.ApplicationSyncS
})
b.WriteString(details)

return model.ApplicationSyncState{
return &model.ApplicationSyncState{
Status: model.ApplicationSyncStatus_OUT_OF_SYNC,
ShortReason: shortReason,
Reason: b.String(),
Expand Down
8 changes: 4 additions & 4 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, appID string, state *model.ApplicationSyncState) error
}

type Detector interface {
Expand Down Expand Up @@ -382,9 +382,9 @@ func filterIgnoringManifests(manifests []provider.Manifest) []provider.Manifest
return out
}

func makeSyncState(r *provider.DiffListResult, commit string) model.ApplicationSyncState {
func makeSyncState(r *provider.DiffListResult, commit string) *model.ApplicationSyncState {
if r.NoChange() {
return model.ApplicationSyncState{
return &model.ApplicationSyncState{
Status: model.ApplicationSyncStatus_SYNCED,
ShortReason: "",
Reason: "",
Expand Down Expand Up @@ -413,7 +413,7 @@ func makeSyncState(r *provider.DiffListResult, commit string) model.ApplicationS
})
b.WriteString(details)

return model.ApplicationSyncState{
return &model.ApplicationSyncState{
Status: model.ApplicationSyncStatus_OUT_OF_SYNC,
ShortReason: shortReason,
Reason: b.String(),
Expand Down
8 changes: 4 additions & 4 deletions pkg/app/piped/driftdetector/lambda/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type secretDecrypter interface {
}

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

type Detector interface {
Expand Down Expand Up @@ -360,9 +360,9 @@ func (d *detector) loadApplicationConfiguration(repoPath string, app *model.Appl
return cfg, nil
}

func makeSyncState(r *provider.DiffResult, commit string) model.ApplicationSyncState {
func makeSyncState(r *provider.DiffResult, commit string) *model.ApplicationSyncState {
if r.NoChange() {
return model.ApplicationSyncState{
return &model.ApplicationSyncState{
Status: model.ApplicationSyncStatus_SYNCED,
Timestamp: time.Now().Unix(),
}
Expand All @@ -385,7 +385,7 @@ func makeSyncState(r *provider.DiffResult, commit string) model.ApplicationSyncS
})
b.WriteString(details)

return model.ApplicationSyncState{
return &model.ApplicationSyncState{
Status: model.ApplicationSyncStatus_OUT_OF_SYNC,
ShortReason: shortReason,
Reason: b.String(),
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, appID string, state *model.ApplicationSyncState) error
}

type Detector interface {
Expand Down Expand Up @@ -281,7 +281,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.Id, state)
}

func makeSyncState(r provider.PlanResult, commit string) (*model.ApplicationSyncState, error) {
Expand Down
10 changes: 5 additions & 5 deletions pkg/app/piped/livestatereporter/kubernetes/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ type reporter struct {
snapshotFlushInterval time.Duration
logger *zap.Logger

snapshotVersions map[string]model.ApplicationLiveStateVersion
snapshotVersions map[string]*model.ApplicationLiveStateVersion
}

func NewReporter(cp config.PipedPlatformProvider, appLister applicationLister, stateGetter kubernetes.Getter, apiClient apiClient, logger *zap.Logger) Reporter {
Expand All @@ -72,7 +72,7 @@ func NewReporter(cp config.PipedPlatformProvider, appLister applicationLister, s
flushInterval: 5 * time.Second,
snapshotFlushInterval: 10 * time.Minute,
logger: logger,
snapshotVersions: make(map[string]model.ApplicationLiveStateVersion),
snapshotVersions: make(map[string]*model.ApplicationLiveStateVersion),
}
}

Expand Down Expand Up @@ -128,7 +128,7 @@ func (r *reporter) flushSnapshots(ctx context.Context) {
Kubernetes: &model.KubernetesApplicationLiveState{
Resources: state.Resources,
},
Version: &state.Version,
Version: state.Version,
}
snapshot.DetermineAppHealthStatus()
req := &pipedservice.ReportApplicationLiveStateRequest{
Expand All @@ -154,12 +154,12 @@ func (r *reporter) flushEvents(ctx context.Context) error {
}

filteredEvents := make([]*model.KubernetesResourceStateEvent, 0, len(events))
for i, event := range events {
for _, event := range events {
snapshotVersion, ok := r.snapshotVersions[event.ApplicationId]
if ok && event.SnapshotVersion.IsBefore(snapshotVersion) {
continue
}
filteredEvents = append(filteredEvents, &events[i])
filteredEvents = append(filteredEvents, event)
}
if len(filteredEvents) == 0 {
return nil
Expand Down
52 changes: 34 additions & 18 deletions pkg/app/piped/livestatestore/kubernetes/appnodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type appNodes struct {
appID string
managingNodes map[string]node
dependedNodes map[string]node
version model.ApplicationLiveStateVersion
version *model.ApplicationLiveStateVersion
mu sync.RWMutex
}

Expand All @@ -39,7 +39,7 @@ type node struct {
appID string
key provider.ResourceKey
unstructured *unstructured.Unstructured
state model.KubernetesResourceState
state *model.KubernetesResourceState
}

func (n node) Manifest() provider.Manifest {
Expand All @@ -64,7 +64,10 @@ func (a *appNodes) addManagingResource(uid string, key provider.ResourceKey, obj

a.mu.Lock()
oriNode, hasOriNode := a.managingNodes[uid]
version := a.version
version := &model.ApplicationLiveStateVersion{
Timestamp: a.version.Timestamp,
Index: a.version.Index,
}
a.managingNodes[uid] = n
a.updateVersion(now)
a.mu.Unlock()
Expand All @@ -78,8 +81,8 @@ func (a *appNodes) addManagingResource(uid string, key provider.ResourceKey, obj
Id: uuid.New().String(),
ApplicationId: a.appID,
Type: model.KubernetesResourceStateEvent_ADD_OR_UPDATED,
State: &n.state,
SnapshotVersion: &version,
State: n.state,
SnapshotVersion: version,
CreatedAt: now.Unix(),
}, true
}
Expand All @@ -92,7 +95,10 @@ func (a *appNodes) deleteManagingResource(uid string, _ provider.ResourceKey, no
return model.KubernetesResourceStateEvent{}, false
}

version := a.version
version := &model.ApplicationLiveStateVersion{
Timestamp: a.version.Timestamp,
Index: a.version.Index,
}
delete(a.managingNodes, uid)
a.updateVersion(now)
a.mu.Unlock()
Expand All @@ -101,8 +107,8 @@ func (a *appNodes) deleteManagingResource(uid string, _ provider.ResourceKey, no
Id: uuid.New().String(),
ApplicationId: a.appID,
Type: model.KubernetesResourceStateEvent_DELETED,
State: &n.state,
SnapshotVersion: &version,
State: n.state,
SnapshotVersion: version,
CreatedAt: now.Unix(),
}, true
}
Expand All @@ -118,7 +124,10 @@ func (a *appNodes) addDependedResource(uid string, key provider.ResourceKey, obj

a.mu.Lock()
oriNode, hasOriNode := a.dependedNodes[uid]
version := a.version
version := &model.ApplicationLiveStateVersion{
Timestamp: a.version.Timestamp,
Index: a.version.Index,
}
a.dependedNodes[uid] = n
a.updateVersion(now)
a.mu.Unlock()
Expand All @@ -132,8 +141,8 @@ func (a *appNodes) addDependedResource(uid string, key provider.ResourceKey, obj
Id: uuid.New().String(),
ApplicationId: a.appID,
Type: model.KubernetesResourceStateEvent_ADD_OR_UPDATED,
State: &n.state,
SnapshotVersion: &version,
State: n.state,
SnapshotVersion: version,
CreatedAt: now.Unix(),
}, true
}
Expand All @@ -146,7 +155,10 @@ func (a *appNodes) deleteDependedResource(uid string, _ provider.ResourceKey, no
return model.KubernetesResourceStateEvent{}, false
}

version := a.version
version := &model.ApplicationLiveStateVersion{
Timestamp: a.version.Timestamp,
Index: a.version.Index,
}
delete(a.dependedNodes, uid)
a.updateVersion(now)
a.mu.Unlock()
Expand All @@ -155,8 +167,8 @@ func (a *appNodes) deleteDependedResource(uid string, _ provider.ResourceKey, no
Id: uuid.New().String(),
ApplicationId: a.appID,
Type: model.KubernetesResourceStateEvent_DELETED,
State: &n.state,
SnapshotVersion: &version,
State: n.state,
SnapshotVersion: version,
CreatedAt: now.Unix(),
}, true
}
Expand All @@ -168,21 +180,25 @@ func (a *appNodes) getManagingNodes() map[string]node {
return a.managingNodes
}

func (a *appNodes) getNodes() (map[string]node, model.ApplicationLiveStateVersion) {
func (a *appNodes) getNodes() (map[string]node, *model.ApplicationLiveStateVersion) {
a.mu.RLock()
defer a.mu.RUnlock()

var (
version = a.version
nodes = make(map[string]node, len(a.managingNodes)+len(a.dependedNodes))
// return a value copy so the caller can't mutate a.version outside the lock
version = model.ApplicationLiveStateVersion{
Timestamp: a.version.Timestamp,
Index: a.version.Index,
}
nodes = make(map[string]node, len(a.managingNodes)+len(a.dependedNodes))
)
for k, n := range a.dependedNodes {
nodes[k] = n
}
for k, n := range a.managingNodes {
nodes[k] = n
}
return nodes, version
return nodes, &version
}

func (a *appNodes) updateVersion(now time.Time) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/app/piped/livestatestore/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ type Getter interface {

type AppState struct {
Resources []*model.KubernetesResourceState
Version model.ApplicationLiveStateVersion
Version *model.ApplicationLiveStateVersion
}

type EventIterator struct {
id int
store *store
}

func (it EventIterator) Next(maxNum int) []model.KubernetesResourceStateEvent {
func (it EventIterator) Next(maxNum int) []*model.KubernetesResourceStateEvent {
return it.store.nextEvents(it.id, maxNum)
}

Expand Down
Loading
Loading