diff --git a/changelogs/unreleased/9705-emirot b/changelogs/unreleased/9705-emirot new file mode 100644 index 000000000..3bd8117bc --- /dev/null +++ b/changelogs/unreleased/9705-emirot @@ -0,0 +1 @@ +perf: better string concatenation diff --git a/pkg/util/csi/volume_snapshot.go b/pkg/util/csi/volume_snapshot.go index 57e6f2e1d..ed6371f7b 100644 --- a/pkg/util/csi/volume_snapshot.go +++ b/pkg/util/csi/volume_snapshot.go @@ -708,17 +708,18 @@ func DiagnoseVS(vs *snapshotv1api.VolumeSnapshot, events *corev1api.EventList) s } } - diag := fmt.Sprintf("VS %s/%s, bind to %s, readyToUse %v, errMessage %s\n", vs.Namespace, vs.Name, vscName, readyToUse, errMessage) + var diag strings.Builder + _, _ = fmt.Fprintf(&diag, "VS %s/%s, bind to %s, readyToUse %v, errMessage %s\n", vs.Namespace, vs.Name, vscName, readyToUse, errMessage) if events != nil { for _, e := range events.Items { if e.InvolvedObject.UID == vs.UID && e.Type == corev1api.EventTypeWarning { - diag += fmt.Sprintf("VS event reason %s, message %s\n", e.Reason, e.Message) + _, _ = fmt.Fprintf(&diag, "VS event reason %s, message %s\n", e.Reason, e.Message) } } } - return diag + return diag.String() } func DiagnoseVSC(vsc *snapshotv1api.VolumeSnapshotContent) string { diff --git a/pkg/util/kube/pod.go b/pkg/util/kube/pod.go index 4ff05b43e..4dc423272 100644 --- a/pkg/util/kube/pod.go +++ b/pkg/util/kube/pod.go @@ -20,6 +20,7 @@ import ( "fmt" "io" "os" + "strings" "time" "github.com/pkg/errors" @@ -183,16 +184,16 @@ func GetPodContainerTerminateMessage(pod *corev1api.Pod, container string) strin // GetPodTerminateMessage returns the terminate message for all containers of a pod func GetPodTerminateMessage(pod *corev1api.Pod) string { - message := "" + var message strings.Builder for _, containerStatus := range pod.Status.ContainerStatuses { if containerStatus.State.Terminated != nil { if containerStatus.State.Terminated.Message != "" { - message += containerStatus.State.Terminated.Message + "/" + message.WriteString(containerStatus.State.Terminated.Message + "/") } } } - return message + return message.String() } func getPodLogReader(ctx context.Context, podGetter corev1client.CoreV1Interface, pod string, namespace string, logOptions *corev1api.PodLogOptions) (io.ReadCloser, error) { @@ -272,21 +273,22 @@ func ToSystemAffinity(loadAffinity *LoadAffinity, volumeTopology *corev1api.Node } func DiagnosePod(pod *corev1api.Pod, events *corev1api.EventList) string { - diag := fmt.Sprintf("Pod %s/%s, phase %s, node name %s, message %s\n", pod.Namespace, pod.Name, pod.Status.Phase, pod.Spec.NodeName, pod.Status.Message) + var diag strings.Builder + _, _ = fmt.Fprintf(&diag, "Pod %s/%s, phase %s, node name %s, message %s\n", pod.Namespace, pod.Name, pod.Status.Phase, pod.Spec.NodeName, pod.Status.Message) for _, condition := range pod.Status.Conditions { - diag += fmt.Sprintf("Pod condition %s, status %s, reason %s, message %s\n", condition.Type, condition.Status, condition.Reason, condition.Message) + _, _ = fmt.Fprintf(&diag, "Pod condition %s, status %s, reason %s, message %s\n", condition.Type, condition.Status, condition.Reason, condition.Message) } if events != nil { for _, e := range events.Items { if e.InvolvedObject.UID == pod.UID && e.Type == corev1api.EventTypeWarning { - diag += fmt.Sprintf("Pod event reason %s, message %s\n", e.Reason, e.Message) + _, _ = fmt.Fprintf(&diag, "Pod event reason %s, message %s\n", e.Reason, e.Message) } } } - return diag + return diag.String() } var funcExit = os.Exit diff --git a/pkg/util/kube/pvc_pv.go b/pkg/util/kube/pvc_pv.go index d5d2e2041..fa886bf60 100644 --- a/pkg/util/kube/pvc_pv.go +++ b/pkg/util/kube/pvc_pv.go @@ -464,17 +464,18 @@ func GetPVCForPodVolume(vol *corev1api.Volume, pod *corev1api.Pod, crClient crcl } func DiagnosePVC(pvc *corev1api.PersistentVolumeClaim, events *corev1api.EventList) string { - diag := fmt.Sprintf("PVC %s/%s, phase %s, binding to %s\n", pvc.Namespace, pvc.Name, pvc.Status.Phase, pvc.Spec.VolumeName) + var diag strings.Builder + _, _ = fmt.Fprintf(&diag, "PVC %s/%s, phase %s, binding to %s\n", pvc.Namespace, pvc.Name, pvc.Status.Phase, pvc.Spec.VolumeName) if events != nil { for _, e := range events.Items { if e.InvolvedObject.UID == pvc.UID && e.Type == corev1api.EventTypeWarning { - diag += fmt.Sprintf("PVC event reason %s, message %s\n", e.Reason, e.Message) + _, _ = fmt.Fprintf(&diag, "PVC event reason %s, message %s\n", e.Reason, e.Message) } } } - return diag + return diag.String() } func DiagnosePV(pv *corev1api.PersistentVolume) string {