Skip to content

Commit

Permalink
rebase main
Browse files Browse the repository at this point in the history
  • Loading branch information
sophon-zt committed Nov 21, 2024
1 parent eaa4ea0 commit 949fa18
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 66 deletions.
24 changes: 1 addition & 23 deletions controllers/apps/componentdefinition_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func (r *ComponentDefinitionReconciler) validate(cli client.Client, rctx intctrl
r.validateVolumes,
r.validateHostNetwork,
r.validateServices,
r.validateComponentTemplate,
validateComponentTemplate,
r.validateSystemAccounts,
r.validateReplicasLimit,
r.validateAvailable,
Expand Down Expand Up @@ -324,28 +324,6 @@ func (r *ComponentDefinitionReconciler) validateServices(cli client.Client, rctx
return nil
}

func (r *ComponentDefinitionReconciler) validateComponentTemplate(cli client.Client, rctx intctrlutil.RequestCtx,
compd *appsv1.ComponentDefinition) error {
validateObject := func(objectKey client.ObjectKey) error {
configObj := &corev1.ConfigMap{}
return cli.Get(rctx.Ctx, objectKey, configObj)
}
validateTemplate := func(tpl appsv1.ComponentTemplateSpec) error {
if tpl.TemplateRef != "" {
return validateObject(client.ObjectKey{Namespace: tpl.Namespace, Name: tpl.TemplateRef})
}
return nil
}
for _, tpls := range [][]appsv1.ComponentTemplateSpec{compd.Spec.Configs, compd.Spec.Scripts} {
for _, tpl := range tpls {
if err := validateTemplate(tpl); err != nil {
return err
}
}
}
return nil
}

func (r *ComponentDefinitionReconciler) validateSystemAccounts(cli client.Client, rctx intctrlutil.RequestCtx,
cmpd *appsv1.ComponentDefinition) error {
for _, v := range cmpd.Spec.SystemAccounts {
Expand Down
36 changes: 6 additions & 30 deletions controllers/apps/sidecardefinition_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"

appsv1 "github.com/apecloud/kubeblocks/apis/apps/v1"
appsconfig "github.com/apecloud/kubeblocks/controllers/apps/configuration"
"github.com/apecloud/kubeblocks/pkg/constant"
"github.com/apecloud/kubeblocks/pkg/controller/component"
intctrlutil "github.com/apecloud/kubeblocks/pkg/controllerutil"
Expand All @@ -53,9 +52,9 @@ type SidecarDefinitionReconciler struct {
Recorder record.EventRecorder
}

//+kubebuilder:rbac:groups=apps.kubeblocks.io,resources=sidecardefinitions,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=apps.kubeblocks.io,resources=sidecardefinitions/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=apps.kubeblocks.io,resources=sidecardefinitions/finalizers,verbs=update
// +kubebuilder:rbac:groups=apps.kubeblocks.io,resources=sidecardefinitions,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=apps.kubeblocks.io,resources=sidecardefinitions/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=apps.kubeblocks.io,resources=sidecardefinitions/finalizers,verbs=update

// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
Expand Down Expand Up @@ -228,25 +227,13 @@ func (r *SidecarDefinitionReconciler) validateConfigNScript(cli client.Client, r
return fmt.Errorf("duplicate names of configs/scripts are not allowed")
}

configs := func() []appsv1.ComponentConfigSpec {
if len(sidecarDef.Spec.Configs) == 0 {
return nil
}
configs := make([]appsv1.ComponentConfigSpec, 0)
for i := range sidecarDef.Spec.Configs {
configs = append(configs, appsv1.ComponentConfigSpec{
ComponentTemplateSpec: sidecarDef.Spec.Configs[i],
})
}
return configs
}
compDef := &appsv1.ComponentDefinition{
Spec: appsv1.ComponentDefinitionSpec{
Configs: configs(),
Configs: sidecarDef.Spec.Configs,
Scripts: sidecarDef.Spec.Scripts,
},
}
return appsconfig.ReconcileConfigSpecsForReferencedCR(cli, rctx, compDef)
return validateComponentTemplate(cli, rctx, compDef)
}

func (r *SidecarDefinitionReconciler) validateNResolveOwner(_ client.Client, _ intctrlutil.RequestCtx,
Expand Down Expand Up @@ -339,18 +326,7 @@ func (r *SidecarDefinitionReconciler) validateMatchedCompDef(sidecarDef *appsv1.
return nil
}

templateOfConfig := func(configs []appsv1.ComponentConfigSpec) []appsv1.ComponentTemplateSpec {
if len(configs) == 0 {
return nil
}
l := make([]appsv1.ComponentTemplateSpec, 0)
for i := range configs {
l = append(l, configs[i].ComponentTemplateSpec)
}
return l
}

if err := validate("config", sidecarDef.Spec.Configs, templateOfConfig(compDef.Spec.Configs)); err != nil {
if err := validate("config", sidecarDef.Spec.Configs, compDef.Spec.Configs); err != nil {
return err
}
return validate("script", sidecarDef.Spec.Scripts, compDef.Spec.Scripts)
Expand Down
23 changes: 23 additions & 0 deletions controllers/apps/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ import (
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

appsv1 "github.com/apecloud/kubeblocks/apis/apps/v1"
"github.com/apecloud/kubeblocks/pkg/constant"
"github.com/apecloud/kubeblocks/pkg/controller/model"
"github.com/apecloud/kubeblocks/pkg/controller/multicluster"
intctrlutil "github.com/apecloud/kubeblocks/pkg/controllerutil"
)

// default reconcile requeue after duration
Expand Down Expand Up @@ -149,3 +151,24 @@ func shouldAllocateNodePorts(svc *corev1.ServiceSpec) bool {
}
return false
}

func validateComponentTemplate(cli client.Client, rctx intctrlutil.RequestCtx, compd *appsv1.ComponentDefinition) error {
validateObject := func(objectKey client.ObjectKey) error {
configObj := &corev1.ConfigMap{}
return cli.Get(rctx.Ctx, objectKey, configObj)
}
validateTemplate := func(tpl appsv1.ComponentTemplateSpec) error {
if tpl.TemplateRef != "" {
return validateObject(client.ObjectKey{Namespace: tpl.Namespace, Name: tpl.TemplateRef})
}
return nil
}
for _, tpls := range [][]appsv1.ComponentTemplateSpec{compd.Spec.Configs, compd.Spec.Scripts} {
for _, tpl := range tpls {
if err := validateTemplate(tpl); err != nil {
return err
}
}
}
return nil
}
4 changes: 2 additions & 2 deletions pkg/controller/component/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ func BuildComponent(cluster *appsv1.Cluster, compSpec *appsv1.ClusterComponentSp
SetRuntimeClassName(cluster.Spec.RuntimeClassName).
SetSystemAccounts(compSpec.SystemAccounts).
SetStop(compSpec.Stop).
SetSidecars(nil)
SetInitParameters(compSpec.InitParameters).
SetSidecars(nil).
SetInitParameters(compSpec.InitParameters)
return compBuilder.GetObject(), nil
}

Expand Down
12 changes: 1 addition & 11 deletions pkg/controller/component/sidecar.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,7 @@ func buildSidecarVars(sidecarDef *appsv1.SidecarDefinition, synthesizedComp *Syn

func buildSidecarConfigs(sidecarDef *appsv1.SidecarDefinition, synthesizedComp *SynthesizedComponent) error {
if sidecarDef.Spec.Configs != nil {
templateToConfig := func() []appsv1.ComponentConfigSpec {
if len(sidecarDef.Spec.Configs) == 0 {
return nil
}
l := make([]appsv1.ComponentConfigSpec, 0)
for i := range sidecarDef.Spec.Configs {
l = append(l, appsv1.ComponentConfigSpec{ComponentTemplateSpec: sidecarDef.Spec.Configs[i]})
}
return l
}
synthesizedComp.ConfigTemplates = append(synthesizedComp.ConfigTemplates, templateToConfig()...)
synthesizedComp.ConfigTemplates = append(synthesizedComp.ConfigTemplates, sidecarDef.Spec.Configs...)
}
return nil
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/controller/configuration/template_render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ var _ = Describe("ToolsImageBuilderTest", func() {
}
pds := []*parametersv1alpha1.ParametersDefinition{paramsDef}
cmObj, err := RerenderParametersTemplate(rctx, item, pdcr, pds)
Expect(err).Should(Succeed())
configdesc := pdcr.Spec.Configs[0]
if len(parameters) == 0 {
configReaders, err := cfgcore.LoadRawConfigObject(cmObj.Data, configdesc.FileFormatConfig, []string{configdesc.Name})
Expand All @@ -112,6 +113,7 @@ var _ = Describe("ToolsImageBuilderTest", func() {
result, err := ApplyParameters(item, cmObj, pdcr, pds)
Expect(err).Should(Succeed())
configReaders, err := cfgcore.LoadRawConfigObject(result.Data, configdesc.FileFormatConfig, []string{configdesc.Name})
Expect(err).Should(Succeed())
return configReaders[configdesc.Name]
}

Expand Down

0 comments on commit 949fa18

Please sign in to comment.