Skip to content

Commit

Permalink
fix: lint
Browse files Browse the repository at this point in the history
  • Loading branch information
powerfooI committed Nov 2, 2023
1 parent edf1934 commit 86fee9b
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 51 deletions.
4 changes: 1 addition & 3 deletions pkg/telemetry/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ package telemetry

import "os"

var (
TelemetryRequestSignature string = os.Getenv(TelemetrySignatureEnvName)
)
var TelemetryRequestSignature = os.Getenv(TelemetrySignatureEnvName)

const (
DefaultThrottlerBufferSize = 30
Expand Down
3 changes: 2 additions & 1 deletion pkg/telemetry/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions pkg/telemetry/record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
41 changes: 12 additions & 29 deletions pkg/telemetry/sentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
3 changes: 2 additions & 1 deletion pkg/telemetry/sentry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
9 changes: 5 additions & 4 deletions pkg/telemetry/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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...)
}
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 2 additions & 3 deletions pkg/telemetry/telemetry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
14 changes: 7 additions & 7 deletions pkg/telemetry/throttler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
3 changes: 2 additions & 1 deletion pkg/telemetry/throttler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 86fee9b

Please sign in to comment.