Skip to content

Commit

Permalink
refactor: refine the struct getter functions
Browse files Browse the repository at this point in the history
  • Loading branch information
zyy17 committed Sep 18, 2024
1 parent 678e7d9 commit 765dec4
Show file tree
Hide file tree
Showing 21 changed files with 501 additions and 393 deletions.
165 changes: 159 additions & 6 deletions apis/v1alpha1/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,13 @@ type MainContainerSpec struct {
VolumeMounts []corev1.VolumeMount `json:"volumeMounts,omitempty"`
}

func (in *MainContainerSpec) GetImage() string {
if in != nil {
return in.Image
}
return ""
}

// PodTemplateSpec defines the template for a pod of cluster.
type PodTemplateSpec struct {
// The annotations to be created to the pod.
Expand Down Expand Up @@ -308,6 +315,41 @@ type FileStorage struct {
StorageRetainPolicy StorageRetainPolicyType `json:"storageRetainPolicy,omitempty"`
}

func (in *FileStorage) GetName() string {
if in != nil {
return in.Name
}
return ""
}

func (in *FileStorage) GetStorageClassName() *string {
if in != nil {
return in.StorageClassName
}
return nil
}

func (in *FileStorage) GetSize() string {
if in != nil {
return in.StorageSize
}
return ""
}

func (in *FileStorage) GetMountPath() string {
if in != nil {
return in.MountPath
}
return ""
}

func (in *FileStorage) GetPolicy() StorageRetainPolicyType {
if in != nil {
return in.StorageRetainPolicy
}
return ""
}

// WALProviderSpec defines the WAL provider for the cluster.
type WALProviderSpec struct {
// RaftEngineWAL is the specification for local WAL that uses raft-engine.
Expand All @@ -334,6 +376,34 @@ type KafkaWAL struct {
BrokerEndpoints []string `json:"brokerEndpoints"`
}

func (in *WALProviderSpec) GetRaftEngineWAL() *RaftEngineWAL {
if in != nil {
return in.RaftEngineWAL
}
return nil
}

func (in *WALProviderSpec) GetKafkaWAL() *KafkaWAL {
if in != nil {
return in.KafkaWAL
}
return nil
}

func (in *RaftEngineWAL) GetFileStorage() *FileStorage {
if in != nil {
return in.FileStorage
}
return nil
}

func (in *KafkaWAL) GetBrokerEndpoints() []string {
if in != nil {
return in.BrokerEndpoints
}
return nil
}

// LoggingLevel is the level of the logging.
type LoggingLevel string

Expand Down Expand Up @@ -387,6 +457,28 @@ type LoggingSpec struct {
Format LogFormat `json:"format,omitempty"`
}

func (in *LoggingSpec) GetLevel() LoggingLevel {
if in != nil {
return in.Level
}
return ""
}

func (in *LoggingSpec) GetLogsDir() string {
if in != nil {
return in.LogsDir
}
return ""
}

func (in *LoggingSpec) IsPersistentWithData() bool {
return in != nil && in.PersistentWithData != nil && *in.PersistentWithData
}

func (in *LoggingSpec) IsOnlyLogToStdout() bool {
return in != nil && in.OnlyLogToStdout != nil && *in.OnlyLogToStdout
}

// ServiceSpec defines the service configuration for the component.
type ServiceSpec struct {
// Type is the type of the service.
Expand Down Expand Up @@ -415,6 +507,13 @@ type TLSSpec struct {
SecretName string `json:"secretName"`
}

func (in *TLSSpec) GetSecretName() string {
if in != nil {
return in.SecretName
}
return ""
}

// ObjectStorageProviderSpec defines the object storage provider for the cluster. The data will be stored in the storage.
type ObjectStorageProviderSpec struct {
// S3 is the S3 storage configuration.
Expand All @@ -434,13 +533,32 @@ type ObjectStorageProviderSpec struct {
Cache *CacheStorage `json:"cache,omitempty"`
}

type DatanodeStorageSpec struct {
// DataHome is the home directory of the data.
DataHome string `json:"dataHome,omitempty"`
func (in *ObjectStorageProviderSpec) GetCacheFileStorage() *FileStorage {
if in != nil && in.Cache != nil {
return in.Cache.FileStorage
}
return nil
}

// FileStorage is the file storage configuration.
// +optional
FileStorage *FileStorage `json:"fs,omitempty"`
func (in *ObjectStorageProviderSpec) GetS3Storage() *S3Storage {
if in != nil {
return in.S3
}
return nil
}

func (in *ObjectStorageProviderSpec) GetGCSStorage() *GCSStorage {
if in != nil {
return in.GCS
}
return nil
}

func (in *ObjectStorageProviderSpec) GetOSSStorage() *OSSStorage {
if in != nil {
return in.OSS
}
return nil
}

func (in *ObjectStorageProviderSpec) getSetObjectStorageCount() int {
Expand All @@ -457,6 +575,16 @@ func (in *ObjectStorageProviderSpec) getSetObjectStorageCount() int {
return count
}

// DatanodeStorageSpec defines the storage specification for the datanode.
type DatanodeStorageSpec struct {
// DataHome is the home directory of the data.
DataHome string `json:"dataHome,omitempty"`

// FileStorage is the file storage configuration.
// +optional
FileStorage *FileStorage `json:"fs,omitempty"`
}

// CacheStorage defines the cache storage specification.
type CacheStorage struct {
// Storage is the storage specification for the cache.
Expand Down Expand Up @@ -494,6 +622,13 @@ type S3Storage struct {
Endpoint string `json:"endpoint,omitempty"`
}

func (in *S3Storage) GetSecretName() string {
if in != nil {
return in.SecretName
}
return ""
}

// OSSStorage defines the Aliyun OSS storage specification.
type OSSStorage struct {
// The data will be stored in the bucket.
Expand All @@ -519,6 +654,13 @@ type OSSStorage struct {
Endpoint string `json:"endpoint,omitempty"`
}

func (in *OSSStorage) GetSecretName() string {
if in != nil {
return in.SecretName
}
return ""
}

// GCSStorage defines the Google GCS storage specification.
type GCSStorage struct {
// The data will be stored in the bucket.
Expand All @@ -544,6 +686,13 @@ type GCSStorage struct {
Endpoint string `json:"endpoint,omitempty"`
}

func (in *GCSStorage) GetSecretName() string {
if in != nil {
return in.SecretName
}
return ""
}

// PrometheusMonitorSpec defines the PodMonitor configuration.
type PrometheusMonitorSpec struct {
// Enabled indicates whether the PodMonitor is enabled.
Expand All @@ -559,6 +708,10 @@ type PrometheusMonitorSpec struct {
Interval string `json:"interval,omitempty"`
}

func (in *PrometheusMonitorSpec) IsEnablePrometheusMonitor() bool {
return in != nil && in.Enabled
}

// ConditionType is the type of the condition.
type ConditionType string

Expand Down
10 changes: 5 additions & 5 deletions apis/v1alpha1/defaulting.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ func (in *GreptimeDBCluster) SetDefaults() error {
}

// Set the version of the GreptimeDBClusterSpec if it is not set.
if in.GetVersion() == "" && in.GetMainContainerImage() != "" {
in.Spec.Version = getVersionFromImage(in.GetMainContainerImage())
if in.GetVersion() == "" && in.GetBaseMainContainer().GetImage() != "" {
in.Spec.Version = getVersionFromImage(in.GetBaseMainContainer().GetImage())
}

// Merge the default settings into the GreptimeDBClusterSpec.
Expand Down Expand Up @@ -94,7 +94,7 @@ func (in *GreptimeDBCluster) defaultFrontend() *FrontendSpec {

func (in *GreptimeDBCluster) defaultMeta() *MetaSpec {
enableRegionFailover := false
if in.GetKafkaWAL() != nil { // If remote wal provider is enabled, enable region failover by default.
if in.GetWALProvider().GetKafkaWAL() != nil { // If remote wal provider is enabled, enable region failover by default.
enableRegionFailover = true
}
return &MetaSpec{
Expand Down Expand Up @@ -214,8 +214,8 @@ func (in *GreptimeDBStandalone) SetDefaults() error {
return nil
}

if in.GetVersion() == "" && in.GetMainContainerImage() != "" {
in.Spec.Version = getVersionFromImage(in.GetMainContainerImage())
if in.GetVersion() == "" && in.GetBaseMainContainer().GetImage() != "" {
in.Spec.Version = getVersionFromImage(in.GetBaseMainContainer().GetImage())
}

if err := mergo.Merge(&in.Spec, in.defaultSpec()); err != nil {
Expand Down
Loading

0 comments on commit 765dec4

Please sign in to comment.