Skip to content

Commit

Permalink
chore: support to install 1.0 KB when existing 0.9.2 KB
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyelei committed Nov 14, 2024
1 parent 509f77e commit 966a901
Show file tree
Hide file tree
Showing 21 changed files with 900 additions and 1,080 deletions.
5 changes: 3 additions & 2 deletions pkg/cmd/backuprepo/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,9 @@ func (o *createOptions) validate(cmd *cobra.Command) error {
func (o *createOptions) createCredentialSecret() (*corev1.Secret, error) {
// if failed to get the namespace of KubeBlocks,
// then create the secret in the current namespace
namespace, err := util.GetKubeBlocksNamespace(o.client)
if err != nil {
var err error
namespace, _ := util.GetKubeBlocksNamespace(o.client, "")
if namespace == "" {
namespace, _, err = o.factory.ToRawKubeConfigLoader().Namespace()
if err != nil {
return nil, err
Expand Down
33 changes: 31 additions & 2 deletions pkg/cmd/kubeblocks/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package kubeblocks

import (
"context"
"fmt"
"strings"
"time"

Expand All @@ -32,6 +33,7 @@ import (
"k8s.io/kubectl/pkg/util/templates"

"github.com/apecloud/kbcli/pkg/printer"
"github.com/apecloud/kbcli/pkg/spinner"
"github.com/apecloud/kbcli/pkg/types"
"github.com/apecloud/kbcli/pkg/util"
"github.com/apecloud/kbcli/pkg/util/helm"
Expand Down Expand Up @@ -83,14 +85,40 @@ func NewConfigCmd(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra.
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
util.CheckErr(o.Complete(f, cmd))
util.CheckErr(o.Upgrade())
util.CheckErr(configKBRelease(o))
util.CheckErr(markKubeBlocksPodsToLoadConfigMap(o.Client))
},
}
helm.AddValueOptionsFlags(cmd.Flags(), &o.ValueOpts)
cmd.Flags().StringVarP(&o.Namespace, "namespace", "n", "", "KubeBlocks namespace")
return cmd
}

func configKBRelease(o *InstallOptions) error {
kbRelease, err := o.getKBRelease()
if err != nil {
return err
}
if helm.ValueOptsIsEmpty(&o.ValueOpts) {
fmt.Fprint(o.Out, "--set should be specified.\n")
return nil
}
var kbVersion string
if kbRelease != nil && kbRelease.Chart != nil && kbRelease.Chart.Metadata != nil {
kbVersion = kbRelease.Chart.Metadata.Version
}
s := spinner.New(o.Out, spinnerMsg("Config KubeBlocks "+kbVersion))
defer s.Fail()
o.disableHelmPreHookJob()
// upgrade KubeBlocks chart
if err = o.upgradeChart(); err != nil {
return err
}
// successfully upgraded
s.Success()
return nil
}

func NewDescribeConfigCmd(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra.Command {
o := &InstallOptions{
Options: Options{
Expand All @@ -109,6 +137,7 @@ func NewDescribeConfigCmd(f cmdutil.Factory, streams genericiooptions.IOStreams)
},
}
printer.AddOutputFlag(cmd, &output)
cmd.Flags().StringVarP(&o.Namespace, "namespace", "n", "", "KubeBlocks namespace")
cmd.Flags().BoolVarP(&showAllConfig, "all", "A", false, "show all kubeblocks configs value")
cmd.Flags().StringVar(&filterConfig, "filter", "", "filter the desired kubeblocks configs, multiple filtered strings are comma separated")
return cmd
Expand All @@ -117,7 +146,7 @@ func NewDescribeConfigCmd(f cmdutil.Factory, streams genericiooptions.IOStreams)
// getHelmValues gets all kubeblocks values by helm and filter the addons values
func getHelmValues(release string, opt *Options) (map[string]interface{}, error) {
if len(opt.HelmCfg.Namespace()) == 0 {
namespace, err := util.GetKubeBlocksNamespace(opt.Client)
namespace, err := util.GetKubeBlocksNamespace(opt.Client, opt.Namespace)
if err != nil {
return nil, err
}
Expand Down
91 changes: 54 additions & 37 deletions pkg/cmd/kubeblocks/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
"github.com/pkg/errors"
"github.com/replicatedhq/troubleshoot/pkg/preflight"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"golang.org/x/exp/maps"
"helm.sh/helm/v3/pkg/cli/values"
Expand All @@ -53,8 +52,8 @@ import (
"github.com/apecloud/kbcli/pkg/spinner"
"github.com/apecloud/kbcli/pkg/types"
"github.com/apecloud/kbcli/pkg/util"
"github.com/apecloud/kbcli/pkg/util/breakingchange"
"github.com/apecloud/kbcli/pkg/util/helm"
"github.com/apecloud/kbcli/pkg/util/prompt"
"github.com/apecloud/kbcli/version"
)

Expand Down Expand Up @@ -95,7 +94,8 @@ type InstallOptions struct {
TopologyKeys []string
NodeLabels map[string]string
TolerationsRaw []string
upgrader breakingchange.Upgrader
upgradeFrom09 bool
kb09Namespace string
}

type addonStatus struct {
Expand Down Expand Up @@ -156,6 +156,7 @@ func newInstallCmd(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra
}

cmd.Flags().StringVar(&o.Version, "version", version.DefaultKubeBlocksVersion, "KubeBlocks version")
cmd.Flags().StringVarP(&o.Namespace, "namespace", "n", types.DefaultNamespace, "KubeBlocks namespace")
cmd.Flags().BoolVar(&o.CreateNamespace, "create-namespace", false, "Create the namespace if not present")
cmd.Flags().BoolVar(&o.Check, "check", true, "Check kubernetes environment before installation")
cmd.Flags().DurationVar(&o.Timeout, "timeout", 1800*time.Second, "Time to wait for installing KubeBlocks, such as --timeout=10m")
Expand All @@ -179,10 +180,6 @@ func (o *Options) Complete(f cmdutil.Factory, cmd *cobra.Command) error {
fmt.Fprintf(o.Out, "Failed to enable the log file %s", err.Error())
}

if o.Namespace, _, err = f.ToRawKubeConfigLoader().Namespace(); err != nil {
return err
}

config, err := cmd.Flags().GetString("kubeconfig")
if err != nil {
return err
Expand All @@ -193,16 +190,7 @@ func (o *Options) Complete(f cmdutil.Factory, cmd *cobra.Command) error {
return err
}

// check whether --namespace is specified, if not, KubeBlocks will be installed
// to the kb-system namespace
var targetNamespace string
cmd.Flags().Visit(func(flag *pflag.Flag) {
if flag.Name == "namespace" {
targetNamespace = o.Namespace
}
})

o.HelmCfg = helm.NewConfig(targetNamespace, config, ctx, klog.V(1).Enabled())
o.HelmCfg = helm.NewConfig(o.Namespace, config, ctx, klog.V(1).Enabled())
if o.Dynamic, err = f.DynamicClient(); err != nil {
return err
}
Expand All @@ -212,28 +200,48 @@ func (o *Options) Complete(f cmdutil.Factory, cmd *cobra.Command) error {
}

func (o *InstallOptions) PreCheck() error {
// check if KubeBlocks has been installed
v, err := util.GetVersionInfo(o.Client)
if err != nil {
return err
}

if v.KubeBlocks != "" {
return fmt.Errorf("KubeBlocks %s already exists, repeated installation is not supported", v.KubeBlocks)
}

// check whether the namespace exists
if err = o.checkNamespace(); err != nil {
if err := o.checkNamespace(); err != nil {
return err
}
return o.PreCheckKBVersion()
}

// check whether there are remained resource left by previous KubeBlocks installation, if yes,
// output the resource name
if err = o.checkRemainedResource(); err != nil {
func (o *InstallOptions) PreCheckKBVersion() error {
o.Version = util.TrimVersionPrefix(o.Version)
v, err := util.GetVersionInfo(o.Client)
if err != nil {
return err
}
o.upgradeFrom09 = o.checkUpgradeFrom09(v.KubeBlocks)
if o.upgradeFrom09 {
if o.upgradeFrom09 {
deploys, err := util.GetKubeBlocksDeploys(o.Client)
if err != nil {
return err
}
for _, deploy := range deploys {
if deploy.Namespace == o.HelmCfg.Namespace() {
return fmt.Errorf(`cannot install KubeBlocks in the same namespace "%s" with KubeBlocks 0.9`, o.HelmCfg.Namespace())
}
}
}
installWarn := fmt.Sprintf("You will Install KubeBlocks %s When existing KubeBlocks %s ", o.Version, v.KubeBlocks)
if err = prompt.Confirm(nil, o.In, installWarn, "Please type 'Yes/yes' to confirm your operation:"); err != nil {
return err
}
} else {
// check if KubeBlocks has been installed
if v.KubeBlocks != "" {
return fmt.Errorf("KubeBlocks %s already exists, repeated installation is not supported", v.KubeBlocks)
}

o.Version = util.TrimVersionPrefix(o.Version)
// check whether there are remained resource left by previous KubeBlocks installation, if yes,
// output the resource name
if err = o.checkRemainedResource(); err != nil {
return err
}
}
if err = o.checkVersion(v); err != nil {
return err
}
Expand Down Expand Up @@ -277,6 +285,11 @@ func (o *InstallOptions) CompleteInstallOptions() error {

func (o *InstallOptions) Install() error {
var err error
if o.upgradeFrom09 {
if err = o.preInstallWhenUpgradeFrom09(); err != nil {
return err
}
}
// create or update crds
s := spinner.New(o.Out, spinnerMsg("Create CRDs"))
defer s.Fail()
Expand Down Expand Up @@ -372,6 +385,12 @@ You can check the KubeBlocks status by running "kbcli kubeblocks status"
fmt.Fprint(o.Out, msg)
o.printNotes()
}
if o.upgradeFrom09 {
fmt.Fprint(o.Out, "Start KubeBlocks 0.9\n")
if err = o.configKB09(); err != nil {
return err
}
}
return nil
}

Expand Down Expand Up @@ -514,11 +533,10 @@ func (o *InstallOptions) checkVersion(v util.Version) error {
func (o *InstallOptions) checkNamespace() error {
// target namespace is not specified, use default namespace
if o.HelmCfg.Namespace() == "" {
o.HelmCfg.SetNamespace(types.DefaultNamespace)
o.HelmCfg.SetNamespace(o.Namespace)
o.CreateNamespace = true
fmt.Fprintf(o.Out, "KubeBlocks will be installed to namespace \"%s\"\n", o.HelmCfg.Namespace())
}

fmt.Fprintf(o.Out, "KubeBlocks will be installed to namespace \"%s\"\n", o.HelmCfg.Namespace())
// check if namespace exists
if !o.CreateNamespace {
_, err := o.Client.CoreV1().Namespaces().Get(context.TODO(), o.Namespace, metav1.GetOptions{})
Expand All @@ -532,7 +550,7 @@ func (o *InstallOptions) checkRemainedResource() error {
return nil
}

ns, _ := util.GetKubeBlocksNamespace(o.Client)
ns, _ := util.GetKubeBlocksNamespace(o.Client, o.Namespace)
if ns == "" {
ns = o.Namespace
}
Expand Down Expand Up @@ -591,7 +609,6 @@ func (o *InstallOptions) buildChart() *helm.InstallOpts {
CreateNamespace: o.CreateNamespace,
Timeout: o.Timeout,
Atomic: false,
Upgrader: o.upgrader,
}
}

Expand Down
Loading

0 comments on commit 966a901

Please sign in to comment.