Skip to content

Commit

Permalink
refactor: use pointer package
Browse files Browse the repository at this point in the history
  • Loading branch information
zyy17 committed Sep 18, 2024
1 parent 6b1f0e7 commit 5eb94cd
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 47 deletions.
10 changes: 5 additions & 5 deletions cmd/initializer/internal/config_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions controllers/greptimedbcluster/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -126,20 +126,20 @@ 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",
},
},
Datanode: &v1alpha1.DatanodeSpec{
ComponentSpec: v1alpha1.ComponentSpec{
Replicas: proto.Int32(3),
Replicas: pointer.Int32(3),
},
},
},
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
)
Expand All @@ -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
Expand Down Expand Up @@ -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
)
12 changes: 6 additions & 6 deletions pkg/dbconfig/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/dbconfig/datanode_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/dbconfig/standalone_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
}
Expand Down
17 changes: 0 additions & 17 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
10 changes: 5 additions & 5 deletions tests/e2e/greptimedbcluster/test_scale_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")

Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 5eb94cd

Please sign in to comment.