Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update druid API for simplifying YAML #1222

Merged
merged 2 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 77 additions & 8 deletions apis/kubedb/v1alpha2/druid_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ import (
"github.com/Masterminds/semver/v3"
promapi "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
"gomodules.xyz/pointer"
core "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -176,12 +178,44 @@ func (d *Druid) PetSetName(nodeRole DruidNodeRoleType) string {
return meta_util.NameWithSuffix(d.OffShootName(), d.DruidNodeRoleString(nodeRole))
}

func (d *Druid) PodLabels(extraLebels ...map[string]string) map[string]string {
return d.offShootLabels(meta_util.OverwriteKeys(d.OffShootSelectors(), extraLebels...), d.Spec.PodTemplate.Labels)
func (d *Druid) PodLabels(nodeType DruidNodeRoleType, extraLabels ...map[string]string) map[string]string {
nodeSpec, dataNodeSpec := d.GetNodeSpec(nodeType)
var labels map[string]string
if nodeSpec != nil {
labels = nodeSpec.PodTemplate.Labels
} else {
labels = dataNodeSpec.PodTemplate.Labels
}
return d.offShootLabels(meta_util.OverwriteKeys(d.OffShootSelectors(), extraLabels...), labels)
}

func (d *Druid) PodControllerLabels(extraLabels ...map[string]string) map[string]string {
return d.offShootLabels(meta_util.OverwriteKeys(d.OffShootSelectors(), extraLabels...), d.Spec.PodTemplate.Controller.Labels)
func (d *Druid) PodControllerLabels(nodeType DruidNodeRoleType, extraLabels ...map[string]string) map[string]string {
nodeSpec, dataNodeSpec := d.GetNodeSpec(nodeType)
var labels map[string]string
if nodeSpec != nil {
labels = nodeSpec.PodTemplate.Controller.Labels
} else {
labels = dataNodeSpec.PodTemplate.Controller.Labels
}
return d.offShootLabels(meta_util.OverwriteKeys(d.OffShootSelectors(), extraLabels...), labels)
}

func (d *Druid) GetNodeSpec(nodeType DruidNodeRoleType) (*DruidNode, *DruidDataNode) {
if nodeType == DruidNodeRoleCoordinators {
return d.Spec.Topology.Coordinators, nil
} else if nodeType == DruidNodeRoleOverlords {
return d.Spec.Topology.Overlords, nil
} else if nodeType == DruidNodeRoleMiddleManagers {
return nil, d.Spec.Topology.MiddleManagers
} else if nodeType == DruidNodeRoleHistoricals {
return nil, d.Spec.Topology.Historicals
} else if nodeType == DruidNodeRoleBrokers {
return d.Spec.Topology.Brokers, nil
} else if nodeType == DruidNodeRoleRouters {
return d.Spec.Topology.Routers, nil
}

panic("Node role name does not match any known types")
}

func (d *Druid) ServiceAccountName() string {
Expand Down Expand Up @@ -339,10 +373,6 @@ func (d *Druid) SetDefaults() {
d.Spec.TerminationPolicy = TerminationPolicyDelete
}

if d.Spec.StorageType == "" {
d.Spec.StorageType = StorageTypeDurable
}

if d.Spec.DisableSecurity == nil {
d.Spec.DisableSecurity = pointer.BoolP(false)
}
Expand Down Expand Up @@ -371,6 +401,9 @@ func (d *Druid) SetDefaults() {
}

if d.Spec.Topology != nil {
if d.Spec.Topology.Coordinators == nil {
d.Spec.Topology.Coordinators = &DruidNode{}
}
if d.Spec.Topology.Coordinators != nil {
if d.Spec.Topology.Coordinators.Replicas == nil {
d.Spec.Topology.Coordinators.Replicas = pointer.Int32P(1)
Expand All @@ -383,6 +416,7 @@ func (d *Druid) SetDefaults() {
d.setDefaultContainerResourceLimits(&d.Spec.Topology.Coordinators.PodTemplate, DruidNodeRoleCoordinators)
}
}

if d.Spec.Topology.Overlords != nil {
if d.Spec.Topology.Overlords.Replicas == nil {
d.Spec.Topology.Overlords.Replicas = pointer.Int32P(1)
Expand All @@ -395,10 +429,20 @@ func (d *Druid) SetDefaults() {
d.setDefaultContainerResourceLimits(&d.Spec.Topology.Overlords.PodTemplate, DruidNodeRoleOverlords)
}
}

if d.Spec.Topology.MiddleManagers == nil {
d.Spec.Topology.MiddleManagers = &DruidDataNode{}
}
if d.Spec.Topology.MiddleManagers != nil {
if d.Spec.Topology.MiddleManagers.Replicas == nil {
d.Spec.Topology.MiddleManagers.Replicas = pointer.Int32P(1)
}
if d.Spec.Topology.MiddleManagers.StorageType == "" {
d.Spec.Topology.MiddleManagers.StorageType = StorageTypeDurable
}
if d.Spec.Topology.MiddleManagers.Storage == nil && d.Spec.Topology.MiddleManagers.StorageType == StorageTypeDurable {
d.Spec.Topology.MiddleManagers.Storage = d.getDefaultPVC()
}
if version.Major() > 25 {
if d.Spec.Topology.MiddleManagers.PodTemplate.Spec.SecurityContext == nil {
d.Spec.Topology.MiddleManagers.PodTemplate.Spec.SecurityContext = &v1.PodSecurityContext{FSGroup: druidVersion.Spec.SecurityContext.RunAsUser}
Expand All @@ -407,10 +451,20 @@ func (d *Druid) SetDefaults() {
d.setDefaultContainerResourceLimits(&d.Spec.Topology.MiddleManagers.PodTemplate, DruidNodeRoleMiddleManagers)
}
}

if d.Spec.Topology.Historicals == nil {
d.Spec.Topology.Historicals = &DruidDataNode{}
}
if d.Spec.Topology.Historicals != nil {
if d.Spec.Topology.Historicals.Replicas == nil {
d.Spec.Topology.Historicals.Replicas = pointer.Int32P(1)
}
if d.Spec.Topology.Historicals.StorageType == "" {
d.Spec.Topology.Historicals.StorageType = StorageTypeDurable
}
if d.Spec.Topology.Historicals.Storage == nil && d.Spec.Topology.Historicals.StorageType == StorageTypeDurable {
d.Spec.Topology.Historicals.Storage = d.getDefaultPVC()
}
if version.Major() > 25 {
if d.Spec.Topology.Historicals.PodTemplate.Spec.SecurityContext == nil {
d.Spec.Topology.Historicals.PodTemplate.Spec.SecurityContext = &v1.PodSecurityContext{FSGroup: druidVersion.Spec.SecurityContext.RunAsUser}
Expand All @@ -419,6 +473,10 @@ func (d *Druid) SetDefaults() {
d.setDefaultContainerResourceLimits(&d.Spec.Topology.Historicals.PodTemplate, DruidNodeRoleHistoricals)
}
}

if d.Spec.Topology.Brokers == nil {
d.Spec.Topology.Brokers = &DruidNode{}
}
tapojit047 marked this conversation as resolved.
Show resolved Hide resolved
if d.Spec.Topology.Brokers != nil {
if d.Spec.Topology.Brokers.Replicas == nil {
d.Spec.Topology.Brokers.Replicas = pointer.Int32P(1)
Expand All @@ -432,6 +490,7 @@ func (d *Druid) SetDefaults() {

}
}

if d.Spec.Topology.Routers != nil {
if d.Spec.Topology.Routers.Replicas == nil {
d.Spec.Topology.Routers.Replicas = pointer.Int32P(1)
Expand Down Expand Up @@ -461,6 +520,16 @@ func (d *Druid) SetDefaults() {
}
}

func (d *Druid) getDefaultPVC() *core.PersistentVolumeClaimSpec {
return &core.PersistentVolumeClaimSpec{
Resources: core.VolumeResourceRequirements{
Requests: core.ResourceList{
core.ResourceStorage: resource.MustParse("1Gi"),
},
},
}
}

func (d *Druid) setDefaultContainerSecurityContext(druidVersion *catalog.DruidVersion, podTemplate *ofst.PodTemplateSpec) {
container := coreutil.GetContainerByName(podTemplate.Spec.Containers, DruidContainerName)
if container == nil {
Expand Down
37 changes: 21 additions & 16 deletions apis/kubedb/v1alpha2/druid_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,6 @@ type DruidSpec struct {
// +optional
Topology *DruidClusterTopology `json:"topology,omitempty"`

// StorageType can be durable (default) or ephemeral.
StorageType StorageType `json:"storageType,omitempty"`

// disable security. It disables authentication security of user.
// If unset, default is false
// +optional
Expand Down Expand Up @@ -95,10 +92,6 @@ type DruidSpec struct {
// +optional
ZookeeperRef *ZookeeperRef `json:"zookeeperRef,omitempty"`

// PodTemplate is an optional configuration
// +optional
PodTemplate ofst.PodTemplateSpec `json:"podTemplate,omitempty"`

// ServiceTemplates is an optional configuration for services used to expose database
// +optional
ServiceTemplates []NamedServiceTemplateSpec `json:"serviceTemplates,omitempty"`
Expand All @@ -122,32 +115,29 @@ type DruidSpec struct {
}

type DruidClusterTopology struct {
Coordinators *DruidNode `json:"coordinators"`
Coordinators *DruidNode `json:"coordinators,omitempty"`
tapojit047 marked this conversation as resolved.
Show resolved Hide resolved
// +optional
Overlords *DruidNode `json:"overlords,omitempty"`

MiddleManagers *DruidNode `json:"middleManagers"`
MiddleManagers *DruidDataNode `json:"middleManagers,omitempty"`

Historicals *DruidNode `json:"historicals"`
Historicals *DruidDataNode `json:"historicals,omitempty"`

Brokers *DruidNode `json:"brokers"`
Brokers *DruidNode `json:"brokers,omitempty"`
// +optional
Routers *DruidNode `json:"routers,omitempty"`
}

type DruidNode struct {
// Replicas represents number of replica for the specific type of node
// Replicas represents number of replicas for the specific type of node
// +kubebuilder:default=1
// +optional
Replicas *int32 `json:"replicas,omitempty"`

// Suffix to append with node name
// +optional
Suffix string `json:"suffix,omitempty"`

// Storage to specify how storage shall be used.
// +optional
Storage *core.PersistentVolumeClaimSpec `json:"storage,omitempty"`

// PodTemplate is an optional configuration for pods used to expose database
// +optional
PodTemplate ofst.PodTemplateSpec `json:"podTemplate,omitempty"`
Expand All @@ -168,6 +158,21 @@ type DruidNode struct {
PodPlacementPolicy *core.LocalObjectReference `json:"podPlacementPolicy,omitempty"`
}

type DruidDataNode struct {
// DruidDataNode has all the characteristics of DruidNode
DruidNode `json:",inline"`

// StorageType specifies if the storage
// of this node is durable (default) or ephemeral.
StorageType StorageType `json:"storageType,omitempty"`

// Storage to specify how storage shall be used.
Storage *core.PersistentVolumeClaimSpec `json:"storage,omitempty"`

// EphemeralStorage spec to specify the configuration of ephemeral storage type.
EphemeralStorage *core.EmptyDirVolumeSource `json:"ephemeralStorage,omitempty"`
}

type MetadataStorage struct {
// Name of the appbinding of zookeeper
// +optional
Expand Down
Loading
Loading