Skip to content

Commit

Permalink
feat: support reconfigure operation for multi components (#5906)
Browse files Browse the repository at this point in the history
  • Loading branch information
sophon-zt committed Dec 5, 2023
1 parent 4928ae7 commit 7586e10
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 28 deletions.
2 changes: 1 addition & 1 deletion pkg/cli/cmd/cluster/config_edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func NewEditConfigureCmd(f cmdutil.Factory, streams genericiooptions.IOStreams)
o := &editConfigOptions{
configOpsOptions: configOpsOptions{
editMode: true,
OperationsOptions: newBaseOperationsOptions(f, streams, appsv1alpha1.ReconfiguringType, false),
OperationsOptions: newBaseOperationsOptions(f, streams, appsv1alpha1.ReconfiguringType, true),
}}

cmd := &cobra.Command{
Expand Down
15 changes: 10 additions & 5 deletions pkg/cli/cmd/cluster/config_ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import (
"github.com/apecloud/kubeblocks/pkg/cli/printer"
"github.com/apecloud/kubeblocks/pkg/cli/types"
"github.com/apecloud/kubeblocks/pkg/cli/util"
"github.com/apecloud/kubeblocks/pkg/cli/util/flags"
"github.com/apecloud/kubeblocks/pkg/cli/util/prompt"
cfgcm "github.com/apecloud/kubeblocks/pkg/configuration/config_manager"
"github.com/apecloud/kubeblocks/pkg/configuration/core"
Expand Down Expand Up @@ -79,6 +78,10 @@ func (o *configOpsOptions) Complete() error {
}
}

if len(o.ComponentNames) > 0 {
o.ComponentName = o.ComponentNames[0]
}

wrapper, err := newConfigWrapper(o.CreateOptions, o.Name, o.ComponentName, o.CfgTemplateName, o.CfgFile, o.KeyValues)
if err != nil {
return err
Expand Down Expand Up @@ -116,7 +119,9 @@ func (o *configOpsOptions) Validate() error {

o.CfgFile = o.wrapper.ConfigFile()
o.CfgTemplateName = o.wrapper.ConfigSpecName()
o.ComponentNames = []string{o.wrapper.ComponentName()}
if len(o.ComponentNames) == 0 {
o.ComponentNames = []string{o.wrapper.ComponentName()}
}

if o.editMode {
return nil
Expand Down Expand Up @@ -240,7 +245,7 @@ func (o *configOpsOptions) buildReconfigureCommonFlags(cmd *cobra.Command, f cmd
"For available templates and configs, refer to: 'kbcli cluster describe-config'.")
cmd.Flags().StringVar(&o.CfgFile, "config-file", "", "Specify the name of the configuration file to be updated (e.g. for mysql: --config-file=my.cnf). "+
"For available templates and configs, refer to: 'kbcli cluster describe-config'.")
flags.AddComponentFlag(f, cmd, &o.ComponentName, "Specify the name of Component to be updated. If the cluster has only one component, unset the parameter.")
// flags.AddComponentFlag(f, cmd, &o.ComponentName, "Specify the name of Component to be updated. If the cluster has only one component, unset the parameter.")
cmd.Flags().BoolVar(&o.ForceRestart, "force-restart", false, "Boolean flag to restart component. Default with false.")
cmd.Flags().StringVar(&o.LocalFilePath, "local-file", "", "Specify the local configuration file to be updated.")
cmd.Flags().BoolVar(&o.replaceFile, "replace", false, "Boolean flag to enable replacing config file. Default with false.")
Expand All @@ -250,10 +255,10 @@ func (o *configOpsOptions) buildReconfigureCommonFlags(cmd *cobra.Command, f cmd
func NewReconfigureCmd(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra.Command {
o := &configOpsOptions{
editMode: false,
OperationsOptions: newBaseOperationsOptions(f, streams, appsv1alpha1.ReconfiguringType, false),
OperationsOptions: newBaseOperationsOptions(f, streams, appsv1alpha1.ReconfiguringType, true),
}
cmd := &cobra.Command{
Use: "configure NAME --set key=value[,key=value] [--component=component-name] [--config-spec=config-spec-name] [--config-file=config-file]",
Use: "configure NAME --set key=value[,key=value] [--components=component1-name,component2-name] [--config-spec=config-spec-name] [--config-file=config-file]",
Short: "Configure parameters with the specified components in the cluster.",
Example: createReconfigureExample,
ValidArgsFunction: util.ResourceNameCompletionFunc(f, types.ClusterGVR()),
Expand Down
22 changes: 16 additions & 6 deletions pkg/cli/cmd/cluster/describe_ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,11 +332,21 @@ func (o *describeOpsOptions) getVolumeExpansionCommand(spec appsv1alpha1.OpsRequ

// getReconfiguringCommand gets the command of the VolumeExpansion command.
func (o *describeOpsOptions) getReconfiguringCommand(spec appsv1alpha1.OpsRequestSpec) []string {
var (
updatedParams = spec.Reconfigure
componentName = updatedParams.ComponentName
)
if spec.Reconfigure != nil {
return generateReconfiguringCommand(spec.ClusterRef, spec.Reconfigure, []string{spec.Reconfigure.ComponentName})
}

if len(spec.Reconfigures) == 0 {
return nil
}
components := make([]string, len(spec.Reconfigures))
for i, reconfigure := range spec.Reconfigures {
components[i] = reconfigure.ComponentName
}
return generateReconfiguringCommand(spec.ClusterRef, &spec.Reconfigures[0], components)
}

func generateReconfiguringCommand(clusterRef string, updatedParams *appsv1alpha1.Reconfigure, components []string) []string {
if len(updatedParams.Configurations) == 0 {
return nil
}
Expand All @@ -350,8 +360,8 @@ func (o *describeOpsOptions) getReconfiguringCommand(spec appsv1alpha1.OpsReques
commandArgs = append(commandArgs, "kbcli")
commandArgs = append(commandArgs, "cluster")
commandArgs = append(commandArgs, "configure")
commandArgs = append(commandArgs, spec.ClusterRef)
commandArgs = append(commandArgs, fmt.Sprintf("--components=%s", componentName))
commandArgs = append(commandArgs, clusterRef)
commandArgs = append(commandArgs, fmt.Sprintf("--components=%s", strings.Join(components, ",")))
commandArgs = append(commandArgs, fmt.Sprintf("--config-spec=%s", configuration.Name))

config := configuration.Keys[0]
Expand Down
57 changes: 41 additions & 16 deletions pkg/cli/create/template/cluster_operations_template.cue
Original file line number Diff line number Diff line change
Expand Up @@ -120,24 +120,49 @@ content: {
}]
}
if options.type == "Reconfiguring" {
reconfigure: {
componentName: options.componentNames[0]
configurations: [ {
name: options.cfgTemplateName
if options.forceRestart {
policy: "simple"
}
keys: [{
key: options.cfgFile
if options.fileContent != "" {
fileContent: options.fileContent
if len(options.componentNames) == 1 {
reconfigure: {
componentName: options.componentNames[0]
configurations: [ {
name: options.cfgTemplateName
if options.forceRestart {
policy: "simple"
}
if options.hasPatch {
parameters: [ for k, v in options.keyValues {
key: k
value: v
}]
keys: [{
key: options.cfgFile
if options.fileContent != "" {
fileContent: options.fileContent
}
if options.hasPatch {
parameters: [ for k, v in options.keyValues {
key: k
value: v
}]
}
}]
}]
}
}
if len(options.componentNames) > 1 {
reconfigures: [ for _, cName in options.componentNames {
componentName: cName
configurations: [ {
name: options.cfgTemplateName
if options.forceRestart {
policy: "simple"
}
keys: [{
key: options.cfgFile
if options.fileContent != "" {
fileContent: options.fileContent
}
if options.hasPatch {
parameters: [ for k, v in options.keyValues {
key: k
value: v
}]
}
}]
}]
}]
}
Expand Down

0 comments on commit 7586e10

Please sign in to comment.