Skip to content

Commit

Permalink
chore: update rebuild-instance cmd to new api (#296)
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyelei authored Mar 29, 2024
1 parent c3f698d commit 32b2fd2
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 8 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/99designs/keyring v1.2.2
github.com/Masterminds/semver/v3 v3.2.1
github.com/apecloud/kubebench v0.0.0-20240313105909-ba8654f654fc
github.com/apecloud/kubeblocks v0.0.0-20240326022625-78469760d798
github.com/apecloud/kubeblocks v0.0.0-20240328092917-34c21903b27b
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2
github.com/benbjohnson/clock v1.3.5
github.com/briandowns/spinner v1.23.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,8 @@ github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230305170008-8188dc5388df h
github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230305170008-8188dc5388df/go.mod h1:pSwJ0fSY5KhvocuWSx4fz3BA8OrA1bQn+K1Eli3BRwM=
github.com/apecloud/kubebench v0.0.0-20240313105909-ba8654f654fc h1:BkFyEBEmDMdDhkWNAs9yg6zAj7xZjWON2u8b265T2a8=
github.com/apecloud/kubebench v0.0.0-20240313105909-ba8654f654fc/go.mod h1:5IZiDkFdgiZRGLsL+FOlvPsiF9LbyU55DVj4/5vT7+4=
github.com/apecloud/kubeblocks v0.0.0-20240326022625-78469760d798 h1:1HKluAKHrdO1l9ETTQVZobB3rYTGjUAeSMq5hK3s34k=
github.com/apecloud/kubeblocks v0.0.0-20240326022625-78469760d798/go.mod h1:nU4ytJVEzPg+n7Py2IDbwPI8k433dsLWSGmn3U0JexU=
github.com/apecloud/kubeblocks v0.0.0-20240328092917-34c21903b27b h1:nLcuqUQDxtD685CqLnnl+4m8YkogJ5p2J20aqofdv3o=
github.com/apecloud/kubeblocks v0.0.0-20240328092917-34c21903b27b/go.mod h1:scOC+CrVliHgn7ZwfKXkoXcTOIynXQhy97dHJLiQF8g=
github.com/apparentlymart/go-textseg v1.0.0/go.mod h1:z96Txxhf3xSFMPmb5X/1W05FF/Nj9VFpLOpjS5yuumk=
github.com/apparentlymart/go-textseg/v13 v13.0.0/go.mod h1:ZK2fH7c4NqDTLtiYLvIkEghdlcqw7yxLeM89kiTRPUo=
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
Expand Down
7 changes: 6 additions & 1 deletion pkg/action/template/cluster_operations_template.cue
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ options: {
...{
componentName: string
backupName?: string
instanceNames: [...string]
instances: [
...{
name: string
targetNodeName?: string
}
]
envForRestore?: [
...{
name: string
Expand Down
23 changes: 21 additions & 2 deletions pkg/cmd/cluster/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ type OperationsOptions struct {
ExecPod *corev1.Pod `json:"-"`
BackupName string `json:"-"`
InstanceNames []string `json:"-"`
Nodes []string `json:"-"`
RebuildInstanceFrom []appsv1alpha1.RebuildInstance `json:"rebuildInstanceFrom,omitempty"`
Env []string `json:"-"`
}
Expand Down Expand Up @@ -1258,6 +1259,7 @@ func NewRebuildInstanceCmd(f cmdutil.Factory, streams genericiooptions.IOStreams
}
compName = insCompName
}
// covert envs
var envVars []corev1.EnvVar
for _, v := range o.Env {
for _, envVar := range strings.Split(v, ",") {
Expand All @@ -1271,12 +1273,28 @@ func NewRebuildInstanceCmd(f cmdutil.Factory, streams genericiooptions.IOStreams
})
}
}
// covert instances
nodeMap := map[string]string{}
for _, node := range o.Nodes {
kv := strings.Split(node, "=")
if len(kv) != 2 {
return fmt.Errorf("unknown format for node: %s", node)
}
nodeMap[kv[0]] = kv[1]
}
var instances []appsv1alpha1.Instance
for _, insName := range o.InstanceNames {
instances = append(instances, appsv1alpha1.Instance{
Name: insName,
TargetNodeName: nodeMap[insName],
})
}
o.RebuildInstanceFrom = []appsv1alpha1.RebuildInstance{
{
ComponentOps: appsv1alpha1.ComponentOps{
ComponentName: compName,
},
InstanceNames: o.InstanceNames,
Instances: instances,
BackupName: o.BackupName,
EnvForRestore: envVars,
},
Expand All @@ -1300,7 +1318,8 @@ func NewRebuildInstanceCmd(f cmdutil.Factory, streams genericiooptions.IOStreams
o.addCommonFlags(cmd, f)
cmd.Flags().BoolVar(&o.AutoApprove, "auto-approve", false, "Skip interactive approval before rebuilding the instances.gi")
cmd.Flags().StringVar(&o.BackupName, "backup", "", "instances will be rebuild by the specified backup.")
cmd.Flags().StringSliceVar(&o.InstanceNames, "instances", nil, "instances which need to rebuild.")
cmd.Flags().StringSliceVar(&o.InstanceNames, "instance", nil, "instance which need to rebuild.")
cmd.Flags().StringSliceVar(&o.Nodes, "node", nil, "specified the target node which rebuilds the instance on the node otherwise will rebuild on a randon node. format: insName1=nodeName,insName2=nodeName")
cmd.Flags().StringArrayVar(&o.Env, "env", []string{}, "provide the necessary env for the 'Restore' operation from the backup. format: key1=value, key2=value")
return cmd
}
4 changes: 2 additions & 2 deletions pkg/testing/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,8 +451,8 @@ func FakeCompDef() *appsv1alpha1.ComponentDefinition {
MemberLeave: defaultLifecycleActionHandler,
Readonly: defaultLifecycleActionHandler,
Readwrite: defaultLifecycleActionHandler,
DataPopulate: defaultLifecycleActionHandler,
DataAssemble: defaultLifecycleActionHandler,
DataDump: defaultLifecycleActionHandler,
DataLoad: defaultLifecycleActionHandler,
Reconfigure: defaultLifecycleActionHandler,
AccountProvision: defaultLifecycleActionHandler,
},
Expand Down

0 comments on commit 32b2fd2

Please sign in to comment.