From 68d5c3fbf9ee2eef885997bc9f309fcdf2ebc87a Mon Sep 17 00:00:00 2001 From: Mitch Connors Date: Wed, 20 Jan 2021 07:53:23 -0800 Subject: [PATCH] improve performance of casting status type (#30204) * improve performance of casting status type * convert status references to pointers --- pilot/pkg/status/state.go | 16 +++++++--------- pilot/pkg/status/state_test.go | 4 ++-- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/pilot/pkg/status/state.go b/pilot/pkg/status/state.go index 3332925f11d2..0f94f5f959b0 100644 --- a/pilot/pkg/status/state.go +++ b/pilot/pkg/status/state.go @@ -15,7 +15,6 @@ package status import ( - "encoding/json" "fmt" "strconv" "strings" @@ -227,12 +226,11 @@ func (c *DistributionController) configDeleted(res config.Config) { c.workers.Delete(*r) } -func GetTypedStatus(in interface{}) (out v1alpha1.IstioStatus, err error) { - var statusBytes []byte - if statusBytes, err = json.Marshal(in); err == nil { - err = json.Unmarshal(statusBytes, &out) +func GetTypedStatus(in interface{}) (out *v1alpha1.IstioStatus, err error) { + if ret, ok := in.(*v1alpha1.IstioStatus); ok { + return ret, nil } - return + return nil, fmt.Errorf("cannot cast %t: %v to IstioStatus", in, in) } func boolToConditionStatus(b bool) string { @@ -259,11 +257,11 @@ func ReconcileStatuses(current *config.Config, desired Progress, generation int6 } else { scope.Warn("Encountered unexpected status content. Overwriting status.") } - currentStatus = v1alpha1.IstioStatus{ + currentStatus = &v1alpha1.IstioStatus{ Conditions: []*v1alpha1.IstioCondition{&desiredCondition}, } currentStatus.ObservedGeneration = generation - return true, ¤tStatus + return true, currentStatus } var currentCondition *v1alpha1.IstioCondition conditionIndex := -1 @@ -284,7 +282,7 @@ func ReconcileStatuses(current *config.Config, desired Progress, generation int6 currentStatus.Conditions = append(currentStatus.Conditions, &desiredCondition) } currentStatus.ObservedGeneration = generation - return needsReconcile, ¤tStatus + return needsReconcile, currentStatus } type DistroReportHandler struct { diff --git a/pilot/pkg/status/state_test.go b/pilot/pkg/status/state_test.go index 039249d8ca3a..bcae408459bd 100644 --- a/pilot/pkg/status/state_test.go +++ b/pilot/pkg/status/state_test.go @@ -23,7 +23,7 @@ import ( "istio.io/istio/pkg/config" ) -var statusStillPropagating = v1alpha1.IstioStatus{ +var statusStillPropagating = &v1alpha1.IstioStatus{ Conditions: []*v1alpha1.IstioCondition{ { Type: "PassedValidation", @@ -175,7 +175,7 @@ func Test_getTypedStatus(t *testing.T) { tests := []struct { name string args args - wantOut v1alpha1.IstioStatus + wantOut *v1alpha1.IstioStatus wantErr bool }{ {