diff --git a/pkg/config/clusteroperator/v1helpers/status.go b/pkg/config/clusteroperator/v1helpers/status.go index c2ddfd9956..956c97d4ab 100644 --- a/pkg/config/clusteroperator/v1helpers/status.go +++ b/pkg/config/clusteroperator/v1helpers/status.go @@ -62,20 +62,28 @@ func FindStatusCondition(conditions []configv1.ClusterOperatorStatusCondition, c } // GetStatusDiff returns a string representing change in condition status in human readable form. -func GetStatusDiff(oldStatus configv1.ClusterOperatorStatus, newStatus configv1.ClusterOperatorStatus) string { +func GetStatusDiff(oldStatus configv1.ClusterOperatorStatus, newStatus configv1.ClusterOperatorStatus, forEvent bool) string { messages := []string{} for _, newCondition := range newStatus.Conditions { existingStatusCondition := FindStatusCondition(oldStatus.Conditions, newCondition.Type) if existingStatusCondition == nil { - messages = append(messages, fmt.Sprintf("%s set to %s (%q)", newCondition.Type, newCondition.Status, newCondition.Message)) + condMessage := "" + if !forEvent { + condMessage = fmt.Sprintf(" (%s)", newCondition.Message) + } + messages = append(messages, fmt.Sprintf("%s set to %s%s", newCondition.Type, newCondition.Status, condMessage)) continue } if existingStatusCondition.Status != newCondition.Status { - messages = append(messages, fmt.Sprintf("%s changed from %s to %s (%q)", existingStatusCondition.Type, existingStatusCondition.Status, newCondition.Status, newCondition.Message)) + if forEvent { + messages = append(messages, fmt.Sprintf("%s changed from %s to %s", existingStatusCondition.Type, existingStatusCondition.Status, newCondition.Status)) + continue + } + messages = append(messages, fmt.Sprintf("%s changed from %s (%s) to %s (%s)", existingStatusCondition.Type, existingStatusCondition.Status, existingStatusCondition.Message, newCondition.Status, newCondition.Message)) continue } if existingStatusCondition.Message != newCondition.Message { - messages = append(messages, fmt.Sprintf("%s message changed from %q to %q", existingStatusCondition.Type, existingStatusCondition.Message, newCondition.Message)) + messages = append(messages, fmt.Sprintf("%s message changed to %q", existingStatusCondition.Type, newCondition.Message)) } } for _, oldCondition := range oldStatus.Conditions { @@ -101,6 +109,10 @@ func GetStatusDiff(oldStatus configv1.ClusterOperatorStatus, newStatus configv1. messages = append(messages, diff.StringDiff(originalJSON.String(), newJSON.String())) } + if !forEvent { + return strings.Join(messages, ",\n") + } + return strings.Join(messages, ",") } diff --git a/pkg/config/clusteroperator/v1helpers/status_test.go b/pkg/config/clusteroperator/v1helpers/status_test.go index b8e72fee5b..78f888bdb1 100644 --- a/pkg/config/clusteroperator/v1helpers/status_test.go +++ b/pkg/config/clusteroperator/v1helpers/status_test.go @@ -14,6 +14,7 @@ func TestGetStatusConditionDiff(t *testing.T) { newConditions []configv1.ClusterOperatorStatusCondition oldConditions []configv1.ClusterOperatorStatusCondition expectedMessages []string + forEvent bool }{ { name: "new condition", @@ -24,7 +25,37 @@ func TestGetStatusConditionDiff(t *testing.T) { Message: "test", }, }, - expectedMessages: []string{`RetrievedUpdates set to True ("test")`}, + expectedMessages: []string{`RetrievedUpdates set to True`}, + forEvent: true, + }, + { + name: "new multiple condition", + newConditions: []configv1.ClusterOperatorStatusCondition{ + { + Type: configv1.RetrievedUpdates, + Status: configv1.ConditionTrue, + Message: "test", + }, + { + Type: "AnotherDegraded", + Status: configv1.ConditionTrue, + Message: "bar", + }, + }, + expectedMessages: []string{"RetrievedUpdates set to True (test)", "\nAnotherDegraded set to True (bar)"}, + forEvent: false, + }, + { + name: "new condition for log", + newConditions: []configv1.ClusterOperatorStatusCondition{ + { + Type: configv1.RetrievedUpdates, + Status: configv1.ConditionTrue, + Message: "test", + }, + }, + expectedMessages: []string{`RetrievedUpdates set to True (test)`}, + forEvent: false, }, { name: "condition status change", @@ -42,7 +73,27 @@ func TestGetStatusConditionDiff(t *testing.T) { Message: "test", }, }, - expectedMessages: []string{`RetrievedUpdates changed from True to False ("test")`}, + expectedMessages: []string{`RetrievedUpdates changed from True to False`}, + forEvent: true, + }, + { + name: "condition status change for log", + newConditions: []configv1.ClusterOperatorStatusCondition{ + { + Type: configv1.RetrievedUpdates, + Status: configv1.ConditionFalse, + Message: "test", + }, + }, + oldConditions: []configv1.ClusterOperatorStatusCondition{ + { + Type: configv1.RetrievedUpdates, + Status: configv1.ConditionTrue, + Message: "test", + }, + }, + expectedMessages: []string{`RetrievedUpdates changed from True (test) to False (test)`}, + forEvent: false, }, { name: "condition message change", @@ -60,7 +111,27 @@ func TestGetStatusConditionDiff(t *testing.T) { Message: "bar", }, }, - expectedMessages: []string{`RetrievedUpdates message changed from "bar" to "foo"`}, + expectedMessages: []string{`RetrievedUpdates message changed to "foo"`}, + forEvent: true, + }, + { + name: "condition message change for log", + newConditions: []configv1.ClusterOperatorStatusCondition{ + { + Type: configv1.RetrievedUpdates, + Status: configv1.ConditionTrue, + Message: "foo", + }, + }, + oldConditions: []configv1.ClusterOperatorStatusCondition{ + { + Type: configv1.RetrievedUpdates, + Status: configv1.ConditionTrue, + Message: "bar", + }, + }, + expectedMessages: []string{`RetrievedUpdates message changed to "foo"`}, + forEvent: false, }, { name: "condition message deleted", @@ -72,13 +143,14 @@ func TestGetStatusConditionDiff(t *testing.T) { }, }, expectedMessages: []string{"RetrievedUpdates was removed"}, + forEvent: true, }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { - result := GetStatusDiff(configv1.ClusterOperatorStatus{Conditions: test.oldConditions}, configv1.ClusterOperatorStatus{Conditions: test.newConditions}) + result := GetStatusDiff(configv1.ClusterOperatorStatus{Conditions: test.oldConditions}, configv1.ClusterOperatorStatus{Conditions: test.newConditions}, test.forEvent) if !reflect.DeepEqual(test.expectedMessages, strings.Split(result, ",")) { - t.Errorf("expected %#v, got %#v", test.expectedMessages, result) + t.Errorf("expected %#v, got %#v", test.expectedMessages, strings.Split(result, ",")) } }) } diff --git a/pkg/operator/status/status_controller.go b/pkg/operator/status/status_controller.go index 94eef82fd3..6d2a613925 100644 --- a/pkg/operator/status/status_controller.go +++ b/pkg/operator/status/status_controller.go @@ -145,7 +145,8 @@ func (c StatusSyncer) Sync(ctx context.Context, syncCtx factory.SyncContext) err if _, updateErr := c.clusterOperatorClient.ClusterOperators().UpdateStatus(ctx, clusterOperatorObj, metav1.UpdateOptions{}); err != nil { return updateErr } - syncCtx.Recorder().Eventf("OperatorStatusChanged", "Status for operator %s changed: %s", c.clusterOperatorName, configv1helpers.GetStatusDiff(originalClusterOperatorObj.Status, clusterOperatorObj.Status)) + syncCtx.Recorder().Eventf("OperatorStatusChanged", "Status for operator %s changed: %s", c.clusterOperatorName, configv1helpers.GetStatusDiff(originalClusterOperatorObj.Status, clusterOperatorObj.Status, true)) + klog.Infof("Status for operator %s changed: %s", c.clusterOperatorName, configv1helpers.GetStatusDiff(originalClusterOperatorObj.Status, clusterOperatorObj.Status, false)) return nil } @@ -174,7 +175,8 @@ func (c StatusSyncer) Sync(ctx context.Context, syncCtx factory.SyncContext) err if _, updateErr := c.clusterOperatorClient.ClusterOperators().UpdateStatus(ctx, clusterOperatorObj, metav1.UpdateOptions{}); err != nil { return updateErr } - syncCtx.Recorder().Eventf("OperatorStatusChanged", "Status for clusteroperator/%s changed: %s", c.clusterOperatorName, configv1helpers.GetStatusDiff(originalClusterOperatorObj.Status, clusterOperatorObj.Status)) + syncCtx.Recorder().Eventf("OperatorStatusChanged", "Status for clusteroperator/%s changed: %s", c.clusterOperatorName, configv1helpers.GetStatusDiff(originalClusterOperatorObj.Status, clusterOperatorObj.Status, true)) + klog.Infof("Status for clusteroperator/%s changed: %s", c.clusterOperatorName, configv1helpers.GetStatusDiff(originalClusterOperatorObj.Status, clusterOperatorObj.Status, true)) return nil }