Skip to content

Commit

Permalink
Improve retrieval of services status from Sveltos (#673)
Browse files Browse the repository at this point in the history
* Improve retrieval of services status from Sveltos

* Rename to SetStatusConditions

* pre-allocate conditions slice capacity

---------

Co-authored-by: Ekaterina Kazakova <[email protected]>
  • Loading branch information
wahabmk and eromanova authored Nov 26, 2024
1 parent cf00132 commit 71c9ae1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
9 changes: 8 additions & 1 deletion internal/controller/multiclusterservice_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,16 @@ func updateServicesStatus(ctx context.Context, c client.Client, profileRef clien
idx = len(servicesStatus) - 1
}

if err := sveltos.SetStatusConditions(&summary, &servicesStatus[idx].Conditions); err != nil {
conditions, err := sveltos.SetStatusConditions(&summary)
if err != nil {
return nil, err
}

// We are overwriting conditions so as to be in-sync with the custom status
// implemented by Sveltos ClusterSummary object. E.g. If a service has been
// removed, the ClusterSummary status will not show that service, therefore
// we also want the entry for that service to be removed from conditions.
servicesStatus[idx].Conditions = conditions
}

return servicesStatus, nil
Expand Down
15 changes: 8 additions & 7 deletions internal/sveltos/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ import (
hmc "github.com/Mirantis/hmc/api/v1alpha1"
)

// SetStatusConditions transforms status from Sveltos ClusterSummary
// object and sets it into the provided list of conditions.
func SetStatusConditions(summary *sveltosv1beta1.ClusterSummary, conditions *[]metav1.Condition) error {
// SetStatusConditions creates & sets a list of conditions from provided ClusterSummary and returns it.
func SetStatusConditions(summary *sveltosv1beta1.ClusterSummary) ([]metav1.Condition, error) {
if summary == nil {
return errors.New("nil summary provided")
return nil, errors.New("error getting status from ClusterSummary: nil summary provided")
}

conditions := make([]metav1.Condition, 0, len(summary.Status.FeatureSummaries)+len(summary.Status.HelmReleaseSummaries))

for _, x := range summary.Status.FeatureSummaries {
msg := ""
status := metav1.ConditionTrue
Expand All @@ -40,7 +41,7 @@ func SetStatusConditions(summary *sveltosv1beta1.ClusterSummary, conditions *[]m
status = metav1.ConditionFalse
}

apimeta.SetStatusCondition(conditions, metav1.Condition{
apimeta.SetStatusCondition(&conditions, metav1.Condition{
Message: msg,
Reason: string(x.Status),
Status: status,
Expand All @@ -54,15 +55,15 @@ func SetStatusConditions(summary *sveltosv1beta1.ClusterSummary, conditions *[]m
status = metav1.ConditionFalse
}

apimeta.SetStatusCondition(conditions, metav1.Condition{
apimeta.SetStatusCondition(&conditions, metav1.Condition{
Message: helmReleaseConditionMessage(x.ReleaseNamespace, x.ReleaseName, x.ConflictMessage),
Reason: string(x.Status),
Status: status,
Type: HelmReleaseReadyConditionType(x.ReleaseNamespace, x.ReleaseName),
})
}

return nil
return conditions, nil
}

// HelmReleaseReadyConditionType returns a SveltosHelmReleaseReady
Expand Down
4 changes: 2 additions & 2 deletions internal/sveltos/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ func TestSetStatusConditions(t *testing.T) {
},
} {
t.Run(tc.name, func(t *testing.T) {
conditions := []metav1.Condition{}
require.NoError(t, SetStatusConditions(&tc.summary, &conditions))
conditions, err := SetStatusConditions(&tc.summary)
require.NoError(t, err)
assert.Len(t, conditions, 1)
assert.Equal(t, tc.expectCondition.Type, conditions[0].Type)
assert.Equal(t, tc.expectCondition.Status, conditions[0].Status)
Expand Down

0 comments on commit 71c9ae1

Please sign in to comment.