From 5eb94cd2579aac085adce9d80d5fb1161beb4051 Mon Sep 17 00:00:00 2001 From: zyy17 Date: Wed, 18 Sep 2024 19:15:19 +0800 Subject: [PATCH] refactor: use pointer package --- cmd/initializer/internal/config_generator.go | 10 +++++----- .../greptimedbcluster/controller_test.go | 8 ++++---- go.mod | 6 +++--- pkg/dbconfig/common.go | 12 ++++++------ pkg/dbconfig/datanode_config.go | 6 +++--- pkg/dbconfig/standalone_config.go | 8 ++++---- pkg/util/util.go | 17 ----------------- .../e2e/greptimedbcluster/test_scale_cluster.go | 10 +++++----- 8 files changed, 30 insertions(+), 47 deletions(-) diff --git a/cmd/initializer/internal/config_generator.go b/cmd/initializer/internal/config_generator.go index e5b3bc32..387bba46 100644 --- a/cmd/initializer/internal/config_generator.go +++ b/cmd/initializer/internal/config_generator.go @@ -21,11 +21,11 @@ import ( "strings" "k8s.io/klog/v2" + "k8s.io/utils/pointer" "github.com/GreptimeTeam/greptimedb-operator/apis/v1alpha1" "github.com/GreptimeTeam/greptimedb-operator/pkg/dbconfig" "github.com/GreptimeTeam/greptimedb-operator/pkg/deployer" - "github.com/GreptimeTeam/greptimedb-operator/pkg/util" ) type Options struct { @@ -114,14 +114,14 @@ func (c *ConfigGenerator) generateDatanodeConfig(initConfig []byte) ([]byte, err if len(podIP) == 0 { return nil, fmt.Errorf("empty pod ip") } - datanodeCfg.RPCAddr = util.StringPtr(fmt.Sprintf("%s:%d", podIP, c.DatanodeRPCPort)) + datanodeCfg.RPCAddr = pointer.String(fmt.Sprintf("%s:%d", podIP, c.DatanodeRPCPort)) podName := os.Getenv(deployer.EnvPodName) if len(podName) == 0 { return nil, fmt.Errorf("empty pod name") } - datanodeCfg.RPCHostName = util.StringPtr(fmt.Sprintf("%s.%s.%s:%d", podName, + datanodeCfg.RPCHostName = pointer.String(fmt.Sprintf("%s.%s.%s:%d", podName, c.DatanodeServiceName, c.Namespace, c.DatanodeRPCPort)) configData, err := dbconfig.Marshal(cfg) @@ -157,14 +157,14 @@ func (c *ConfigGenerator) generateFlownodeConfig(initConfig []byte) ([]byte, err if len(podIP) == 0 { return nil, fmt.Errorf("empty pod ip") } - flownodeCfg.Addr = util.StringPtr(fmt.Sprintf("%s:%d", podIP, c.RPCPort)) + flownodeCfg.Addr = pointer.String(fmt.Sprintf("%s:%d", podIP, c.RPCPort)) podName := os.Getenv(deployer.EnvPodName) if len(podName) == 0 { return nil, fmt.Errorf("empty pod name") } - flownodeCfg.Hostname = util.StringPtr(fmt.Sprintf("%s.%s.%s:%d", podName, + flownodeCfg.Hostname = pointer.String(fmt.Sprintf("%s.%s.%s:%d", podName, c.ServiceName, c.Namespace, c.RPCPort)) configData, err := dbconfig.Marshal(cfg) diff --git a/controllers/greptimedbcluster/controller_test.go b/controllers/greptimedbcluster/controller_test.go index c42aff93..7f1b1d37 100644 --- a/controllers/greptimedbcluster/controller_test.go +++ b/controllers/greptimedbcluster/controller_test.go @@ -21,10 +21,10 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" - "google.golang.org/protobuf/proto" appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/utils/pointer" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/reconcile" @@ -126,12 +126,12 @@ func createCluster(name, namespace string) *v1alpha1.GreptimeDBCluster { }, Frontend: &v1alpha1.FrontendSpec{ ComponentSpec: v1alpha1.ComponentSpec{ - Replicas: proto.Int32(1), + Replicas: pointer.Int32(1), }, }, Meta: &v1alpha1.MetaSpec{ ComponentSpec: v1alpha1.ComponentSpec{ - Replicas: proto.Int32(1), + Replicas: pointer.Int32(1), }, EtcdEndpoints: []string{ "etcd.default:2379", @@ -139,7 +139,7 @@ func createCluster(name, namespace string) *v1alpha1.GreptimeDBCluster { }, Datanode: &v1alpha1.DatanodeSpec{ ComponentSpec: v1alpha1.ComponentSpec{ - Replicas: proto.Int32(3), + Replicas: pointer.Int32(3), }, }, }, diff --git a/go.mod b/go.mod index f7117998..34fbdee7 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,6 @@ go 1.21 require ( dario.cat/mergo v1.0.1 - github.com/gogo/protobuf v1.3.2 github.com/jackc/pgx/v5 v5.6.0 github.com/onsi/ginkgo/v2 v2.11.0 github.com/onsi/gomega v1.27.10 @@ -15,12 +14,12 @@ require ( github.com/spf13/pflag v1.0.5 go.etcd.io/etcd/client/v3 v3.5.9 google.golang.org/grpc v1.56.3 - google.golang.org/protobuf v1.33.0 k8s.io/api v0.28.3 k8s.io/apiextensions-apiserver v0.28.3 k8s.io/apimachinery v0.28.3 k8s.io/client-go v0.28.3 k8s.io/klog/v2 v2.100.1 + k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 sigs.k8s.io/controller-runtime v0.16.4 sigs.k8s.io/yaml v1.3.0 ) @@ -40,6 +39,7 @@ require ( github.com/go-openapi/jsonreference v0.20.2 // indirect github.com/go-openapi/swag v0.22.3 // indirect github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect + github.com/gogo/protobuf v1.3.2 // indirect github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect github.com/golang/protobuf v1.5.3 // indirect github.com/google/gnostic-models v0.6.8 // indirect @@ -83,12 +83,12 @@ require ( google.golang.org/genproto v0.0.0-20230526161137-0005af68ea54 // indirect google.golang.org/genproto/googleapis/api v0.0.0-20230525234035-dd9d682886f9 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20230525234030-28d5490b6b19 // indirect + google.golang.org/protobuf v1.33.0 // indirect gopkg.in/inf.v0 v0.9.1 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect k8s.io/component-base v0.28.3 // indirect k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9 // indirect - k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 // indirect sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect ) diff --git a/pkg/dbconfig/common.go b/pkg/dbconfig/common.go index af6686d8..bafa115a 100644 --- a/pkg/dbconfig/common.go +++ b/pkg/dbconfig/common.go @@ -38,8 +38,8 @@ type StorageConfig struct { StorageCredential *string `tomlmapping:"storage.credential"` } -// ConfigureS3Storage configures the storage config with the given S3 storage. -func (c *StorageConfig) ConfigureS3Storage(namespace string, s3 *v1alpha1.S3Storage) error { +// ConfigureS3 configures the storage config with the given S3 storage. +func (c *StorageConfig) ConfigureS3(namespace string, s3 *v1alpha1.S3Storage) error { if s3 == nil { return nil } @@ -62,8 +62,8 @@ func (c *StorageConfig) ConfigureS3Storage(namespace string, s3 *v1alpha1.S3Stor return nil } -// ConfigureOSSStorage configures the storage config with the given OSS storage. -func (c *StorageConfig) ConfigureOSSStorage(namespace string, oss *v1alpha1.OSSStorage) error { +// ConfigureOSS configures the storage config with the given OSS storage. +func (c *StorageConfig) ConfigureOSS(namespace string, oss *v1alpha1.OSSStorage) error { if oss == nil { return nil } @@ -86,8 +86,8 @@ func (c *StorageConfig) ConfigureOSSStorage(namespace string, oss *v1alpha1.OSSS return nil } -// ConfigureGCSStorage configures the storage config with the given GCS storage. -func (c *StorageConfig) ConfigureGCSStorage(namespace string, gcs *v1alpha1.GCSStorage) error { +// ConfigureGCS configures the storage config with the given GCS storage. +func (c *StorageConfig) ConfigureGCS(namespace string, gcs *v1alpha1.GCSStorage) error { if gcs == nil { return nil } diff --git a/pkg/dbconfig/datanode_config.go b/pkg/dbconfig/datanode_config.go index 0615ca33..ee25eda0 100644 --- a/pkg/dbconfig/datanode_config.go +++ b/pkg/dbconfig/datanode_config.go @@ -41,19 +41,19 @@ type DatanodeConfig struct { // ConfigureByCluster configures the datanode config by the given cluster. func (c *DatanodeConfig) ConfigureByCluster(cluster *v1alpha1.GreptimeDBCluster) error { if s3 := cluster.GetObjectStorageProvider().GetS3Storage(); s3 != nil { - if err := c.ConfigureS3Storage(cluster.GetNamespace(), s3); err != nil { + if err := c.ConfigureS3(cluster.GetNamespace(), s3); err != nil { return err } } if oss := cluster.GetObjectStorageProvider().GetOSSStorage(); oss != nil { - if err := c.ConfigureOSSStorage(cluster.GetNamespace(), oss); err != nil { + if err := c.ConfigureOSS(cluster.GetNamespace(), oss); err != nil { return err } } if gcs := cluster.GetObjectStorageProvider().GetGCSStorage(); gcs != nil { - if err := c.ConfigureGCSStorage(cluster.GetNamespace(), gcs); err != nil { + if err := c.ConfigureGCS(cluster.GetNamespace(), gcs); err != nil { return err } } diff --git a/pkg/dbconfig/standalone_config.go b/pkg/dbconfig/standalone_config.go index 4a97d42a..7432f629 100644 --- a/pkg/dbconfig/standalone_config.go +++ b/pkg/dbconfig/standalone_config.go @@ -20,7 +20,7 @@ import ( "github.com/GreptimeTeam/greptimedb-operator/apis/v1alpha1" ) -var _ Config = &FrontendConfig{} +var _ Config = &StandaloneConfig{} // StandaloneConfig is the configuration for the frontend. type StandaloneConfig struct { @@ -42,19 +42,19 @@ func (c *StandaloneConfig) ConfigureByCluster(_ *v1alpha1.GreptimeDBCluster) err // ConfigureByStandalone is not need to implement in cluster mode. func (c *StandaloneConfig) ConfigureByStandalone(standalone *v1alpha1.GreptimeDBStandalone) error { if s3 := standalone.GetObjectStorageProvider().GetS3Storage(); s3 != nil { - if err := c.ConfigureS3Storage(standalone.GetNamespace(), s3); err != nil { + if err := c.ConfigureS3(standalone.GetNamespace(), s3); err != nil { return err } } if oss := standalone.GetObjectStorageProvider().GetOSSStorage(); oss != nil { - if err := c.ConfigureOSSStorage(standalone.GetNamespace(), oss); err != nil { + if err := c.ConfigureOSS(standalone.GetNamespace(), oss); err != nil { return err } } if gcs := standalone.GetObjectStorageProvider().GetGCSStorage(); gcs != nil { - if err := c.ConfigureGCSStorage(standalone.GetNamespace(), gcs); err != nil { + if err := c.ConfigureGCS(standalone.GetNamespace(), gcs); err != nil { return err } } diff --git a/pkg/util/util.go b/pkg/util/util.go index 72f84ca2..cf66df7d 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -41,20 +41,3 @@ func CalculateConfigHash(config []byte) string { hash := sha256.Sum256(config) return hex.EncodeToString(hash[:]) } - -// TODO(zyy17): Use generic to implement the following functions. - -func StringPtr(s string) *string { - if len(s) > 0 { - return &s - } - return nil -} - -func BoolPtr(b bool) *bool { - return &b -} - -func Uint64Ptr(i uint64) *uint64 { - return &i -} diff --git a/tests/e2e/greptimedbcluster/test_scale_cluster.go b/tests/e2e/greptimedbcluster/test_scale_cluster.go index 35dc8bf8..3ef18047 100644 --- a/tests/e2e/greptimedbcluster/test_scale_cluster.go +++ b/tests/e2e/greptimedbcluster/test_scale_cluster.go @@ -23,7 +23,7 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" - "github.com/gogo/protobuf/proto" + "k8s.io/utils/pointer" "sigs.k8s.io/controller-runtime/pkg/client" greptimev1alpha1 "github.com/GreptimeTeam/greptimedb-operator/apis/v1alpha1" @@ -79,8 +79,8 @@ func TestScaleCluster(ctx context.Context, h *helper.Helper) { By("Scale up the cluster") err = h.Get(ctx, client.ObjectKey{Name: testCluster.Name, Namespace: testCluster.Namespace}, testCluster) Expect(err).NotTo(HaveOccurred(), "failed to get cluster") - testCluster.Spec.Datanode.Replicas = proto.Int(3) - testCluster.Spec.Frontend.Replicas = proto.Int(2) + testCluster.Spec.Datanode.Replicas = pointer.Int32(3) + testCluster.Spec.Frontend.Replicas = pointer.Int32(2) err = h.Update(ctx, testCluster) Expect(err).NotTo(HaveOccurred(), "failed to update cluster") @@ -113,8 +113,8 @@ func TestScaleCluster(ctx context.Context, h *helper.Helper) { By("Scale down the cluster") err = h.Get(ctx, client.ObjectKey{Name: testCluster.Name, Namespace: testCluster.Namespace}, testCluster) Expect(err).NotTo(HaveOccurred(), "failed to get cluster") - testCluster.Spec.Datanode.Replicas = proto.Int(1) - testCluster.Spec.Frontend.Replicas = proto.Int(1) + testCluster.Spec.Datanode.Replicas = pointer.Int32(1) + testCluster.Spec.Frontend.Replicas = pointer.Int32(1) err = h.Update(ctx, testCluster) Expect(err).NotTo(HaveOccurred(), "failed to update cluster")