diff --git a/apis/apps/v1/types.go b/apis/apps/v1/types.go index 8bf470af44e..4f5ce6534f7 100644 --- a/apis/apps/v1/types.go +++ b/apis/apps/v1/types.go @@ -755,6 +755,11 @@ type InstanceTemplate struct { // +optional ServiceVersion string `json:"serviceVersion,omitempty"` + // Specifies the name of the referenced ComponentDefinition. + // + // +kubebuilder:validation:MaxLength=64 + CompDef string `json:"compDef,omitempty"` + // Indicate whether the instances belonging to this template are canary instances. // // +optional diff --git a/config/crd/bases/apps.kubeblocks.io_clusters.yaml b/config/crd/bases/apps.kubeblocks.io_clusters.yaml index 205053e59f5..e3f2817e352 100644 --- a/config/crd/bases/apps.kubeblocks.io_clusters.yaml +++ b/config/crd/bases/apps.kubeblocks.io_clusters.yaml @@ -801,6 +801,10 @@ spec: description: Indicate whether the instances belonging to this template are canary instances. type: boolean + compDef: + description: Specifies the name of the referenced ComponentDefinition. + maxLength: 64 + type: string env: description: |- Defines Env to override. @@ -8489,6 +8493,11 @@ spec: description: Indicate whether the instances belonging to this template are canary instances. type: boolean + compDef: + description: Specifies the name of the referenced + ComponentDefinition. + maxLength: 64 + type: string env: description: |- Defines Env to override. diff --git a/config/crd/bases/apps.kubeblocks.io_components.yaml b/config/crd/bases/apps.kubeblocks.io_components.yaml index 04980b1d4a3..f3eb3260981 100644 --- a/config/crd/bases/apps.kubeblocks.io_components.yaml +++ b/config/crd/bases/apps.kubeblocks.io_components.yaml @@ -668,6 +668,10 @@ spec: description: Indicate whether the instances belonging to this template are canary instances. type: boolean + compDef: + description: Specifies the name of the referenced ComponentDefinition. + maxLength: 64 + type: string env: description: |- Defines Env to override. diff --git a/config/crd/bases/operations.kubeblocks.io_opsrequests.yaml b/config/crd/bases/operations.kubeblocks.io_opsrequests.yaml index 9a7a317fa59..66c3104d2cd 100644 --- a/config/crd/bases/operations.kubeblocks.io_opsrequests.yaml +++ b/config/crd/bases/operations.kubeblocks.io_opsrequests.yaml @@ -659,6 +659,11 @@ spec: description: Indicate whether the instances belonging to this template are canary instances. type: boolean + compDef: + description: Specifies the name of the referenced + ComponentDefinition. + maxLength: 64 + type: string env: description: |- Defines Env to override. @@ -3487,6 +3492,11 @@ spec: description: Indicate whether the instances belonging to this template are canary instances. type: boolean + compDef: + description: Specifies the name of the referenced + ComponentDefinition. + maxLength: 64 + type: string env: description: |- Defines Env to override. diff --git a/controllers/apps/cluster/transformer_cluster_normalization.go b/controllers/apps/cluster/transformer_cluster_normalization.go index 45752e36cde..7c90d1b39c0 100644 --- a/controllers/apps/cluster/transformer_cluster_normalization.go +++ b/controllers/apps/cluster/transformer_cluster_normalization.go @@ -345,7 +345,7 @@ func (t *clusterNormalizationTransformer) resolveDefinitions4Component(transCtx compSpec.ServiceVersion = serviceVersion for i, tpl := range compSpec.Instances { - if len(tpl.ServiceVersion) == 0 { + if len(tpl.ServiceVersion) == 0 && len(tpl.CompDef) == 0 { continue } compDef, serviceVersion, err = t.resolveCompDefinitionNServiceVersionWithTemplate(transCtx, compSpec, comp, &tpl) @@ -353,7 +353,8 @@ func (t *clusterNormalizationTransformer) resolveDefinitions4Component(transCtx return nil, err } compDefs = append(compDefs, compDef) - // set the serviceVersion as resolved + // set the componentDef and serviceVersion as resolved + compSpec.Instances[i].CompDef = compDef.Name compSpec.Instances[i].ServiceVersion = serviceVersion } return compDefs, nil @@ -386,19 +387,27 @@ func (t *clusterNormalizationTransformer) resolveCompDefinitionNServiceVersionWi } } } - if comp == nil || runningTpl == nil || t.checkTemplateUpgrade(compSpec, comp, protoTpl, runningTpl) { - return resolveCompDefinitionNServiceVersion(ctx, cli, compSpec.ComponentDef, protoTpl.ServiceVersion) + + serviceVersion := compSpec.ServiceVersion + if len(protoTpl.ServiceVersion) > 0 { + serviceVersion = protoTpl.ServiceVersion + } + compDefName := compSpec.ComponentDef + if len(protoTpl.CompDef) > 0 { + compDefName = protoTpl.CompDef + } + if comp == nil || runningTpl == nil || t.checkTemplateUpgrade(serviceVersion, compDefName, runningTpl) { + return resolveCompDefinitionNServiceVersion(ctx, cli, compDefName, serviceVersion) } - return resolveCompDefinitionNServiceVersion(ctx, cli, comp.Spec.CompDef, runningTpl.ServiceVersion) + return resolveCompDefinitionNServiceVersion(ctx, cli, runningTpl.CompDef, runningTpl.ServiceVersion) } func (t *clusterNormalizationTransformer) checkCompUpgrade(compSpec *appsv1.ClusterComponentSpec, comp *appsv1.Component) bool { return compSpec.ServiceVersion != comp.Spec.ServiceVersion || compSpec.ComponentDef != comp.Spec.CompDef } -func (t *clusterNormalizationTransformer) checkTemplateUpgrade(compSpec *appsv1.ClusterComponentSpec, - comp *appsv1.Component, protoTpl, runningTpl *appsv1.InstanceTemplate) bool { - return protoTpl.ServiceVersion != runningTpl.ServiceVersion || compSpec.ComponentDef != comp.Spec.CompDef +func (t *clusterNormalizationTransformer) checkTemplateUpgrade(serviceVersion, compDefName string, runningTpl *appsv1.InstanceTemplate) bool { + return serviceVersion != runningTpl.ServiceVersion || compDefName != runningTpl.CompDef } func (t *clusterNormalizationTransformer) buildShardingComps(transCtx *clusterTransformContext) (map[string][]*appsv1.ClusterComponentSpec, error) { diff --git a/controllers/apps/component/transformer_component_load_resources.go b/controllers/apps/component/transformer_component_load_resources.go index ddbe9449590..9258b2914ef 100644 --- a/controllers/apps/component/transformer_component_load_resources.go +++ b/controllers/apps/component/transformer_component_load_resources.go @@ -62,7 +62,6 @@ func (t *componentLoadResourcesTransformer) transformForNativeComponent(transCtx if err != nil { return intctrlutil.NewRequeueError(appsutil.RequeueDuration, err.Error()) } - compDefCopy := compDef.DeepCopy() if err = component.UpdateCompDefinitionImages4ServiceVersion(ctx, cli, compDef, comp.Spec.ServiceVersion); err != nil { return intctrlutil.NewRequeueError(appsutil.RequeueDuration, err.Error()) @@ -76,7 +75,12 @@ func (t *componentLoadResourcesTransformer) transformForNativeComponent(transCtx } for _, tpl := range comp.Spec.Instances { if len(tpl.ServiceVersion) > 0 { - images, err := component.ResolveInstanceTemplateImages4ServiceVersion(ctx, cli, compDefCopy, tpl.ServiceVersion) + // TODO: comp defs? + compDef, err = getNCheckCompDefinition(ctx, cli, tpl.CompDef) + if err != nil { + return intctrlutil.NewRequeueError(appsutil.RequeueDuration, err.Error()) + } + images, err := component.ResolveInstanceTemplateImages4ServiceVersion(ctx, cli, compDef, tpl.ServiceVersion) if err != nil { return intctrlutil.NewRequeueError(appsutil.RequeueDuration, err.Error()) } diff --git a/deploy/helm/crds/apps.kubeblocks.io_clusters.yaml b/deploy/helm/crds/apps.kubeblocks.io_clusters.yaml index 205053e59f5..e3f2817e352 100644 --- a/deploy/helm/crds/apps.kubeblocks.io_clusters.yaml +++ b/deploy/helm/crds/apps.kubeblocks.io_clusters.yaml @@ -801,6 +801,10 @@ spec: description: Indicate whether the instances belonging to this template are canary instances. type: boolean + compDef: + description: Specifies the name of the referenced ComponentDefinition. + maxLength: 64 + type: string env: description: |- Defines Env to override. @@ -8489,6 +8493,11 @@ spec: description: Indicate whether the instances belonging to this template are canary instances. type: boolean + compDef: + description: Specifies the name of the referenced + ComponentDefinition. + maxLength: 64 + type: string env: description: |- Defines Env to override. diff --git a/deploy/helm/crds/apps.kubeblocks.io_components.yaml b/deploy/helm/crds/apps.kubeblocks.io_components.yaml index 04980b1d4a3..f3eb3260981 100644 --- a/deploy/helm/crds/apps.kubeblocks.io_components.yaml +++ b/deploy/helm/crds/apps.kubeblocks.io_components.yaml @@ -668,6 +668,10 @@ spec: description: Indicate whether the instances belonging to this template are canary instances. type: boolean + compDef: + description: Specifies the name of the referenced ComponentDefinition. + maxLength: 64 + type: string env: description: |- Defines Env to override. diff --git a/deploy/helm/crds/operations.kubeblocks.io_opsrequests.yaml b/deploy/helm/crds/operations.kubeblocks.io_opsrequests.yaml index 9a7a317fa59..66c3104d2cd 100755 --- a/deploy/helm/crds/operations.kubeblocks.io_opsrequests.yaml +++ b/deploy/helm/crds/operations.kubeblocks.io_opsrequests.yaml @@ -659,6 +659,11 @@ spec: description: Indicate whether the instances belonging to this template are canary instances. type: boolean + compDef: + description: Specifies the name of the referenced + ComponentDefinition. + maxLength: 64 + type: string env: description: |- Defines Env to override. @@ -3487,6 +3492,11 @@ spec: description: Indicate whether the instances belonging to this template are canary instances. type: boolean + compDef: + description: Specifies the name of the referenced + ComponentDefinition. + maxLength: 64 + type: string env: description: |- Defines Env to override. diff --git a/docs/developer_docs/api-reference/cluster.md b/docs/developer_docs/api-reference/cluster.md index c544192ec2e..5c4a4e7f0de 100644 --- a/docs/developer_docs/api-reference/cluster.md +++ b/docs/developer_docs/api-reference/cluster.md @@ -7929,6 +7929,17 @@ The version should follow the syntax and semantics of the “Semantic Versio +compDef
+ +string + + + +

Specifies the name of the referenced ComponentDefinition.

+ + + + canary
bool diff --git a/pkg/controller/component/component_version.go b/pkg/controller/component/component_version.go index 26210edbc41..425bdfd0b18 100644 --- a/pkg/controller/component/component_version.go +++ b/pkg/controller/component/component_version.go @@ -220,8 +220,12 @@ func resolveImagesWithCompVersions4Template(compDef *appsv1.ComponentDefinition, if app.err != nil { return app.err } - images[kbagent.ContainerName] = app.image - images[kbagent.ContainerName4Worker] = app.image + if image, ok := images[kbagent.ContainerName]; !ok || len(image) == 0 { + images[kbagent.ContainerName] = app.image + } + if image, ok := images[kbagent.ContainerName4Worker]; !ok || len(image) == 0 { + images[kbagent.ContainerName4Worker] = app.image + } } } }