Skip to content

Commit

Permalink
feat: addon install require to specify the version (#457)
Browse files Browse the repository at this point in the history
(cherry picked from commit 65f8876)
  • Loading branch information
ldming committed Oct 10, 2024
1 parent 9e01533 commit 3db8f66
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
2 changes: 1 addition & 1 deletion docs/user_docs/cli/kbcli_addon_install.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ kbcli addon install [flags]
--force force install the addon and ignore the version check
-h, --help help for install
--index string specify the addon index index, use 'kubeblocks' by default (default "kubeblocks")
--version string specify the addon version
--version string specify the addon version to install, run 'kbcli addon search <addon-name>' to get the available versions
```

### Options inherited from parent commands
Expand Down
23 changes: 11 additions & 12 deletions pkg/cmd/addon/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,11 @@ func newInstallCmd(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra
},
}
cmd.Flags().BoolVar(&o.force, "force", false, "force install the addon and ignore the version check")
cmd.Flags().StringVar(&o.version, "version", "", "specify the addon version")
cmd.Flags().StringVar(&o.version, "version", "", "specify the addon version to install, run 'kbcli addon search <addon-name>' to get the available versions")
cmd.Flags().StringVar(&o.index, "index", types.DefaultIndexName, "specify the addon index index, use 'kubeblocks' by default")

_ = cmd.MarkFlagRequired("version")

return cmd
}

Expand All @@ -139,6 +141,11 @@ func (o *installOption) Complete() error {
if err = o.baseOption.complete(); err != nil {
return err
}

if o.version == "" {
return fmt.Errorf("please specify the version, run 'kbcli addon search %s' to get the available versions", o.name)
}

// search specified addon and match its index

if _, err = semver.NewVersion(o.version); err != nil && o.version != "" {
Expand All @@ -165,22 +172,14 @@ func (o *installOption) Complete() error {

// descending order of versions
for _, item := range addons {
if item.index.name == o.index {
// if the version not specified, use the latest version
if o.version == "" {
if item.index.name == o.index && o.version == getAddonVersion(item.addon) {
o.addon = item.addon
break
} else if o.version == getAddonVersion(item.addon) {
o.addon = item.addon
break
}
}
}
if o.addon == nil {
var addonInfo = o.name
if o.version != "" {
addonInfo += "-" + o.version
}
addonInfo += "-" + o.version
return fmt.Errorf("addon '%s' not found in the index '%s'", addonInfo, o.index)
}
return nil
Expand Down Expand Up @@ -234,7 +233,7 @@ func (o *installOption) Run() error {
// 1.0.0-alpha < 1.0.0-alpha.1 < 1.0.0-alpha.beta < 1.0.0-beta < 1.0.0-beta.2 < 1.0.0-beta.11 < 1.0.0-rc.1 < 1.0.0.
// https://semver.org/
func validateVersion(annotations, kbVersion string) (bool, error) {
// if kb version is a prerelease version, we will break the rules for developing
// if kb version is a pre-release version, we will break the rules for developing
if strings.Contains(kbVersion, "-") {
addPreReleaseInfo := func(constrain string) string {
constrain = strings.Trim(constrain, " ")
Expand Down
1 change: 1 addition & 0 deletions pkg/cmd/addon/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ var _ = Describe("test addon upgrade", func() {
tf.FakeDynamicClient = testing.FakeDynamicClient(testing.FakeAddon("apecloud-mysql"))
option = newUpgradeOption(tf, streams)
option.name = "apecloud-mysql"
option.version = "0.7.0"
Expect(option.Complete()).Should(Succeed())
})

Expand Down

0 comments on commit 3db8f66

Please sign in to comment.