Skip to content

Commit 1dc89b3

Browse files
authored
chore: add restart to ITS config API (#10067)
1 parent 72097f8 commit 1dc89b3

File tree

19 files changed

+244
-148
lines changed

19 files changed

+244
-148
lines changed

apis/apps/v1/deprecated.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,9 @@ func (r *Cluster) IsStatusUpdating() bool {
4040
return !r.IsDeleting() && !r.IsUpdating()
4141
}
4242

43-
func (r *Cluster) GetComponentByName(componentName string) *ClusterComponentSpec {
44-
for _, v := range r.Spec.ComponentSpecs {
45-
if v.Name == componentName {
46-
return &v
47-
}
48-
}
49-
return nil
50-
}
51-
52-
func (r *ClusterSpec) GetComponentByName(componentName string) *ClusterComponentSpec {
43+
func (r *ClusterSpec) GetComponentByName(compName string) *ClusterComponentSpec {
5344
for i, v := range r.ComponentSpecs {
54-
if v.Name == componentName {
45+
if v.Name == compName {
5546
return &r.ComponentSpecs[i]
5647
}
5748
}

apis/workloads/v1/instanceset_types.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -534,15 +534,15 @@ type ConfigTemplate struct {
534534
// The name of the config.
535535
Name string `json:"name"`
536536

537-
// The generation of the config content.
537+
// Represents a checksum or hash of the config content.
538538
//
539539
// +optional
540-
Generation int64 `json:"generation,omitempty"`
540+
ConfigHash *string `json:"configHash,omitempty"`
541541

542-
// Represents a checksum or hash of the config content.
542+
// Specifies whether to restart instances.
543543
//
544544
// +optional
545-
ConfigHash *string `json:"configHash,omitempty"`
545+
Restart *bool `json:"restart,omitempty"`
546546

547547
// The custom reconfigure action.
548548
//
@@ -591,11 +591,6 @@ type InstanceConfigStatus struct {
591591
// +kubebuilder:validation:Required
592592
Name string `json:"name"`
593593

594-
// The generation of the config.
595-
//
596-
// +optional
597-
Generation int64 `json:"generation,omitempty"`
598-
599594
// Represents a checksum or hash of the config content.
600595
//
601596
// +optional

apis/workloads/v1/zz_generated.deepcopy.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/workloads.kubeblocks.io_instancesets.yaml

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,6 @@ spec:
7272
configHash:
7373
description: Represents a checksum or hash of the config content.
7474
type: string
75-
generation:
76-
description: The generation of the config content.
77-
format: int64
78-
type: integer
7975
name:
8076
description: The name of the config.
8177
type: string
@@ -525,6 +521,9 @@ spec:
525521

526522
An empty name indicates that the reconfigure action is the default one defined by lifecycle actions.
527523
type: string
524+
restart:
525+
description: Specifies whether to restart instances.
526+
type: boolean
528527
required:
529528
- name
530529
type: object
@@ -11783,10 +11782,6 @@ spec:
1178311782
description: Represents a checksum or hash of the config
1178411783
content.
1178511784
type: string
11786-
generation:
11787-
description: The generation of the config.
11788-
format: int64
11789-
type: integer
1179011785
name:
1179111786
description: The name of the config.
1179211787
type: string

controllers/apps/component/component_controller_test.go

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,6 +1522,7 @@ var _ = Describe("Component Controller", func() {
15221522
Variables: map[string]string{
15231523
"LOG_LEVEL": "debug",
15241524
},
1525+
ConfigHash: ptr.To("123456"),
15251526
},
15261527
}
15271528
})()).Should(Succeed())
@@ -1536,7 +1537,8 @@ var _ = Describe("Component Controller", func() {
15361537
Eventually(testapps.CheckObj(&testCtx, itsKey, func(g Gomega, its *workloads.InstanceSet) {
15371538
g.Expect(its.Spec.Configs).Should(HaveLen(1))
15381539
g.Expect(its.Spec.Configs[0].Name).Should(Equal(fileTemplate))
1539-
g.Expect(its.Spec.Configs[0].Generation).Should(Equal(its.Generation))
1540+
g.Expect(its.Spec.Configs[0].ConfigHash).ShouldNot(BeNil())
1541+
g.Expect(*its.Spec.Configs[0].ConfigHash).Should(Equal("123456"))
15401542
g.Expect(its.Spec.Configs[0].Reconfigure).ShouldNot(BeNil())
15411543
g.Expect(its.Spec.Configs[0].ReconfigureActionName).Should(BeEmpty())
15421544
g.Expect(its.Spec.Configs[0].Parameters).Should(HaveKey("KB_CONFIG_FILES_UPDATED"))
@@ -1571,6 +1573,7 @@ var _ = Describe("Component Controller", func() {
15711573
comp.Spec.Configs[0].Variables = map[string]string{
15721574
"LOG_LEVEL": "warn",
15731575
}
1576+
comp.Spec.Configs[0].ConfigHash = ptr.To("123456")
15741577
})()).Should(Succeed())
15751578

15761579
By("check the file template object again")
@@ -1583,7 +1586,8 @@ var _ = Describe("Component Controller", func() {
15831586
Eventually(testapps.CheckObj(&testCtx, itsKey, func(g Gomega, its *workloads.InstanceSet) {
15841587
g.Expect(its.Spec.Configs).Should(HaveLen(1))
15851588
g.Expect(its.Spec.Configs[0].Name).Should(Equal(fileTemplate))
1586-
g.Expect(its.Spec.Configs[0].Generation).Should(Equal(its.Generation))
1589+
g.Expect(its.Spec.Configs[0].ConfigHash).ShouldNot(BeNil())
1590+
g.Expect(*its.Spec.Configs[0].ConfigHash).Should(Equal("123456"))
15871591
g.Expect(its.Spec.Configs[0].Reconfigure).ShouldNot(BeNil())
15881592
g.Expect(its.Spec.Configs[0].ReconfigureActionName).Should(Equal(fmt.Sprintf("reconfigure-%s", fileTemplate)))
15891593
g.Expect(its.Spec.Configs[0].Parameters).Should(HaveKey("KB_CONFIG_FILES_UPDATED"))
@@ -1672,6 +1676,54 @@ var _ = Describe("Component Controller", func() {
16721676
})).Should(Succeed())
16731677
}
16741678

1679+
testReconfigureConfigHash := func(compName, compDefName, fileTemplate string) {
1680+
var (
1681+
initConfigHash, newConfigHash string
1682+
)
1683+
1684+
createCompObj(compName, compDefName, nil)
1685+
1686+
By("check the file template object")
1687+
fileTemplateCMKey := types.NamespacedName{
1688+
Namespace: testCtx.DefaultNamespace,
1689+
Name: fileTemplateObjectName(&component.SynthesizedComponent{FullCompName: compKey.Name}, fileTemplate),
1690+
}
1691+
Eventually(testapps.CheckObj(&testCtx, fileTemplateCMKey, func(g Gomega, cm *corev1.ConfigMap) {
1692+
g.Expect(cm.Data).Should(HaveKeyWithValue("level", "info"))
1693+
initConfigHash = cm.Annotations[constant.CMInsConfigurationHashLabelKey]
1694+
g.Expect(initConfigHash).NotTo(BeEmpty())
1695+
})).Should(Succeed())
1696+
1697+
By("update the config template variables")
1698+
Expect(testapps.GetAndChangeObj(&testCtx, compKey, func(comp *kbappsv1.Component) {
1699+
comp.Spec.Configs = []kbappsv1.ClusterComponentConfig{
1700+
{
1701+
Name: ptr.To(fileTemplate),
1702+
Variables: map[string]string{
1703+
"LOG_LEVEL": "debug",
1704+
},
1705+
},
1706+
}
1707+
})()).Should(Succeed())
1708+
1709+
By("check the file template object again")
1710+
Eventually(testapps.CheckObj(&testCtx, fileTemplateCMKey, func(g Gomega, cm *corev1.ConfigMap) {
1711+
g.Expect(cm.Data).Should(HaveKeyWithValue("level", "debug"))
1712+
newConfigHash = cm.Annotations[constant.CMInsConfigurationHashLabelKey]
1713+
g.Expect(newConfigHash).NotTo(BeEmpty())
1714+
g.Expect(newConfigHash).NotTo(Equal(initConfigHash))
1715+
})).Should(Succeed())
1716+
1717+
By("check the workload updated")
1718+
itsKey := compKey
1719+
Eventually(testapps.CheckObj(&testCtx, itsKey, func(g Gomega, its *workloads.InstanceSet) {
1720+
g.Expect(its.Spec.Configs).Should(HaveLen(1))
1721+
g.Expect(its.Spec.Configs[0].Name).Should(Equal(fileTemplate))
1722+
g.Expect(its.Spec.Configs[0].ConfigHash).ShouldNot(BeNil())
1723+
g.Expect(*its.Spec.Configs[0].ConfigHash).Should(Equal(newConfigHash))
1724+
})).Should(Succeed())
1725+
}
1726+
16751727
Context("provisioning", func() {
16761728
BeforeEach(func() {
16771729
createDefinitionObjects()
@@ -2245,5 +2297,9 @@ var _ = Describe("Component Controller", func() {
22452297
It("reconfigure - restart", func() {
22462298
testReconfigureRestart(defaultCompName, compDefObj.Name, fileTemplate)
22472299
})
2300+
2301+
PIt("reconfigure - config hash", func() {
2302+
testReconfigureConfigHash(defaultCompName, compDefObj.Name, fileTemplate)
2303+
})
22482304
})
22492305
})

controllers/apps/component/transformer_component_template.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,12 +213,22 @@ func buildFileTemplateObject(transCtx *componentTransformContext, tpl component.
213213
return nil, err
214214
}
215215

216+
configHash := tpl.ConfigHash
217+
if configHash == nil {
218+
hash, err1 := intctrlutil.ComputeHash(data)
219+
if err1 != nil {
220+
return nil, err1
221+
}
222+
configHash = &hash
223+
}
224+
216225
objName := fileTemplateObjectName(transCtx.SynthesizeComponent, tpl.Name)
217226
obj := builder.NewConfigMapBuilder(synthesizedComp.Namespace, objName).
218227
AddLabelsInMap(synthesizedComp.StaticLabels).
219228
AddLabelsInMap(constant.GetCompLabelsWithDef(synthesizedComp.ClusterName, synthesizedComp.Name, compDef.Name)).
220229
AddLabels(kubeBlockFileTemplateLabelKey, "true").
221230
AddAnnotationsInMap(synthesizedComp.StaticAnnotations).
231+
AddAnnotations(constant.CMInsConfigurationHashLabelKey, *configHash).
222232
SetData(data).
223233
GetObject()
224234
if err := setCompOwnershipNFinalizer(transCtx.Component, obj); err != nil {

controllers/apps/component/transformer_component_workload_ops.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"crypto/sha256"
2424
"errors"
2525
"fmt"
26+
"maps"
2627
"path/filepath"
2728
"reflect"
2829
"slices"
@@ -452,7 +453,7 @@ func (r *componentWorkloadOps) handleReconfigure(transCtx *componentTransformCon
452453
action *appsv1.Action
453454
actionName string
454455
)
455-
if tpl.ExternalManaged != nil && *tpl.ExternalManaged {
456+
if ptr.Deref(tpl.ExternalManaged, false) {
456457
if tpl.Reconfigure == nil {
457458
return // disabled by the external system
458459
}
@@ -467,12 +468,15 @@ func (r *componentWorkloadOps) handleReconfigure(transCtx *componentTransformCon
467468
return // has no reconfigure action defined
468469
}
469470

471+
parameters := lifecycle.FileTemplateChanges(changes.Created, changes.Removed, changes.Updated)
472+
maps.Copy(parameters, tpl.Variables)
470473
config := workloads.ConfigTemplate{
471474
Name: tpl.Name,
472-
Generation: r.component.Generation,
475+
ConfigHash: tpl.ConfigHash,
476+
Restart: tpl.RestartOnFileChange,
473477
Reconfigure: action,
474478
ReconfigureActionName: actionName,
475-
Parameters: lifecycle.FileTemplateChanges(changes.Created, changes.Removed, changes.Updated),
479+
Parameters: parameters,
476480
}
477481
if r.protoITS.Spec.Configs == nil {
478482
r.protoITS.Spec.Configs = make([]workloads.ConfigTemplate, 0)

0 commit comments

Comments
 (0)