Skip to content

Commit

Permalink
Update druid API for simplifying YAML
Browse files Browse the repository at this point in the history
Signed-off-by: Tapajit Chandra Paul <[email protected]>
  • Loading branch information
tapojit047 committed May 24, 2024
1 parent 08fb210 commit b5c9e50
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 2,940 deletions.
80 changes: 58 additions & 22 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,32 @@ 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 := d.GetNodeSpec(nodeType)
return d.offShootLabels(meta_util.OverwriteKeys(d.OffShootSelectors(), extraLabels...), nodeSpec.PodTemplate.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 := d.GetNodeSpec(nodeType)
return d.offShootLabels(meta_util.OverwriteKeys(d.OffShootSelectors(), extraLabels...), nodeSpec.PodTemplate.Controller.Labels)
}

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

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

func (d *Druid) ServiceAccountName() string {
Expand Down Expand Up @@ -371,10 +393,10 @@ 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)
}
if version.Major() > 25 {
if d.Spec.Topology.Coordinators.PodTemplate.Spec.SecurityContext == nil {
d.Spec.Topology.Coordinators.PodTemplate.Spec.SecurityContext = &v1.PodSecurityContext{FSGroup: druidVersion.Spec.SecurityContext.RunAsUser}
Expand All @@ -384,9 +406,6 @@ func (d *Druid) SetDefaults() {
}
}
if d.Spec.Topology.Overlords != nil {
if d.Spec.Topology.Overlords.Replicas == nil {
d.Spec.Topology.Overlords.Replicas = pointer.Int32P(1)
}
if version.Major() > 25 {
if d.Spec.Topology.Overlords.PodTemplate.Spec.SecurityContext == nil {
d.Spec.Topology.Overlords.PodTemplate.Spec.SecurityContext = &v1.PodSecurityContext{FSGroup: druidVersion.Spec.SecurityContext.RunAsUser}
Expand All @@ -395,10 +414,14 @@ func (d *Druid) SetDefaults() {
d.setDefaultContainerResourceLimits(&d.Spec.Topology.Overlords.PodTemplate, DruidNodeRoleOverlords)
}
}

if d.Spec.Topology.MiddleManagers == nil {
d.Spec.Topology.MiddleManagers = &DruidNode{}
}
if d.Spec.Topology.MiddleManagers.Storage == nil && d.Spec.StorageType == StorageTypeDurable {
d.Spec.Topology.MiddleManagers.Storage = d.getDefaultPVC()
}
if d.Spec.Topology.MiddleManagers != nil {
if d.Spec.Topology.MiddleManagers.Replicas == nil {
d.Spec.Topology.MiddleManagers.Replicas = pointer.Int32P(1)
}
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 +430,14 @@ func (d *Druid) SetDefaults() {
d.setDefaultContainerResourceLimits(&d.Spec.Topology.MiddleManagers.PodTemplate, DruidNodeRoleMiddleManagers)
}
}

if d.Spec.Topology.Historicals == nil {
d.Spec.Topology.Historicals = &DruidNode{}
}
if d.Spec.Topology.Historicals.Storage == nil && d.Spec.StorageType == StorageTypeDurable {
d.Spec.Topology.Historicals.Storage = d.getDefaultPVC()
}
if d.Spec.Topology.Historicals != nil {
if d.Spec.Topology.Historicals.Replicas == nil {
d.Spec.Topology.Historicals.Replicas = pointer.Int32P(1)
}
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,10 +446,11 @@ func (d *Druid) SetDefaults() {
d.setDefaultContainerResourceLimits(&d.Spec.Topology.Historicals.PodTemplate, DruidNodeRoleHistoricals)
}
}

if d.Spec.Topology.Brokers == nil {
d.Spec.Topology.Brokers = &DruidNode{}
}
if d.Spec.Topology.Brokers != nil {
if d.Spec.Topology.Brokers.Replicas == nil {
d.Spec.Topology.Brokers.Replicas = pointer.Int32P(1)
}
if version.Major() > 25 {
if d.Spec.Topology.Brokers.PodTemplate.Spec.SecurityContext == nil {
d.Spec.Topology.Brokers.PodTemplate.Spec.SecurityContext = &v1.PodSecurityContext{FSGroup: druidVersion.Spec.SecurityContext.RunAsUser}
Expand All @@ -432,10 +460,8 @@ 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)
}
if version.Major() > 25 {
if d.Spec.Topology.Routers.PodTemplate.Spec.SecurityContext == nil {
d.Spec.Topology.Routers.PodTemplate.Spec.SecurityContext = &v1.PodSecurityContext{FSGroup: druidVersion.Spec.SecurityContext.RunAsUser}
Expand All @@ -461,6 +487,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
18 changes: 9 additions & 9 deletions apis/kubedb/v1alpha2/druid_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,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,21 +118,22 @@ type DruidSpec struct {
}

type DruidClusterTopology struct {
Coordinators *DruidNode `json:"coordinators"`
Coordinators *DruidNode `json:"coordinators,omitempty"`
// +optional
Overlords *DruidNode `json:"overlords,omitempty"`

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

Historicals *DruidNode `json:"historicals"`
Historicals *DruidNode `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"`

Expand Down Expand Up @@ -166,6 +163,9 @@ type DruidNode struct {
// +kubebuilder:default={name: "default"}
// +optional
PodPlacementPolicy *core.LocalObjectReference `json:"podPlacementPolicy,omitempty"`

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

type MetadataStorage struct {
Expand Down
32 changes: 32 additions & 0 deletions apis/kubedb/v1alpha2/druid_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,22 @@ func (d *Druid) validateCreateOrUpdate() field.ErrorList {
"number of replicas can not be 0 or less"))
}

if d.Spec.StorageType == StorageTypeEphemeral && d.Spec.Topology.MiddleManagers.Storage != nil {
allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("topology").Child("middleManagers").Child("storage"),
d.Name,
"spec.topology.middleManagers.storage can not be set when storageType is Ephemeral"))
}
if d.Spec.StorageType == StorageTypeDurable && d.Spec.Topology.MiddleManagers.EphemeralStorage != nil {
allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("topology").Child("middleManagers").Child("ephemeralStorage"),
d.Name,
"spec.topology.middleManagers.ephemeralStorage can not be set when storageType is Durable"))
}
if d.Spec.StorageType == StorageTypeDurable && d.Spec.Topology.MiddleManagers.Storage == nil {
allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("topology").Child("middleManagers").Child("storage"),
d.Name,
"spec.topology.middleManagers.storage needs to be set when storageType is Durable"))
}

err := druidValidateVolumes(&d.Spec.Topology.MiddleManagers.PodTemplate, DruidNodeRoleMiddleManagers)
if err != nil {
allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("topology").Child("middleManagers").Child("podTemplate").Child("spec").Child("volumes"),
Expand All @@ -215,6 +231,22 @@ func (d *Druid) validateCreateOrUpdate() field.ErrorList {
"number of replicas can not be 0 or less"))
}

if d.Spec.StorageType == StorageTypeEphemeral && d.Spec.Topology.Historicals.Storage != nil {
allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("topology").Child("historicals").Child("storage"),
d.Name,
"spec.topology.historicals.storage can not be set when storageType is Ephemeral"))
}
if d.Spec.StorageType == StorageTypeDurable && d.Spec.Topology.Historicals.EphemeralStorage != nil {
allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("topology").Child("historicals").Child("ephemeralStorage"),
d.Name,
"spec.topology.historicals.ephemeralStorage can not be set when storageType is Durable"))
}
if d.Spec.StorageType == StorageTypeDurable && d.Spec.Topology.Historicals.Storage == nil {
allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("topology").Child("historicals").Child("storage"),
d.Name,
"spec.topology.historicals.storage needs to be set when storageType is Durable"))
}

err := druidValidateVolumes(&d.Spec.Topology.Historicals.PodTemplate, DruidNodeRoleHistoricals)
if err != nil {
allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("topology").Child("historicals").Child("podTemplate").Child("spec").Child("volumes"),
Expand Down
20 changes: 9 additions & 11 deletions apis/kubedb/v1alpha2/openapi_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion apis/kubedb/v1alpha2/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b5c9e50

Please sign in to comment.