Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions pkg/cmd/kubeblocks/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ type InstallOptions struct {
Options
OldVersion string
Version string
chartFile string
crdsFile string
Quiet bool
CreateNamespace bool
Check bool
Expand Down Expand Up @@ -293,7 +295,7 @@ func (o *InstallOptions) Install() error {
// create or update crds
s := spinner.New(o.Out, spinnerMsg("Create CRDs"))
defer s.Fail()
if err = createOrUpdateCRDS(o.Dynamic, o.Version); err != nil {
if err = createOrUpdateCRDS(o.Dynamic, o.Version, ""); err != nil {
return fmt.Errorf("install crds failed: %s", err.Error())
}
s.Success()
Expand Down Expand Up @@ -502,11 +504,13 @@ func (o *InstallOptions) checkVersion(v util.Version) error {
}

// check installing version exists
if exists, err := versionExists(o.Version); !exists {
if err != nil {
return err
if o.chartFile == "" {
if exists, err := versionExists(o.Version); !exists {
if err != nil {
return err
}
return fmt.Errorf("version %s does not exist, please use \"kbcli kubeblocks list-versions --devel\" to show the available versions", o.Version)
}
return fmt.Errorf("version %s does not exist, please use \"kbcli kubeblocks list-versions --devel\" to show the available versions", o.Version)
}

versionErr := fmt.Errorf("failed to get kubernetes version")
Expand Down Expand Up @@ -611,9 +615,13 @@ func (o *InstallOptions) printNotes() {
}

func (o *InstallOptions) buildChart() *helm.InstallOpts {
chart := types.KubeBlocksChartName + "/" + types.KubeBlocksChartName
if o.chartFile != "" {
chart = o.chartFile
}
return &helm.InstallOpts{
Name: types.KubeBlocksChartName,
Chart: types.KubeBlocksChartName + "/" + types.KubeBlocksChartName,
Chart: chart,
Wait: o.Wait,
Version: o.Version,
Namespace: o.HelmCfg.Namespace(),
Expand Down
21 changes: 13 additions & 8 deletions pkg/cmd/kubeblocks/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ func newUpgradeCmd(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra
}

cmd.Flags().StringVar(&o.Version, "version", "", "Set KubeBlocks version")
cmd.Flags().StringVar(&o.chartFile, "chart-file", "", "Path to the KubeBlocks Helm chart")
cmd.Flags().StringVar(&o.crdsFile, "crds-file", "", "Path to the KubeBlocks CRDs")
cmd.Flags().StringVarP(&o.Namespace, "namespace", "n", "", "KubeBlocks namespace")
cmd.Flags().BoolVar(&o.Check, "check", true, "Check kubernetes environment before upgrade")
cmd.Flags().DurationVar(&o.Timeout, "timeout", 1800*time.Second, "Time to wait for upgrading KubeBlocks, such as --timeout=10m")
Expand Down Expand Up @@ -177,14 +179,17 @@ func (o *InstallOptions) Upgrade() error {
}
}

// add helm repo
s := spinner.New(o.Out, spinnerMsg("Add and update repo %s", types.KubeBlocksChartName))
defer s.Fail()
// Add repo, if exists, will update it
if err = helm.AddRepo(newHelmRepoEntry()); err != nil {
return err
var s spinner.Interface
if o.chartFile == "" {
// add helm repo
s = spinner.New(o.Out, spinnerMsg("Add and update repo %s", types.KubeBlocksChartName))
defer s.Fail()
// Add repo, if exists, will update it
if err = helm.AddRepo(newHelmRepoEntry()); err != nil {
return err
}
s.Success()
}
s.Success()

// it's time to upgrade
msg := ""
Expand Down Expand Up @@ -229,7 +234,7 @@ func (o *InstallOptions) Upgrade() error {
// create or update crds
s = spinner.New(o.Out, spinnerMsg("Upgrade CRDs"))
defer s.Fail()
if err = createOrUpdateCRDS(o.Dynamic, o.Version); err != nil {
if err = createOrUpdateCRDS(o.Dynamic, o.Version, o.crdsFile); err != nil {
return fmt.Errorf("upgrade crds failed: %s", err.Error())
}
s.Success()
Expand Down
42 changes: 26 additions & 16 deletions pkg/cmd/kubeblocks/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"fmt"
"io"
"net/http"
"os"
"sort"
"strings"
"time"
Expand Down Expand Up @@ -317,23 +318,32 @@ func newHelmRepoEntry() *repo.Entry {
}

// createOrUpdateCRDS creates or updates the kubeBlocks crds.
func createOrUpdateCRDS(dynamic dynamic.Interface, kbVersion string) error {
if kbVersion == "" {
kbVersion = version.GetVersion()
}
crdsURL := util.GetKubeBlocksCRDsURL(kbVersion)
resp, err := http.Get(crdsURL)
if err != nil {
return err
}
if resp.StatusCode == http.StatusNotFound {
fmt.Printf("not found CRDs from %s, please specify the right version", crdsURL)
return nil
} else if resp.StatusCode != http.StatusOK {
return fmt.Errorf("failed to download CRDs from %s", crdsURL)
func createOrUpdateCRDS(dynamic dynamic.Interface, kbVersion, crdsFile string) error {
var reader io.ReadCloser
var err error
if crdsFile != "" {
if reader, err = os.Open(crdsFile); err != nil {
return err
}
} else {
if kbVersion == "" {
kbVersion = version.GetVersion()
}
crdsURL := util.GetKubeBlocksCRDsURL(kbVersion)
resp, err := http.Get(crdsURL)
if err != nil {
return err
}
if resp.StatusCode == http.StatusNotFound {
fmt.Printf("not found CRDs from %s, please specify the right version", crdsURL)
return nil
} else if resp.StatusCode != http.StatusOK {
return fmt.Errorf("failed to download CRDs from %s", crdsURL)
}
reader = resp.Body
}
defer resp.Body.Close()
d := yaml.NewYAMLToJSONDecoder(resp.Body)
defer reader.Close()
d := yaml.NewYAMLToJSONDecoder(reader)
var objs []unstructured.Unstructured
for {
var obj unstructured.Unstructured
Expand Down
Loading