Skip to content

Commit

Permalink
chore: support to upgrade componentDef and serviceVersion (#348)
Browse files Browse the repository at this point in the history
Co-authored-by: wangyelei <wangyelei@users.noreply.github.com>
  • Loading branch information
wangyelei and wangyelei authored May 11, 2024
1 parent 9a0c477 commit 2627e26
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 48 deletions.
5 changes: 4 additions & 1 deletion docs/user_docs/cli/kbcli_cluster_upgrade.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ kbcli cluster upgrade NAME [flags]

```
--auto-approve Skip interactive approval before upgrading the cluster
--cluster-version string Reference cluster version (required)
--cluster-version string Referring to the ClusterVersion CR(deprecated)
--component-definition string Referring to the ComponentDefinition (default "nil")
--components strings Component names to this operations
--dry-run string[="unchanged"] Must be "client", or "server". If with client strategy, only print the object that would be sent, and no data is actually sent. If with server strategy, submit the server-side request, but no data is persistent. (default "none")
--force skip the pre-checks of the opsRequest to run the opsRequest forcibly
-h, --help help for upgrade
--name string OpsRequest name. if not specified, it will be randomly generated
-o, --output format Prints the output in the specified format. Allowed values: JSON and YAML (default yaml)
--service-version string Referring to the serviceVersion that is provided by ComponentDefinition and ComponentVersion (default "nil")
--ttlSecondsAfterSucceed int Time to live after the OpsRequest succeed
```

Expand Down
95 changes: 54 additions & 41 deletions pkg/action/template/cluster_operations_template.cue
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,38 @@

// required, command line input options for parameters and flags
options: {
name: string
namespace: string
opsRequestName: string
type: string
typeLower: string
ttlSecondsAfterSucceed: int
clusterVersionRef: string
component: string
instance: string
name: string
namespace: string
opsRequestName: string
type: string
typeLower: string
ttlSecondsAfterSucceed: int
clusterVersionRef: string
componentDefinitionName: string
serviceVersion: string
component: string
instance: string
componentNames: [...string]
rebuildInstanceFrom: [
...{
componentName: string
backupName?: string
instances: [
...{
name: string
targetNodeName?: string
}
]
envForRestore?: [
...{
name: string
value: string
},
]
},
...{
componentName: string
backupName?: string
instances: [
...{
name: string
targetNodeName?: string
},
]
envForRestore?: [
...{
name: string
value: string
},
]
},
]
cpu: string
memory: string
cpu: string
memory: string
replicas: int
storage: string
vctNames: [...string]
Expand All @@ -56,7 +58,7 @@ options: {
cfgTemplateName: string
cfgFile: string
forceRestart: bool
force: bool
force: bool
services: [
...{
name: string
Expand All @@ -66,7 +68,7 @@ options: {
]
params: [
...{
name: string
name: string
value: string
},
]
Expand All @@ -76,6 +78,17 @@ options: {
// define operation block,
#upgrade: {
clusterVersionRef: options.clusterVersionRef
if len(options.componentNames) > 0 {
components: [ for _, cName in options.componentNames {
componentName: cName
if options.componentDefinitionName != "nil" {
componentDefinitionName: options.componentDefinitionName
}
if options.serviceVersion != "nil" {
serviceVersion: options.serviceVersion
}
}]
}
}

// required, k8s api resource content
Expand All @@ -96,10 +109,10 @@ content: {
}
}
spec: {
clusterName: options.name
clusterName: options.name
type: options.type
ttlSecondsAfterSucceed: options.ttlSecondsAfterSucceed
force: options.force
force: options.force
if options.type == "Upgrade" {
upgrade: #upgrade
}
Expand Down Expand Up @@ -227,18 +240,18 @@ content: {
}
}]
}
if options.type == "RebuildInstance" {
rebuildFrom: options.rebuildInstanceFrom
}
if options.type == "RebuildInstance" {
rebuildFrom: options.rebuildInstanceFrom
}
if options.type == "Custom" {
custom: {
opsDefinitionName: options.opsDefinitionName
components: [
{
componentName: options.component
parameters: options.params
}
]
opsDefinitionName: options.opsDefinitionName
components: [
{
componentName: options.component
parameters: options.params
},
]
}
}
}
Expand Down
20 changes: 15 additions & 5 deletions pkg/cmd/cluster/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ type OperationsOptions struct {
OpsTypeLower string `json:"typeLower"`

// Upgrade options
ClusterVersionRef string `json:"clusterVersionRef"`
ClusterVersionRef string `json:"clusterVersionRef"`
ComponentDefinitionName string `json:"componentDefinitionName"`
ServiceVersion string `json:"serviceVersion"`

// VerticalScaling options
CPU string `json:"cpu"`
Expand Down Expand Up @@ -325,10 +327,13 @@ func (o *OperationsOptions) CompleteHaEnabled() {
}

func (o *OperationsOptions) validateUpgrade() error {
if len(o.ClusterVersionRef) == 0 {
return fmt.Errorf("missing cluster-version")
if len(o.ClusterVersionRef) > 0 {
return nil
}
return nil
if len(o.ComponentNames) > 0 {
return nil
}
return fmt.Errorf("missing cluster-version or components")
}

func (o *OperationsOptions) validateVolumeExpansion() error {
Expand Down Expand Up @@ -683,6 +688,8 @@ var upgradeExample = templates.Examples(`
// NewUpgradeCmd creates an upgrade command
func NewUpgradeCmd(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra.Command {
o := newBaseOperationsOptions(f, streams, appsv1alpha1.UpgradeType, false)
compDefFlag := "component-definition"
serviceVersionFlag := "service-version"
cmd := &cobra.Command{
Use: "upgrade NAME",
Short: "Upgrade the cluster version.",
Expand All @@ -697,8 +704,11 @@ func NewUpgradeCmd(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra
},
}
o.addCommonFlags(cmd, f)
cmd.Flags().StringVar(&o.ClusterVersionRef, "cluster-version", "", "Reference cluster version (required)")
cmd.Flags().StringVar(&o.ClusterVersionRef, "cluster-version", "", "Referring to the ClusterVersion CR(deprecated)")
cmd.Flags().StringVar(&o.ComponentDefinitionName, compDefFlag, "nil", "Referring to the ComponentDefinition")
cmd.Flags().StringVar(&o.ServiceVersion, serviceVersionFlag, "nil", "Referring to the serviceVersion that is provided by ComponentDefinition and ComponentVersion")
cmd.Flags().BoolVar(&o.AutoApprove, "auto-approve", false, "Skip interactive approval before upgrading the cluster")
flags.AddComponentsFlag(f, cmd, &o.ComponentNames, "Component names to this operations")
return cmd
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/cluster/operations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ var _ = Describe("operations", func() {
o.Namespace = testing.Namespace
o.Name = clusterName
o.OpsType = appsv1alpha1.UpgradeType
Expect(o.Validate()).To(MatchError("missing cluster-version"))
Expect(o.Validate()).To(MatchError("missing cluster-version or components"))

By("expect to validate success")
o.ClusterVersionRef = "test-cluster-version"
Expand Down

0 comments on commit 2627e26

Please sign in to comment.