Skip to content

Commit

Permalink
temp save
Browse files Browse the repository at this point in the history
  • Loading branch information
1aal committed Dec 6, 2023
1 parent 606a929 commit 5fb09b4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 37 deletions.
8 changes: 5 additions & 3 deletions pkg/cmd/addon/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ func newInstallCmd(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra
util.CheckErr(o.Complete())
util.CheckErr(o.Validate())
util.CheckErr(o.Run())
// avoid unnecessary messages for upgrade
fmt.Fprintf(o.Out, "%s install successed\n", o.name)
},
}
cmd.Flags().BoolVar(&o.force, "force", false, "force install the addon and ignore the version check")
Expand Down Expand Up @@ -200,13 +202,14 @@ func (o *installOption) Validate() error {
return fmt.Errorf("KubeBlocks is not yet installed,please install it first")
}
if o.force {
fmt.Fprint(o.Out, printer.BoldYellow("Warning: --force flag will skip version checks, which may result in the cluster not running correctly."))
fmt.Fprint(o.Out, printer.BoldYellow("Warning: --force flag will skip version checks, which may result in the cluster not running correctly.\n"))
return nil
}

if o.addon.Annotations == nil || len(o.addon.Annotations[types.KBVersionValidateAnnotationKey]) == 0 {
fmt.Fprint(o.Out, printer.BoldYellow(fmt.Sprintf(`Warning: The addon %s is missing annotations to validate KubeBlocks versions.
It will automatically skip version checks, which may result in the cluster not running correctly.`, o.name)))
It will automatically skip version checks, which may result in the cluster not running correctly.
`, o.name)))
} else if ok, err = validateVersion(o.addon.Annotations[types.KBVersionValidateAnnotationKey], v.KubeBlocks); err == nil && !ok {
return fmt.Errorf("KubeBlocks version %s does not meet the requirements for addon installation\nUse --force option to skip this check", v.KubeBlocks)
}
Expand All @@ -225,7 +228,6 @@ func (o *installOption) Run() error {
if err != nil {
return err
}
fmt.Fprintf(o.Out, "%s install successed\n", o.name)
return nil
}

Expand Down
63 changes: 32 additions & 31 deletions pkg/cmd/addon/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,37 +95,38 @@ var _ = Describe("index test", func() {
ok bool
err error
)
ok, err = validateVersion("0.7.0", "0.7.1")
Expect(err).Should(BeNil())
Expect(ok).Should(BeFalse())

ok, err = validateVersion("0.7.0", "0.7.0")
Expect(err).Should(BeNil())
Expect(ok).Should(BeTrue())

ok, err = validateVersion(">=0.7.0", "0.7.1")
Expect(err).Should(BeNil())
Expect(ok).Should(BeTrue())

ok, err = validateVersion(">=0.7.0", "0.6.0")
Expect(err).Should(BeNil())
Expect(ok).Should(BeFalse())

ok, err = validateVersion(">=0.7.0,<=0.8.0", "0.9.0")
Expect(err).Should(BeNil())
Expect(ok).Should(BeFalse())

ok, err = validateVersion(">=0.7.0 || <=0.5.0", "0.3.0")
Expect(err).Should(BeNil())
Expect(ok).Should(BeTrue())

ok, err = validateVersion("", "0.3.0")
Expect(err).Should(HaveOccurred())
Expect(ok).Should(BeFalse())

ok, err = validateVersion(">=0.7.0", "")
Expect(err).Should(HaveOccurred())
Expect(ok).Should(BeFalse())
testCases := []struct {
constraint string
kbVersion string
success bool
result bool
}{
{"0.7.0", "0.7.1", true, false},
{"0.7.0", "0.7.0", true, true},
{">=0.7.0", "0.7.1", true, true},
{">=0.7.0", "0.6.0", true, false},
{">=0.7.0,<=0.8.0", "0.9.0", true, false},
{">=0.7.0 || <=0.5.0", "0.3.0", true, true},
{"", "0.7.1", false, false},
{"0.7.0", "", false, false},
{">=0.7.0", "0.8.0-alpha.0", true, false},
{">=0.7.0", "0.8.0-beta.0", true, false},
// todo(al): fix blocks-index addon.yaml version constraint annotations
{">=0.7.0-beta.0", "0.8.0-beta.0", true, true},
{">=0.7.0-beta.0", "0.8.0-alpha.11", true, true},
{">=0.8.0-beta.0", "0.8.0-alpha.11", true, false},
{">=0.8.0-alpha.11", "0.8.0-beta.0", true, true},
}

for _, c := range testCases {
ok, err = validateVersion(c.constraint, c.kbVersion)
if c.success {
Expect(err).Should(Succeed())
Expect(ok).Should(Equal(c.result))
} else {
Expect(err).Should(HaveOccurred())
}
}
})

By("validate --force")
Expand Down
7 changes: 4 additions & 3 deletions pkg/cmd/addon/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func (o *upgradeOption) Validate() error {
}
if !o.inplace && o.rename == "" {
o.rename = fmt.Sprintf("%s-%s", o.name, o.version)
fmt.Printf("--name is not specified by user when upgrade is non-inplace, use \"%s\" by default", o.rename)
fmt.Printf("--name is not specified by user when upgrade is non-inplace, use \"%s\" by default\n", o.rename)
}
target, err := semver.NewVersion(o.version)
if err != nil {
Expand All @@ -138,7 +138,7 @@ func (o *upgradeOption) Validate() error {
return err
}
if !target.GreaterThan(current) {
fmt.Printf(`%s addon %s current version %s is either the latest or newer than the expected version %s.`, printer.BoldYellow("Warn:"), o.name, o.currentVersion, o.version)
fmt.Printf("%s addon %s current version %s is either the latest or newer than the expected version %s.\n", printer.BoldYellow("Warn:"), o.name, o.currentVersion, o.version)
}
return o.installOption.Validate()
}
Expand All @@ -149,9 +149,10 @@ func (o *upgradeOption) Run() error {
o.addon.Spec.Helm.InstallValues.SetValues = append(o.addon.Spec.Helm.InstallValues.SetValues, fmt.Sprintf("%s=%s", types.AddonResourceNamePrefix, o.rename))
}
o.addon.Spec.Helm.InstallValues.SetValues = []string{fmt.Sprintf("%s=%s", types.AddonResourceNamePrefix, o.rename)}
o.addon.Name = o.rename
err := o.installOption.Run()
if err == nil {
fmt.Printf("Addon %s-%s upgrade successed.", o.name, o.version)
fmt.Printf("Addon %s-%s upgrade successed.\n", o.rename, o.version)
}
return err
}
Expand Down

0 comments on commit 5fb09b4

Please sign in to comment.