diff --git a/docs/user_docs/cli/kbcli_kubeblocks_install.md b/docs/user_docs/cli/kbcli_kubeblocks_install.md index 704c34a0f..6cd12e78d 100644 --- a/docs/user_docs/cli/kbcli_kubeblocks_install.md +++ b/docs/user_docs/cli/kbcli_kubeblocks_install.md @@ -40,7 +40,7 @@ kbcli kubeblocks install [flags] --set-file stringArray Set values from respective files specified via the command line (can specify multiple or separate values with commas: key1=path1,key2=path2) --set-json stringArray Set JSON values on the command line (can specify multiple or separate values with commas: key1=jsonval1,key2=jsonval2) --set-string stringArray Set STRING values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2) - --timeout duration Time to wait for installing KubeBlocks, such as --timeout=10m (default 10m0s) + --timeout duration Time to wait for installing KubeBlocks, such as --timeout=10m (default 30m0s) --tolerations strings Tolerations for Kubeblocks, such as '"dev=true:NoSchedule,large=true:NoSchedule"' --topology-keys stringArray Topology keys for affinity -f, --values strings Specify values in a YAML file or a URL (can specify multiple) diff --git a/docs/user_docs/cli/kbcli_kubeblocks_upgrade.md b/docs/user_docs/cli/kbcli_kubeblocks_upgrade.md index 870ca1eb9..cfd375528 100644 --- a/docs/user_docs/cli/kbcli_kubeblocks_upgrade.md +++ b/docs/user_docs/cli/kbcli_kubeblocks_upgrade.md @@ -28,7 +28,7 @@ kbcli kubeblocks upgrade [flags] --set-file stringArray Set values from respective files specified via the command line (can specify multiple or separate values with commas: key1=path1,key2=path2) --set-json stringArray Set JSON values on the command line (can specify multiple or separate values with commas: key1=jsonval1,key2=jsonval2) --set-string stringArray Set STRING values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2) - --timeout duration Time to wait for upgrading KubeBlocks, such as --timeout=10m (default 10m0s) + --timeout duration Time to wait for upgrading KubeBlocks, such as --timeout=10m (default 30m0s) -f, --values strings Specify values in a YAML file or a URL (can specify multiple) --version string Set KubeBlocks version --wait Wait for KubeBlocks to be ready. It will wait for a --timeout period (default true) diff --git a/pkg/cmd/kubeblocks/compare.go b/pkg/cmd/kubeblocks/compare.go index 64752e85d..275e0bb4b 100644 --- a/pkg/cmd/kubeblocks/compare.go +++ b/pkg/cmd/kubeblocks/compare.go @@ -107,8 +107,7 @@ func (o *InstallOptions) compare(args []string, detail bool) error { } // update repo if err := helm.AddRepo(&repo.Entry{Name: types.KubeBlocksRepoName, URL: util.GetHelmChartRepoURL()}); err != nil { - - return fmt.Errorf(err.Error()) + return err } // check version is available if exists, err := versionExists(versionA); !exists { diff --git a/pkg/cmd/kubeblocks/install.go b/pkg/cmd/kubeblocks/install.go index a3262b05d..e3dd44c10 100644 --- a/pkg/cmd/kubeblocks/install.go +++ b/pkg/cmd/kubeblocks/install.go @@ -158,7 +158,7 @@ func newInstallCmd(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra cmd.Flags().StringVar(&o.Version, "version", version.DefaultKubeBlocksVersion, "KubeBlocks version") 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", 600*time.Second, "Time to wait for installing KubeBlocks, such as --timeout=10m") + cmd.Flags().DurationVar(&o.Timeout, "timeout", 1800*time.Second, "Time to wait for installing KubeBlocks, such as --timeout=10m") cmd.Flags().BoolVar(&o.Wait, "wait", true, "Wait for KubeBlocks to be ready, including all the auto installed add-ons. It will wait for a --timeout period") cmd.Flags().BoolVar(&o.WaitAddons, "wait-addons", true, "Wait for auto installed add-ons. It will wait for a --timeout period") cmd.Flags().BoolVar(&p.force, flagForce, p.force, "If present, just print fail item and continue with the following steps") diff --git a/pkg/cmd/kubeblocks/upgrade.go b/pkg/cmd/kubeblocks/upgrade.go index 07418b631..b0c0f289d 100644 --- a/pkg/cmd/kubeblocks/upgrade.go +++ b/pkg/cmd/kubeblocks/upgrade.go @@ -39,6 +39,7 @@ import ( "k8s.io/klog/v2" cmdutil "k8s.io/kubectl/pkg/cmd/util" "k8s.io/kubectl/pkg/util/templates" + "sigs.k8s.io/yaml" "github.com/apecloud/kbcli/pkg/printer" "github.com/apecloud/kbcli/pkg/spinner" @@ -59,7 +60,7 @@ var ( kbcli kubeblocks upgrade --set replicaCount=3`) ) -type getDeploymentFunc func(client kubernetes.Interface) (*appsv1.Deployment, error) +type deploymentGetter func(client kubernetes.Interface) (*appsv1.Deployment, error) func newUpgradeCmd(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra.Command { o := &InstallOptions{ @@ -81,7 +82,7 @@ func newUpgradeCmd(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra cmd.Flags().StringVar(&o.Version, "version", "", "Set KubeBlocks version") cmd.Flags().BoolVar(&o.Check, "check", true, "Check kubernetes environment before upgrade") - cmd.Flags().DurationVar(&o.Timeout, "timeout", 600*time.Second, "Time to wait for upgrading KubeBlocks, such as --timeout=10m") + cmd.Flags().DurationVar(&o.Timeout, "timeout", 1800*time.Second, "Time to wait for upgrading KubeBlocks, such as --timeout=10m") cmd.Flags().BoolVar(&o.Wait, "wait", true, "Wait for KubeBlocks to be ready. It will wait for a --timeout period") cmd.Flags().BoolVar(&o.autoApprove, "auto-approve", false, "Skip interactive approval before upgrading KubeBlocks") helm.AddValueOptionsFlags(cmd.Flags(), &o.ValueOpts) @@ -181,7 +182,7 @@ func (o *InstallOptions) Upgrade() error { // KubeBlocks after upgrade. s = spinner.New(o.Out, spinnerMsg("Stop KubeBlocks "+kbVersion)) defer s.Fail() - if err = o.stopDeployment(util.GetKubeBlocksDeploy); err != nil { + if err = o.deleteDeployment(util.GetKubeBlocksDeploy); err != nil { return err } s.Success() @@ -189,7 +190,7 @@ func (o *InstallOptions) Upgrade() error { // stop the data protection deployment s = spinner.New(o.Out, spinnerMsg("Stop DataProtection")) defer s.Fail() - if err = o.stopDeployment(util.GetDataProtectionDeploy); err != nil { + if err = o.deleteDeployment(util.GetDataProtectionDeploy); err != nil { return err } s.Success() @@ -258,9 +259,9 @@ func (o *InstallOptions) upgradeChart() error { return o.buildChart().Upgrade(o.HelmCfg) } -// stopDeployment stops the deployment by setting the replicas to 0 -func (o *InstallOptions) stopDeployment(getDeployFn getDeploymentFunc) error { - deploy, err := getDeployFn(o.Client) +// deleteDeployment deletes deployment. +func (o *InstallOptions) deleteDeployment(getter deploymentGetter) error { + deploy, err := getter(o.Client) if err != nil { if apierrors.IsNotFound(err) { return nil @@ -273,26 +274,43 @@ func (o *InstallOptions) stopDeployment(getDeployFn getDeploymentFunc) error { return nil } - if _, err = o.Client.AppsV1().Deployments(deploy.Namespace).Patch( - context.TODO(), deploy.Name, apitypes.JSONPatchType, - []byte(`[{"op": "replace", "path": "/spec/replicas", "value": 0}]`), - metav1.PatchOptions{}); err != nil { + // before delete deployment, output the deployment yaml, if deployment was deleted + // by mistake, we can recover it by apply the yaml. + deploy.ManagedFields = nil + bytes, err := yaml.Marshal(deploy) + if err != nil { + return err + } + klog.Infof(` +------------------- Deployment %s ------------------- +%s +------------------ Deployment %s end ----------------`, + deploy.Name, string(bytes), deploy.Name) + + if err = o.Client.AppsV1().Deployments(deploy.Namespace).Delete(context.TODO(), + deploy.Name, + metav1.DeleteOptions{ + GracePeriodSeconds: func() *int64 { + seconds := int64(0) + return &seconds + }(), + }); err != nil { return err } - // wait for deployment to be stopped - return wait.PollUntilContextTimeout(context.Background(), 5*time.Second, o.Timeout, true, + // wait for deployment to be deleted + return wait.PollUntilContextTimeout(context.Background(), 5*time.Second, 1*time.Minute, true, func(_ context.Context) (bool, error) { - deploy, err = util.GetKubeBlocksDeploy(o.Client) + deploy, err = getter(o.Client) if err != nil { + if apierrors.IsNotFound(err) { + return true, nil + } return false, err - } - if *deploy.Spec.Replicas == 0 && - deploy.Status.Replicas == 0 && - deploy.Status.AvailableReplicas == 0 { + } else if deploy == nil { return true, nil } - return false, nil + return false, err }) } diff --git a/pkg/cmd/report/zipwritter.go b/pkg/cmd/report/zipwritter.go index 30823d40b..d2b404b48 100644 --- a/pkg/cmd/report/zipwritter.go +++ b/pkg/cmd/report/zipwritter.go @@ -177,7 +177,7 @@ func (w *reportZipWritter) WriteLogs(folderName string, ctx context.Context, cli } for _, pod := range pods.Items { - if pod.Spec.Containers == nil || len(pod.Spec.Containers) == 0 { + if len(pod.Spec.Containers) == 0 { continue } // write pod log to file diff --git a/pkg/preflight/analyzer/kb_taint.go b/pkg/preflight/analyzer/kb_taint.go index da5acde1c..a24c6738c 100644 --- a/pkg/preflight/analyzer/kb_taint.go +++ b/pkg/preflight/analyzer/kb_taint.go @@ -101,12 +101,12 @@ func (a *AnalyzeTaintClassByKb) analyzeTaint(getFile GetCollectedFileContents, f func (a *AnalyzeTaintClassByKb) doAnalyzeTaint(nodes v1.NodeList) (*analyze.AnalyzeResult, error) { taintFailResult := []string{} for _, node := range nodes.Items { - if node.Spec.Taints == nil || len(node.Spec.Taints) == 0 { + if len(node.Spec.Taints) == 0 { return newAnalyzeResult(a.Title(), PassType, a.analyzer.Outcomes), nil } } - if a.analyzer.TolerationsMap == nil || len(a.analyzer.TolerationsMap) == 0 { + if len(a.analyzer.TolerationsMap) == 0 { return newAnalyzeResult(a.Title(), FailType, a.analyzer.Outcomes), nil }