Skip to content

[Plugin SDK] Change ApplicationLiveState.HealthStatus to private method #5829

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 16, 2025
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
3 changes: 1 addition & 2 deletions pkg/app/pipedv1/plugin/kubernetes/livestate/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@

return &sdk.GetLivestateResponse{
LiveState: sdk.ApplicationLiveState{
Resources: resourceStates,
HealthStatus: sdk.ApplicationHealthStateUnknown, // TODO: Implement health status calculation
Resources: resourceStates,

Check warning on line 105 in pkg/app/pipedv1/plugin/kubernetes/livestate/plugin.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/plugin/kubernetes/livestate/plugin.go#L105

Added line #L105 was not covered by tests
},
SyncState: syncState,
}, nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,8 @@
appLiveState := sdk.ApplicationLiveState{}
for _, ls := range liveStates {
appLiveState.Resources = append(appLiveState.Resources, ls.Resources...)
appLiveState.HealthStatus = sdk.ApplicationHealthStateUnknown // TODO: Implement health status calculation
}

appLiveState.HealthStatus = calculateHealthStatus(liveStates)

appSyncState := sdk.ApplicationSyncState{}
for _, ss := range syncStates {
appSyncState.Reason = fmt.Sprintf("%s\n%s", appSyncState.Reason, ss.Reason)
Expand All @@ -154,8 +151,7 @@
}

return sdk.ApplicationLiveState{
Resources: resourceStates,
HealthStatus: sdk.ApplicationHealthStateUnknown, // TODO: Implement health status calculation
Resources: resourceStates,

Check warning on line 154 in pkg/app/pipedv1/plugin/kubernetes_multicluster/livestate/plugin.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/plugin/kubernetes_multicluster/livestate/plugin.go#L154

Added line #L154 was not covered by tests
}
}

Expand All @@ -174,24 +170,6 @@
return calculateSyncState(diffResult, commit, dt), nil
}

// calculateHealthStatus returns the overall health status with the following priority:
// Unknown > Other > Healthy.
func calculateHealthStatus(states []sdk.ApplicationLiveState) sdk.ApplicationHealthStatus {
hasOther := false
for _, s := range states {
if s.HealthStatus == sdk.ApplicationHealthStateUnknown {
return sdk.ApplicationHealthStateUnknown
}
if s.HealthStatus == sdk.ApplicationHealthStateOther {
hasOther = true
}
}
if hasOther {
return sdk.ApplicationHealthStateOther
}
return sdk.ApplicationHealthStateHealthy
}

func calculateSyncState(diffResult *provider.DiffListResult, commit string, dt *sdk.DeployTarget[kubeconfig.KubernetesDeployTargetConfig]) sdk.ApplicationSyncState {
if diffResult.NoChanges() {
return sdk.ApplicationSyncState{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -659,58 +659,3 @@ func Test_calculateSyncStatus(t *testing.T) {
})
}
}
func Test_calculateHealthStatus(t *testing.T) {
t.Parallel()

tests := []struct {
name string
states []sdk.ApplicationLiveState
want sdk.ApplicationHealthStatus
}{
{
name: "all states are Healthy",
states: []sdk.ApplicationLiveState{
{HealthStatus: sdk.ApplicationHealthStateHealthy},
{HealthStatus: sdk.ApplicationHealthStateHealthy},
},
want: sdk.ApplicationHealthStateHealthy,
},
{
name: "one state is Other",
states: []sdk.ApplicationLiveState{
{HealthStatus: sdk.ApplicationHealthStateHealthy},
{HealthStatus: sdk.ApplicationHealthStateOther},
},
want: sdk.ApplicationHealthStateOther,
},
{
name: "one state is Unknown",
states: []sdk.ApplicationLiveState{
{HealthStatus: sdk.ApplicationHealthStateHealthy},
{HealthStatus: sdk.ApplicationHealthStateUnknown},
},
want: sdk.ApplicationHealthStateUnknown,
},
{
name: "priority: Unknown > Other > Healthy",
states: []sdk.ApplicationLiveState{
{HealthStatus: sdk.ApplicationHealthStateOther},
{HealthStatus: sdk.ApplicationHealthStateUnknown},
},
want: sdk.ApplicationHealthStateUnknown,
},
{
name: "empty states returns Healthy",
states: []sdk.ApplicationLiveState{},
want: sdk.ApplicationHealthStateHealthy,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got := calculateHealthStatus(tt.states)
assert.Equal(t, tt.want, got)
})
}
}
36 changes: 33 additions & 3 deletions pkg/plugin/sdk/livestate.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,38 @@ func (r *GetLivestateResponse) toModel(pluginName string, now time.Time) *livest

// ApplicationLiveState represents the live state of an application.
type ApplicationLiveState struct {
Resources []ResourceState
HealthStatus ApplicationHealthStatus
Resources []ResourceState
}

// healthStatus returns the health status of the application.
// It returns ApplicationHealthStateUnknown in the following priority:
// 1. If there is any unknown health status resource, it returns ApplicationHealthStateUnknown.
// 2. If there is any unhealthy resource, it returns ApplicationHealthStateOther.
// 3. Otherwise, it returns ApplicationHealthStateHealthy.
func (s *ApplicationLiveState) healthStatus() ApplicationHealthStatus {
var (
unhealthy bool
unknown bool
)

for _, rs := range s.Resources {
switch rs.HealthStatus {
case ResourceHealthStateUnhealthy:
unhealthy = true
case ResourceHealthStateUnknown:
unknown = true
}
}

if unknown {
return ApplicationHealthStateUnknown
}

if unhealthy {
return ApplicationHealthStateOther
}

return ApplicationHealthStateHealthy
}

// toModel converts the ApplicationLiveState to the model.ApplicationLiveState.
Expand All @@ -177,7 +207,7 @@ func (s *ApplicationLiveState) toModel(pluginName string, now time.Time) *model.
}
return &model.ApplicationLiveState{
Resources: resources,
HealthStatus: s.HealthStatus.toModel(),
HealthStatus: s.healthStatus().toModel(),
}
}

Expand Down
132 changes: 121 additions & 11 deletions pkg/plugin/sdk/livestate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ spec: {}
Name: "Resource 1",
},
},
HealthStatus: ApplicationHealthStateHealthy,
},
SyncState: ApplicationSyncState{
Status: ApplicationSyncStateSynced,
Expand Down Expand Up @@ -163,6 +162,7 @@ spec: {}
})
}
}

func TestApplicationLiveState_toModel(t *testing.T) {
t.Parallel()

Expand All @@ -173,30 +173,83 @@ func TestApplicationLiveState_toModel(t *testing.T) {
expected *model.ApplicationLiveState
}{
{
name: "convert ApplicationLiveState to model",
name: "convert ApplicationLiveState to model with healthy resource",
input: ApplicationLiveState{
Resources: []ResourceState{
{
ID: "resource1",
Name: "Resource 1",
CreatedAt: now,
ID: "resource1",
Name: "Resource 1",
HealthStatus: ResourceHealthStateHealthy,
CreatedAt: now,
},
},
HealthStatus: ApplicationHealthStateHealthy,
},
expected: &model.ApplicationLiveState{
Resources: []*model.ResourceState{
{
Id: "resource1",
Name: "Resource 1",
PluginName: "test-plugin",
CreatedAt: now.Unix(),
UpdatedAt: now.Unix(),
Id: "resource1",
Name: "Resource 1",
PluginName: "test-plugin",
HealthStatus: model.ResourceState_HEALTHY,
CreatedAt: now.Unix(),
UpdatedAt: now.Unix(),
},
},
HealthStatus: model.ApplicationLiveState_HEALTHY,
},
},
{
name: "convert ApplicationLiveState to model with unhealthy resource",
input: ApplicationLiveState{
Resources: []ResourceState{
{
ID: "resource1",
Name: "Resource 1",
HealthStatus: ResourceHealthStateUnhealthy,
CreatedAt: now,
},
},
},
expected: &model.ApplicationLiveState{
Resources: []*model.ResourceState{
{
Id: "resource1",
Name: "Resource 1",
PluginName: "test-plugin",
HealthStatus: model.ResourceState_UNHEALTHY,
CreatedAt: now.Unix(),
UpdatedAt: now.Unix(),
},
},
HealthStatus: model.ApplicationLiveState_OTHER,
},
},
{
name: "convert ApplicationLiveState to model with unknown health status resource",
input: ApplicationLiveState{
Resources: []ResourceState{
{
ID: "resource1",
Name: "Resource 1",
HealthStatus: ResourceHealthStateUnknown,
CreatedAt: now,
},
},
},
expected: &model.ApplicationLiveState{
Resources: []*model.ResourceState{
{
Id: "resource1",
Name: "Resource 1",
PluginName: "test-plugin",
HealthStatus: model.ResourceState_UNKNOWN,
CreatedAt: now.Unix(),
UpdatedAt: now.Unix(),
},
},
HealthStatus: model.ApplicationLiveState_UNKNOWN,
},
},
}

for _, tt := range tests {
Expand Down Expand Up @@ -389,3 +442,60 @@ func TestApplicationSyncStatus_toModel(t *testing.T) {
})
}
}

func TestApplicationLiveState_healthStatus(t *testing.T) {
t.Parallel()

tests := []struct {
name string
resources []ResourceState
expected ApplicationHealthStatus
}{
{
name: "all healthy",
resources: []ResourceState{
{HealthStatus: ResourceHealthStateHealthy},
},
expected: ApplicationHealthStateHealthy,
},
{
name: "one unhealthy, none unknown",
resources: []ResourceState{
{HealthStatus: ResourceHealthStateHealthy},
{HealthStatus: ResourceHealthStateUnhealthy},
},
expected: ApplicationHealthStateOther,
},
{
name: "one unknown, others healthy",
resources: []ResourceState{
{HealthStatus: ResourceHealthStateHealthy},
{HealthStatus: ResourceHealthStateUnknown},
},
expected: ApplicationHealthStateUnknown,
},
{
name: "one unknown, one unhealthy",
resources: []ResourceState{
{HealthStatus: ResourceHealthStateUnhealthy},
{HealthStatus: ResourceHealthStateUnknown},
},
expected: ApplicationHealthStateUnknown,
},
{
name: "empty resources",
resources: []ResourceState{},
expected: ApplicationHealthStateHealthy,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := &ApplicationLiveState{Resources: tt.resources}
result := s.healthStatus()
if result != tt.expected {
t.Errorf("expected %v, got %v", tt.expected, result)
}
})
}
}