From 86fee9b9de4f28f1995d0866c05e5b021a10aa90 Mon Sep 17 00:00:00 2001 From: yuyi Date: Thu, 2 Nov 2023 21:34:40 +0800 Subject: [PATCH] fix: lint --- pkg/telemetry/configs.go | 4 +--- pkg/telemetry/metrics.go | 3 ++- pkg/telemetry/record_test.go | 5 ++-- pkg/telemetry/sentry.go | 41 ++++++++++----------------------- pkg/telemetry/sentry_test.go | 3 ++- pkg/telemetry/telemetry.go | 9 ++++---- pkg/telemetry/telemetry_test.go | 5 ++-- pkg/telemetry/throttler.go | 14 +++++------ pkg/telemetry/throttler_test.go | 3 ++- 9 files changed, 36 insertions(+), 51 deletions(-) diff --git a/pkg/telemetry/configs.go b/pkg/telemetry/configs.go index 781900162..b721503fe 100644 --- a/pkg/telemetry/configs.go +++ b/pkg/telemetry/configs.go @@ -14,9 +14,7 @@ package telemetry import "os" -var ( - TelemetryRequestSignature string = os.Getenv(TelemetrySignatureEnvName) -) +var TelemetryRequestSignature = os.Getenv(TelemetrySignatureEnvName) const ( DefaultThrottlerBufferSize = 30 diff --git a/pkg/telemetry/metrics.go b/pkg/telemetry/metrics.go index 2cc8eb6eb..ff528712f 100644 --- a/pkg/telemetry/metrics.go +++ b/pkg/telemetry/metrics.go @@ -21,13 +21,14 @@ import ( "path/filepath" "sync" - "github.com/oceanbase/ob-operator/pkg/telemetry/models" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" "k8s.io/client-go/tools/clientcmd" "k8s.io/client-go/util/homedir" + + "github.com/oceanbase/ob-operator/pkg/telemetry/models" ) type hostMetrics struct { diff --git a/pkg/telemetry/record_test.go b/pkg/telemetry/record_test.go index 1ab51891a..30d1b6cc9 100644 --- a/pkg/telemetry/record_test.go +++ b/pkg/telemetry/record_test.go @@ -17,10 +17,11 @@ import ( "fmt" "time" - "github.com/oceanbase/ob-operator/api/v1alpha1" - "github.com/oceanbase/ob-operator/pkg/telemetry/models" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" + + "github.com/oceanbase/ob-operator/api/v1alpha1" + "github.com/oceanbase/ob-operator/pkg/telemetry/models" ) var _ = Describe("Telemetry record", func() { diff --git a/pkg/telemetry/sentry.go b/pkg/telemetry/sentry.go index 59f307c40..3bf09d940 100644 --- a/pkg/telemetry/sentry.go +++ b/pkg/telemetry/sentry.go @@ -22,51 +22,34 @@ func objectSentry(object any) { if object == nil { return } - switch object.(type) { - case *v1alpha1.OBCluster: - cluster, ok := object.(*v1alpha1.OBCluster) - if !ok { - return - } + if cluster, ok := object.(*v1alpha1.OBCluster); ok { processOBCluster(cluster) - case *v1alpha1.OBTenant: - tenant, ok := object.(*v1alpha1.OBTenant) - if !ok { - return - } + } else if tenant, ok := object.(*v1alpha1.OBTenant); ok { processOBTenant(tenant) - case *v1alpha1.OBServer: - server, ok := object.(*v1alpha1.OBServer) - if !ok { - return - } + } else if server, ok := object.(*v1alpha1.OBServer); ok { processOBServer(server) - case *v1alpha1.OBZone: - zone, ok := object.(*v1alpha1.OBZone) - if !ok { - return - } + } else if zone, ok := object.(*v1alpha1.OBZone); ok { processOBZone(zone) } } func processOBCluster(cluster *v1alpha1.OBCluster) { - fmt.Printf("[OBCluster Before] %+v\n", cluster) + _, _ = fmt.Printf("[OBCluster Before] %+v\n", cluster) if cluster.Spec.BackupVolume != nil && cluster.Spec.BackupVolume.Volume != nil && cluster.Spec.BackupVolume.Volume.NFS != nil { cluster.Spec.BackupVolume.Volume.NFS.Server = md5Hash(cluster.Spec.BackupVolume.Volume.NFS.Server) } - fmt.Printf("[OBCluster After] %+v\n", cluster) + _, _ = fmt.Printf("[OBCluster After] %+v\n", cluster) } func processOBServer(server *v1alpha1.OBServer) { - fmt.Printf("[OBServer Before] %+v\n", server) + _, _ = fmt.Printf("[OBServer Before] %+v\n", server) server.Status.PodIp = md5Hash(server.Status.PodIp) server.Status.NodeIp = md5Hash(server.Status.NodeIp) - fmt.Printf("[OBServer After] %+v\n", server) + _, _ = fmt.Printf("[OBServer After] %+v\n", server) } func processOBTenant(tenant *v1alpha1.OBTenant) { - fmt.Printf("[OBTenant After] %+v\n", tenant) + _, _ = fmt.Printf("[OBTenant After] %+v\n", tenant) for i := range tenant.Status.Pools { for j := range tenant.Status.Pools[i].Units { tenant.Status.Pools[i].Units[j].ServerIP = md5Hash(tenant.Status.Pools[i].Units[j].ServerIP) @@ -75,16 +58,16 @@ func processOBTenant(tenant *v1alpha1.OBTenant) { } } } - fmt.Printf("[OBTenant After] %+v\n", tenant) + _, _ = fmt.Printf("[OBTenant After] %+v\n", tenant) } func processOBZone(zone *v1alpha1.OBZone) { - fmt.Printf("[OBZone Before] %+v\n", zone) + _, _ = fmt.Printf("[OBZone Before] %+v\n", zone) for i := range zone.Status.OBServerStatus { zone.Status.OBServerStatus[i].Server = md5Hash(zone.Status.OBServerStatus[i].Server) } if zone.Spec.BackupVolume != nil && zone.Spec.BackupVolume.Volume != nil && zone.Spec.BackupVolume.Volume.NFS != nil { zone.Spec.BackupVolume.Volume.NFS.Server = md5Hash(zone.Spec.BackupVolume.Volume.NFS.Server) } - fmt.Printf("[OBZone After] %+v\n", zone) + _, _ = fmt.Printf("[OBZone After] %+v\n", zone) } diff --git a/pkg/telemetry/sentry_test.go b/pkg/telemetry/sentry_test.go index aec340374..d301f9c61 100644 --- a/pkg/telemetry/sentry_test.go +++ b/pkg/telemetry/sentry_test.go @@ -13,10 +13,11 @@ See the Mulan PSL v2 for more details. package telemetry import ( - "github.com/oceanbase/ob-operator/api/v1alpha1" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" corev1 "k8s.io/api/core/v1" + + "github.com/oceanbase/ob-operator/api/v1alpha1" ) var _ = Describe("Telemetry sentry", Label("sentry"), func() { diff --git a/pkg/telemetry/telemetry.go b/pkg/telemetry/telemetry.go index bd7be5799..322deb4ae 100644 --- a/pkg/telemetry/telemetry.go +++ b/pkg/telemetry/telemetry.go @@ -18,9 +18,10 @@ import ( "os" "time" - "github.com/oceanbase/ob-operator/pkg/telemetry/models" "k8s.io/apimachinery/pkg/runtime" record "k8s.io/client-go/tools/record" + + "github.com/oceanbase/ob-operator/pkg/telemetry/models" ) type Telemetry interface { @@ -65,13 +66,13 @@ func (t *telemetry) Event(object runtime.Object, eventType, reason, message stri } // Implement record.EventRecorder interface -func (t *telemetry) Eventf(object runtime.Object, eventType, reason, messageFmt string, args ...interface{}) { +func (t *telemetry) Eventf(object runtime.Object, eventType, reason, messageFmt string, args ...any) { t.EventRecorder.Eventf(object, eventType, reason, messageFmt, args...) t.generateFromEvent(object, nil, eventType, reason, messageFmt, args...) } // Implement record.EventRecorder interface -func (t *telemetry) AnnotatedEventf(object runtime.Object, annotations map[string]string, eventType, reason, messageFmt string, args ...interface{}) { +func (t *telemetry) AnnotatedEventf(object runtime.Object, annotations map[string]string, eventType, reason, messageFmt string, args ...any) { t.EventRecorder.AnnotatedEventf(object, annotations, eventType, reason, messageFmt, args...) t.generateFromEvent(object, annotations, eventType, reason, messageFmt, args...) } @@ -104,7 +105,7 @@ func (t *telemetry) GetHostMetrics() *hostMetrics { return t.hostMetrics } -func (t *telemetry) generateFromEvent(object runtime.Object, annotations map[string]string, eventType, reason, messageFmt string, args ...interface{}) { +func (t *telemetry) generateFromEvent(object runtime.Object, annotations map[string]string, eventType, reason, messageFmt string, args ...any) { if object == nil { t.GenerateTelemetryRecord(nil, "Unknown", eventType, reason, fmt.Sprintf(messageFmt, args...), annotations) } else { diff --git a/pkg/telemetry/telemetry_test.go b/pkg/telemetry/telemetry_test.go index 1a026ddab..ba0b27790 100644 --- a/pkg/telemetry/telemetry_test.go +++ b/pkg/telemetry/telemetry_test.go @@ -15,13 +15,12 @@ package telemetry import ( "time" - "github.com/oceanbase/ob-operator/api/v1alpha1" . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" + + "github.com/oceanbase/ob-operator/api/v1alpha1" ) type fakeEventRecorder struct{} diff --git a/pkg/telemetry/throttler.go b/pkg/telemetry/throttler.go index e14f3dd9c..58d1cd10f 100644 --- a/pkg/telemetry/throttler.go +++ b/pkg/telemetry/throttler.go @@ -87,28 +87,28 @@ func (t *throttler) sendTelemetryRecord(record *models.TelemetryRecord) (*http.R func (t *throttler) startWorkers() { for i := 0; i < DefaultThrottlerWorkerCount; i++ { - go func(ctx context.Context, ch <-chan *models.TelemetryRecord) error { + go func(ctx context.Context, ch <-chan *models.TelemetryRecord) { for { select { case record, ok := <-ch: if !ok { // channel closed - return nil + return } res, err := t.sendTelemetryRecord(record) if t.debug { if err != nil { - fmt.Printf("send telemetry record error: %v\n", err) + _, _ = fmt.Printf("send telemetry record error: %v\n", err) } bts, err := io.ReadAll(res.Body) if err != nil { - fmt.Printf("read response body error: %v\n", err) + _, _ = fmt.Printf("read response body error: %v\n", err) } - fmt.Printf("[Event %s.%s] %s\n", record.ResourceType, record.EventType, string(bts)) + _, _ = fmt.Printf("[Event %s.%s] %s\n", record.ResourceType, record.EventType, string(bts)) } case <-ctx.Done(): - return ctx.Err() - default: + _, _ = fmt.Println(ctx.Err()) + return } } }(t.ctx, t.chanOut()) diff --git a/pkg/telemetry/throttler_test.go b/pkg/telemetry/throttler_test.go index 73f9ae11b..3194fc78f 100644 --- a/pkg/telemetry/throttler_test.go +++ b/pkg/telemetry/throttler_test.go @@ -17,9 +17,10 @@ import ( "io" "time" - "github.com/oceanbase/ob-operator/pkg/telemetry/models" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" + + "github.com/oceanbase/ob-operator/pkg/telemetry/models" ) var _ = Describe("Telemetry throttler", Ordered, Label("throttler"), func() {