Skip to content

Commit d5a55ee

Browse files
authored
chore: instance template supports specifying the comp-def (#9469)
1 parent fcec232 commit d5a55ee

11 files changed

Lines changed: 91 additions & 12 deletions

apis/apps/v1/types.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,11 @@ type InstanceTemplate struct {
755755
// +optional
756756
ServiceVersion string `json:"serviceVersion,omitempty"`
757757

758+
// Specifies the name of the referenced ComponentDefinition.
759+
//
760+
// +kubebuilder:validation:MaxLength=64
761+
CompDef string `json:"compDef,omitempty"`
762+
758763
// Indicate whether the instances belonging to this template are canary instances.
759764
//
760765
// +optional

config/crd/bases/apps.kubeblocks.io_clusters.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,10 @@ spec:
801801
description: Indicate whether the instances belonging
802802
to this template are canary instances.
803803
type: boolean
804+
compDef:
805+
description: Specifies the name of the referenced ComponentDefinition.
806+
maxLength: 64
807+
type: string
804808
env:
805809
description: |-
806810
Defines Env to override.
@@ -8489,6 +8493,11 @@ spec:
84898493
description: Indicate whether the instances belonging
84908494
to this template are canary instances.
84918495
type: boolean
8496+
compDef:
8497+
description: Specifies the name of the referenced
8498+
ComponentDefinition.
8499+
maxLength: 64
8500+
type: string
84928501
env:
84938502
description: |-
84948503
Defines Env to override.

config/crd/bases/apps.kubeblocks.io_components.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,10 @@ spec:
668668
description: Indicate whether the instances belonging to this
669669
template are canary instances.
670670
type: boolean
671+
compDef:
672+
description: Specifies the name of the referenced ComponentDefinition.
673+
maxLength: 64
674+
type: string
671675
env:
672676
description: |-
673677
Defines Env to override.

config/crd/bases/operations.kubeblocks.io_opsrequests.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,11 @@ spec:
659659
description: Indicate whether the instances belonging
660660
to this template are canary instances.
661661
type: boolean
662+
compDef:
663+
description: Specifies the name of the referenced
664+
ComponentDefinition.
665+
maxLength: 64
666+
type: string
662667
env:
663668
description: |-
664669
Defines Env to override.
@@ -3487,6 +3492,11 @@ spec:
34873492
description: Indicate whether the instances belonging
34883493
to this template are canary instances.
34893494
type: boolean
3495+
compDef:
3496+
description: Specifies the name of the referenced
3497+
ComponentDefinition.
3498+
maxLength: 64
3499+
type: string
34903500
env:
34913501
description: |-
34923502
Defines Env to override.

controllers/apps/cluster/transformer_cluster_normalization.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -345,15 +345,16 @@ func (t *clusterNormalizationTransformer) resolveDefinitions4Component(transCtx
345345
compSpec.ServiceVersion = serviceVersion
346346

347347
for i, tpl := range compSpec.Instances {
348-
if len(tpl.ServiceVersion) == 0 {
348+
if len(tpl.ServiceVersion) == 0 && len(tpl.CompDef) == 0 {
349349
continue
350350
}
351351
compDef, serviceVersion, err = t.resolveCompDefinitionNServiceVersionWithTemplate(transCtx, compSpec, comp, &tpl)
352352
if err != nil {
353353
return nil, err
354354
}
355355
compDefs = append(compDefs, compDef)
356-
// set the serviceVersion as resolved
356+
// set the componentDef and serviceVersion as resolved
357+
compSpec.Instances[i].CompDef = compDef.Name
357358
compSpec.Instances[i].ServiceVersion = serviceVersion
358359
}
359360
return compDefs, nil
@@ -386,19 +387,27 @@ func (t *clusterNormalizationTransformer) resolveCompDefinitionNServiceVersionWi
386387
}
387388
}
388389
}
389-
if comp == nil || runningTpl == nil || t.checkTemplateUpgrade(compSpec, comp, protoTpl, runningTpl) {
390-
return resolveCompDefinitionNServiceVersion(ctx, cli, compSpec.ComponentDef, protoTpl.ServiceVersion)
390+
391+
serviceVersion := compSpec.ServiceVersion
392+
if len(protoTpl.ServiceVersion) > 0 {
393+
serviceVersion = protoTpl.ServiceVersion
394+
}
395+
compDefName := compSpec.ComponentDef
396+
if len(protoTpl.CompDef) > 0 {
397+
compDefName = protoTpl.CompDef
398+
}
399+
if comp == nil || runningTpl == nil || t.checkTemplateUpgrade(serviceVersion, compDefName, runningTpl) {
400+
return resolveCompDefinitionNServiceVersion(ctx, cli, compDefName, serviceVersion)
391401
}
392-
return resolveCompDefinitionNServiceVersion(ctx, cli, comp.Spec.CompDef, runningTpl.ServiceVersion)
402+
return resolveCompDefinitionNServiceVersion(ctx, cli, runningTpl.CompDef, runningTpl.ServiceVersion)
393403
}
394404

395405
func (t *clusterNormalizationTransformer) checkCompUpgrade(compSpec *appsv1.ClusterComponentSpec, comp *appsv1.Component) bool {
396406
return compSpec.ServiceVersion != comp.Spec.ServiceVersion || compSpec.ComponentDef != comp.Spec.CompDef
397407
}
398408

399-
func (t *clusterNormalizationTransformer) checkTemplateUpgrade(compSpec *appsv1.ClusterComponentSpec,
400-
comp *appsv1.Component, protoTpl, runningTpl *appsv1.InstanceTemplate) bool {
401-
return protoTpl.ServiceVersion != runningTpl.ServiceVersion || compSpec.ComponentDef != comp.Spec.CompDef
409+
func (t *clusterNormalizationTransformer) checkTemplateUpgrade(serviceVersion, compDefName string, runningTpl *appsv1.InstanceTemplate) bool {
410+
return serviceVersion != runningTpl.ServiceVersion || compDefName != runningTpl.CompDef
402411
}
403412

404413
func (t *clusterNormalizationTransformer) buildShardingComps(transCtx *clusterTransformContext) (map[string][]*appsv1.ClusterComponentSpec, error) {

controllers/apps/component/transformer_component_load_resources.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ func (t *componentLoadResourcesTransformer) transformForNativeComponent(transCtx
6262
if err != nil {
6363
return intctrlutil.NewRequeueError(appsutil.RequeueDuration, err.Error())
6464
}
65-
compDefCopy := compDef.DeepCopy()
6665

6766
if err = component.UpdateCompDefinitionImages4ServiceVersion(ctx, cli, compDef, comp.Spec.ServiceVersion); err != nil {
6867
return intctrlutil.NewRequeueError(appsutil.RequeueDuration, err.Error())
@@ -76,7 +75,12 @@ func (t *componentLoadResourcesTransformer) transformForNativeComponent(transCtx
7675
}
7776
for _, tpl := range comp.Spec.Instances {
7877
if len(tpl.ServiceVersion) > 0 {
79-
images, err := component.ResolveInstanceTemplateImages4ServiceVersion(ctx, cli, compDefCopy, tpl.ServiceVersion)
78+
// TODO: comp defs?
79+
compDef, err = getNCheckCompDefinition(ctx, cli, tpl.CompDef)
80+
if err != nil {
81+
return intctrlutil.NewRequeueError(appsutil.RequeueDuration, err.Error())
82+
}
83+
images, err := component.ResolveInstanceTemplateImages4ServiceVersion(ctx, cli, compDef, tpl.ServiceVersion)
8084
if err != nil {
8185
return intctrlutil.NewRequeueError(appsutil.RequeueDuration, err.Error())
8286
}

deploy/helm/crds/apps.kubeblocks.io_clusters.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,10 @@ spec:
801801
description: Indicate whether the instances belonging
802802
to this template are canary instances.
803803
type: boolean
804+
compDef:
805+
description: Specifies the name of the referenced ComponentDefinition.
806+
maxLength: 64
807+
type: string
804808
env:
805809
description: |-
806810
Defines Env to override.
@@ -8489,6 +8493,11 @@ spec:
84898493
description: Indicate whether the instances belonging
84908494
to this template are canary instances.
84918495
type: boolean
8496+
compDef:
8497+
description: Specifies the name of the referenced
8498+
ComponentDefinition.
8499+
maxLength: 64
8500+
type: string
84928501
env:
84938502
description: |-
84948503
Defines Env to override.

deploy/helm/crds/apps.kubeblocks.io_components.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,10 @@ spec:
668668
description: Indicate whether the instances belonging to this
669669
template are canary instances.
670670
type: boolean
671+
compDef:
672+
description: Specifies the name of the referenced ComponentDefinition.
673+
maxLength: 64
674+
type: string
671675
env:
672676
description: |-
673677
Defines Env to override.

deploy/helm/crds/operations.kubeblocks.io_opsrequests.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,11 @@ spec:
659659
description: Indicate whether the instances belonging
660660
to this template are canary instances.
661661
type: boolean
662+
compDef:
663+
description: Specifies the name of the referenced
664+
ComponentDefinition.
665+
maxLength: 64
666+
type: string
662667
env:
663668
description: |-
664669
Defines Env to override.
@@ -3487,6 +3492,11 @@ spec:
34873492
description: Indicate whether the instances belonging
34883493
to this template are canary instances.
34893494
type: boolean
3495+
compDef:
3496+
description: Specifies the name of the referenced
3497+
ComponentDefinition.
3498+
maxLength: 64
3499+
type: string
34903500
env:
34913501
description: |-
34923502
Defines Env to override.

docs/developer_docs/api-reference/cluster.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7929,6 +7929,17 @@ The version should follow the syntax and semantics of the “Semantic Versio
79297929
</tr>
79307930
<tr>
79317931
<td>
7932+
<code>compDef</code><br/>
7933+
<em>
7934+
string
7935+
</em>
7936+
</td>
7937+
<td>
7938+
<p>Specifies the name of the referenced ComponentDefinition.</p>
7939+
</td>
7940+
</tr>
7941+
<tr>
7942+
<td>
79327943
<code>canary</code><br/>
79337944
<em>
79347945
bool

0 commit comments

Comments
 (0)