Skip to content

Commit

Permalink
feat: add ,inline tag for toml mapping (#183)
Browse files Browse the repository at this point in the history
* feat: add `,inline` tag for toml mapping

Signed-off-by: zyy17 <[email protected]>

* refactor: use pointer package

* refactor: add ConfigureObjectStorage()

* refactor: refine naming

---------

Signed-off-by: zyy17 <[email protected]>
  • Loading branch information
zyy17 authored Sep 20, 2024
1 parent 53816a2 commit 6a205f1
Show file tree
Hide file tree
Showing 14 changed files with 343 additions and 319 deletions.
11 changes: 11 additions & 0 deletions apis/v1alpha1/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,17 @@ type ObjectStorageProviderSpec struct {
Cache *CacheStorage `json:"cache,omitempty"`
}

// ObjectStorageProviderAccessor is the interface that wraps the basic methods for the ObjectStorageProviderSpec.
// +kubebuilder:object:generate=false
type ObjectStorageProviderAccessor interface {
GetS3Storage() *S3Storage
GetGCSStorage() *GCSStorage
GetOSSStorage() *OSSStorage
GetCacheFileStorage() *FileStorage
}

var _ ObjectStorageProviderAccessor = &ObjectStorageProviderSpec{}

func (in *ObjectStorageProviderSpec) GetCacheFileStorage() *FileStorage {
if in != nil && in.Cache != nil {
return in.Cache.FileStorage
Expand Down
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
2 changes: 2 additions & 0 deletions docs/api-references/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,8 @@ _Appears in:_
| `endpoint` _string_ | The endpoint of the bucket. | | |




#### ObjectStorageProviderSpec


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
)
142 changes: 142 additions & 0 deletions pkg/dbconfig/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
// Copyright 2024 Greptime Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package dbconfig

import (
"encoding/base64"

"k8s.io/utils/pointer"

"github.com/GreptimeTeam/greptimedb-operator/apis/v1alpha1"
k8sutil "github.com/GreptimeTeam/greptimedb-operator/pkg/util/k8s"
)

// StorageConfig is the configuration for the storage.
type StorageConfig struct {
StorageType *string `tomlmapping:"storage.type"`
StorageDataHome *string `tomlmapping:"storage.data_home"`
StorageAccessKeyID *string `tomlmapping:"storage.access_key_id"`
StorageSecretAccessKey *string `tomlmapping:"storage.secret_access_key"`
StorageAccessKeySecret *string `tomlmapping:"storage.access_key_secret"`
StorageBucket *string `tomlmapping:"storage.bucket"`
StorageRoot *string `tomlmapping:"storage.root"`
StorageRegion *string `tomlmapping:"storage.region"`
StorageEndpoint *string `tomlmapping:"storage.endpoint"`
StorageScope *string `tomlmapping:"storage.scope"`
StorageCredential *string `tomlmapping:"storage.credential"`
}

// ConfigureObjectStorage configures the storage config by the given object storage provider accessor.
func (c *StorageConfig) ConfigureObjectStorage(namespace string, accessor v1alpha1.ObjectStorageProviderAccessor) error {
if s3 := accessor.GetS3Storage(); s3 != nil {
if err := c.configureS3(namespace, s3); err != nil {
return err
}
} else if oss := accessor.GetOSSStorage(); oss != nil {
if err := c.configureOSS(namespace, oss); err != nil {
return err
}
} else if gcs := accessor.GetGCSStorage(); gcs != nil {
if err := c.configureGCS(namespace, gcs); err != nil {
return err
}
}

return nil
}

func (c *StorageConfig) configureS3(namespace string, s3 *v1alpha1.S3Storage) error {
if s3 == nil {
return nil
}

c.StorageType = pointer.String("S3")
c.StorageBucket = pointer.String(s3.Bucket)
c.StorageRoot = pointer.String(s3.Root)
c.StorageEndpoint = pointer.String(s3.Endpoint)
c.StorageRegion = pointer.String(s3.Region)

if s3.SecretName != "" {
data, err := k8sutil.GetSecretsData(namespace, s3.SecretName, []string{v1alpha1.AccessKeyIDSecretKey, v1alpha1.SecretAccessKeySecretKey})
if err != nil {
return err
}
c.StorageAccessKeyID = pointer.String(string(data[0]))
c.StorageSecretAccessKey = pointer.String(string(data[1]))
}

return nil
}

func (c *StorageConfig) configureOSS(namespace string, oss *v1alpha1.OSSStorage) error {
if oss == nil {
return nil
}

c.StorageType = pointer.String("Oss")
c.StorageBucket = pointer.String(oss.Bucket)
c.StorageRoot = pointer.String(oss.Root)
c.StorageEndpoint = pointer.String(oss.Endpoint)
c.StorageRegion = pointer.String(oss.Region)

if oss.SecretName != "" {
data, err := k8sutil.GetSecretsData(namespace, oss.SecretName, []string{v1alpha1.AccessKeyIDSecretKey, v1alpha1.SecretAccessKeySecretKey})
if err != nil {
return err
}
c.StorageAccessKeyID = pointer.String(string(data[0]))
c.StorageAccessKeySecret = pointer.String(string(data[1]))
}

return nil
}

func (c *StorageConfig) configureGCS(namespace string, gcs *v1alpha1.GCSStorage) error {
if gcs == nil {
return nil
}

c.StorageType = pointer.String("Gcs")
c.StorageBucket = pointer.String(gcs.Bucket)
c.StorageRoot = pointer.String(gcs.Root)
c.StorageEndpoint = pointer.String(gcs.Endpoint)
c.StorageScope = pointer.String(gcs.Scope)

if gcs.SecretName != "" {
data, err := k8sutil.GetSecretsData(namespace, gcs.SecretName, []string{v1alpha1.ServiceAccountKey})
if err != nil {
return err
}

serviceAccountKey := data[0]
if len(serviceAccountKey) != 0 {
c.StorageCredential = pointer.String(base64.StdEncoding.EncodeToString(serviceAccountKey))
}
}

return nil
}

// WALConfig is the configuration for the WAL.
type WALConfig struct {
// The wal file directory.
WalDir *string `tomlmapping:"wal.dir"`

// The wal provider.
WalProvider *string `tomlmapping:"wal.provider"`

// The kafka broker endpoints.
WalBrokerEndpoints []string `tomlmapping:"wal.broker_endpoints"`
}
Loading

0 comments on commit 6a205f1

Please sign in to comment.