Skip to content

Commit

Permalink
chore: refactor the dependencies between the cluster, component, and …
Browse files Browse the repository at this point in the history
…its objects (#8809)
  • Loading branch information
leon-inf authored Jan 21, 2025
1 parent ee38f3e commit fbd2ea0
Show file tree
Hide file tree
Showing 83 changed files with 1,634 additions and 2,118 deletions.
6 changes: 6 additions & 0 deletions apis/apps/v1/component_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ func init() {

// ComponentSpec defines the desired state of Component
type ComponentSpec struct {
// Specifies the behavior when a Component is deleted.
//
// +kubebuilder:default=Delete
// +optional
TerminationPolicy TerminationPolicyType `json:"terminationPolicy"`

// Specifies the name of the referenced ComponentDefinition.
//
// +kubebuilder:validation:Required
Expand Down
100 changes: 0 additions & 100 deletions apis/apps/v1/deprecated.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@ package v1
import (
"fmt"
"strings"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/apecloud/kubeblocks/pkg/constant"
viper "github.com/apecloud/kubeblocks/pkg/viperx"
)

const (
Expand Down Expand Up @@ -55,58 +49,6 @@ func (r *Cluster) GetComponentByName(componentName string) *ClusterComponentSpec
return nil
}

// GetVolumeClaimNames gets all PVC names of component compName.
//
// r.Spec.GetComponentByName(compName).VolumeClaimTemplates[*].Name will be used if no claimNames provided
//
// nil return if:
// 1. component compName not found or
// 2. len(VolumeClaimTemplates)==0 or
// 3. any claimNames not found
func (r *Cluster) GetVolumeClaimNames(compName string, claimNames ...string) []string {
if r == nil {
return nil
}
comp := r.Spec.GetComponentByName(compName)
if comp == nil {
return nil
}
if len(comp.VolumeClaimTemplates) == 0 {
return nil
}
if len(claimNames) == 0 {
for _, template := range comp.VolumeClaimTemplates {
claimNames = append(claimNames, template.Name)
}
}
allExist := true
for _, name := range claimNames {
found := false
for _, template := range comp.VolumeClaimTemplates {
if template.Name == name {
found = true
break
}
}
if !found {
allExist = false
break
}
}
if !allExist {
return nil
}

pvcNames := make([]string, 0)
for _, claimName := range claimNames {
for i := 0; i < int(comp.Replicas); i++ {
pvcName := fmt.Sprintf("%s-%s-%s-%d", claimName, r.Name, compName, i)
pvcNames = append(pvcNames, pvcName)
}
}
return pvcNames
}

func (r *ClusterSpec) GetComponentByName(componentName string) *ClusterComponentSpec {
for _, v := range r.ComponentSpecs {
if v.Name == componentName {
Expand All @@ -133,53 +75,11 @@ func (r *ClusterStatus) SetComponentStatus(name string, status ClusterComponentS
r.Components[name] = status
}

func (r *ClusterComponentSpec) ToVolumeClaimTemplates() []corev1.PersistentVolumeClaimTemplate {
if r == nil {
return nil
}
var ts []corev1.PersistentVolumeClaimTemplate
for _, t := range r.VolumeClaimTemplates {
ts = append(ts, t.toVolumeClaimTemplate())
}
return ts
}

func (r *ClusterComponentStatus) GetObjectMessage(objectKind, objectName string) string {
messageKey := fmt.Sprintf("%s/%s", objectKind, objectName)
return r.Message[messageKey]
}

func (r *ClusterComponentVolumeClaimTemplate) toVolumeClaimTemplate() corev1.PersistentVolumeClaimTemplate {
return corev1.PersistentVolumeClaimTemplate{
ObjectMeta: metav1.ObjectMeta{
Name: r.Name,
},
Spec: r.Spec.ToV1PersistentVolumeClaimSpec(),
}
}

func (r *PersistentVolumeClaimSpec) ToV1PersistentVolumeClaimSpec() corev1.PersistentVolumeClaimSpec {
return corev1.PersistentVolumeClaimSpec{
AccessModes: r.AccessModes,
Resources: r.Resources,
StorageClassName: r.getStorageClassName(viper.GetString(constant.CfgKeyDefaultStorageClass)),
VolumeMode: r.VolumeMode,
VolumeAttributesClassName: r.VolumeAttributesClassName,
}
}

// getStorageClassName returns PersistentVolumeClaimSpec.StorageClassName if a value is assigned; otherwise,
// it returns the defaultStorageClass argument.
func (r *PersistentVolumeClaimSpec) getStorageClassName(defaultStorageClass string) *string {
if r.StorageClassName != nil && *r.StorageClassName != "" {
return r.StorageClassName
}
if defaultStorageClass != "" {
return &defaultStorageClass
}
return nil
}

func GetClusterUpRunningPhases() []ClusterPhase {
return []ClusterPhase{
RunningClusterPhase,
Expand Down
8 changes: 8 additions & 0 deletions config/crd/bases/apps.kubeblocks.io_components.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5576,6 +5576,14 @@ spec:
- name
type: object
type: array
terminationPolicy:
default: Delete
description: Specifies the behavior when a Component is deleted.
enum:
- DoNotTerminate
- Delete
- WipeOut
type: string
tlsConfig:
description: "Specifies the TLS configuration for the Component, including:\n\n\n-
A boolean flag that indicates whether the Component should use Transport
Expand Down
8 changes: 7 additions & 1 deletion controllers/apps/cluster/cluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package cluster
import (
"context"
"math"
"time"

corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
Expand All @@ -34,6 +35,7 @@ import (

appsv1 "github.com/apecloud/kubeblocks/apis/apps/v1"
dpv1alpha1 "github.com/apecloud/kubeblocks/apis/dataprotection/v1alpha1"
appsutil "github.com/apecloud/kubeblocks/controllers/apps/util"
"github.com/apecloud/kubeblocks/pkg/constant"
"github.com/apecloud/kubeblocks/pkg/controller/multicluster"
intctrlutil "github.com/apecloud/kubeblocks/pkg/controllerutil"
Expand Down Expand Up @@ -108,7 +110,7 @@ func (r *ClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
return intctrlutil.Requeue(reqCtx.Log, err.Error())
}
c := planBuilder.(*clusterPlanBuilder)
sendWarningEventWithError(r.Recorder, c.transCtx.Cluster, corev1.EventTypeWarning, err)
appsutil.SendWarningEventWithError(r.Recorder, c.transCtx.Cluster, corev1.EventTypeWarning, err)
return intctrlutil.RequeueWithError(err, reqCtx.Log, "")
}

Expand Down Expand Up @@ -171,6 +173,10 @@ func (r *ClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct

// SetupWithManager sets up the controller with the Manager.
func (r *ClusterReconciler) SetupWithManager(mgr ctrl.Manager) error {
retryDurationMS := viper.GetInt(constant.CfgKeyCtrlrReconcileRetryDurationMS)
if retryDurationMS != 0 {
appsutil.RequeueDuration = time.Millisecond * time.Duration(retryDurationMS)
}
return intctrlutil.NewControllerManagedBy(mgr).
For(&appsv1.Cluster{}).
WithOptions(controller.Options{
Expand Down
Loading

0 comments on commit fbd2ea0

Please sign in to comment.