Skip to content

Commit a501a0a

Browse files
committed
update API
1 parent 23c8d20 commit a501a0a

File tree

14 files changed

+163
-113
lines changed

14 files changed

+163
-113
lines changed

apis/apps/v1/types.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,11 @@ type ClusterComponentConfig struct {
502502
// +optional
503503
ExternalManaged *bool `json:"externalManaged,omitempty"`
504504

505+
// The versioned hash of the config content.
506+
//
507+
// +optional
508+
VersionHash string `json:"versionHash,omitempty"`
509+
505510
// The custom reconfigure action to reload the configuration whenever changes to this config are detected.
506511
//
507512
// The container executing this action has access to following variables:

apis/workloads/v1/instanceset_types.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -514,8 +514,15 @@ type ConfigTemplate struct {
514514
// The name of the config.
515515
Name string `json:"name"`
516516

517-
// The generation of the config.
518-
Generation int64 `json:"generation"`
517+
// The generation of the config content.
518+
//
519+
// +optional
520+
Generation int64 `json:"generation,omitempty"`
521+
522+
// The versioned hash of the config content.
523+
//
524+
// +optional
525+
VersionHash string `json:"versionHash,omitempty"`
519526

520527
// The custom reconfigure action.
521528
//
@@ -566,8 +573,13 @@ type InstanceConfigStatus struct {
566573

567574
// The generation of the config.
568575
//
569-
// +kubebuilder:validation:Required
570-
Generation int64 `json:"generation"`
576+
// +optional
577+
Generation int64 `json:"generation,omitempty"`
578+
579+
// The hash of the config content.
580+
//
581+
// +optional
582+
VersionHash string `json:"versionHash,omitempty"`
571583
}
572584

573585
// InstanceTemplateStatus aggregates the status of replicas for each InstanceTemplate

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,9 @@ spec:
739739
description: Variables are key-value pairs for dynamic
740740
configuration values that can be provided by the user.
741741
type: object
742+
versionHash:
743+
description: The versioned hash of the config content.
744+
type: string
742745
type: object
743746
type: array
744747
disableExporter:
@@ -11963,6 +11966,9 @@ spec:
1196311966
configuration values that can be provided by the
1196411967
user.
1196511968
type: object
11969+
versionHash:
11970+
description: The versioned hash of the config content.
11971+
type: string
1196611972
type: object
1196711973
type: array
1196811974
disableExporter:

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,9 @@ spec:
610610
description: Variables are key-value pairs for dynamic configuration
611611
values that can be provided by the user.
612612
type: object
613+
versionHash:
614+
description: The versioned hash of the config content.
615+
type: string
613616
type: object
614617
type: array
615618
disableExporter:

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ spec:
7070
items:
7171
properties:
7272
generation:
73-
description: The generation of the config.
73+
description: The generation of the config content.
7474
format: int64
7575
type: integer
7676
name:
@@ -519,8 +519,10 @@ spec:
519519

520520
An empty name indicates that the reconfigure action is the default one defined by lifecycle actions.
521521
type: string
522+
versionHash:
523+
description: The versioned hash of the config content.
524+
type: string
522525
required:
523-
- generation
524526
- name
525527
type: object
526528
type: array
@@ -11716,8 +11718,10 @@ spec:
1171611718
name:
1171711719
description: The name of the config.
1171811720
type: string
11721+
versionHash:
11722+
description: The hash of the config content.
11723+
type: string
1171911724
required:
11720-
- generation
1172111725
- name
1172211726
type: object
1172311727
type: array

controllers/parameters/config_reconcile_wrapper.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import (
2525

2626
appsv1 "github.com/apecloud/kubeblocks/apis/apps/v1"
2727
parametersv1alpha1 "github.com/apecloud/kubeblocks/apis/parameters/v1alpha1"
28+
workloads "github.com/apecloud/kubeblocks/apis/workloads/v1"
29+
"github.com/apecloud/kubeblocks/pkg/constant"
2830
"github.com/apecloud/kubeblocks/pkg/controller/component"
2931
"github.com/apecloud/kubeblocks/pkg/controller/render"
3032
intctrlutil "github.com/apecloud/kubeblocks/pkg/controllerutil"
@@ -39,6 +41,8 @@ type ReconcileContext struct {
3941
ConfigMap *corev1.ConfigMap
4042
BuiltinComponent *component.SynthesizedComponent
4143

44+
ITS *workloads.InstanceSet
45+
4246
ConfigRender *parametersv1alpha1.ParamConfigRenderer
4347
ParametersDefs map[string]*parametersv1alpha1.ParametersDefinition
4448
}
@@ -63,11 +67,23 @@ func (c *ReconcileContext) GetRelatedObjects() error {
6367
return c.Cluster().
6468
ComponentAndComponentDef().
6569
ComponentSpec().
70+
Workload().
6671
SynthesizedComponent().
6772
ParametersDefinitions().
6873
Complete()
6974
}
7075

76+
func (c *ReconcileContext) Workload() *ReconcileContext {
77+
return c.Wrap(func() error {
78+
itsKey := client.ObjectKey{
79+
Namespace: c.Namespace,
80+
Name: constant.GenerateWorkloadNamePattern(c.ClusterName, c.ComponentName),
81+
}
82+
c.ITS = &workloads.InstanceSet{}
83+
return c.Client.Get(c.Context, itsKey, c.ITS)
84+
})
85+
}
86+
7187
func (c *ReconcileContext) SynthesizedComponent() *ReconcileContext {
7288
return c.Wrap(func() (err error) {
7389
// build synthesized component for the component

controllers/parameters/policy_util.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
2020
package parameters
2121

2222
import (
23-
"context"
2423
"fmt"
2524

2625
corev1 "k8s.io/api/core/v1"
@@ -74,11 +73,6 @@ func getPodsForOnlineUpdate(params reconfigureContext) ([]corev1.Pod, error) {
7473
return podList.Items, nil
7574
}
7675

77-
func commonOnlineUpdateWithPod(pod *corev1.Pod, ctx context.Context, configSpec string, configFile string, updatedParams map[string]string) error {
78-
// TODO: update cluster spec to call the reconfigure action
79-
return fmt.Errorf("not yet implemented")
80-
}
81-
8276
func getComponentSpecPtrByName(cli client.Client, ctx intctrlutil.RequestCtx, cluster *appsv1.Cluster, compName string) (*appsv1.ClusterComponentSpec, error) {
8377
for i := range cluster.Spec.ComponentSpecs {
8478
componentSpec := &cluster.Spec.ComponentSpecs[i]
@@ -147,7 +141,6 @@ func (r reconfigureTask) ExecReload() (returnedStatus, error) {
147141
if executor, ok := upgradePolicyMap[r.ReloadPolicy]; ok {
148142
return executor.Upgrade(r.taskCtx)
149143
}
150-
151144
return returnedStatus{}, fmt.Errorf("not support reload action[%s]", r.ReloadPolicy)
152145
}
153146

@@ -233,6 +226,7 @@ func buildReloadActionTask(reloadPolicy parametersv1alpha1.ReloadPolicy, templat
233226
Cluster: rctx.ClusterObj,
234227
ClusterComponent: rctx.ClusterComObj,
235228
SynthesizedComponent: rctx.BuiltinComponent,
229+
ITS: rctx.ITS,
236230
Patch: patch,
237231
},
238232
}
@@ -248,6 +242,7 @@ func buildRestartTask(configTemplate *appsv1.ComponentFileTemplate, rctx *Reconc
248242
ClusterComponent: rctx.ClusterComObj,
249243
Cluster: rctx.ClusterObj,
250244
SynthesizedComponent: rctx.BuiltinComponent,
245+
ITS: rctx.ITS,
251246
ConfigMap: rctx.ConfigMap,
252247
},
253248
}

controllers/parameters/reconfigure_policy.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727

2828
appsv1 "github.com/apecloud/kubeblocks/apis/apps/v1"
2929
parametersv1alpha1 "github.com/apecloud/kubeblocks/apis/parameters/v1alpha1"
30+
workloads "github.com/apecloud/kubeblocks/apis/workloads/v1"
3031
"github.com/apecloud/kubeblocks/pkg/controller/component"
3132
intctrlutil "github.com/apecloud/kubeblocks/pkg/controllerutil"
3233
"github.com/apecloud/kubeblocks/pkg/parameters/core"
@@ -69,6 +70,8 @@ type reconfigureContext struct {
6970
// Associated component for component and component definition.
7071
SynthesizedComponent *component.SynthesizedComponent
7172

73+
ITS *workloads.InstanceSet // TODO: use cluster or component API?
74+
7275
// Configmap object of the configuration template instance in the component.
7376
ConfigMap *corev1.ConfigMap
7477

@@ -109,8 +112,8 @@ func (param *reconfigureContext) getTargetVersionHash() string {
109112
return hash
110113
}
111114

112-
func (param *reconfigureContext) getTargetReplicas() int {
113-
return int(param.ClusterComponent.Replicas)
115+
func (param *reconfigureContext) getTargetReplicas() int32 {
116+
return param.ClusterComponent.Replicas
114117
}
115118

116119
func enableSyncTrigger(reloadAction *parametersv1alpha1.ReloadAction) bool {

controllers/parameters/sync_upgrade_policy.go

Lines changed: 45 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,12 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
2020
package parameters
2121

2222
import (
23-
"context"
2423
"fmt"
2524

26-
corev1 "k8s.io/api/core/v1"
27-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28-
"k8s.io/apimachinery/pkg/labels"
29-
"sigs.k8s.io/controller-runtime/pkg/client"
25+
"k8s.io/utils/ptr"
3026

27+
apisappsv1 "github.com/apecloud/kubeblocks/apis/apps/v1"
3128
parametersv1alpha1 "github.com/apecloud/kubeblocks/apis/parameters/v1alpha1"
32-
intctrlutil "github.com/apecloud/kubeblocks/pkg/controllerutil"
33-
"github.com/apecloud/kubeblocks/pkg/parameters"
34-
"github.com/apecloud/kubeblocks/pkg/parameters/core"
3529
)
3630

3731
var syncPolicyInstance = &syncPolicy{}
@@ -47,87 +41,61 @@ func (o *syncPolicy) Upgrade(rctx reconfigureContext) (returnedStatus, error) {
4741
if len(updatedParameters) == 0 {
4842
return makeReturnedStatus(ESNone), nil
4943
}
44+
return o.sync(rctx, updatedParameters)
45+
}
5046

51-
pods, err := getPodsForOnlineUpdate(rctx)
52-
if err != nil {
53-
return makeReturnedStatus(ESFailedAndRetry), err
47+
func (o *syncPolicy) sync(rctx reconfigureContext, parameters map[string]string) (returnedStatus, error) {
48+
var targetCfg *apisappsv1.ClusterComponentConfig
49+
for i, cfg := range rctx.ClusterComponent.Configs {
50+
if ptr.Deref(cfg.Name, "") == rctx.ConfigTemplate.Name {
51+
targetCfg = &rctx.ClusterComponent.Configs[i]
52+
break
53+
}
54+
}
55+
if targetCfg == nil {
56+
return makeReturnedStatus(ESFailedAndRetry), fmt.Errorf("config %s not found", rctx.ConfigTemplate.Name)
5457
}
55-
return o.sync(rctx, updatedParameters, pods)
58+
if targetCfg.VersionHash != rctx.getTargetVersionHash() {
59+
return o.update(rctx, targetCfg, parameters)
60+
}
61+
return o.check(rctx)
5662
}
5763

58-
func (o *syncPolicy) sync(rctx reconfigureContext, updatedParameters map[string]string, pods []corev1.Pod) (returnedStatus, error) {
64+
func (o *syncPolicy) update(rctx reconfigureContext, targetCfg *apisappsv1.ClusterComponentConfig, parameters map[string]string) (returnedStatus, error) {
5965
var (
60-
r = ESNone
61-
total = int32(len(pods))
62-
replicas = int32(rctx.getTargetReplicas())
63-
progress = core.NotStarted
64-
65-
err error
66-
ctx = rctx.Ctx
67-
configKey = rctx.generateConfigIdentifier()
68-
versionHash = rctx.getTargetVersionHash()
69-
selector = parameters.GetPodSelector(rctx.ParametersDef)
70-
fileName string
66+
replicas = rctx.getTargetReplicas()
67+
// fileName string
7168
)
69+
// if rctx.ConfigDescription != nil {
70+
// fileName = rctx.ConfigDescription.Name
71+
// }
7272

73-
if selector != nil {
74-
pods, err = o.matchLabel(pods, selector)
75-
}
76-
if err != nil {
77-
return makeReturnedStatus(ESFailedAndRetry), err
78-
}
79-
if len(pods) == 0 {
80-
rctx.Log.Info(fmt.Sprintf("no pods to update, and retry, selector: %v", selector))
81-
return makeReturnedStatus(ESRetry), nil
82-
}
83-
if rctx.ConfigDescription != nil {
84-
fileName = rctx.ConfigDescription.Name
85-
}
73+
targetCfg.Variables = parameters
74+
targetCfg.VersionHash = rctx.getTargetVersionHash()
8675

87-
requireUpdatedCount := int32(len(pods))
88-
for _, pod := range pods {
89-
rctx.Log.V(1).Info(fmt.Sprintf("sync pod: %s", pod.Name))
90-
if intctrlutil.IsMatchConfigVersion(&pod, configKey, versionHash) {
91-
progress++
92-
continue
93-
}
94-
if !intctrlutil.IsPodReady(&pod) {
95-
continue
96-
}
97-
if err = commonOnlineUpdateWithPod(&pod, ctx, rctx.ConfigTemplate.Name, fileName, updatedParameters); err != nil {
98-
return makeReturnedStatus(ESFailedAndRetry), err
99-
}
100-
if err = o.updatePodLabelsWithConfigVersion(&pod, configKey, versionHash, rctx.Client, ctx); err != nil {
101-
return makeReturnedStatus(ESFailedAndRetry), err
102-
}
103-
progress++
104-
}
76+
// TODO: update cluster spec
10577

106-
if requireUpdatedCount != progress || replicas != total {
107-
r = ESRetry
108-
}
109-
return makeReturnedStatus(r, withExpected(requireUpdatedCount), withSucceed(progress)), nil
78+
return makeReturnedStatus(ESRetry, withExpected(replicas), withSucceed(0)), nil
11079
}
11180

112-
func (o *syncPolicy) matchLabel(pods []corev1.Pod, selector *metav1.LabelSelector) ([]corev1.Pod, error) {
113-
var result []corev1.Pod
114-
match, err := metav1.LabelSelectorAsSelector(selector)
115-
if err != nil {
116-
return nil, core.WrapError(err, "failed to convert selector: %v", selector)
117-
}
118-
for _, pod := range pods {
119-
if match.Matches(labels.Set(pod.Labels)) {
120-
result = append(result, pod)
81+
func (o *syncPolicy) check(rctx reconfigureContext) (returnedStatus, error) {
82+
var (
83+
replicas = rctx.getTargetReplicas()
84+
versionHash = rctx.getTargetVersionHash()
85+
)
86+
progress := int32(0)
87+
for _, inst := range rctx.ITS.Status.InstanceStatus {
88+
for _, cfg := range inst.Configs {
89+
if cfg.Name == rctx.ConfigTemplate.Name {
90+
if cfg.VersionHash == versionHash {
91+
progress++
92+
}
93+
break
94+
}
12195
}
12296
}
123-
return result, nil
124-
}
125-
126-
func (o *syncPolicy) updatePodLabelsWithConfigVersion(pod *corev1.Pod, labelKey, configVersion string, cli client.Client, ctx context.Context) error {
127-
patch := client.MergeFrom(pod.DeepCopy())
128-
if pod.Labels == nil {
129-
pod.Labels = make(map[string]string, 1)
97+
if progress == replicas {
98+
return makeReturnedStatus(ESNone, withExpected(replicas), withSucceed(progress)), nil
13099
}
131-
pod.Labels[labelKey] = configVersion
132-
return cli.Patch(ctx, pod, patch)
100+
return makeReturnedStatus(ESRetry, withExpected(replicas), withSucceed(progress)), nil
133101
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,9 @@ spec:
739739
description: Variables are key-value pairs for dynamic
740740
configuration values that can be provided by the user.
741741
type: object
742+
versionHash:
743+
description: The versioned hash of the config content.
744+
type: string
742745
type: object
743746
type: array
744747
disableExporter:
@@ -11963,6 +11966,9 @@ spec:
1196311966
configuration values that can be provided by the
1196411967
user.
1196511968
type: object
11969+
versionHash:
11970+
description: The versioned hash of the config content.
11971+
type: string
1196611972
type: object
1196711973
type: array
1196811974
disableExporter:

0 commit comments

Comments
 (0)