Skip to content
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
7 changes: 7 additions & 0 deletions apis/parameters/v1alpha1/paramconfigrenderer_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ func init() {
// ParamConfigRendererSpec defines the desired state of ParamConfigRenderer
type ParamConfigRendererSpec struct {
// Specifies the ComponentDefinition custom resource (CR) that defines the Component's characteristics and behavior.
// The value can represent an exact name, a name prefix, or a regular expression pattern.
//
// For example:
//
// - "clickhouse-1.0.0": Matches the exact name "clickhouse-1.0.0"
// - "clickhouse-1": Matches all names starting with "clickhouse-1"
// - "^clickhouse-1\\.\\d+\\.\\d+$": Matches all names starting with "clickhouse-1." followed by version numbers.
//
// +kubebuilder:validation:Required
ComponentDef string `json:"componentDef"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,17 @@ spec:
description: ParamConfigRendererSpec defines the desired state of ParamConfigRenderer
properties:
componentDef:
description: Specifies the ComponentDefinition custom resource (CR)
that defines the Component's characteristics and behavior.
description: |-
Specifies the ComponentDefinition custom resource (CR) that defines the Component's characteristics and behavior.
The value can represent an exact name, a name prefix, or a regular expression pattern.


For example:


- "clickhouse-1.0.0": Matches the exact name "clickhouse-1.0.0"
- "clickhouse-1": Matches all names starting with "clickhouse-1"
- "^clickhouse-1\\.\\d+\\.\\d+$": Matches all names starting with "clickhouse-1." followed by version numbers.
type: string
configs:
description: Specifies the configuration files.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ import (
"encoding/json"
"fmt"
"reflect"
"slices"
"strconv"
"strings"

corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -76,11 +78,14 @@ func newTaskContext(ctx context.Context, cli client.Client, componentParameter *
if err := cli.List(ctx, configDefList); err != nil {
return nil, err
}
slices.SortFunc(configDefList.Items, func(a, b parametersv1alpha1.ParamConfigRenderer) int {
return strings.Compare(b.Spec.ComponentDef, a.Spec.ComponentDef)
})

var paramsDefs []*parametersv1alpha1.ParametersDefinition
var configRender *parametersv1alpha1.ParamConfigRenderer
for i, item := range configDefList.Items {
if item.Spec.ComponentDef != cmpd.Name {
if !component.PrefixOrRegexMatched(cmpd.Name, item.Spec.ComponentDef) {
continue
}
if item.Spec.ServiceVersion == "" || item.Spec.ServiceVersion == cmpd.Spec.ServiceVersion {
Expand Down
37 changes: 35 additions & 2 deletions controllers/parameters/paramconfigrenderer_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ package parameters
import (
"context"
"fmt"
"slices"
"strings"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand All @@ -33,6 +35,7 @@ import (
appsv1 "github.com/apecloud/kubeblocks/apis/apps/v1"
parametersv1alpha1 "github.com/apecloud/kubeblocks/apis/parameters/v1alpha1"
"github.com/apecloud/kubeblocks/pkg/constant"
"github.com/apecloud/kubeblocks/pkg/controller/component"
intctrlutil "github.com/apecloud/kubeblocks/pkg/controllerutil"
"github.com/apecloud/kubeblocks/pkg/generics"
"github.com/apecloud/kubeblocks/pkg/parameters"
Expand Down Expand Up @@ -87,8 +90,8 @@ func (r *ParameterDrivenConfigRenderReconciler) reconcile(reqCtx intctrlutil.Req
if parameters.ParametersDrivenConfigRenderTerminalPhases(parameterTemplate.Status, parameterTemplate.Generation) {
return intctrlutil.Reconciled()
}
cmpd := &appsv1.ComponentDefinition{}
if err := r.Get(reqCtx.Ctx, client.ObjectKey{Name: parameterTemplate.Spec.ComponentDef}, cmpd); err != nil {
cmpd, err := r.resolveComponentDefinition(reqCtx, parameterTemplate)
if err != nil {
return intctrlutil.RequeueWithError(err, reqCtx.Log, "")
}
if err := fillParameterTemplate(reqCtx, r.Client, parameterTemplate, cmpd); err != nil {
Expand All @@ -108,6 +111,36 @@ func (r *ParameterDrivenConfigRenderReconciler) reconcile(reqCtx intctrlutil.Req
return intctrlutil.Reconciled()
}

func (r *ParameterDrivenConfigRenderReconciler) resolveComponentDefinition(reqCtx intctrlutil.RequestCtx, pcr *parametersv1alpha1.ParamConfigRenderer) (*appsv1.ComponentDefinition, error) {
componentDefPattern := pcr.Spec.ComponentDef
if err := component.ValidateDefNameRegexp(componentDefPattern); err != nil {
return nil, fmt.Errorf("invalid componentDef pattern %q: %w", componentDefPattern, err)
}

cmpd := &appsv1.ComponentDefinition{}
if err := r.Get(reqCtx.Ctx, client.ObjectKey{Name: componentDefPattern}, cmpd); err == nil {
return cmpd, nil
}

compDefList := &appsv1.ComponentDefinitionList{}
if err := r.List(reqCtx.Ctx, compDefList); err != nil {
return nil, err
}
slices.SortFunc(compDefList.Items, func(a, b appsv1.ComponentDefinition) int {
return strings.Compare(b.Name, a.Name)
})

for i, item := range compDefList.Items {
if !component.PrefixOrRegexMatched(item.Name, componentDefPattern) {
continue
}
if pcr.Spec.ServiceVersion == "" || pcr.Spec.ServiceVersion == item.Spec.ServiceVersion {
return &compDefList.Items[i], nil
}
}
return nil, fmt.Errorf("no ComponentDefinition found matching pattern %q", componentDefPattern)
}

func (r *ParameterDrivenConfigRenderReconciler) validate(ctx intctrlutil.RequestCtx, cli client.Client, parameterTemplate *parametersv1alpha1.ParamConfigRendererSpec, cmpd *appsv1.ComponentDefinition) error {
if err := validateParametersDefs(ctx, cli, parameterTemplate.ParametersDefs); err != nil {
return err
Expand Down
30 changes: 30 additions & 0 deletions controllers/parameters/paramconfigrenderer_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,5 +124,35 @@ var _ = Describe("ParamConfigRenderer Controller", func() {
g.Expect(pdcr.Status.Phase).Should(BeEquivalentTo(parametersv1alpha1.PDAvailablePhase))
})).ShouldNot(Succeed())
})

It("regex componentDef matching", func() {
initPDCRTest()

pdcr := testparameters.NewParamConfigRendererFactory(pdcrName).
SetParametersDefs(paramsDefName).
SetComponentDefinition("^" + compDefName + ".*").
SetTemplateName(configSpecName).
Create(&testCtx).
GetObject()

Eventually(testapps.CheckObj(&testCtx, client.ObjectKeyFromObject(pdcr), func(g Gomega, pdcr *parametersv1alpha1.ParamConfigRenderer) {
g.Expect(pdcr.Status.Phase).Should(BeEquivalentTo(parametersv1alpha1.PDAvailablePhase))
})).Should(Succeed())
})

It("prefix componentDef matching", func() {
initPDCRTest()

pdcr := testparameters.NewParamConfigRendererFactory(pdcrName).
SetParametersDefs(paramsDefName).
SetComponentDefinition(compDefName).
SetTemplateName(configSpecName).
Create(&testCtx).
GetObject()

Eventually(testapps.CheckObj(&testCtx, client.ObjectKeyFromObject(pdcr), func(g Gomega, pdcr *parametersv1alpha1.ParamConfigRenderer) {
g.Expect(pdcr.Status.Phase).Should(BeEquivalentTo(parametersv1alpha1.PDAvailablePhase))
})).Should(Succeed())
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,17 @@ spec:
description: ParamConfigRendererSpec defines the desired state of ParamConfigRenderer
properties:
componentDef:
description: Specifies the ComponentDefinition custom resource (CR)
that defines the Component's characteristics and behavior.
description: |-
Specifies the ComponentDefinition custom resource (CR) that defines the Component's characteristics and behavior.
The value can represent an exact name, a name prefix, or a regular expression pattern.


For example:


- "clickhouse-1.0.0": Matches the exact name "clickhouse-1.0.0"
- "clickhouse-1": Matches all names starting with "clickhouse-1"
- "^clickhouse-1\\.\\d+\\.\\d+$": Matches all names starting with "clickhouse-1." followed by version numbers.
type: string
configs:
description: Specifies the configuration files.
Expand Down
18 changes: 16 additions & 2 deletions docs/developer_docs/api-reference/parameters.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,14 @@ string
</em>
</td>
<td>
<p>Specifies the ComponentDefinition custom resource (CR) that defines the Component&rsquo;s characteristics and behavior.</p>
<p>Specifies the ComponentDefinition custom resource (CR) that defines the Component&rsquo;s characteristics and behavior.
The value can represent an exact name, a name prefix, or a regular expression pattern.</p>
<p>For example:</p>
<ul>
<li>&ldquo;clickhouse-1.0.0&rdquo;: Matches the exact name &ldquo;clickhouse-1.0.0&rdquo;</li>
<li>&ldquo;clickhouse-1&rdquo;: Matches all names starting with &ldquo;clickhouse-1&rdquo;</li>
<li>&rdquo;^clickhouse-1\.\d+\.\d+$&ldquo;: Matches all names starting with &ldquo;clickhouse-1.&rdquo; followed by version numbers.</li>
</ul>
</td>
</tr>
<tr>
Expand Down Expand Up @@ -1628,7 +1635,14 @@ string
</em>
</td>
<td>
<p>Specifies the ComponentDefinition custom resource (CR) that defines the Component&rsquo;s characteristics and behavior.</p>
<p>Specifies the ComponentDefinition custom resource (CR) that defines the Component&rsquo;s characteristics and behavior.
The value can represent an exact name, a name prefix, or a regular expression pattern.</p>
<p>For example:</p>
<ul>
<li>&ldquo;clickhouse-1.0.0&rdquo;: Matches the exact name &ldquo;clickhouse-1.0.0&rdquo;</li>
<li>&ldquo;clickhouse-1&rdquo;: Matches all names starting with &ldquo;clickhouse-1&rdquo;</li>
<li>&rdquo;^clickhouse-1\.\d+\.\d+$&ldquo;: Matches all names starting with &ldquo;clickhouse-1.&rdquo; followed by version numbers.</li>
</ul>
</td>
</tr>
<tr>
Expand Down
6 changes: 5 additions & 1 deletion pkg/parameters/config_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"reflect"
"slices"
"strconv"
"strings"

"github.com/StudioSol/set"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -291,6 +292,9 @@ func ResolveComponentConfigRender(ctx context.Context, reader client.Reader, cmp
if err := reader.List(ctx, configDefList); err != nil {
return nil, err
}
slices.SortFunc(configDefList.Items, func(a, b parametersv1alpha1.ParamConfigRenderer) int {
return strings.Compare(b.Spec.ComponentDef, a.Spec.ComponentDef)
})

checkAvailable := func(configDef parametersv1alpha1.ParamConfigRenderer) error {
if configDef.Status.Phase != parametersv1alpha1.PDAvailablePhase {
Expand All @@ -300,7 +304,7 @@ func ResolveComponentConfigRender(ctx context.Context, reader client.Reader, cmp
}

for i, item := range configDefList.Items {
if item.Spec.ComponentDef != cmpd.Name {
if !component.PrefixOrRegexMatched(cmpd.Name, item.Spec.ComponentDef) {
continue
}
if item.Spec.ServiceVersion == "" || item.Spec.ServiceVersion == cmpd.Spec.ServiceVersion {
Expand Down
Loading