Skip to content

Commit

Permalink
Merge branch 'feature/component-definition' of github.com:apecloud/ku…
Browse files Browse the repository at this point in the history
…beblocks into feature/component-definition
  • Loading branch information
Y-Rookie committed Oct 30, 2023
2 parents 5e051f5 + d95295a commit c1c784b
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 137 deletions.
7 changes: 3 additions & 4 deletions controllers/apps/cluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,12 @@ func (r *ClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
&ClusterServiceTransformer{},
// create default cluster connection credential secret object
&ClusterCredentialTransformer{},
// TODO(component): handle restore before ClusterComponentTransformer
// handle restore before ClusterComponentTransformer
&ClusterRestoreTransformer{Client: r.Client},
// create all cluster components objects
&ClusterComponentTransformer{Client: r.Client},
// TODO(component): transform backupPolicyTemplate to backuppolicy.dataprotection.kubeblocks.io
// and backupschedule.dataprotection.kubeblocks.io
&BackupPolicyTplTransformer{},
// build backuppolicy and backupschedule from backupPolicyTemplate
&clusterBackupPolicyTransformer{},
// add our finalizer to all objects
&ClusterOwnershipTransformer{},
// make all workload objects depending on credential secret
Expand Down
33 changes: 15 additions & 18 deletions controllers/apps/component_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,23 @@ package apps

import (
"context"
workloads "github.com/apecloud/kubeblocks/apis/workloads/v1alpha1"
"github.com/apecloud/kubeblocks/pkg/constant"
"time"

corev1 "k8s.io/api/core/v1"
policyv1 "k8s.io/api/policy/v1"
rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"time"

"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

appsv1alpha1 "github.com/apecloud/kubeblocks/apis/apps/v1alpha1"
workloads "github.com/apecloud/kubeblocks/apis/workloads/v1alpha1"
"github.com/apecloud/kubeblocks/pkg/constant"
intctrlutil "github.com/apecloud/kubeblocks/pkg/controllerutil"
viper "github.com/apecloud/kubeblocks/pkg/viperx"
)
Expand Down Expand Up @@ -124,27 +124,24 @@ func (r *ComponentReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
AddTransformer(
// handle component deletion first
&componentDeletionTransformer{},
// handle finalizers and referenced definition labels
&componentMetaTransformer{},
// validate referenced componentDefinition objects existence and availability, and build synthesized component
&componentLoadResourcesTransformer{},
// do spec & definition consistency validation
// validate referenced componentDefinition objects, and build synthesized component
&componentLoadResourcesTransformer{Client: r.Client},
// do validation for the spec & definition consistency
&componentValidationTransformer{},
// handle the component PDB
// handle component PDB
&componentPDBTransformer{},
// handle the component services
// handle component services
&componentServiceTransformer{},
// handle the connection credentials
// handle component connection credentials
&componentCredentialTransformer{},
// handle tls volume and cert
&componentTLSTransformer{},
// render the component configurations
// render component configurations
&componentConfigurationTransformer{Client: r.Client},
// TODO(component): handle restore before component transformer
&componentRestoreTransformer{},
// handle the component workload
&componentWorkloadTransformer{Client: r.Client},
// TODO(component): transform backupPolicyTemplate to backuppolicy.dataprotection.kubeblocks.io and backupschedule.dataprotection.kubeblocks.io
&componentBackupPolicyTransformer{},
// handle RBAC for component workloads
&componentRBACTransformer{},
// add our finalizer to all objects
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ package apps

import (
"fmt"

"golang.org/x/exp/slices"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -38,9 +38,8 @@ import (
dputils "github.com/apecloud/kubeblocks/pkg/dataprotection/utils"
)

// BackupPolicyTplTransformer transforms the backup policy template to the data
// protection backup policy and backup schedule.
type BackupPolicyTplTransformer struct {
// clusterBackupPolicyTransformer transforms the backup policy template to the data protection backup policy and backup schedule.
type clusterBackupPolicyTransformer struct {
*clusterTransformContext

tplCount int
Expand All @@ -52,12 +51,15 @@ type BackupPolicyTplTransformer struct {
compWorkloadType appsv1alpha1.WorkloadType
}

var _ graph.Transformer = &BackupPolicyTplTransformer{}
var _ graph.Transformer = &clusterBackupPolicyTransformer{}

// Transform transforms the backup policy template to the backup policy and
// backup schedule.
func (r *BackupPolicyTplTransformer) Transform(ctx graph.TransformContext, dag *graph.DAG) error {
// Transform transforms the backup policy template to the backup policy and backup schedule.
func (r *clusterBackupPolicyTransformer) Transform(ctx graph.TransformContext, dag *graph.DAG) error {
r.clusterTransformContext = ctx.(*clusterTransformContext)
if model.IsObjectDeleting(r.clusterTransformContext.OrigCluster) {
return nil
}

graphCli, _ := r.clusterTransformContext.Client.(model.GraphClient)

clusterDefName := r.ClusterDef.Name
Expand Down Expand Up @@ -178,7 +180,7 @@ func (r *BackupPolicyTplTransformer) Transform(ctx graph.TransformContext, dag *
}

// transformBackupPolicy transforms backup policy template to backup policy.
func (r *BackupPolicyTplTransformer) transformBackupPolicy() (*dpv1alpha1.BackupPolicy, *model.Action) {
func (r *clusterBackupPolicyTransformer) transformBackupPolicy() (*dpv1alpha1.BackupPolicy, *model.Action) {
cluster := r.OrigCluster
backupPolicyName := generateBackupPolicyName(cluster.Name, r.backupPolicy.ComponentDefRef, r.tplIdentifier)
backupPolicy := &dpv1alpha1.BackupPolicy{}
Expand All @@ -199,7 +201,7 @@ func (r *BackupPolicyTplTransformer) transformBackupPolicy() (*dpv1alpha1.Backup
return backupPolicy, model.ActionUpdatePtr()
}

func (r *BackupPolicyTplTransformer) transformBackupSchedule(
func (r *clusterBackupPolicyTransformer) transformBackupSchedule(
backupPolicy *dpv1alpha1.BackupPolicy) (*dpv1alpha1.BackupSchedule, *model.Action) {
cluster := r.OrigCluster
scheduleName := generateBackupScheduleName(cluster.Name, r.backupPolicy.ComponentDefRef, r.tplIdentifier)
Expand All @@ -220,7 +222,7 @@ func (r *BackupPolicyTplTransformer) transformBackupSchedule(
return backupSchedule, model.ActionUpdatePtr()
}

func (r *BackupPolicyTplTransformer) buildBackupSchedule(
func (r *clusterBackupPolicyTransformer) buildBackupSchedule(
name string,
backupPolicy *dpv1alpha1.BackupPolicy) *dpv1alpha1.BackupSchedule {
cluster := r.OrigCluster
Expand Down Expand Up @@ -249,7 +251,7 @@ func (r *BackupPolicyTplTransformer) buildBackupSchedule(
return backupSchedule
}

func (r *BackupPolicyTplTransformer) syncBackupSchedule(backupSchedule *dpv1alpha1.BackupSchedule) {
func (r *clusterBackupPolicyTransformer) syncBackupSchedule(backupSchedule *dpv1alpha1.BackupSchedule) {
scheduleMethodMap := map[string]struct{}{}
for _, s := range backupSchedule.Spec.Schedules {
scheduleMethodMap[s.BackupMethod] = struct{}{}
Expand All @@ -269,7 +271,7 @@ func (r *BackupPolicyTplTransformer) syncBackupSchedule(backupSchedule *dpv1alph
}

// syncBackupPolicy syncs labels and annotations of the backup policy with the cluster changes.
func (r *BackupPolicyTplTransformer) syncBackupPolicy(backupPolicy *dpv1alpha1.BackupPolicy) {
func (r *clusterBackupPolicyTransformer) syncBackupPolicy(backupPolicy *dpv1alpha1.BackupPolicy) {
// update labels and annotations of the backup policy.
if backupPolicy.Annotations == nil {
backupPolicy.Annotations = map[string]string{}
Expand Down Expand Up @@ -319,7 +321,7 @@ func (r *BackupPolicyTplTransformer) syncBackupPolicy(backupPolicy *dpv1alpha1.B
}
}

func (r *BackupPolicyTplTransformer) getCompReplicas() int32 {
func (r *clusterBackupPolicyTransformer) getCompReplicas() int32 {
rsm := &workloads.ReplicatedStateMachine{}
compSpec := r.getClusterComponentSpec()
rsmName := fmt.Sprintf("%s-%s", r.Cluster.Name, compSpec.Name)
Expand All @@ -330,7 +332,7 @@ func (r *BackupPolicyTplTransformer) getCompReplicas() int32 {
}

// buildBackupPolicy builds a new backup policy by the backup policy template.
func (r *BackupPolicyTplTransformer) buildBackupPolicy(backupPolicyName string) *dpv1alpha1.BackupPolicy {
func (r *clusterBackupPolicyTransformer) buildBackupPolicy(backupPolicyName string) *dpv1alpha1.BackupPolicy {
comp := r.getClusterComponentSpec()
if comp == nil {
return nil
Expand Down Expand Up @@ -358,7 +360,7 @@ func (r *BackupPolicyTplTransformer) buildBackupPolicy(backupPolicyName string)
}

// syncBackupMethods syncs the backupMethod of tpl to backupPolicy.
func (r *BackupPolicyTplTransformer) syncBackupMethods(backupPolicy *dpv1alpha1.BackupPolicy) {
func (r *clusterBackupPolicyTransformer) syncBackupMethods(backupPolicy *dpv1alpha1.BackupPolicy) {
var backupMethods []dpv1alpha1.BackupMethod
for _, v := range r.backupPolicy.BackupMethods {
mappingEnv := r.doEnvMapping(v.EnvMapping)
Expand All @@ -368,7 +370,7 @@ func (r *BackupPolicyTplTransformer) syncBackupMethods(backupPolicy *dpv1alpha1.
backupPolicy.Spec.BackupMethods = backupMethods
}

func (r *BackupPolicyTplTransformer) doEnvMapping(envMapping []appsv1alpha1.EnvMappingVar) []corev1.EnvVar {
func (r *clusterBackupPolicyTransformer) doEnvMapping(envMapping []appsv1alpha1.EnvMappingVar) []corev1.EnvVar {
var env []corev1.EnvVar
for _, v := range envMapping {
for _, cv := range v.ValueFrom.ClusterVersionRef {
Expand All @@ -384,7 +386,7 @@ func (r *BackupPolicyTplTransformer) doEnvMapping(envMapping []appsv1alpha1.EnvM
return env
}

func (r *BackupPolicyTplTransformer) buildBackupTarget(
func (r *clusterBackupPolicyTransformer) buildBackupTarget(
comp *appsv1alpha1.ClusterComponentSpec) *dpv1alpha1.BackupTarget {
targetTpl := r.backupPolicy.Target
clusterName := r.OrigCluster.Name
Expand Down Expand Up @@ -432,7 +434,7 @@ func (r *BackupPolicyTplTransformer) buildBackupTarget(
return target
}

func (r *BackupPolicyTplTransformer) mergeClusterBackup(
func (r *clusterBackupPolicyTransformer) mergeClusterBackup(
backupPolicy *dpv1alpha1.BackupPolicy,
backupSchedule *dpv1alpha1.BackupSchedule) *dpv1alpha1.BackupSchedule {
cluster := r.OrigCluster
Expand Down Expand Up @@ -491,7 +493,7 @@ func (r *BackupPolicyTplTransformer) mergeClusterBackup(
}

// getClusterComponentSpec returns the first component name of the componentDefRef.
func (r *BackupPolicyTplTransformer) getClusterComponentSpec() *appsv1alpha1.ClusterComponentSpec {
func (r *clusterBackupPolicyTransformer) getClusterComponentSpec() *appsv1alpha1.ClusterComponentSpec {
for _, v := range r.clusterTransformContext.ComponentSpecs {
if v.ComponentDefRef == r.backupPolicy.ComponentDefRef {
return v
Expand All @@ -500,14 +502,14 @@ func (r *BackupPolicyTplTransformer) getClusterComponentSpec() *appsv1alpha1.Clu
return nil
}

func (r *BackupPolicyTplTransformer) defaultPolicyAnnotationValue() string {
func (r *clusterBackupPolicyTransformer) defaultPolicyAnnotationValue() string {
if r.tplCount > 1 && r.isDefaultTemplate != trueVal {
return "false"
}
return trueVal
}

func (r *BackupPolicyTplTransformer) buildAnnotations() map[string]string {
func (r *clusterBackupPolicyTransformer) buildAnnotations() map[string]string {
annotations := map[string]string{
dptypes.DefaultBackupPolicyAnnotationKey: r.defaultPolicyAnnotationValue(),
constant.BackupPolicyTemplateAnnotationKey: r.backupPolicyTpl.Name,
Expand All @@ -518,7 +520,7 @@ func (r *BackupPolicyTplTransformer) buildAnnotations() map[string]string {
return annotations
}

func (r *BackupPolicyTplTransformer) buildLabels() map[string]string {
func (r *clusterBackupPolicyTransformer) buildLabels() map[string]string {
return map[string]string{
constant.AppInstanceLabelKey: r.OrigCluster.Name,
constant.KBAppComponentDefRefLabelKey: r.backupPolicy.ComponentDefRef,
Expand All @@ -528,7 +530,7 @@ func (r *BackupPolicyTplTransformer) buildLabels() map[string]string {

// buildTargetPodLabels builds the target labels for the backup policy that will be
// used to select the target pod.
func (r *BackupPolicyTplTransformer) buildTargetPodLabels(comp *appsv1alpha1.ClusterComponentSpec) map[string]string {
func (r *clusterBackupPolicyTransformer) buildTargetPodLabels(comp *appsv1alpha1.ClusterComponentSpec) map[string]string {
labels := map[string]string{
constant.AppInstanceLabelKey: r.OrigCluster.Name,
constant.KBAppComponentLabelKey: comp.Name,
Expand Down
39 changes: 0 additions & 39 deletions controllers/apps/transformer_component_backup_policy.go

This file was deleted.

Loading

0 comments on commit c1c784b

Please sign in to comment.